mirror of
https://github.com/evennia/evennia.git
synced 2026-03-16 21:06:30 +01:00
Merge pull request #3689 from InspectorCaracal/search-fix
Ensure partial-match object searches only match the start of words
This commit is contained in:
commit
d764c3cc97
2 changed files with 25 additions and 1 deletions
|
|
@ -322,7 +322,7 @@ class ObjectDBManager(TypedObjectManager):
|
|||
)
|
||||
|
||||
# convert search term to partial-match regex
|
||||
search_regex = r".* ".join(re.escape(word) for word in ostring.split()) + r'.*'
|
||||
search_regex = r".* ".join(r"\b" + re.escape(word) for word in ostring.split()) + r'.*'
|
||||
|
||||
# do the fuzzy search and return whatever it matches
|
||||
return (
|
||||
|
|
|
|||
|
|
@ -301,6 +301,30 @@ class TestObjectManager(BaseEvenniaTest):
|
|||
)
|
||||
self.assertEqual(list(query), [self.char1, self.char2])
|
||||
|
||||
def test_key_alias_search_partial_match(self):
|
||||
"""
|
||||
verify that get_objs_with_key_or_alias will partial match the first part of
|
||||
any words in the name, when given in the correct order
|
||||
"""
|
||||
self.obj1.key = "big sword"
|
||||
self.obj2.key = "shiny sword"
|
||||
|
||||
# beginning of "sword", should match both
|
||||
query = ObjectDB.objects.get_objs_with_key_or_alias("sw", exact=False)
|
||||
self.assertEqual(list(query), [self.obj1, self.obj2])
|
||||
|
||||
# middle of "sword", should NOT match
|
||||
query = ObjectDB.objects.get_objs_with_key_or_alias("wor", exact=False)
|
||||
self.assertEqual(list(query), [])
|
||||
|
||||
# beginning of "big" then "sword", should match obj1
|
||||
query = ObjectDB.objects.get_objs_with_key_or_alias("b sw", exact=False)
|
||||
self.assertEqual(list(query), [self.obj1])
|
||||
|
||||
# beginning of "sword" then "big", should NOT match
|
||||
query = ObjectDB.objects.get_objs_with_key_or_alias("sw b", exact=False)
|
||||
self.assertEqual(list(query), [])
|
||||
|
||||
def test_search_object(self):
|
||||
self.char1.tags.add("test tag")
|
||||
self.obj1.tags.add("test tag")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue