87 lines
3.1 KiB
Python
87 lines
3.1 KiB
Python
"""Tests for the domain models and catalogue."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from genshin_tcg.constants import CardType, Element, WeaponType
|
|
from genshin_tcg.data.catalog import Catalog, load_catalog, save_catalog
|
|
from genshin_tcg.models import ActionCard, CharacterCard, parse_catalog
|
|
|
|
|
|
def test_parse_catalog_classifies_cards(sample_payload: list[dict[str, object]]) -> None:
|
|
cards = parse_catalog(sample_payload)
|
|
assert len(cards) == 4
|
|
characters = [c for c in cards if isinstance(c, CharacterCard)]
|
|
actions = [c for c in cards if isinstance(c, ActionCard)]
|
|
assert len(characters) == 1
|
|
assert len(actions) == 3
|
|
|
|
|
|
def test_character_card_parsing(sample_payload: list[dict[str, object]]) -> None:
|
|
character = next(
|
|
card for card in parse_catalog(sample_payload) if isinstance(card, CharacterCard)
|
|
)
|
|
assert character.id == "test_pyro"
|
|
assert character.element is Element.PYRO
|
|
assert character.weapon is WeaponType.CLAYMORE
|
|
assert character.hp == 10
|
|
assert character.energy == 3
|
|
assert len(character.skills) == 3
|
|
assert character.skills[0].cost[0].count == 1
|
|
assert character.skills[1].sub is not None
|
|
assert character.skills[1].sub.name == "Flamme éternelle"
|
|
|
|
|
|
def test_action_card_flattens_skills_object(sample_payload: list[dict[str, object]]) -> None:
|
|
talent = next(
|
|
card
|
|
for card in parse_catalog(sample_payload)
|
|
if isinstance(card, ActionCard) and card.id == "test_talent"
|
|
)
|
|
assert talent.type is CardType.TALENT
|
|
assert talent.requirement == "test_pyro"
|
|
assert talent.cost[0].count == 4
|
|
assert "Action de combat" in talent.description
|
|
|
|
|
|
def test_action_card_with_empty_cost(sample_payload: list[dict[str, object]]) -> None:
|
|
artifact = next(
|
|
card
|
|
for card in parse_catalog(sample_payload)
|
|
if isinstance(card, ActionCard) and card.id == "test_artifact"
|
|
)
|
|
assert artifact.cost == ()
|
|
|
|
|
|
def test_unknown_card_shape_raises() -> None:
|
|
with pytest.raises(ValidationError):
|
|
parse_catalog([{"_id": 1, "id": "x", "type": "character", "name": "x"}])
|
|
|
|
|
|
def test_catalog_queries(sample_catalog: Catalog) -> None:
|
|
assert len(sample_catalog) == 4
|
|
assert len(sample_catalog.characters) == 1
|
|
assert len(sample_catalog.actions) == 3
|
|
assert sample_catalog.get("test_food").name == "Plat de test"
|
|
subset = sample_catalog.select(["test_food", "test_pyro"])
|
|
assert [card.id for card in subset] == ["test_food", "test_pyro"]
|
|
|
|
|
|
def test_catalog_duplicate_ids_warn(sample_payload: list[dict[str, object]]) -> None:
|
|
duplicated = [*sample_payload, sample_payload[0]]
|
|
catalog = Catalog(cards=tuple(parse_catalog(duplicated)))
|
|
assert len(catalog.cards) == 5
|
|
assert len(catalog._by_id) == 4
|
|
|
|
|
|
def test_catalog_round_trip(sample_catalog: Catalog, tmp_path: Path) -> None:
|
|
path = tmp_path / "catalog.json"
|
|
save_catalog(sample_catalog, path)
|
|
reloaded = load_catalog(path)
|
|
assert [card.id for card in reloaded] == [card.id for card in sample_catalog]
|
|
assert reloaded.get("test_pyro") == sample_catalog.get("test_pyro")
|