2024-03-24 16:56:13 -07:00
|
|
|
import json
|
|
|
|
from pathlib import Path
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from ttfrog.db import schema
|
|
|
|
from ttfrog.db.manager import db as _db
|
|
|
|
|
2024-03-26 00:53:21 -07:00
|
|
|
FIXTURE_PATH = Path(__file__).parent / "fixtures"
|
2024-03-24 16:56:13 -07:00
|
|
|
|
|
|
|
|
|
|
|
def load_fixture(db, fixture_name):
|
|
|
|
with db.transaction():
|
|
|
|
data = json.loads((FIXTURE_PATH / f"{fixture_name}.json").read_text())
|
|
|
|
for schema_name in data:
|
|
|
|
for record in data[schema_name]:
|
|
|
|
print(f"Loading {schema_name} {record = }")
|
|
|
|
obj = getattr(schema, schema_name)(**record)
|
|
|
|
db.session.add(obj)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def db(monkeypatch):
|
2024-03-26 00:53:21 -07:00
|
|
|
monkeypatch.setattr("ttfrog.db.manager.database", MagicMock(return_value=""))
|
|
|
|
monkeypatch.setenv("DATABASE_URL", "sqlite:///:memory:")
|
|
|
|
monkeypatch.setenv("DEBUG", "1")
|
2024-03-24 16:56:13 -07:00
|
|
|
_db.init()
|
2024-04-20 23:27:47 -07:00
|
|
|
yield _db
|
|
|
|
_db.metadata.drop_all(bind=_db.engine)
|
2024-03-24 16:56:13 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-05-04 13:15:54 -07:00
|
|
|
def bootstrap(db):
|
|
|
|
with db.transaction():
|
|
|
|
# ancestries
|
|
|
|
human = schema.Ancestry("human")
|
2024-04-20 20:35:24 -07:00
|
|
|
|
2024-05-04 13:15:54 -07:00
|
|
|
tiefling = schema.Ancestry("tiefling")
|
|
|
|
tiefling.add_modifier(schema.Modifier("Ability Score Increase", target="intelligence", relative_value=1))
|
|
|
|
tiefling.add_modifier(schema.Modifier("Ability Score Increase", target="charisma", relative_value=2))
|
2024-04-20 20:35:24 -07:00
|
|
|
|
2024-05-04 13:15:54 -07:00
|
|
|
# ancestry traits
|
|
|
|
darkvision = schema.AncestryTrait("Darkvision")
|
|
|
|
darkvision.add_modifier(schema.Modifier("Darkvision", target="vision_in_darkness", absolute_value=120))
|
|
|
|
tiefling.add_trait(darkvision)
|
2024-03-24 16:56:13 -07:00
|
|
|
|
2024-05-04 13:15:54 -07:00
|
|
|
dragonborn = schema.Ancestry("dragonborn")
|
|
|
|
dragonborn.add_trait(darkvision)
|
2024-03-24 16:56:13 -07:00
|
|
|
|
2024-05-04 13:15:54 -07:00
|
|
|
db.add_or_update([human, dragonborn, tiefling])
|
|
|
|
|
|
|
|
# classes
|
|
|
|
fighting_style = schema.ClassAttribute("Fighting Style")
|
|
|
|
fighting_style.add_option(name="Archery")
|
|
|
|
fighting_style.add_option(name="Defense")
|
|
|
|
db.add_or_update(fighting_style)
|
|
|
|
|
|
|
|
fighter = schema.CharacterClass("fighter", hit_dice="1d10", hit_dice_stat="CON")
|
|
|
|
fighter.add_attribute(fighting_style, level=2)
|
|
|
|
|
|
|
|
rogue = schema.CharacterClass("rogue", hit_dice="1d8", hit_dice_stat="DEX")
|
|
|
|
db.add_or_update([rogue, fighter])
|
|
|
|
|
|
|
|
# characters
|
|
|
|
foo = schema.Character("Foo", ancestry=tiefling, _intelligence=14)
|
|
|
|
foo.add_class(fighter, level=2)
|
|
|
|
foo.add_class(rogue, level=3)
|
2024-04-20 20:35:24 -07:00
|
|
|
|
2024-05-04 13:15:54 -07:00
|
|
|
bar = schema.Character("Bar", ancestry=human)
|
2024-04-20 20:35:24 -07:00
|
|
|
|
2024-05-04 13:15:54 -07:00
|
|
|
# persist all the records we've created
|
|
|
|
db.add_or_update([foo, bar])
|