Auto-tag spawned objects. Clean up unit tests

This commit is contained in:
Griatch 2018-05-10 20:37:21 +02:00
parent d71b281b56
commit b12b466fe2
4 changed files with 251 additions and 86 deletions

View file

@ -7,16 +7,29 @@ from evennia.utils.test_resources import EvenniaTest
from evennia.utils import spawner
class TestSpawner(EvenniaTest):
def setUp(self):
super(TestSpawner, self).setUp()
self.prot1 = {"prototype_key": "testprototype"}
def test_spawn(self):
obj1 = spawner.spawn(self.prot1)
# check spawned objects have the right tag
self.assertEqual(list(spawner.search_objects_with_prototype("testprototype")), obj1)
class TestPrototypeStorage(EvenniaTest):
def setUp(self):
super(TestPrototypeStorage, self).setUp()
self.prot1 = {"key": "testprototype"}
self.prot2 = {"key": "testprototype2"}
self.prot3 = {"key": "testprototype3"}
self.prot1 = {"prototype_key": "testprototype"}
self.prot2 = {"prototype_key": "testprototype2"}
self.prot3 = {"prototype_key": "testprototype3"}
def _get_metaproto(
self, key='testprototype', desc='testprototype', locks=['edit:id(6) or perm(Admin)', 'use:all()'],
self, key='testprototype', desc='testprototype',
locks=['edit:id(6) or perm(Admin)', 'use:all()'],
tags=[], prototype={"key": "testprototype"}):
return spawner.build_metaproto(key, desc, locks, tags, prototype)
@ -28,34 +41,39 @@ class TestPrototypeStorage(EvenniaTest):
def test_prototype_storage(self):
prot = spawner.save_db_prototype(self.char1, "testprot", self.prot1, desc='testdesc0', tags=["foo"])
prot = spawner.save_db_prototype(self.char1, self.prot1, "testprot",
desc='testdesc0', tags=["foo"])
self.assertTrue(bool(prot))
self.assertEqual(prot.db.prototype, self.prot1)
self.assertEqual(prot.desc, "testdesc0")
prot = spawner.save_db_prototype(self.char1, "testprot", self.prot1, desc='testdesc', tags=["fooB"])
prot = spawner.save_db_prototype(self.char1, self.prot1, "testprot",
desc='testdesc', tags=["fooB"])
self.assertEqual(prot.db.prototype, self.prot1)
self.assertEqual(prot.desc, "testdesc")
self.assertTrue(bool(prot.tags.get("fooB", "db_prototype")))
self.assertEqual(list(prot.__class__.objects.get_by_tag("foo", "db_prototype")), [prot])
prot2 = spawner.save_db_prototype(self.char1, "testprot2", self.prot2, desc='testdesc2b', tags=["foo"])
self.assertEqual(list(prot.__class__.objects.get_by_tag("foo", "db_prototype")), [prot, prot2])
prot2 = spawner.save_db_prototype(self.char1, self.prot2, "testprot2",
desc='testdesc2b', tags=["foo"])
self.assertEqual(
list(prot.__class__.objects.get_by_tag("foo", "db_prototype")), [prot, prot2])
prot3 = spawner.save_db_prototype(self.char1, "testprot2", self.prot3, desc='testdesc2')
prot3 = spawner.save_db_prototype(self.char1, self.prot3, "testprot2", desc='testdesc2')
self.assertEqual(prot2.id, prot3.id)
self.assertEqual(list(prot.__class__.objects.get_by_tag("foo", "db_prototype")), [prot, prot2])
self.assertEqual(
list(prot.__class__.objects.get_by_tag("foo", "db_prototype")), [prot, prot2])
# returns DBPrototype
self.assertEqual(list(spawner.search_db_prototype("testprot")), [prot])
self.assertEqual(list(spawner.search_db_prototype("testprot", return_queryset=True)), [prot])
# returns metaprotos
prot = self._to_metaproto(prot)
prot3 = self._to_metaproto(prot3)
prot = prot.db.prototype
prot3 = prot3.db.prototype
self.assertEqual(list(spawner.search_prototype("testprot")), [prot])
self.assertEqual(list(spawner.search_prototype("testprot", return_meta=False)), [self.prot1])
self.assertEqual(
list(spawner.search_prototype("testprot")), [self.prot1])
# partial match
self.assertEqual(list(spawner.search_prototype("prot")), [prot, prot3])
self.assertEqual(list(spawner.search_prototype(tags="foo")), [prot, prot3])