Unittest object.get_obj_with_attr
This commit is contained in:
parent
f3fa6bad07
commit
181a3e04e7
2 changed files with 38 additions and 9 deletions
|
|
@ -105,7 +105,7 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
candidates (list, optional): Only match among the given list of candidates.
|
candidates (list, optional): Only match among the given list of candidates.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
matches (list): The matching objects.
|
matches (query): The matching objects.
|
||||||
"""
|
"""
|
||||||
cand_restriction = candidates is not None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates)
|
cand_restriction = candidates is not None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates)
|
||||||
if obj]) or Q()
|
if obj]) or Q()
|
||||||
|
|
@ -119,18 +119,19 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
attribute_name (str): Attribute name to search for.
|
attribute_name (str): Attribute name to search for.
|
||||||
candidates (list, optional): Only match among the given list of candidates.
|
candidates (list, optional): Only match among the given list of object
|
||||||
|
candidates.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
matches (list): All objects having the given attribute_name defined at all.
|
matches (query): All objects having the given attribute_name defined at all.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
cand_restriction = candidates is not None and Q(db_attributes__db_obj__pk__in=[_GA(obj, "id") for obj
|
cand_restriction = \
|
||||||
in make_iter(candidates)
|
candidates is not None and Q(id__in=[obj.id for obj in candidates]) or Q()
|
||||||
if obj]) or Q()
|
return self.filter(cand_restriction & Q(db_attributes__db_key=attribute_name))
|
||||||
return list(self.filter(cand_restriction & Q(db_attributes__db_key=attribute_name)))
|
|
||||||
|
|
||||||
def get_objs_with_attr_value(self, attribute_name, attribute_value, candidates=None, typeclasses=None):
|
def get_objs_with_attr_value(self, attribute_name, attribute_value,
|
||||||
|
candidates=None, typeclasses=None):
|
||||||
"""
|
"""
|
||||||
Get all objects having the given attrname set to the given value.
|
Get all objects having the given attrname set to the given value.
|
||||||
|
|
||||||
|
|
@ -141,7 +142,8 @@ class ObjectDBManager(TypedObjectManager):
|
||||||
typeclasses (list, optional): Python pats to restrict matches with.
|
typeclasses (list, optional): Python pats to restrict matches with.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
matches (list): Objects fullfilling both the `attribute_name` and `attribute_value` criterions.
|
matches (list): Objects fullfilling both the `attribute_name` and
|
||||||
|
`attribute_value` criterions.
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
This uses the Attribute's PickledField to transparently search the database by matching
|
This uses the Attribute's PickledField to transparently search the database by matching
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,8 @@ class TestObjectManager(EvenniaTest):
|
||||||
self.assertEqual(query, self.char1)
|
self.assertEqual(query, self.char1)
|
||||||
query = ObjectDB.objects.get_object_with_account(self.account.dbref)
|
query = ObjectDB.objects.get_object_with_account(self.account.dbref)
|
||||||
self.assertEqual(query, self.char1)
|
self.assertEqual(query, self.char1)
|
||||||
|
query = ObjectDB.objects.get_object_with_account("#123456")
|
||||||
|
self.assertEqual(query, None)
|
||||||
query = ObjectDB.objects.get_object_with_account("TestAccou").first()
|
query = ObjectDB.objects.get_object_with_account("TestAccou").first()
|
||||||
self.assertEqual(query, None)
|
self.assertEqual(query, None)
|
||||||
|
|
||||||
|
|
@ -64,3 +66,28 @@ class TestObjectManager(EvenniaTest):
|
||||||
query = ObjectDB.objects.get_object_with_account(
|
query = ObjectDB.objects.get_object_with_account(
|
||||||
"TestAccou", candidates=[self.char1, self.obj1], exact=False)
|
"TestAccou", candidates=[self.char1, self.obj1], exact=False)
|
||||||
self.assertEqual(list(query), [self.char1])
|
self.assertEqual(list(query), [self.char1])
|
||||||
|
|
||||||
|
def test_get_objs_with_key_and_typeclass(self):
|
||||||
|
query = ObjectDB.objects.get_objs_with_key_and_typeclass(
|
||||||
|
"Char", "evennia.objects.objects.DefaultCharacter")
|
||||||
|
self.assertEqual(list(query), [self.char1])
|
||||||
|
query = ObjectDB.objects.get_objs_with_key_and_typeclass(
|
||||||
|
"Char", "evennia.objects.objects.DefaultObject")
|
||||||
|
self.assertFalse(query)
|
||||||
|
query = ObjectDB.objects.get_objs_with_key_and_typeclass(
|
||||||
|
"NotFound", "evennia.objects.objects.DefaultCharacter")
|
||||||
|
self.assertFalse(query)
|
||||||
|
query = ObjectDB.objects.get_objs_with_key_and_typeclass(
|
||||||
|
"Char", "evennia.objects.objects.DefaultCharacter", candidates=[self.char1, self.char2])
|
||||||
|
self.assertEqual(list(query), [self.char1])
|
||||||
|
|
||||||
|
def test_get_objs_with_attr(self):
|
||||||
|
self.obj1.db.testattr = "testval1"
|
||||||
|
query = ObjectDB.objects.get_objs_with_attr("testattr")
|
||||||
|
self.assertEqual(list(query), [self.obj1])
|
||||||
|
query = ObjectDB.objects.get_objs_with_attr(
|
||||||
|
"testattr", candidates=[self.char1, self.obj1] )
|
||||||
|
self.assertEqual(list(query), [self.obj1])
|
||||||
|
query = ObjectDB.objects.get_objs_with_attr(
|
||||||
|
"NotFound", candidates=[self.char1, self.obj1] )
|
||||||
|
self.assertFalse(query)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue