Files
uconsole_asw_game/tests/test_gen_contacts.py
T
Claude 66fc80127d Check shipped boat consistency in the catalog validator only
Older save snapshots and user edits keep loading; the packaged data is still
held to its machine and endurance by tools/gen_contacts.py.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CW6FkwLLc6KvPHvCs5KTRo
2026-10-04 08:50:27 +00:00

77 lines
2.6 KiB
Python

import json
import shutil
from importlib import resources
import pytest
from tools.gen_contacts import check, validate
def _copy_catalog(destination):
source = resources.files("data.contacts")
for item in source.iterdir():
if item.name.endswith(".json"):
shutil.copyfile(item, destination / item.name)
def test_validator_accepts_packaged_catalog():
with resources.as_file(resources.files("data.contacts")) as contact_dir:
loaded = validate(contact_dir)
assert check(contact_dir) == 0
assert loaded.db_source == "contacts"
def test_cli_check_rejects_incomplete_provenance_coverage(tmp_path, capsys):
_copy_catalog(tmp_path)
path = tmp_path / "sources.json"
data = json.loads(path.read_text(encoding="utf-8"))
data["claims"].pop()
path.write_text(json.dumps(data), encoding="utf-8")
with pytest.raises(ValueError, match="incomplete field coverage"):
validate(tmp_path)
assert check(tmp_path) == 1
assert "incomplete field coverage" in capsys.readouterr().err
@pytest.mark.parametrize(
("filename", "mutate", "message"),
[
("subs.json", lambda data: data["entries"][0].update(quiet=float("nan")),
"finite number"),
("aircraft.json", lambda data: data["entries"][0].update(kind="unknown"),
"invalid aircraft kind"),
("torpedoes.json", lambda data: data["entries"].append(data["entries"][0]),
"duplicate key"),
("acoustics.json", lambda data: data["entries"].pop(),
"every decoy"),
],
)
def test_validator_rejects_invalid_data(tmp_path, filename, mutate, message):
_copy_catalog(tmp_path)
path = tmp_path / filename
data = json.loads(path.read_text(encoding="utf-8"))
mutate(data)
path.write_text(json.dumps(data, allow_nan=True), encoding="utf-8")
with pytest.raises(ValueError, match=message):
validate(tmp_path)
assert check(tmp_path) == 1
@pytest.mark.parametrize(("mutate", "message"), [
(lambda d: d["entries"][0].update(speed_kn=[4, 30]), "top speed differs"),
(lambda d: d["entries"][0]["acoustic"].update(propulsion="elektrisch/AIP"),
"AIP endurance contradicts"),
])
def test_validator_holds_shipped_boats_to_their_machine(tmp_path, mutate, message):
from src.data import catalog
_copy_catalog(tmp_path)
path = tmp_path / "subs.json"
data = json.loads(path.read_text(encoding="utf-8"))
mutate(data)
path.write_text(json.dumps(data), encoding="utf-8")
# The runtime loader stays lenient for save snapshots and user edits.
catalog._load_catalog_from(tmp_path)
with pytest.raises(ValueError, match=message):
validate(tmp_path)