Skip to content

Commit

Permalink
add test coverage for deep_merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasha10 committed Feb 3, 2022
1 parent 510faa7 commit 012cbbd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/maison/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def deep_merge(destination: Dict[Any, Any], source: Dict[Any, Any]) -> Dict[Any,
... }
True
Note that the arguments may be modified!
Based on https://stackoverflow.com/a/20666342
Args:
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
"""Tests for the `utils` module."""
from pathlib import Path
from typing import Any
from typing import Callable
from typing import Dict
from typing import Union
from unittest.mock import MagicMock
from unittest.mock import patch

from _pytest.python_api import RaisesContext
from pytest import mark
from pytest import param
from pytest import raises

from maison.utils import deep_merge
from maison.utils import get_file_path
from maison.utils import path_contains_file

Expand Down Expand Up @@ -113,3 +122,44 @@ def test_absolute_path_not_exist(self) -> None:
result = get_file_path(filename="~/xxxx/yyyy/doesnotexist.xyz")

assert result is None


@mark.parametrize(
"a,b,expected",
[
param(
{1: 2, 2: 5},
{1: 3},
{1: 3, 2: 5},
id="simple",
),
param(
{1: {2: 3}, 2: 5},
{1: {2: 4}},
{1: {2: 4}, 2: 5},
id="nested",
),
param(
{1: 2, 2: 5},
{1: {3: 4}},
raises(RuntimeError),
id="merge-dict-into-scalar",
),
],
)
def test_deep_merge(
a: Dict[Any, Any],
b: Dict[Any, Any],
expected: Union[Dict[Any, Any], RaisesContext[Any]],
) -> None:
"""
Given two dictionaries `a` and `b`,
when the `deep_merge` function is invoked with `a` and `b` as arguments,
Test that the returned value is as expected.
"""
if isinstance(expected, RaisesContext):
with expected:
deep_merge(a, b)
else:
assert deep_merge(a, b) == expected
assert a == expected

0 comments on commit 012cbbd

Please sign in to comment.