ravenous_roll20/tests/test_monster_export.py

66 lines
3.1 KiB
Python

import json
import re
import sqlite3
import unittest
from tools.export_monsters import DEFAULT_DB, convert, fixed_successes
class MonsterExportTests(unittest.TestCase):
def setUp(self):
self.assertTrue(DEFAULT_DB.is_file(), "initialize the nested monster database submodule")
self.db = sqlite3.connect(DEFAULT_DB)
self.db.row_factory = sqlite3.Row
def tearDown(self):
self.db.close()
def test_master_creatures_have_importable_json_and_complete_source(self):
ids = [row[0] for row in self.db.execute(
"SELECT id FROM creature WHERE show_to_master = 1 ORDER BY id")]
self.assertEqual(len(ids), 21)
for creature_id in ids:
with self.subTest(creature_id=creature_id):
data = convert(self.db, creature_id)
self.assertEqual(data["format"], "ravenous-roll20-character")
self.assertEqual(data["version"], 1)
self.assertTrue(data["name"])
attrs = {item["name"]: item["current"] for item in data["attributes"]}
self.assertEqual(len(attrs), len(data["attributes"]))
self.assertTrue(any(name.startswith("repeating_locations_") for name in attrs))
source = json.loads(data["gmnotes"])
self.assertEqual(source["database_creature_id"], creature_id)
self.assertEqual(len(source["body_parts"]),
sum(name.endswith("_hp") for name in attrs))
self.assertEqual(len(source["abilities"]),
sum(name.endswith("_notes") and name.startswith("repeating_attacks_")
for name in attrs) +
sum(name.endswith("_description") and name.startswith("repeating_powers_")
for name in attrs))
rendered = "\n".join(value for name, value in attrs.items()
if name.endswith(("_notes", "_description")))
self.assertIsNone(re.search(r"@[A-Za-z_]", rendered))
self.assertNotIn("#ddlvl", rendered)
for ability in source["abilities"]:
if not ability["damage_regular"]:
continue
prefix = f"repeating_attacks_dbability{ability['attack_id']}_"
successes = fixed_successes(ability["success"])
self.assertEqual(attrs[prefix + "auto_successes"], str(successes))
self.assertEqual(attrs[prefix + "roll_dice"], "0")
self.assertEqual(attrs[prefix + "rmax"], str(successes))
def test_fixed_success_notation(self):
self.assertEqual(fixed_successes("4r"), 4)
self.assertEqual(fixed_successes(" 10R "), 10)
self.assertEqual(fixed_successes(None), 0)
self.assertEqual(fixed_successes("variable"), 0)
def test_missing_creature_fails(self):
with self.assertRaisesRegex(ValueError, "No creature"):
convert(self.db, -1)
if __name__ == "__main__":
unittest.main()