Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor bug fixes and features #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions jsoncomparison/compare.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import json
import math
from typing import Optional

from .config import Config
Expand Down Expand Up @@ -101,6 +102,12 @@ def _str_diff(cls, e, a):
def _float_diff(self, e, a):
if a == e:
return NO_DIFF

if self._can_percent_diff():
p = self._float_percent_diff()
if math.isclose(a, e, rel_tol=p):
return NO_DIFF

if self._can_rounded_float():
p = self._float_precision()
e, a = round(e, p), round(a, p)
Expand All @@ -112,10 +119,18 @@ def _can_rounded_float(self):
p = self._float_precision()
return type(p) is int

def _can_percent_diff(self):
p = self._float_percent_diff()
return type(p) is float

def _float_precision(self):
path = 'types.float.allow_round'
return self._config.get(path)

def _float_percent_diff(self):
path = 'types.float.allow_percent_diff'
return self._config.get(path)

def _dict_diff(self, e, a):
d = {}
for k in e:
Expand Down Expand Up @@ -149,7 +164,9 @@ def _list_content_diff(self, e, a):
if v in a:
continue
t = type(v)
if t in (int, str, bool, float):
if t is float:
d[i] = self._min_diff(v, a, self._float_diff)
if t in (int, str, bool):
d[i] = ValueNotFound(v, None).explain()
elif t is dict:
d[i] = self._min_diff(v, a, self._dict_diff)
Expand Down Expand Up @@ -177,7 +194,6 @@ def _min_diff(cls, e, lst, method):
dd = method(e, v)
if len(dd) <= len(d):
d = dd
break
return d

@classmethod
Expand Down
3 changes: 3 additions & 0 deletions jsoncomparison/ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Ignore(ABC):

@classmethod
def transform(cls, obj, rules):
if obj is None:
return None

t = type(rules)
if t is dict:
return cls._apply_dictable_rule(obj, rules)
Expand Down
5 changes: 3 additions & 2 deletions tests/data/compare/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"types": {
"float": {
"allow_round": 2
"allow_round": 2,
"allow_percent_diff": 0.05
},
"list": {
"check_length": true
Expand All @@ -11,4 +12,4 @@
"console": false,
"file": false
}
}
}
11 changes: 7 additions & 4 deletions tests/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def test_compare_float(self):
diff = self.compare.check(1.23456, 1.23)
self.assertEqual(diff, NO_DIFF)

diff = self.compare.check(100.0, 105.0)
self.assertEqual(diff, NO_DIFF)

diff = self.compare.check(1.2, 1.3)
self.assertEqual(diff, ValuesNotEqual(1.2, 1.3).explain())

Expand Down Expand Up @@ -126,7 +129,7 @@ def test_simple_list_with_dicts(self):
self.assertEqual(
diff, {
'_content': {
0: {'a': ValuesNotEqual('xxx', 'zzz').explain()},
0: {'a': ValuesNotEqual('xxx', 'yyy').explain()},
},
},
)
Expand All @@ -152,8 +155,8 @@ def test_list_with_dicts(self):
self.assertEqual(
diff, {
'_content': {
1: {'a': ValuesNotEqual('iii', 'zzz').explain()},
3: {'a': ValuesNotEqual('jjj', 'zzz').explain()},
1: {'a': ValuesNotEqual('iii', 'eee').explain()},
3: {'a': ValuesNotEqual('jjj', 'eee').explain()},
},
},
)
Expand All @@ -175,7 +178,7 @@ def test_list_with_dicts_with_duplicates(self):
self.assertEqual(
diff, {
'_content': {
3: {'a': ValuesNotEqual('jjj', 'zzz').explain()},
3: {'a': ValuesNotEqual('jjj', 'iii').explain()},
},
},
)
Expand Down