Updates with Griatch's recs, addition of unit test

This commit is contained in:
Wendy Wang 2022-10-21 01:14:41 +02:00
parent 473fba5c13
commit adf2804328
2 changed files with 25 additions and 13 deletions

View file

@ -3134,24 +3134,19 @@ class DefaultExit(DefaultObject):
""" """
traversing_object.msg(_("You cannot go there.")) traversing_object.msg(_("You cannot go there."))
def matching_exit(self, as_list=False): def get_return_exit(self, return_all=False):
""" """
Get the exits that pair with this one in its destination room Get the exits that pair with this one in its destination room
(i.e. returns to its location) (i.e. returns to its location)
Args: Args:
as_list (bool): Whether to return available results as a return_all (bool): Whether to return available results as a
list or single matching exit. list or single matching exit.
Returns: Returns:
list, exit (Exit), or None: The matching exit(s). queryset or exit (Exit): The matching exit(s).
""" """
no_match = [] if as_list else None query = ObjectDB.objects.filter(db_location=self.destination, db_destination=self.location)
if not self.destination: if return_all:
return no_match return query
matching_exits = [x for x in self.destination.exits if x.destination == self.location] return query.first()
if not matching_exits:
return no_match
if as_list:
return matching_exits
return matching_exits[0]

View file

@ -69,6 +69,23 @@ class DefaultObjectTest(BaseEvenniaTest):
self.assertEqual(description, obj.db.desc) self.assertEqual(description, obj.db.desc)
self.assertEqual(obj.db.creator_ip, self.ip) self.assertEqual(obj.db.creator_ip, self.ip)
def test_exit_get_return_exit(self):
ex1, _ = DefaultExit.create("north", self.room1, self.room2, account=self.account)
single_return_exit = ex1.get_return_exit()
all_return_exit = ex1.get_return_exit(return_all=True)
self.assertEqual(single_return_exit, None)
self.assertEqual(len(all_return_exit), 0)
ex2, _ = DefaultExit.create("south", self.room2, self.room1, account=self.account)
single_return_exit = ex1.get_return_exit()
all_return_exit = ex1.get_return_exit(return_all=True)
self.assertEqual(single_return_exit, ex2)
self.assertEqual(len(all_return_exit), 1)
ex3, _ = DefaultExit.create("also_south", self.room2, self.room1, account=self.account)
all_return_exit = ex1.get_return_exit(return_all=True)
self.assertEqual(len(all_return_exit), 2)
def test_urls(self): def test_urls(self):
"Make sure objects are returning URLs" "Make sure objects are returning URLs"
self.assertTrue(self.char1.get_absolute_url()) self.assertTrue(self.char1.get_absolute_url())