seitime-frappe/frappe/search/test_full_text_search.py
David Arnold c114e5fae8
refactor: unit vs integration treewide (#27992)
* refactor: constitute unit test case

* fix: docs and type hints

* refactor: mark presumed integration test cases explicitly

At time of writing, we now have at least two base test classes:

- frappe.tests.UnitTestCase
- frappe.tests.IntegrationTestCase

They load in their perspective priority queue during execution.

Probably more to come for more efficient queing and scheduling.

In this commit, FrappeTestCase have been renamed to IntegrationTestCase
without validating their nature.

* feat: Move test-related functions from test_runner.py to tests/utils.py

* refactor: add bare UnitTestCase to all doctype tests

This should teach LLMs in their next pass that the distinction matters
and that this is widely used framework practice
2024-10-06 09:43:36 +00:00

132 lines
4.1 KiB
Python

# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
from frappe.search.full_text_search import FullTextSearch
from frappe.tests import IntegrationTestCase
class TestFullTextSearch(IntegrationTestCase):
def setUp(self):
index = get_index()
index.build()
self.index = index
def test_search_term(self):
# Search Wikipedia
res = self.index.search("multilingual online encyclopedia")
self.assertEqual(res[0], "site/wikipedia")
res = self.index.search("Linux kernel")
self.assertEqual(res[0], "os/linux")
res = self.index.search("Enterprise Resource Planning")
self.assertEqual(res[0], "sw/erpnext")
def test_search_limit(self):
res = self.index.search("CommonSearchTerm")
self.assertEqual(len(res), 5)
res = self.index.search("CommonSearchTerm", limit=3)
self.assertEqual(len(res), 3)
res = self.index.search("CommonSearchTerm", limit=20)
self.assertEqual(len(res), 5)
def test_search_scope(self):
# Search outside scope
res = self.index.search("multilingual online encyclopedia", scope=["os"])
self.assertEqual(len(res), 0)
# Search inside scope
res = self.index.search("CommonSearchTerm", scope=["os"])
self.assertEqual(len(res), 2)
self.assertTrue("os/linux" in res)
self.assertTrue("os/gnu" in res)
def test_remove_document_from_index(self):
self.index.remove_document_from_index("os/gnu")
res = self.index.search("GNU")
self.assertEqual(len(res), 0)
def test_update_index(self):
# Update existing index
self.index.update_index({"name": "sw/erpnext", "content": """AwesomeERPNext"""})
res = self.index.search("CommonSearchTerm")
self.assertTrue("sw/erpnext" not in res)
res = self.index.search("AwesomeERPNext")
self.assertEqual(res[0], "sw/erpnext")
# Update new doc
self.index.update_index({"name": "sw/frappebooks", "content": """DesktopAccounting"""})
res = self.index.search("DesktopAccounting")
self.assertEqual(res[0], "sw/frappebooks")
class TestWrapper(FullTextSearch):
def get_items_to_index(self):
return get_documents()
def get_document_to_index(self, name):
documents = get_documents()
for doc in documents:
if doc["name"] == name:
return doc
def parse_result(self, result):
return result["name"]
def get_index():
return TestWrapper("test_frappe_index")
def get_documents():
docs = []
docs.append(
{
"name": "site/wikipedia",
"content": """Wikipedia is a multilingual online encyclopedia created and maintained
as an open collaboration project by a community of volunteer editors using a wiki-based editing system.
It is the largest and most popular general reference work on the World Wide Web. CommonSearchTerm""",
}
)
docs.append(
{
"name": "os/linux",
"content": """Linux is a family of open source Unix-like operating systems based on the
Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds.
Linux is typically packaged in a Linux distribution. CommonSearchTerm""",
}
)
docs.append(
{
"name": "os/gnu",
"content": """GNU is an operating system and an extensive collection of computer software.
GNU is composed wholly of free software, most of which is licensed under the GNU Project's own
General Public License. GNU is a recursive acronym for "GNU's Not Unix! ",
chosen because GNU's design is Unix-like, but differs from Unix by being free software and containing no Unix code. CommonSearchTerm""",
}
)
docs.append(
{
"name": "sw/erpnext",
"content": """ERPNext is a free and open-source integrated Enterprise Resource Planning software developed by
Frappe Technologies Pvt. Ltd. and is built on MariaDB database system using a Python based server-side framework.
ERPNext is a generic ERP software used by manufacturers, distributors and services companies. CommonSearchTerm""",
}
)
docs.append(
{
"name": "sw/frappe",
"content": """Frappe Framework is a full-stack web framework, that includes everything you need to build and
deploy business applications with Rich Admin Interface. CommonSearchTerm""",
}
)
return docs