tabletop-frog/test/test_inventories.py

86 lines
2.7 KiB
Python
Raw Normal View History

2024-07-28 20:47:42 -07:00
from ttfrog.db.schema.item import Item, Spell, ItemType
2024-07-28 13:55:19 -07:00
def test_equipment_inventory(db, carl):
with db.transaction():
# trigger the creation of inventory mappings
db.add_or_update(carl)
2024-07-28 20:47:42 -07:00
# create some items
2024-07-28 13:55:19 -07:00
ten_foot_pole = Item(name="10ft. Pole", item_type=ItemType.ITEM, consumable=False)
2024-07-28 20:47:42 -07:00
fireball = Spell(name="Fireball", level=3, concentration=False)
db.add_or_update([ten_foot_pole, fireball])
2024-07-28 13:55:19 -07:00
2024-07-28 20:47:42 -07:00
# add the pole to carl's equipment, and the spell to his spell list.
assert carl.equipment.add(ten_foot_pole)
assert carl.spells.add(fireball)
# can't mix and match inventory item types
assert not carl.equipment.add(fireball)
assert not carl.spells.add(ten_foot_pole)
# add two more 10 foot poles. You can never have too many.
carl.equipment.add(ten_foot_pole)
2024-07-28 13:55:19 -07:00
carl.equipment.add(ten_foot_pole)
db.add_or_update(carl)
2024-07-28 20:47:42 -07:00
all_carls_poles = carl.equipment.get(ten_foot_pole)
assert len(all_carls_poles) == 3
# check the "contains" logic
assert ten_foot_pole in carl.equipment
assert ten_foot_pole not in carl.spells
assert fireball in carl.spells
assert fireball not in carl.equipment
# equip one pole
assert carl.equip(all_carls_poles[0])
# can't equip it twice
assert not carl.equip(all_carls_poles[0])
# unequip it
assert carl.unequip(all_carls_poles[0])
# can't unequip the unequipped ones
assert not carl.unequip(all_carls_poles[1])
assert not carl.unequip(all_carls_poles[2])
# drop one pole
assert carl.equipment.remove(all_carls_poles[0])
2024-07-28 13:55:19 -07:00
assert ten_foot_pole in carl.equipment
2024-07-28 20:47:42 -07:00
# drop the remaining poles
assert carl.equipment.remove(all_carls_poles[1])
assert carl.equipment.remove(all_carls_poles[2])
assert ten_foot_pole not in carl.equipment
# can't drop what you don't have
assert not carl.equipment.remove(all_carls_poles[0])
def test_inventory_bundles(db, carl):
with db.transaction():
arrows = Item(name="Arrows", item_type=ItemType.ITEM, consumable=True, count=20)
db.add_or_update([carl, arrows])
quiver = carl.equipment.add(arrows)
db.add_or_update(carl)
# full quiver
assert arrows in carl.equipment
assert quiver.count == 20
# use one
assert quiver.use(1) == 19
assert quiver.count == 19
# cannot use more than you have
assert not quiver.use(20)
# cannot use a negative amount
assert not quiver.use(-1)
# consume all remaining arrows
assert quiver.use(19) == 0
assert arrows not in carl.equipment