Adding tests of character schema

This commit is contained in:
evilchili
2024-03-24 16:56:13 -07:00
parent dba8bb315a
commit b1d7639a62
9 changed files with 211 additions and 4 deletions
+42
View File
@@ -0,0 +1,42 @@
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
FIXTURE_PATH = Path(__file__).parent / 'fixtures'
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):
monkeypatch.setattr('ttfrog.db.manager.database', MagicMock(return_value=""))
monkeypatch.setenv('DATABASE_URL', "sqlite:///:memory:")
monkeypatch.setenv('DEBUG', '1')
_db.init()
return _db
@pytest.fixture
def classes(db):
load_fixture(db, 'classes')
return dict((rec.name, rec) for rec in db.session.query(schema.CharacterClass).all())
@pytest.fixture
def ancestries(db):
load_fixture(db, 'ancestry')
return dict((rec.name, rec) for rec in db.session.query(schema.Ancestry).all())
+19
View File
@@ -0,0 +1,19 @@
{
"Ancestry": [
{"id": 1, "name": "human", "creature_type": "humanoid"},
{"id": 2, "name": "dragonborn", "creature_type": "humanoid"},
{"id": 3, "name": "tiefling", "creature_type": "humanoid"},
{"id": 4, "name": "elf", "creature_type": "humanoid"}
],
"AncestryTrait": [
{"id": 1, "name": "+1 to All Ability Scores"},
{"id": 2, "name": "Breath Weapon"},
{"id": 3, "name": "Darkvision"}
],
"AncestryTraitMap": [
{"ancestry_id": 1, "ancestry_trait_id": 1, "level": 1},
{"ancestry_id": 2, "ancestry_trait_id": 2, "level": 1},
{"ancestry_id": 2, "ancestry_trait_id": 2, "level": 1},
{"ancestry_id": 3, "ancestry_trait_id": 3, "level": 1}
]
}
+7
View File
@@ -0,0 +1,7 @@
{
"Ancestry": [
{"id": 1, "name": "+1 to All Ability Scores"},
{"id": 2, "name": "Breath Weapon"},
{"id": 3, "name": "Darkvision"}
]
}
+20
View File
@@ -0,0 +1,20 @@
{
"CharacterClass": [
{
"name": "fighter",
"hit_dice": "1d10",
"hit_dice_stat": "CON",
"proficiencies": "all armor, all shields, simple weapons, martial weapons",
"saving_throws": ["STR, CON"],
"skills": ["Acrobatics", "Animal Handling", "Athletics", "History", "Insight", "Intimidation", "Perception", "Survival"]
},
{
"name": "rogue",
"hit_dice": "1d8",
"hit_dice_stat": "DEX",
"proficiencies": "simple weapons, hand crossbows, longswords, rapiers, shortswords",
"saving_throws": ["DEX", "INT"],
"skills": ["Acrobatics", "Athletics", "Deception", "Insight", "Intimidation", "Investigation", "Perception", "Performance", "Persuasion", "Sleight of Hand", "Stealth"]
}
]
}
+7
View File
@@ -0,0 +1,7 @@
{
"name": "multiclass_pc",
"classes": [
"fighter": 5,
"rogue": 3
]
}
+56
View File
@@ -0,0 +1,56 @@
from ttfrog.db import schema
def test_create_character(db, classes, ancestries):
with db.transaction():
darkvision = db.session.query(schema.AncestryTrait).filter_by(name='Darkvision')[0]
# create a human character (the default)
char = schema.Character(name='Test Character')
db.add(char)
assert char.id == 1
assert char.armor_class == 10
assert char.name == 'Test Character'
assert char.ancestry.name == 'human'
assert darkvision not in char.traits
# switch ancestry to tiefling
char.ancestry = ancestries['tiefling']
db.add(char)
char = db.session.get(schema.Character, 1)
assert char.ancestry.name == 'tiefling'
assert darkvision in char.traits
# assign a class and level
char.add_class(classes['fighter'], level=1)
db.add(char)
assert char.levels == {'fighter': 1}
assert char.level == 1
assert char.class_attributes == []
# level up
char.add_class(classes['fighter'], level=2)
db.add(char)
assert char.levels == {'fighter': 2}
assert char.level == 2
assert char.class_attributes == []
# multiclass
char.add_class(classes['rogue'], level=1)
db.add(char)
assert char.level == 3
assert char.levels == {'fighter': 2, 'rogue': 1}
# remove a class
char.remove_class(classes['rogue'])
db.add(char)
assert char.levels == {'fighter': 2}
assert char.level == 2
# remove all remaining classes
char.remove_class(classes['fighter'])
db.add(char)
# ensure we're not persisting any orphan records in the map table
dump = db.dump()
assert dump['class_map'] == []