"""What this module covers: `models.io.lineage`, the vocabulary files, runs and versions record their origin with: the hashes of files and tables, and the rule that decides an output is already done. Fixtures: none; each test builds its own small table or file. """ import pyarrow as pa import pytest from models.io.lineage import file_sha256, matches, registered_name, require_match, table_sha256 def test_a_table_hashes_by_its_rows_not_its_metadata(): """Pins down: the same rows hash the same whatever metadata the table carries, and another row changes the hash. Fails if metadata such as `built_at` enters the hash, so every rebuild would look new.""" table = pa.table({"pac_id": [1, 2]}) assert table_sha256(table) == table_sha256(table.replace_schema_metadata({"built_at": "now"})) assert table_sha256(table) != table_sha256(pa.table({"pac_id": [1, 2, 3]})) def test_files_hash_in_order(tmp_path): """Pins down: several files hash as their bytes in the order given, so a model's code hash covers each file. Fails if a file is skipped or the order stops mattering.""" a, b = tmp_path / "a", tmp_path / "b" a.write_bytes(b"one") b.write_bytes(b"two") assert file_sha256(a, b) != file_sha256(b, a) != file_sha256(a) def test_an_output_is_done_when_its_lineage_has_every_expected_value(): """Pins down: recorded lineage matches when every expected key has its value, compared as text since files and MLflow store text; extra keys don't matter. Fails if a missing key counts as a match or numbers stop matching text.""" recorded = {"armado_id": "7", "n_members": "2", "built_at": "now"} assert matches(recorded, {"armado_id": 7, "n_members": 2}) assert not matches(recorded, {"armado_id": 7, "content_sha256": "x"}) def test_a_mismatch_is_refused_with_both_sides(): """Pins down: `require_match` names what was expected and what was found. Fails if a mismatch passes silently.""" with pytest.raises(ValueError, match="doesn't belong.*armado_id"): require_match({"armado_id": "1"}, {"armado_id": 2}, "build.parquet") def test_models_are_registered_as_domain_and_scenario(): """Pins down: the name runs and versions share. Fails if the convention changes on one side only.""" assert registered_name("cacu", "cin2_logistic") == "cacu-cin2_logistic"