|
20 | 20 | the original `hash_value()` and the `hash_primitive()` functions. |
21 | 21 | """ |
22 | 22 |
|
| 23 | +import functools |
| 24 | +import hashlib |
| 25 | +import importlib |
| 26 | +import sys |
| 27 | +import types |
| 28 | + |
23 | 29 | import numpy as np |
24 | 30 | import pandas as pd |
25 | 31 | import pytest |
26 | 32 |
|
27 | 33 | from hamilton.caching import fingerprinting |
28 | 34 |
|
29 | 35 |
|
| 36 | +@pytest.fixture |
| 37 | +def force_md5_backend(monkeypatch): |
| 38 | + """Force the hashlib.md5 fallback backend, regardless of whether xxhash |
| 39 | + is installed in the current environment.""" |
| 40 | + monkeypatch.setattr( |
| 41 | + fingerprinting, "hash_func", functools.partial(hashlib.md5, usedforsecurity=False) |
| 42 | + ) |
| 43 | + |
| 44 | + |
30 | 45 | def test_hash_none(): |
31 | 46 | fingerprint = fingerprinting.hash_value(None) |
32 | 47 | assert fingerprint == "<none>" |
@@ -226,6 +241,123 @@ def test_hash_numpy(): |
226 | 241 | assert fingerprint == expected_hash |
227 | 242 |
|
228 | 243 |
|
| 244 | +# --------------------------------------------------------------------------- |
| 245 | +# hashlib.md5 fallback backend |
| 246 | +# |
| 247 | +# These mirror the pinned tests above but force `hash_func` to the hashlib.md5 |
| 248 | +# fallback (via the `force_md5_backend` fixture) so the fallback path used |
| 249 | +# when `xxhash` isn't installed is verified regardless of what's installed in |
| 250 | +# the environment running the suite. Expected digests are the pre-xxh3_128 |
| 251 | +# values this module used before xxhash became the default backend. |
| 252 | +# --------------------------------------------------------------------------- |
| 253 | + |
| 254 | + |
| 255 | +@pytest.mark.usefixtures("force_md5_backend") |
| 256 | +@pytest.mark.parametrize( |
| 257 | + ("obj", "expected_hash"), |
| 258 | + [ |
| 259 | + ("hello-world", "L1Q1Kh6_t1atHO_H8RbBeA=="), |
| 260 | + (17.31231, "mJPTpPyXDSZgU-u8NuztIQ=="), |
| 261 | + (16474, "6MgAp1NbMW0ZZpe_8iKVsg=="), |
| 262 | + (True, "J2eGynSuIpd5bwVQzO9VVg=="), |
| 263 | + (b"\x951!\x89u=\xe6\xadG\xdf", "d1DufDgRQmqi9Kt4Z2PeUQ=="), |
| 264 | + ], |
| 265 | +) |
| 266 | +def test_hash_primitive_md5_fallback(obj, expected_hash): |
| 267 | + fingerprint = fingerprinting.hash_primitive(obj) |
| 268 | + assert fingerprint == expected_hash |
| 269 | + |
| 270 | + |
| 271 | +@pytest.mark.usefixtures("force_md5_backend") |
| 272 | +@pytest.mark.parametrize( |
| 273 | + ("obj", "expected_hash"), |
| 274 | + [ |
| 275 | + ([0, True, "hello-world"], "mlOjj4yeCrSDFSn5zgdEIg=="), |
| 276 | + ((17.0, False, "world"), "BcRSGfyKeIOdym9B6TmAyQ=="), |
| 277 | + ], |
| 278 | +) |
| 279 | +def test_hash_sequence_md5_fallback(obj, expected_hash): |
| 280 | + fingerprint = fingerprinting.hash_sequence(obj) |
| 281 | + assert fingerprint == expected_hash |
| 282 | + |
| 283 | + |
| 284 | +@pytest.mark.usefixtures("force_md5_backend") |
| 285 | +def test_hash_ordered_mapping_md5_fallback(): |
| 286 | + obj = {0: True, "key": "value", 17.0: None} |
| 287 | + expected_hash = "GyxyI9-pq-EJJvSAIN509g==" |
| 288 | + fingerprint = fingerprinting.hash_mapping(obj, ignore_order=False) |
| 289 | + assert fingerprint == expected_hash |
| 290 | + |
| 291 | + |
| 292 | +@pytest.mark.usefixtures("force_md5_backend") |
| 293 | +def test_hash_unordered_mapping_md5_fallback(): |
| 294 | + obj = {0: True, "key": "value", 17.0: None} |
| 295 | + expected_hash = "cDuuL2eA3DaSWlWW3u7o9g==" |
| 296 | + fingerprint = fingerprinting.hash_mapping(obj, ignore_order=True) |
| 297 | + assert fingerprint == expected_hash |
| 298 | + |
| 299 | + |
| 300 | +@pytest.mark.usefixtures("force_md5_backend") |
| 301 | +def test_hash_set_md5_fallback(): |
| 302 | + obj = {0, True, "key", "value", 17.0, None} |
| 303 | + expected_hash = "E_f_tjbi6qn7KL3NUCZayg==" |
| 304 | + fingerprint = fingerprinting.hash_set(obj) |
| 305 | + assert fingerprint == expected_hash |
| 306 | + |
| 307 | + |
| 308 | +@pytest.mark.usefixtures("force_md5_backend") |
| 309 | +def test_hash_numpy_md5_fallback(): |
| 310 | + array = np.array([[0, 1], [2, 3]], dtype=np.int64) |
| 311 | + expected_hash = "024zwZIcWy6r4dlX4AMTow==" |
| 312 | + fingerprint = fingerprinting.hash_value(array) |
| 313 | + assert fingerprint == expected_hash |
| 314 | + |
| 315 | + |
| 316 | +def test_hash_bytes_digest_width(): |
| 317 | + """Both backends produce a 16-byte digest (24 base64url chars), so swapping |
| 318 | + the algorithm doesn't change the shape of cache keys / `data_version` strings. |
| 319 | + """ |
| 320 | + assert len(fingerprinting._hash_bytes(b"x")) == 24 |
| 321 | + |
| 322 | + |
| 323 | +@pytest.mark.usefixtures("force_md5_backend") |
| 324 | +def test_hash_bytes_digest_width_md5_fallback(): |
| 325 | + assert len(fingerprinting._hash_bytes(b"x")) == 24 |
| 326 | + |
| 327 | + |
| 328 | +# --------------------------------------------------------------------------- |
| 329 | +# Import-time backend resolution |
| 330 | +# |
| 331 | +# These reload the module to actually exercise the try/except at import time |
| 332 | +# (as opposed to `force_md5_backend`, which only overrides the already-resolved |
| 333 | +# `hash_func`), then restore the module to its real, ambient-environment state |
| 334 | +# so later tests aren't affected. |
| 335 | +# --------------------------------------------------------------------------- |
| 336 | + |
| 337 | + |
| 338 | +def test_falls_back_when_xxhash_not_installed(monkeypatch): |
| 339 | + """Simulate xxhash being absent: `import xxhash` raises ModuleNotFoundError.""" |
| 340 | + monkeypatch.setitem(sys.modules, "xxhash", None) |
| 341 | + try: |
| 342 | + reloaded = importlib.reload(fingerprinting) |
| 343 | + assert reloaded.hash_func.func is hashlib.md5 |
| 344 | + assert reloaded.hash_func.keywords == {"usedforsecurity": False} |
| 345 | + finally: |
| 346 | + importlib.reload(fingerprinting) |
| 347 | + |
| 348 | + |
| 349 | +def test_falls_back_when_xxhash_lacks_xxh3_128(monkeypatch): |
| 350 | + """Simulate an xxhash older than 0.8.0, which doesn't define xxh3_128.""" |
| 351 | + stub = types.ModuleType("xxhash") |
| 352 | + monkeypatch.setitem(sys.modules, "xxhash", stub) |
| 353 | + try: |
| 354 | + reloaded = importlib.reload(fingerprinting) |
| 355 | + assert reloaded.hash_func.func is hashlib.md5 |
| 356 | + assert reloaded.hash_func.keywords == {"usedforsecurity": False} |
| 357 | + finally: |
| 358 | + importlib.reload(fingerprinting) |
| 359 | + |
| 360 | + |
229 | 361 | def test_hash_numpy_different_shapes_differ(): |
230 | 362 | """Arrays with the same raw bytes but different shapes must hash differently.""" |
231 | 363 | a = np.array([1, 2, 3, 4, 5, 6]) |
|
0 commit comments