Make objects iterable by default, add tests, refactoring

This commit is contained in:
evilchili
2024-04-20 20:35:07 -07:00
parent 44cd8fe9c9
commit 412efe2aec
10 changed files with 104 additions and 75 deletions
+1 -1
View File
@@ -13,7 +13,7 @@
"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": 2, "ancestry_trait_id": 3, "level": 1},
{"ancestry_id": 3, "ancestry_trait_id": 3, "level": 1}
]
}
+18 -15
View File
@@ -1,7 +1,8 @@
import json
from ttfrog.db import schema
def test_create_character(db, classes_factory, ancestries_factory):
def test_manage_character(db, classes_factory, ancestries_factory):
with db.transaction():
# load the fixtures so they are bound to the current session
classes = classes_factory()
@@ -10,7 +11,7 @@ def test_create_character(db, classes_factory, ancestries_factory):
# create a human character (the default)
char = schema.Character(name="Test Character")
db.add(char)
db.add_or_update(char)
assert char.id == 1
assert char.armor_class == 10
assert char.name == "Test Character"
@@ -19,14 +20,14 @@ def test_create_character(db, classes_factory, ancestries_factory):
# switch ancestry to tiefling
char.ancestry = ancestries["tiefling"]
db.add(char)
db.add_or_update(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)
db.add_or_update(char)
assert char.levels == {"fighter": 1}
assert char.level == 1
assert char.class_attributes == {}
@@ -34,37 +35,39 @@ def test_create_character(db, classes_factory, ancestries_factory):
# 'fighting style' is available, but not at this level
fighting_style = char.classes["fighter"].attributes_by_level[2]["Fighting Style"]
assert char.add_class_attribute(fighting_style, fighting_style.options[0]) is False
db.add(char)
db.add_or_update(char)
assert char.class_attributes == {}
# level up
char.add_class(classes["fighter"], level=2)
db.add(char)
db.add_or_update(char)
assert char.levels == {"fighter": 2}
assert char.level == 2
# Assign the fighting style
assert char.add_class_attribute(fighting_style, fighting_style.options[0])
db.add(char)
# Assert the fighting style is added automatically and idempotent...ly?
assert char.class_attributes[fighting_style.name] == fighting_style.options[0]
assert char.add_class_attribute(fighting_style, fighting_style.options[0]) is True
db.add_or_update(char)
# classes
char.add_class(classes["rogue"], level=1)
db.add(char)
db.add_or_update(char)
assert char.level == 3
assert char.levels == {"fighter": 2, "rogue": 1}
# remove a class
char.remove_class(classes["rogue"])
db.add(char)
db.add_or_update(char)
assert char.levels == {"fighter": 2}
assert char.level == 2
# remove all remaining classes
char.remove_class(classes["fighter"])
db.add(char)
# remove remaining class by setting level to zero
char.add_class(classes["fighter"], level=0)
db.add_or_update(char)
assert char.levels == {}
# ensure we're not persisting any orphan records in the map tables
dump = db.dump()
dump = json.loads(db.dump())
assert dump["class_map"] == []
assert dump["class_map"] == []
assert dump["character_class_attribute_map"] == []