Skip to content

Commit

Permalink
add tests and small merge improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesturk committed Jan 8, 2020
1 parent 5c4f226 commit a25b719
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scripts/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def __eq__(self, other):
def __str__(self):
return f"{self.key_name}: append {dict(self.list_item)}"

def __repr__(self):
return f"Append({self.key_name}, {self.list_item})"


class Replace:
def __init__(self, key_name, value_one, value_two):
Expand All @@ -35,6 +38,9 @@ def __eq__(self, other):
def __str__(self):
return f"{self.key_name}: {self.value_one} => {self.value_two}"

def __repr__(self):
return f"Append({self.key_name}, {self.value_one}, {self.value_two})"


def compute_merge(obj1, obj2, prefix="", keep_both_ids=False):
combined_keys = set(obj1) | set(obj2)
Expand Down
25 changes: 25 additions & 0 deletions scripts/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ def test_compute_merge_list(a, b, output):
assert compute_merge(a, b) == output


@pytest.mark.parametrize(
"a, b, keep_both, output",
[
# discard id
({"id": "1"}, {"id": "2"}, False, []),
# keep id
(
{"id": "1"},
{"id": "2"},
True,
[Append("other_identifiers", {"identifier": "2", "scheme": "openstates"})],
),
# append name
(
{"name": "A"},
{"name": "B"},
True,
[Append("other_names", {"name": "A"}), Replace("name", "A", "B")],
),
],
)
def test_compute_merge_special_cases(a, b, keep_both, output):
assert compute_merge(a, b, keep_both_ids=keep_both) == output


@pytest.mark.parametrize(
"old, new, expected",
[
Expand Down

0 comments on commit a25b719

Please sign in to comment.