Unittest manager get_object_with_account

This commit is contained in:
Griatch 2019-02-07 23:23:51 +01:00
parent 0f7d29783e
commit f3fa6bad07
2 changed files with 28 additions and 9 deletions

View file

@ -69,7 +69,7 @@ class ObjectDBManager(TypedObjectManager):
object candidates. object candidates.
Return: Return:
match (Object or list): One or more matching results. match (query): Matching query.
""" """
ostring = to_unicode(ostring).lstrip('*') ostring = to_unicode(ostring).lstrip('*')
@ -77,7 +77,7 @@ class ObjectDBManager(TypedObjectManager):
dbref = self.dbref(ostring) dbref = self.dbref(ostring)
if dbref: if dbref:
try: try:
return self.get(id=dbref) return self.get(db_account__id=dbref)
except self.model.DoesNotExist: except self.model.DoesNotExist:
pass pass
@ -87,13 +87,13 @@ class ObjectDBManager(TypedObjectManager):
if exact: if exact:
return self.filter(cand_restriction & Q(db_account__username__iexact=ostring)) return self.filter(cand_restriction & Q(db_account__username__iexact=ostring))
else: # fuzzy matching else: # fuzzy matching
ply_cands = self.filter(cand_restriction & Q(accountdb__username__istartswith=ostring) obj_cands = self.select_related().filter(cand_restriction & Q(db_account__username__istartswith=ostring))
).values_list("db_key", flat=True) acct_cands = [obj.account for obj in obj_cands]
if candidates:
index_matches = string_partial_matching(ply_cands, ostring, ret_index=True) if obj_cands:
return [obj for ind, obj in enumerate(make_iter(candidates)) if ind in index_matches] index_matches = string_partial_matching([acct.key for acct in acct_cands], ostring, ret_index=True)
else: acct_cands = [acct_cands[i].id for i in index_matches]
return string_partial_matching(ply_cands, ostring, ret_index=False) return obj_cands.filter(db_account__id__in=acct_cands)
def get_objs_with_key_and_typeclass(self, oname, otypeclass_path, candidates=None): def get_objs_with_key_and_typeclass(self, oname, otypeclass_path, candidates=None):
""" """

View file

@ -1,5 +1,6 @@
from evennia.utils.test_resources import EvenniaTest from evennia.utils.test_resources import EvenniaTest
from evennia import DefaultObject, DefaultCharacter, DefaultRoom, DefaultExit from evennia import DefaultObject, DefaultCharacter, DefaultRoom, DefaultExit
from evennia.objects.models import ObjectDB
class DefaultObjectTest(EvenniaTest): class DefaultObjectTest(EvenniaTest):
@ -45,3 +46,21 @@ class DefaultObjectTest(EvenniaTest):
self.assertTrue(self.room1.get_absolute_url()) self.assertTrue(self.room1.get_absolute_url())
self.assertTrue('admin' in self.room1.web_get_admin_url()) self.assertTrue('admin' in self.room1.web_get_admin_url())
class TestObjectManager(EvenniaTest):
"Test object manager methods"
def test_get_object_with_account(self):
query = ObjectDB.objects.get_object_with_account("TestAccount").first()
self.assertEqual(query, self.char1)
query = ObjectDB.objects.get_object_with_account(self.account.dbref)
self.assertEqual(query, self.char1)
query = ObjectDB.objects.get_object_with_account("TestAccou").first()
self.assertEqual(query, None)
query = ObjectDB.objects.get_object_with_account("TestAccou", exact=False)
self.assertEqual(tuple(query), (self.char1, self.char2))
query = ObjectDB.objects.get_object_with_account(
"TestAccou", candidates=[self.char1, self.obj1], exact=False)
self.assertEqual(list(query), [self.char1])