Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def assert_same(thing1, thing2):
"""
Compares two things. Debug logs the differences between them before
asserting that they are the same.
"""
assert cmp(thing1, thing2) == 0, \
"Items are not the same. Difference is:\n %s" % \
pformat(DeepDiff(thing1, thing2), indent=2)
def test_negative_significant_digits(self):
with self.assertRaises(ValueError):
DeepDiff(1, 1, significant_digits=-1)
def test_nested_list_with_dictionarry_difference_ignore_order(self):
t1 = [1, 2, [3, 4, {1: 2}]]
t2 = [[4, 3, {1: 2}], 2, 1]
ddiff = DeepDiff(t1, t2, ignore_order=True)
result = {}
assert result == ddiff
def test_type_change_numeric_when_ignore_order(self, t1, t2, expected_result):
ddiff = DeepDiff(t1, t2, ignore_order=True, ignore_numeric_type_changes=True, ignore_string_type_changes=True)
assert expected_result == ddiff
fields=[
ParsedField(name='title', type='str', nullable=False),
ParsedField(name='director', type='str', nullable=False),
]
)
]
)
]
)
]
))
parsed_dict = asdict(parsed)
assert bool(parsed)
assert parsed_dict == expected, str(DeepDiff(parsed_dict, expected))
def test_same_objects(self):
t1 = {1: 1, 2: 2, 3: 3}
t2 = t1
ddiff = DeepDiff(t1, t2)
res = ddiff.tree
self.assertEqual(res, {})
def diff_configs(current, new):
"""Diffs to dict using DeepDiff and returning the changes ready to print
Keys present in current but missing in new are considered unchanged
Returns a tuple of added, removed and updated
"""
current = {k: current[k] for k in new}
d = DeepDiff(current, new, ignore_order=True)
added = set()
removed = set()
changed = d.get("values_changed", dict())
for key_change, change in d.get("type_changes", dict()).items():
if change["new_value"] == change["old_value"]: # str vs unicode type changes
continue
else:
changed[key_change] = change
for key in ["dictionary_item_added", "iterable_item_added", "attribute_added",
"set_item_added"]:
added = added.union(d.get(key, set()))
for key in ["dictionary_item_removed", "iterable_item_removed", "attribute_removed",
"set_item_removed"]:
removed = removed.union(d.get(key, set()))
return added, removed, changed
The solution used here was to keep the policy as a JSON-formatted string in the state
dictionaries. However, this means if a DeepDiff is performed on the strings, there is a
high chance of failure just due to formatting and ordering, even if the policies are the
same. So first, if there is a Policy attribute, do a DeepDiff of just the JSON parsed from
the strings of both, then if they match, remove the elements from the dictionaries. Then
there is another DeepDiff on the rest of the state, which wont fail erroneously now that
the policy strings have been removed.
There is another fun little detail to note here. By deleting the policy from the desired
state definition, it prevents future lookups of the policy, and comparisons of the policy
with the current state, since both are only performed if the Policy key is present. When
the user goes to update the policy, the key is reintroduced, and this whole process would
happen once more.
"""
if 'Policy' in desired_filtered:
policy_diff = DeepDiff(json.loads(desired_filtered['Policy']),
json.loads(current_filtered['Policy']), ignore_order=True)
if policy_diff:
logger.debug("Key policy is not equivalent for {0} with diff: {1}".format(
self.key_name, json.dumps(policy_diff)))
logger.warning(
'Update for policies not implemented. Will assert equivalence (incorrectly)')
# TODO: Change to return False once policy updates are supported
return True
# Remove policy strings since they are equivalent
desired_filtered.pop('Policy')
current_filtered.pop('Policy')
diff = DeepDiff(desired_filtered, current_filtered, ignore_order=True)
if not diff or len(diff) == 0:
return True
else:
print(diff)
def compare_dicts(expected, computed, tol, label):
"""Compares dictionaries *computed* to *expected* using DeepDiff
Float comparisons made to *tol* significant decimal places.
Note that a clean DeepDiff returns {}, which evaluates to False, hence the compare_integers.
"""
import pprint
import deepdiff
ans = deepdiff.DeepDiff(expected, computed, significant_digits=tol, verbose_level=2)
clean = not bool(ans)
if not clean:
pprint.pprint(ans)
return compare_integers(True, clean, label)
def difference(self, other):
from deepdiff import DeepDiff
return DeepDiff(self.dumpDict(), other.dumpDict(), ignore_order=True)