Skip to content

Commit

Permalink
Merge pull request #68 from sameerdp5/master
Browse files Browse the repository at this point in the history
Support records not having same keys
  • Loading branch information
Adam DePue authored Apr 18, 2017
2 parents ae76d16 + f93a579 commit 8b36522
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
12 changes: 12 additions & 0 deletions normalize/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ def compare_collection_iter(propval_a, propval_b, fs_a=None, fs_b=None,
set_a = set(rev_keys["a"].values())
set_b = set(rev_keys["b"].values())
shared_keys = set_a.intersection(set_b)
removed = set_a - set_b
added = set_b - set_a
for key in shared_keys:
if (isinstance(propval_a, collections.Iterable) and
isinstance(propval_b, collections.Iterable)):
Expand All @@ -680,6 +682,16 @@ def compare_collection_iter(propval_a, propval_b, fs_a=None, fs_b=None,

for diff in diffs:
yield diff

for key in removed:
yield DiffInfo(diff_type=DiffTypes.REMOVED,
base=fs_a + [key],
other=fs_b)

for key in added:
yield DiffInfo(diff_type=DiffTypes.ADDED,
base=fs_a,
other=fs_b + [key])
else:
removed = values['a'] - values['b']
added = values['b'] - values['a']
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
install_requires=('richenum>=1.0.0',),
tests_require=('nose', 'unittest2'),
test_suite="run_tests",
version='1.0.3',
version='1.0.4',
url="http://hearsaycorp.github.io/normalize",
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
25 changes: 24 additions & 1 deletion tests/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

import unittest

from normalize.coll import Collection
from normalize.coll import list_of
from normalize.diff import *
from normalize.record import Record
from normalize.record.json import JsonRecord
from normalize.property import Property
from normalize.property.coll import DictProperty
from normalize.property.coll import ListProperty
from normalize.property.json import JsonProperty
from normalize.property.json import JsonListProperty
Expand Down Expand Up @@ -645,3 +646,25 @@ class FakeThing(JsonRecord):
ignore_empty_slots=True,
)
self.assertEqual(len(diffs), 1)

def test_recurse_remove_field(self):
class FakeSite(JsonRecord):
slug = Property()
_custom_tags = DictProperty(of=list_of(str))

fake1 = FakeSite(slug="my_site", _custom_tags={'languages': ['English']})
fake2 = FakeSite(slug="my_site", _custom_tags={})

diffs = fake1.diff(fake2, recurse=True)
self.assertDifferences(diffs, {"REMOVED ._custom_tags.languages"})

def test_recurse_add_field(self):
class FakeSite(JsonRecord):
slug = Property()
_custom_tags = DictProperty(of=list_of(str))

fake1 = FakeSite(slug="my_site", _custom_tags={})
fake2 = FakeSite(slug="my_site", _custom_tags={'languages': ['English']})

diffs = fake1.diff(fake2, recurse=True)
self.assertDifferences(diffs, {"ADDED ._custom_tags.languages"})

0 comments on commit 8b36522

Please sign in to comment.