tabletop-frog/test/conftest.py

50 lines
1.2 KiB
Python
Raw Normal View History

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()
return _db
@pytest.fixture
2024-04-14 11:37:34 -07:00
def classes_factory(db):
2024-03-26 00:53:21 -07:00
load_fixture(db, "classes")
2024-04-20 20:35:24 -07:00
2024-04-14 11:37:34 -07:00
def factory():
return dict((rec.name, rec) for rec in db.session.query(schema.CharacterClass).all())
2024-04-20 20:35:24 -07:00
2024-04-14 11:37:34 -07:00
return factory
2024-03-24 16:56:13 -07:00
@pytest.fixture
2024-04-14 11:37:34 -07:00
def ancestries_factory(db):
2024-03-26 00:53:21 -07:00
load_fixture(db, "ancestry")
2024-04-20 20:35:24 -07:00
2024-04-14 11:37:34 -07:00
def factory():
return dict((rec.name, rec) for rec in db.session.query(schema.Ancestry).all())
2024-04-20 20:35:24 -07:00
2024-04-14 11:37:34 -07:00
return factory