|
| 1 | +"""Tests for VRS allele identification helpers. |
| 2 | +
|
| 3 | +These tests run offline: ``ga4gh_identify`` is pure computation given a |
| 4 | +well-formed VRS object, and ``normalize`` is patched so no SeqRepo or UTA |
| 5 | +access is required. |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | +from ga4gh.vrs._internal.models import ( |
| 10 | + Allele, |
| 11 | + LiteralSequenceExpression, |
| 12 | + SequenceLocation, |
| 13 | + SequenceReference, |
| 14 | +) |
| 15 | + |
| 16 | +from dcd_mapping.vrs_utils import identify_allele, normalize_and_identify |
| 17 | + |
| 18 | + |
| 19 | +def _make_allele(start: int = 1, end: int = 2, sequence: str = "A") -> Allele: |
| 20 | + """Build a minimal in-memory VRS Allele with no cached digests.""" |
| 21 | + return Allele( |
| 22 | + location=SequenceLocation( |
| 23 | + sequenceReference=SequenceReference( |
| 24 | + refgetAccession="SQ.0123456789abcdef0123456789abcdef" |
| 25 | + ), |
| 26 | + start=start, |
| 27 | + end=end, |
| 28 | + ), |
| 29 | + state=LiteralSequenceExpression(sequence=sequence), |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +def test_identify_allele_returns_va_digest(): |
| 34 | + assert identify_allele(_make_allele()).startswith("ga4gh:VA.") |
| 35 | + |
| 36 | + |
| 37 | +def test_identify_allele_is_content_addressed(): |
| 38 | + # Identical content -> identical digest; differing content -> differing digest. |
| 39 | + a = _make_allele() |
| 40 | + b = _make_allele() |
| 41 | + c = _make_allele(start=5, end=6) |
| 42 | + assert identify_allele(a) == identify_allele(b) |
| 43 | + assert identify_allele(a) != identify_allele(c) |
| 44 | + |
| 45 | + |
| 46 | +def test_identify_allele_clears_stale_digests(): |
| 47 | + """The whole point of the helper: a cached pre-mutation digest must not |
| 48 | + leak into the post-mutation identifier. |
| 49 | +
|
| 50 | + Reproduces the bug pattern where an allele is constructed, its location |
| 51 | + gets a refgetAccession mutation, and then it's identified. If the helper |
| 52 | + is bypassed and ``ga4gh_identify`` is called directly, the Merkle-tree |
| 53 | + returns the stale digest without recomputing, so the identifier doesn't |
| 54 | + reflect the current content. By clearing cached digests first, the helper |
| 55 | + ensures the identifier is always correct even if the input allele has been |
| 56 | + mutated since the last identification. |
| 57 | + """ |
| 58 | + allele = _make_allele(start=5, end=6) |
| 59 | + allele.location.digest = "ga4gh:SL.stale-location-digest" |
| 60 | + allele.digest = "ga4gh:VA.stale-allele-digest" |
| 61 | + |
| 62 | + identifier = identify_allele(allele) |
| 63 | + |
| 64 | + assert identifier.startswith("ga4gh:VA.") |
| 65 | + assert identifier != "ga4gh:VA.stale-allele-digest" |
| 66 | + # The identifier must match a freshly-built allele with the same content, |
| 67 | + # not anything derived from the stale digests. |
| 68 | + assert identifier == identify_allele(_make_allele(start=5, end=6)) |
| 69 | + |
| 70 | + |
| 71 | +def test_normalize_and_identify_pairs_both_steps(mocker): |
| 72 | + """normalize_and_identify normalizes then identifies in that order.""" |
| 73 | + allele = _make_allele() |
| 74 | + # Pass-through normalize so the test stays offline (no SeqRepo). |
| 75 | + mock_normalize = mocker.patch( |
| 76 | + "dcd_mapping.vrs_utils.normalize", side_effect=lambda a, **_: a |
| 77 | + ) |
| 78 | + mocker.patch("dcd_mapping.vrs_utils.get_seqrepo", return_value=mocker.MagicMock()) |
| 79 | + |
| 80 | + result = normalize_and_identify(allele) |
| 81 | + |
| 82 | + mock_normalize.assert_called_once() |
| 83 | + assert result is allele |
| 84 | + assert result.id is not None |
| 85 | + assert result.id.startswith("ga4gh:VA.") |
| 86 | + |
| 87 | + |
| 88 | +def test_normalize_and_identify_clears_stale_digest_through_helper(mocker): |
| 89 | + """Stale digests survive a no-op normalize, so the identify step must |
| 90 | + still clear them. Verifies the pairing actually delivers the invariant. |
| 91 | + """ |
| 92 | + allele = _make_allele() |
| 93 | + allele.location.digest = "ga4gh:SL.stale" |
| 94 | + allele.digest = "ga4gh:VA.stale" |
| 95 | + mocker.patch("dcd_mapping.vrs_utils.normalize", side_effect=lambda a, **_: a) |
| 96 | + mocker.patch("dcd_mapping.vrs_utils.get_seqrepo", return_value=mocker.MagicMock()) |
| 97 | + |
| 98 | + result = normalize_and_identify(allele) |
| 99 | + |
| 100 | + assert result.id != "ga4gh:VA.stale" |
| 101 | + assert result.id.startswith("ga4gh:VA.") |
| 102 | + |
| 103 | + |
| 104 | +def test_identify_allele_raises_when_digest_unobtainable(mocker): |
| 105 | + """When ``ga4gh_identify`` returns ``None`` (malformed input), surface it |
| 106 | + as a ValueError rather than silently stamping an unidentifiable allele. |
| 107 | + """ |
| 108 | + mocker.patch("dcd_mapping.vrs_utils.ga4gh_identify", return_value=None) |
| 109 | + with pytest.raises(ValueError, match="Failed to compute GA4GH identifier"): |
| 110 | + identify_allele(_make_allele()) |
0 commit comments