Fix unittests, resolve bugs
This commit is contained in:
parent
e52503b710
commit
116f6e7505
4 changed files with 87 additions and 58 deletions
|
|
@ -660,7 +660,7 @@ def validate_lockstring(lockstring):
|
||||||
if no error was found.
|
if no error was found.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return _LOCK_HANDLER.valdate(lockstring)
|
return _LOCK_HANDLER.validate(lockstring)
|
||||||
|
|
||||||
|
|
||||||
def _test():
|
def _test():
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,8 @@ from evennia.prototypes.protfuncs import protfunc_parser
|
||||||
_MODULE_PROTOTYPE_MODULES = {}
|
_MODULE_PROTOTYPE_MODULES = {}
|
||||||
_MODULE_PROTOTYPES = {}
|
_MODULE_PROTOTYPES = {}
|
||||||
_PROTOTYPE_META_NAMES = ("prototype_key", "prototype_desc", "prototype_tags", "prototype_locks")
|
_PROTOTYPE_META_NAMES = ("prototype_key", "prototype_desc", "prototype_tags", "prototype_locks")
|
||||||
_PROTOTYPE_TAG_CATEGORY = "spawned_by_prototype"
|
_PROTOTYPE_TAG_CATEGORY = "from_prototype"
|
||||||
|
_PROTOTYPE_TAG_META_CATEGORY = "db_prototype"
|
||||||
|
|
||||||
|
|
||||||
class PermissionError(RuntimeError):
|
class PermissionError(RuntimeError):
|
||||||
|
|
@ -167,7 +168,7 @@ class DbPrototype(DefaultScript):
|
||||||
|
|
||||||
def create_prototype(**kwargs):
|
def create_prototype(**kwargs):
|
||||||
"""
|
"""
|
||||||
Store a prototype persistently.
|
Create/Store a prototype persistently.
|
||||||
|
|
||||||
Kwargs:
|
Kwargs:
|
||||||
prototype_key (str): This is required for any storage.
|
prototype_key (str): This is required for any storage.
|
||||||
|
|
@ -204,27 +205,32 @@ def create_prototype(**kwargs):
|
||||||
raise PermissionError("{} is a read-only prototype "
|
raise PermissionError("{} is a read-only prototype "
|
||||||
"(defined as code in {}).".format(prototype_key, mod))
|
"(defined as code in {}).".format(prototype_key, mod))
|
||||||
|
|
||||||
# want to create- or edit
|
|
||||||
prototype = kwargs
|
|
||||||
|
|
||||||
# make sure meta properties are included with defaults
|
# make sure meta properties are included with defaults
|
||||||
prototype['prototype_desc'] = prototype.get('prototype_desc', '')
|
stored_prototype = DbPrototype.objects.filter(db_key=prototype_key)
|
||||||
locks = prototype.get('prototype_locks', "spawn:all();edit:perm(Admin)")
|
prototype = dict(stored_prototype[0].db.prototype) if stored_prototype else {}
|
||||||
is_valid, err = validate_lockstring(locks)
|
|
||||||
|
kwargs['prototype_desc'] = kwargs.get("prototype_desc", prototype.get("prototype_desc", ""))
|
||||||
|
prototype_locks = kwargs.get(
|
||||||
|
"prototype_locks", prototype.get('prototype_locks', "spawn:all();edit:perm(Admin)"))
|
||||||
|
is_valid, err = validate_lockstring(prototype_locks)
|
||||||
if not is_valid:
|
if not is_valid:
|
||||||
raise ValidationError("Lock error: {}".format(err))
|
raise ValidationError("Lock error: {}".format(err))
|
||||||
prototype["prototype_locks"] = locks
|
kwargs['prototype_locks'] = prototype_locks
|
||||||
prototype["prototype_tags"] = [
|
|
||||||
_to_batchtuple(tag, "db_prototype")
|
|
||||||
for tag in make_iter(prototype.get("prototype_tags", []))]
|
|
||||||
|
|
||||||
stored_prototype = DbPrototype.objects.filter(db_key=prototype_key)
|
prototype_tags = [
|
||||||
|
_to_batchtuple(tag, _PROTOTYPE_TAG_META_CATEGORY)
|
||||||
|
for tag in make_iter(kwargs.get("prototype_tags",
|
||||||
|
prototype.get('prototype_tags', [])))]
|
||||||
|
kwargs["prototype_tags"] = prototype_tags
|
||||||
|
|
||||||
|
prototype.update(kwargs)
|
||||||
|
|
||||||
if stored_prototype:
|
if stored_prototype:
|
||||||
# edit existing prototype
|
# edit existing prototype
|
||||||
stored_prototype = stored_prototype[0]
|
stored_prototype = stored_prototype[0]
|
||||||
|
|
||||||
stored_prototype.desc = prototype['prototype_desc']
|
stored_prototype.desc = prototype['prototype_desc']
|
||||||
|
if prototype_tags:
|
||||||
|
stored_prototype.tags.clear(category=_PROTOTYPE_TAG_CATEGORY)
|
||||||
stored_prototype.tags.batch_add(*prototype['prototype_tags'])
|
stored_prototype.tags.batch_add(*prototype['prototype_tags'])
|
||||||
stored_prototype.locks.add(prototype['prototype_locks'])
|
stored_prototype.locks.add(prototype['prototype_locks'])
|
||||||
stored_prototype.attributes.add('prototype', prototype)
|
stored_prototype.attributes.add('prototype', prototype)
|
||||||
|
|
@ -232,8 +238,12 @@ def create_prototype(**kwargs):
|
||||||
# create a new prototype
|
# create a new prototype
|
||||||
stored_prototype = create_script(
|
stored_prototype = create_script(
|
||||||
DbPrototype, key=prototype_key, desc=prototype['prototype_desc'], persistent=True,
|
DbPrototype, key=prototype_key, desc=prototype['prototype_desc'], persistent=True,
|
||||||
locks=locks, tags=prototype['prototype_tags'], attributes=[("prototype", prototype)])
|
locks=prototype_locks, tags=prototype['prototype_tags'],
|
||||||
return stored_prototype
|
attributes=[("prototype", prototype)])
|
||||||
|
return stored_prototype.db.prototype
|
||||||
|
|
||||||
|
# alias
|
||||||
|
save_prototype = create_prototype
|
||||||
|
|
||||||
|
|
||||||
def delete_prototype(key, caller=None):
|
def delete_prototype(key, caller=None):
|
||||||
|
|
|
||||||
|
|
@ -186,6 +186,7 @@ def prototype_from_object(obj):
|
||||||
obj.key, hashlib.md5(str(time.time())).hexdigest()[:7])
|
obj.key, hashlib.md5(str(time.time())).hexdigest()[:7])
|
||||||
prot['prototype_desc'] = "Built from {}".format(str(obj))
|
prot['prototype_desc'] = "Built from {}".format(str(obj))
|
||||||
prot['prototype_locks'] = "spawn:all();edit:all()"
|
prot['prototype_locks'] = "spawn:all();edit:all()"
|
||||||
|
prot['prototype_tags'] = []
|
||||||
|
|
||||||
prot['key'] = obj.db_key or hashlib.md5(str(time.time())).hexdigest()[:6]
|
prot['key'] = obj.db_key or hashlib.md5(str(time.time())).hexdigest()[:6]
|
||||||
prot['typeclass'] = obj.db_typeclass_path
|
prot['typeclass'] = obj.db_typeclass_path
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ from anything import Anything, Something
|
||||||
from evennia.utils.test_resources import EvenniaTest
|
from evennia.utils.test_resources import EvenniaTest
|
||||||
from evennia.prototypes import spawner, prototypes as protlib
|
from evennia.prototypes import spawner, prototypes as protlib
|
||||||
|
|
||||||
|
from evennia.prototypes.prototypes import _PROTOTYPE_TAG_META_CATEGORY
|
||||||
|
|
||||||
_PROTPARENTS = {
|
_PROTPARENTS = {
|
||||||
"NOBODY": {},
|
"NOBODY": {},
|
||||||
|
|
@ -151,63 +152,80 @@ class TestUtils(EvenniaTest):
|
||||||
new_prot)
|
new_prot)
|
||||||
|
|
||||||
|
|
||||||
|
class TestProtLib(EvenniaTest):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestProtLib, self).setUp()
|
||||||
|
self.obj1.attributes.add("testattr", "testval")
|
||||||
|
self.prot = spawner.prototype_from_object(self.obj1)
|
||||||
|
|
||||||
|
def test_prototype_to_str(self):
|
||||||
|
prstr = protlib.prototype_to_str(self.prot)
|
||||||
|
self.assertTrue(prstr.startswith("|cprototype key:|n"))
|
||||||
|
|
||||||
|
def test_check_permission(self):
|
||||||
|
pass
|
||||||
|
|
||||||
class TestPrototypeStorage(EvenniaTest):
|
class TestPrototypeStorage(EvenniaTest):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestPrototypeStorage, self).setUp()
|
super(TestPrototypeStorage, self).setUp()
|
||||||
self.prot1 = {"prototype_key": "testprototype"}
|
self.maxDiff = None
|
||||||
self.prot2 = {"prototype_key": "testprototype2"}
|
|
||||||
self.prot3 = {"prototype_key": "testprototype3"}
|
|
||||||
|
|
||||||
def _get_metaproto(
|
self.prot1 = spawner.prototype_from_object(self.obj1)
|
||||||
self, key='testprototype', desc='testprototype',
|
self.prot1['prototype_key'] = 'testprototype1'
|
||||||
locks=['edit:id(6) or perm(Admin)', 'use:all()'],
|
self.prot1['prototype_desc'] = 'testdesc1'
|
||||||
tags=[], prototype={"key": "testprototype"}):
|
self.prot1['prototype_tags'] = [('foo1', _PROTOTYPE_TAG_META_CATEGORY)]
|
||||||
return spawner.build_metaproto(key, desc, locks, tags, prototype)
|
|
||||||
|
|
||||||
def _to_metaproto(self, db_prototype):
|
self.prot2 = self.prot1.copy()
|
||||||
return spawner.build_metaproto(
|
self.prot2['prototype_key'] = 'testprototype2'
|
||||||
db_prototype.key, db_prototype.desc, db_prototype.locks.all(),
|
self.prot2['prototype_desc'] = 'testdesc2'
|
||||||
db_prototype.tags.get(category="db_prototype", return_list=True),
|
self.prot2['prototype_tags'] = [('foo1', _PROTOTYPE_TAG_META_CATEGORY)]
|
||||||
db_prototype.attributes.get("prototype"))
|
|
||||||
|
self.prot3 = self.prot2.copy()
|
||||||
|
self.prot3['prototype_key'] = 'testprototype3'
|
||||||
|
self.prot3['prototype_desc'] = 'testdesc3'
|
||||||
|
self.prot3['prototype_tags'] = [('foo1', _PROTOTYPE_TAG_META_CATEGORY)]
|
||||||
|
|
||||||
def test_prototype_storage(self):
|
def test_prototype_storage(self):
|
||||||
|
|
||||||
prot = spawner.save_db_prototype(self.char1, self.prot1, "testprot",
|
prot1 = protlib.create_prototype(**self.prot1)
|
||||||
desc='testdesc0', tags=["foo"])
|
|
||||||
|
|
||||||
self.assertTrue(bool(prot))
|
self.assertTrue(bool(prot1))
|
||||||
self.assertEqual(prot.db.prototype, self.prot1)
|
self.assertEqual(prot1, self.prot1)
|
||||||
self.assertEqual(prot.desc, "testdesc0")
|
|
||||||
|
|
||||||
prot = spawner.save_db_prototype(self.char1, self.prot1, "testprot",
|
self.assertEqual(prot1['prototype_desc'], "testdesc1")
|
||||||
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])
|
self.assertEqual(prot1['prototype_tags'], [("foo1", _PROTOTYPE_TAG_META_CATEGORY)])
|
||||||
|
|
||||||
prot2 = spawner.save_db_prototype(self.char1, self.prot2, "testprot2",
|
|
||||||
desc='testdesc2b', tags=["foo"])
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
list(prot.__class__.objects.get_by_tag("foo", "db_prototype")), [prot, prot2])
|
protlib.DbPrototype.objects.get_by_tag(
|
||||||
|
"foo1", _PROTOTYPE_TAG_META_CATEGORY)[0].db.prototype, prot1)
|
||||||
|
|
||||||
prot3 = spawner.save_db_prototype(self.char1, self.prot3, "testprot2", desc='testdesc2')
|
prot2 = protlib.create_prototype(**self.prot2)
|
||||||
self.assertEqual(prot2.id, prot3.id)
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
list(prot.__class__.objects.get_by_tag("foo", "db_prototype")), [prot, prot2])
|
[pobj.db.prototype
|
||||||
|
for pobj in protlib.DbPrototype.objects.get_by_tag(
|
||||||
|
"foo1", _PROTOTYPE_TAG_META_CATEGORY)],
|
||||||
|
[prot1, prot2])
|
||||||
|
|
||||||
# returns DBPrototype
|
# add to existing prototype
|
||||||
self.assertEqual(list(spawner.search_db_prototype("testprot", return_queryset=True)), [prot])
|
prot1b = protlib.create_prototype(
|
||||||
|
prototype_key='testprototype1', foo='bar', prototype_tags=['foo2'])
|
||||||
|
|
||||||
prot = prot.db.prototype
|
|
||||||
prot3 = prot3.db.prototype
|
|
||||||
self.assertEqual(list(spawner.search_prototype("testprot")), [prot])
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
list(spawner.search_prototype("testprot")), [self.prot1])
|
[pobj.db.prototype
|
||||||
|
for pobj in protlib.DbPrototype.objects.get_by_tag(
|
||||||
|
"foo2", _PROTOTYPE_TAG_META_CATEGORY)],
|
||||||
|
[prot1b])
|
||||||
|
|
||||||
|
self.assertEqual(list(protlib.search_prototype("testprototype2")), [prot2])
|
||||||
|
self.assertNotEqual(list(protlib.search_prototype("testprototype1")), [prot1])
|
||||||
|
self.assertEqual(list(protlib.search_prototype("testprototype1")), [prot1b])
|
||||||
|
|
||||||
|
prot3 = protlib.create_prototype(**self.prot3)
|
||||||
|
|
||||||
# partial match
|
# partial match
|
||||||
self.assertEqual(list(spawner.search_prototype("prot")), [prot, prot3])
|
self.assertEqual(list(protlib.search_prototype("prot")), [prot1b, prot2, prot3])
|
||||||
self.assertEqual(list(spawner.search_prototype(tags="foo")), [prot, prot3])
|
self.assertEqual(list(protlib.search_prototype(tags="foo1")), [prot1b, prot2, prot3])
|
||||||
|
|
||||||
self.assertTrue(str(unicode(spawner.list_prototypes(self.char1))))
|
self.assertTrue(str(unicode(protlib.list_prototypes(self.char1))))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue