fix schemas
This commit is contained in:
+34
-11
@@ -31,20 +31,43 @@ def db(monkeypatch):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def classes_factory(db):
|
||||
load_fixture(db, "classes")
|
||||
def bootstrap(db):
|
||||
with db.transaction():
|
||||
# ancestries
|
||||
human = schema.Ancestry("human")
|
||||
|
||||
def factory():
|
||||
return dict((rec.name, rec) for rec in db.session.query(schema.CharacterClass).all())
|
||||
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))
|
||||
|
||||
return factory
|
||||
# ancestry traits
|
||||
darkvision = schema.AncestryTrait("Darkvision")
|
||||
darkvision.add_modifier(schema.Modifier("Darkvision", target="vision_in_darkness", absolute_value=120))
|
||||
tiefling.add_trait(darkvision)
|
||||
|
||||
dragonborn = schema.Ancestry("dragonborn")
|
||||
dragonborn.add_trait(darkvision)
|
||||
|
||||
@pytest.fixture
|
||||
def ancestries_factory(db):
|
||||
load_fixture(db, "ancestry")
|
||||
db.add_or_update([human, dragonborn, tiefling])
|
||||
|
||||
def factory():
|
||||
return dict((rec.name, rec) for rec in db.session.query(schema.Ancestry).all())
|
||||
# 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)
|
||||
|
||||
return factory
|
||||
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)
|
||||
|
||||
bar = schema.Character("Bar", ancestry=human)
|
||||
|
||||
# persist all the records we've created
|
||||
db.add_or_update([foo, bar])
|
||||
|
||||
+37
-34
@@ -3,62 +3,64 @@ import json
|
||||
from ttfrog.db import schema
|
||||
|
||||
|
||||
def test_manage_character(db, classes_factory, ancestries_factory):
|
||||
def test_manage_character(db, bootstrap):
|
||||
with db.transaction():
|
||||
# load the fixtures so they are bound to the current session
|
||||
classes = classes_factory()
|
||||
ancestries = ancestries_factory()
|
||||
darkvision = db.AncestryTrait.filter_by(name="Darkvision")[0]
|
||||
darkvision = db.AncestryTrait.filter_by(name="Darkvision").one()
|
||||
human = db.Ancestry.filter_by(name="human").one()
|
||||
|
||||
# create a human character (the default)
|
||||
char = schema.Character(name="Test Character")
|
||||
char = schema.Character(name="Test Character", ancestry=human)
|
||||
db.add_or_update(char)
|
||||
assert char.id == 1
|
||||
assert char.id == 3
|
||||
assert char.name == "Test Character"
|
||||
assert char.ancestry.name == "human"
|
||||
assert char.AC == 10
|
||||
assert char.HP == 10
|
||||
assert char.STR == 10
|
||||
assert char.DEX == 10
|
||||
assert char.CON == 10
|
||||
assert char.INT == 10
|
||||
assert char.WIS == 10
|
||||
assert char.CHA == 10
|
||||
assert char.armor_class == 10
|
||||
assert char.hit_points == 10
|
||||
assert char.strength == 10
|
||||
assert char.dexterity == 10
|
||||
assert char.constitution == 10
|
||||
assert char.intelligence == 10
|
||||
assert char.wisdom == 10
|
||||
assert char.charisma == 10
|
||||
assert darkvision not in char.traits
|
||||
|
||||
# switch ancestry to tiefling
|
||||
char.ancestry = ancestries["tiefling"]
|
||||
tiefling = db.Ancestry.filter_by(name="tiefling").one()
|
||||
char.ancestry = tiefling
|
||||
db.add_or_update(char)
|
||||
char = db.session.get(schema.Character, 1)
|
||||
char = db.session.get(schema.Character, char.id)
|
||||
assert char.ancestry_id == tiefling.id
|
||||
assert char.ancestry.name == "tiefling"
|
||||
assert darkvision in char.traits
|
||||
|
||||
# switch ancestry to dragonborn and assert darkvision persists
|
||||
char.ancestry = ancestries["dragonborn"]
|
||||
char.ancestry = db.Ancestry.filter_by(name="dragonborn").one()
|
||||
db.add_or_update(char)
|
||||
assert darkvision in char.traits
|
||||
|
||||
# switch ancestry to human and assert darkvision is removed
|
||||
char.ancestry = ancestries["human"]
|
||||
char.ancestry = human
|
||||
db.add_or_update(char)
|
||||
assert darkvision not in char.traits
|
||||
|
||||
fighter = db.CharacterClass.filter_by(name="fighter").one()
|
||||
rogue = db.CharacterClass.filter_by(name="rogue").one()
|
||||
|
||||
# assign a class and level
|
||||
char.add_class(classes["fighter"], level=1)
|
||||
char.add_class(fighter, level=1)
|
||||
db.add_or_update(char)
|
||||
assert char.levels == {"fighter": 1}
|
||||
assert char.level == 1
|
||||
assert char.class_attributes == {}
|
||||
|
||||
# 'fighting style' is available, but not at this level
|
||||
fighter = classes["fighter"]
|
||||
fighting_style = fighter.attributes_by_level[2]["Fighting Style"]
|
||||
assert char.add_class_attribute(fighting_style, fighting_style.options[0]) is False
|
||||
db.add_or_update(char)
|
||||
assert char.class_attributes == {}
|
||||
|
||||
# level up
|
||||
char.add_class(classes["fighter"], level=2)
|
||||
char.add_class(fighter, level=2)
|
||||
db.add_or_update(char)
|
||||
assert char.levels == {"fighter": 2}
|
||||
assert char.level == 2
|
||||
@@ -69,26 +71,27 @@ def test_manage_character(db, classes_factory, ancestries_factory):
|
||||
db.add_or_update(char)
|
||||
|
||||
# classes
|
||||
char.add_class(classes["rogue"], level=1)
|
||||
char.add_class(rogue, level=1)
|
||||
db.add_or_update(char)
|
||||
assert char.level == 3
|
||||
assert char.levels == {"fighter": 2, "rogue": 1}
|
||||
|
||||
# remove a class
|
||||
char.remove_class(classes["rogue"])
|
||||
char.remove_class(rogue)
|
||||
db.add_or_update(char)
|
||||
assert char.levels == {"fighter": 2}
|
||||
assert char.level == 2
|
||||
|
||||
# remove remaining class by setting level to zero
|
||||
char.add_class(classes["fighter"], level=0)
|
||||
char.add_class(fighter, level=0)
|
||||
db.add_or_update(char)
|
||||
assert char.levels == {}
|
||||
assert char.class_attributes == {}
|
||||
|
||||
# ensure we're not persisting any orphan records in the map tables
|
||||
dump = json.loads(db.dump())
|
||||
assert dump["class_map"] == []
|
||||
assert dump["character_class_attribute_map"] == []
|
||||
assert not [m for m in dump["character_class_attribute_map"] if m["character_id"] == char.id]
|
||||
assert not [m for m in dump["class_map"] if m["character_id"] == char.id]
|
||||
|
||||
|
||||
def test_ancestries(db):
|
||||
@@ -129,19 +132,19 @@ def test_ancestries(db):
|
||||
assert endurance in grognak.traits
|
||||
|
||||
# verify the strength bonus is applied
|
||||
assert grognak.strength == 10
|
||||
assert grognak.strength.base == 10
|
||||
assert str_plus_one in grognak.modifiers["strength"]
|
||||
assert grognak.STR == 11
|
||||
assert grognak.strength == 11
|
||||
|
||||
|
||||
def test_modifiers(db, classes_factory, ancestries_factory):
|
||||
def test_modifiers(db, bootstrap):
|
||||
with db.transaction():
|
||||
classes_factory()
|
||||
ancestries = ancestries_factory()
|
||||
human = db.Ancestry.filter_by(name="human").one()
|
||||
tiefling = db.Ancestry.filter_by(name="tiefling").one()
|
||||
|
||||
# no modifiers; speed is ancestry speed
|
||||
carl = schema.Character(name="Carl", ancestry=ancestries["elf"])
|
||||
marx = schema.Character(name="Marx", ancestry=ancestries["human"])
|
||||
carl = schema.Character(name="Carl", ancestry=tiefling)
|
||||
marx = schema.Character(name="Marx", ancestry=human)
|
||||
db.add_or_update([carl, marx])
|
||||
assert carl.speed == carl.ancestry.speed == 30
|
||||
|
||||
|
||||
Reference in New Issue
Block a user