"""What this module covers: `models.io.lineage`, the vocabulary files, runs and versions record their origin with, and the rule that decides an output is already done; and the hashes of files and tables in `models.io.storage`. Fixtures: none; each test builds its own small table or file. """ import pyarrow as pa import pytest from models.io.lineage import matches, registered_name from models.io.storage import cohort_reference, file_sha256, 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" @pytest.mark.parametrize("metadata, expected", [ ({"url": "u", "producer": "dbt", "build": "4"}, ("dbt", "4")), ({"url": "u", "producer": "kit", "armado_id": "2"}, ("kit", "2")), ({"url": "u", "producer": "kit build, timeline from dbt view", "armado_id": "1"}, ("kit", "1")), ({"url": "u", "armado_id": "3"}, ("kit", "3")), ]) def test_a_file_is_dbts_only_when_its_producer_says_dbt(metadata, expected): """Pins down: a cohort file's source is dbt only for `producer: dbt`; any other producer, or none, is a kit build numbered by `armado_id`. Fails if an older kit file with a descriptive producer is read as dbt's and loses its `armado_id`.""" reference = cohort_reference(metadata, "f" * 64) assert (reference["cohort.source"], reference["cohort.build"]) == expected