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_prep_str_murmur3_128bit(self):
obj = "a"
expected_result = {
obj: 119173504597196970070553896747624927922
}
result = DeepHash(obj, ignore_string_type_changes=True, hasher=DeepHash.murmur3_128bit)
assert expected_result == result
def test_prep_str_murmur3_64bit(self):
obj = "a"
expected_result = {
obj: 424475663186367154
}
result = DeepHash(obj, ignore_string_type_changes=True, hasher=DeepHash.murmur3_64bit)
assert expected_result == result
def test_already_calculated_hash_wont_be_recalculated(self):
hashes = (i for i in range(10))
def hasher(obj):
return str(next(hashes))
obj = "a"
expected_result = {obj: '0'}
result = DeepHash(obj, hasher=hasher)
assert expected_result == result
# we simply feed the last result to DeepHash
# So it can re-use the results.
result2 = DeepHash(obj, hasher=hasher, hashes=result)
# if hashes are not cached and re-used,
# then the next time hasher runs, it returns
# number 1 instead of 0.
assert expected_result == result2
result3 = DeepHash(obj, hasher=hasher)
expected_result = {obj: '1'}
assert expected_result == result3
def test_built_in_hash_not_sensitive_to_bytecode_vs_unicode(self):
a = 'hello'
b = b'hello'
a_hash = DeepHash(a, ignore_string_type_changes=True)[a]
b_hash = DeepHash(b, ignore_string_type_changes=True)[b]
assert a_hash == b_hash
def test_dictionary(self):
obj = {1: 1}
result = DeepHash(obj)
assert set(result.keys()) == {1, get_id(obj)}
def test_get_hash_by_obj_when_does_not_exist(self):
a = "a"
obj = {1: a}
result = DeepHash(obj)
with pytest.raises(KeyError):
result[2]
def test_sha1_hash_not_sensitive_to_bytecode_vs_unicode(self):
a = 'hello'
b = b'hello'
a_hash = DeepHash(a, ignore_string_type_changes=True, hasher=DeepHash.sha1hex)[a]
b_hash = DeepHash(b, ignore_string_type_changes=True, hasher=DeepHash.sha1hex)[b]
assert a_hash == b_hash
def test_bad_attribute(self):
class Bad:
__slots__ = ['x', 'y']
def __getattr__(self, key):
raise AttributeError("Bad item")
def __str__(self):
return "Bad Object"
t1 = Bad()
result = DeepHash(t1)
expected_result = {t1: unprocessed, 'unprocessed': [t1]}
assert expected_result == result
def test_prep_str_sha1_fail_if_mutable(self):
"""
This test fails if DeepHash is getting a mutable copy of hashes
which means each init of the DeepHash will have hashes from
the previous init.
"""
obj1 = "a"
expected_result = {
obj1: '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8'
}
result = DeepHash(obj1, ignore_string_type_changes=True, hasher=DeepHash.sha1hex)
assert expected_result == result
obj2 = "b"
result = DeepHash(obj2, ignore_string_type_changes=True, hasher=DeepHash.sha1hex)
assert obj1 not in result
def get_query_hash(s: Search) -> str:
"""
Generates a deterministic Murmur3 or SHA256 hash from the serialized Search
object using DeepHash so that two Search objects with the same content will
produce the same hash.
:param s: Search object to be serialized and hashed.
:return: Serialized Search object hash.
"""
serialized_search_obj = s.to_dict()
serialized_search_obj.pop('from', None)
serialized_search_obj.pop('size', None)
deep_hash = DeepHash(serialized_search_obj)[serialized_search_obj]
return deep_hash