Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_numkey(self):
rel = DictRelationship(parent=self.d, child=self.d[42], param=42)
assert rel.get_param_repr() == "[42]"
def test_automatic_child_rel(self):
assert isinstance(self.highest.t1_child_rel, DictRelationship)
assert isinstance(self.highest.t2_child_rel, DictRelationship)
assert self.highest.t1_child_rel.parent == self.highest.t1
assert self.highest.t2_child_rel.parent == self.highest.t2
assert self.highest.t1_child_rel.parent == self.highest.t1
assert self.highest.t2_child_rel.parent == self.highest.t2
# Provides textual relationship from t1 to t1[1337]
assert '[1337]' == self.highest.t2_child_rel.get_param_repr()
def test_objkey_misleading_repr(self):
rel = DictRelationship(
parent=self.d,
child=self.d[self.customkey_misleading],
param=self.customkey_misleading)
assert rel.get_param_repr() is None
def test_get_param_from_dict(self):
param = 42
rel = DictRelationship(parent=self.d, child=self.d[param], param=param)
obj = {10: 10, param: 123}
assert rel.get_param_from_obj(obj) == 123
def test_objkey(self):
rel = DictRelationship(
parent=self.d, child=self.d[self.customkey], param=self.customkey)
assert rel.get_param_repr() is None
cls.intermediate = DiffLevel(
cls.custom1,
cls.custom2,
down=cls.lowest,
child_rel1=rel_int_low_t1,
child_rel2=rel_int_low_t2)
cls.lowest.up = cls.intermediate
# Test automatic child relationship
t1_child_rel = ChildRelationship.create(
klass=DictRelationship,
parent=cls.t1,
child=cls.intermediate.t1,
param=1337)
t2_child_rel = ChildRelationship.create(
klass=DictRelationship,
parent=cls.t2,
child=cls.intermediate.t2,
param=1337)
cls.highest = DiffLevel(
cls.t1,
cls.t2,
down=cls.intermediate,
child_rel1=t1_child_rel,
child_rel2=t2_child_rel)
cls.intermediate.up = cls.highest
# for special stuff like custom objects and named tuples we receive preprocessed t1 and t2
# but must not spoil the chain (=level) with it
t1 = override_t1
t2 = override_t2
else:
t1 = level.t1
t2 = level.t2
if print_as_attribute:
item_added_key = "attribute_added"
item_removed_key = "attribute_removed"
rel_class = AttributeRelationship
else:
item_added_key = "dictionary_item_added"
item_removed_key = "dictionary_item_removed"
rel_class = DictRelationship
t1_keys = set(t1.keys())
t2_keys = set(t2.keys())
if self.ignore_string_type_changes or self.ignore_numeric_type_changes:
t1_clean_to_keys = self.__get_clean_to_keys_mapping(keys=t1_keys, level=level)
t2_clean_to_keys = self.__get_clean_to_keys_mapping(keys=t2_keys, level=level)
t1_keys = set(t1_clean_to_keys.keys())
t2_keys = set(t2_clean_to_keys.keys())
else:
t1_clean_to_keys = t2_clean_to_keys = None
t_keys_intersect = t2_keys.intersection(t1_keys)
t_keys_added = t2_keys - t_keys_intersect
t_keys_removed = t1_keys - t_keys_intersect
if result:
result = ':' if self.param_repr_format is None else self.param_repr_format.format(result)
return result
class DictRelationship(ChildRelationship):
param_repr_format = "[{}]"
quote_str = "'{}'"
def get_param_from_obj(self, obj):
return obj.get(self.param)
class SubscriptableIterableRelationship(DictRelationship):
# for our purposes, we can see lists etc. as special cases of dicts
def get_param_from_obj(self, obj):
return obj[self.param]
class InaccessibleRelationship(ChildRelationship):
pass
# there is no random access to set elements
class SetRelationship(InaccessibleRelationship):
pass
class NonSubscriptableIterableRelationship(InaccessibleRelationship):