test_commit_record_schema_v2.py
python
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9
docs: expand cache plan with all seven testing tiers and do…
Sonnet 4.6
132 days ago
| 1 | """Tests for CommitRecord schema v2. |
| 2 | |
| 3 | Changes: |
| 4 | - ``format_version`` field removed entirely. |
| 5 | - ``branch`` renamed to ``created_on_branch``. |
| 6 | |
| 7 | These tests are written RED-first. They fail against the current implementation |
| 8 | and pass once the schema changes are applied to ``muse/core/store.py``. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | import datetime |
| 14 | |
| 15 | import pytest |
| 16 | |
| 17 | from muse.core.store import CommitRecord, CommitDict |
| 18 | from muse.core._types import MsgpackDict |
| 19 | |
| 20 | |
| 21 | # --------------------------------------------------------------------------- |
| 22 | # Helpers |
| 23 | # --------------------------------------------------------------------------- |
| 24 | |
| 25 | _NOW = datetime.datetime(2025, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc) |
| 26 | _TS = _NOW.isoformat() |
| 27 | _CID = "sha256:" + "a" * 64 |
| 28 | _SID = "sha256:" + "b" * 64 |
| 29 | |
| 30 | |
| 31 | def _minimal_msgpack(branch_key: str = "created_on_branch") -> MsgpackDict: |
| 32 | """Minimal raw dict for CommitRecord.from_msgpack.""" |
| 33 | return { |
| 34 | "commit_id": _CID, |
| 35 | "repo_id": "test-repo", |
| 36 | branch_key: "main", |
| 37 | "snapshot_id": _SID, |
| 38 | "message": "test commit", |
| 39 | "committed_at": _TS, |
| 40 | } |
| 41 | |
| 42 | |
| 43 | # --------------------------------------------------------------------------- |
| 44 | # CommitRecord field existence |
| 45 | # --------------------------------------------------------------------------- |
| 46 | |
| 47 | |
| 48 | class TestCommitRecordFields: |
| 49 | def test_has_created_on_branch(self) -> None: |
| 50 | rec = CommitRecord( |
| 51 | commit_id=_CID, |
| 52 | repo_id="r", |
| 53 | created_on_branch="main", |
| 54 | snapshot_id=_SID, |
| 55 | message="m", |
| 56 | committed_at=_NOW, |
| 57 | ) |
| 58 | assert rec.created_on_branch == "main" |
| 59 | |
| 60 | def test_has_no_branch_field(self) -> None: |
| 61 | rec = CommitRecord( |
| 62 | commit_id=_CID, |
| 63 | repo_id="r", |
| 64 | created_on_branch="main", |
| 65 | snapshot_id=_SID, |
| 66 | message="m", |
| 67 | committed_at=_NOW, |
| 68 | ) |
| 69 | assert not hasattr(rec, "branch"), ( |
| 70 | "CommitRecord must not have a 'branch' field after v2 schema change" |
| 71 | ) |
| 72 | |
| 73 | def test_has_no_format_version_field(self) -> None: |
| 74 | rec = CommitRecord( |
| 75 | commit_id=_CID, |
| 76 | repo_id="r", |
| 77 | created_on_branch="main", |
| 78 | snapshot_id=_SID, |
| 79 | message="m", |
| 80 | committed_at=_NOW, |
| 81 | ) |
| 82 | assert not hasattr(rec, "format_version"), ( |
| 83 | "CommitRecord must not have a 'format_version' field after v2 schema change" |
| 84 | ) |
| 85 | |
| 86 | |
| 87 | # --------------------------------------------------------------------------- |
| 88 | # to_dict serialisation |
| 89 | # --------------------------------------------------------------------------- |
| 90 | |
| 91 | |
| 92 | class TestCommitRecordToDict: |
| 93 | def _make(self) -> CommitRecord: |
| 94 | return CommitRecord( |
| 95 | commit_id=_CID, |
| 96 | repo_id="r", |
| 97 | created_on_branch="dev", |
| 98 | snapshot_id=_SID, |
| 99 | message="m", |
| 100 | committed_at=_NOW, |
| 101 | ) |
| 102 | |
| 103 | def test_to_dict_has_created_on_branch(self) -> None: |
| 104 | d = self._make().to_dict() |
| 105 | assert d["created_on_branch"] == "dev" |
| 106 | |
| 107 | def test_to_dict_has_no_branch_key(self) -> None: |
| 108 | d = self._make().to_dict() |
| 109 | assert "branch" not in d, ( |
| 110 | "to_dict() must not emit 'branch' key in v2 schema" |
| 111 | ) |
| 112 | |
| 113 | def test_to_dict_has_no_format_version_key(self) -> None: |
| 114 | d = self._make().to_dict() |
| 115 | assert "format_version" not in d, ( |
| 116 | "to_dict() must not emit 'format_version' key in v2 schema" |
| 117 | ) |
| 118 | |
| 119 | |
| 120 | # --------------------------------------------------------------------------- |
| 121 | # from_msgpack deserialisation |
| 122 | # --------------------------------------------------------------------------- |
| 123 | |
| 124 | |
| 125 | class TestCommitRecordFromMsgpack: |
| 126 | def test_reads_created_on_branch(self) -> None: |
| 127 | raw = _minimal_msgpack("created_on_branch") |
| 128 | rec = CommitRecord.from_msgpack(raw) |
| 129 | assert rec.created_on_branch == "main" |
| 130 | |
| 131 | def test_falls_back_to_branch_key_for_old_records(self) -> None: |
| 132 | """Old records serialised with 'branch' key must still deserialise. |
| 133 | |
| 134 | This shim supports the migration window: old commit files on disk |
| 135 | carry 'branch', new files carry 'created_on_branch'. Once all repos |
| 136 | are migrated via 'muse code migrate', this path is dead but harmless. |
| 137 | """ |
| 138 | raw = _minimal_msgpack("branch") |
| 139 | rec = CommitRecord.from_msgpack(raw) |
| 140 | assert rec.created_on_branch == "main" |
| 141 | |
| 142 | def test_raises_when_neither_key_present(self) -> None: |
| 143 | raw = _minimal_msgpack("created_on_branch") |
| 144 | del raw["created_on_branch"] |
| 145 | with pytest.raises(TypeError, match="created_on_branch"): |
| 146 | CommitRecord.from_msgpack(raw) |
| 147 | |
| 148 | def test_from_msgpack_ignores_format_version_key(self) -> None: |
| 149 | """Old records with format_version in the dict must still deserialise |
| 150 | without storing the field on the returned record.""" |
| 151 | raw = _minimal_msgpack("created_on_branch") |
| 152 | raw["format_version"] = 8 |
| 153 | rec = CommitRecord.from_msgpack(raw) |
| 154 | assert not hasattr(rec, "format_version") |
| 155 | |
| 156 | |
| 157 | # --------------------------------------------------------------------------- |
| 158 | # CommitDict TypedDict |
| 159 | # --------------------------------------------------------------------------- |
| 160 | |
| 161 | |
| 162 | class TestCommitDict: |
| 163 | def test_commit_dict_has_created_on_branch_key(self) -> None: |
| 164 | """CommitDict must declare 'created_on_branch', not 'branch'.""" |
| 165 | annotations = CommitDict.__annotations__ |
| 166 | assert "created_on_branch" in annotations, ( |
| 167 | "CommitDict must have 'created_on_branch' key" |
| 168 | ) |
| 169 | assert "branch" not in annotations, ( |
| 170 | "CommitDict must not have 'branch' key" |
| 171 | ) |
| 172 | |
| 173 | def test_commit_dict_has_no_format_version_key(self) -> None: |
| 174 | annotations = CommitDict.__annotations__ |
| 175 | assert "format_version" not in annotations, ( |
| 176 | "CommitDict must not have 'format_version' key" |
| 177 | ) |
File History
1 commit
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9
docs: expand cache plan with all seven testing tiers and do…
Sonnet 4.6
132 days ago