"""Tests for the genotype file QC checks.""" import pytest from uploader.genotypes.qc import (allowed_symbols, check_file, sort_markers) CONFIG = { "markers": "Locus", "chromosome": "Chr", "cm": "cM", "mb": "Mb", "separator": ",", "comment.char": "#", "transposed": False, } STAGE02 = { "geno_encoding": {"mat": "A", "pat": "T", "het": "H"}, "missing_symbols": ["N"], } @pytest.mark.unit_test def test_allowed_symbols_includes_encoding_and_missing_values(): """ GIVEN: a Stage 02 encoding and missing-value symbols WHEN: the allowed symbols are computed THEN: all non-empty encoding and missing-value symbols are included """ assert allowed_symbols(STAGE02) == {"A", "T", "H", "N"} assert allowed_symbols({"geno_encoding": {"mat": ""}, "missing_symbols": []}) == set() @pytest.mark.unit_test def test_sort_markers_orders_by_chromosome_then_position_and_warns(): """ GIVEN: marker records in arbitrary order WHEN: they are sorted THEN: they are ordered by chromosome, cM, then Mb and a placeholder warning is emitted """ markers = [ {"name": "m3", "chromosome": "2", "cm": 0, "mb": 0}, {"name": "m2", "chromosome": "1", "cm": 2, "mb": 3}, {"name": "m1", "chromosome": "1", "cm": 1, "mb": 1}, ] with pytest.warns(UserWarning, match="placeholder"): ordered = sort_markers(markers) assert [marker["name"] for marker in ordered] == ["m1", "m2", "m3"] @pytest.mark.unit_test def test_check_file_accepts_values_from_the_encoding(tmp_path): """ GIVEN: an untransposed file whose values are encoding/missing symbols WHEN: the file is checked THEN: no errors are reported and all sample values are counted """ filepath = tmp_path / "genotypes.csv" filepath.write_text( "Locus,Chr,cM,Mb,s1,s2\n" "m1,1,0.0,1.0,A,T\n" "m2,1,1.0,2.0,H,N\n", encoding="utf8") result = check_file(filepath, CONFIG, STAGE02) assert result["values_checked"] == 4 assert result["errors"] == [] @pytest.mark.unit_test def test_check_file_flags_empty_and_unknown_values(tmp_path): """ GIVEN: an untransposed file with an unknown symbol and an empty value WHEN: the file is checked THEN: both are reported as errors """ filepath = tmp_path / "genotypes.csv" filepath.write_text( "Locus,Chr,cM,Mb,s1,s2\n" "m1,1,0.0,1.0,A,Z\n" "m2,1,1.0,2.0,,T\n", encoding="utf8") result = check_file(filepath, CONFIG, STAGE02) assert result["values_checked"] == 4 messages = [error["message"] for error in result["errors"]] assert "Unknown genotype symbol 'Z'." in messages assert "Empty genotype value." in messages @pytest.mark.unit_test def test_check_file_flags_duplicate_markers_within_a_chromosome(tmp_path): """ GIVEN: a marker name repeated within one chromosome, and reused on another WHEN: the file is checked THEN: only the within-chromosome duplicate is reported as an error """ filepath = tmp_path / "duplicates.csv" filepath.write_text( "Locus,Chr,cM,Mb,s1,s2\n" "m1,1,0.0,1.0,A,T\n" "m1,1,1.0,2.0,T,H\n" "m1,2,0.0,3.0,H,A\n" "m2,2,1.0,4.0,A,T\n", encoding="utf8") result = check_file(filepath, CONFIG, STAGE02) assert len(result["errors"]) == 1 assert ("Duplicate marker 'm1' on chromosome '1'." in result["errors"][0]["message"]) assert result["errors"][0]["row"] == "m1"