mirror of
https://github.com/searxng/searxng.git
synced 2025-07-16 09:49:21 +02:00
[refactor] unit tests to utilize paramaterized and break down monolithic tests
- for tests which perform the same arrange/act/assert pattern but with different data, the data portion has been moved to the ``paramaterized.expand`` fields - for monolithic tests which performed multiple arrange/act/asserts, they have been broken up into different unit tests. - when possible, change generic assert statements to more concise asserts (i.e. ``assertIsNone``) This work ultimately is focused on creating smaller and more concise tests. While paramaterized may make adding new configurations for existing tests easier, that is just a beneficial side effect. The main benefit is that smaller tests are easier to reason about, meaning they are easier to debug when they start failing. This improves the developer experience in debugging what went wrong when refactoring the project. Total number of tests went from 192 -> 259; or, broke apart larger tests into 69 more concise ones.
This commit is contained in:
parent
042c7190e6
commit
44a06190bb
12 changed files with 341 additions and 342 deletions
|
@ -1,6 +1,7 @@
|
|||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# pylint: disable=missing-module-docstring
|
||||
|
||||
from parameterized.parameterized import parameterized
|
||||
from searx.engines import load_engines
|
||||
from searx.query import RawTextQuery
|
||||
from tests import SearxTestCase
|
||||
|
@ -129,49 +130,32 @@ class TestLanguageParser(SearxTestCase): # pylint:disable=missing-class-docstri
|
|||
query = RawTextQuery(query_text, [])
|
||||
self.assertEqual(query.autocomplete_list, [":en", ":en_us", ":english", ":united_kingdom"])
|
||||
|
||||
def test_autocomplete(self):
|
||||
query = RawTextQuery(':englis', [])
|
||||
self.assertEqual(query.autocomplete_list, [":english"])
|
||||
|
||||
query = RawTextQuery(':deutschla', [])
|
||||
self.assertEqual(query.autocomplete_list, [":deutschland"])
|
||||
|
||||
query = RawTextQuery(':new_zea', [])
|
||||
self.assertEqual(query.autocomplete_list, [":new_zealand"])
|
||||
|
||||
query = RawTextQuery(':hu-H', [])
|
||||
self.assertEqual(query.autocomplete_list, [":hu-hu"])
|
||||
|
||||
query = RawTextQuery(':zh-', [])
|
||||
self.assertEqual(query.autocomplete_list, [':zh-cn', ':zh-hk', ':zh-tw'])
|
||||
@parameterized.expand(
|
||||
[
|
||||
(':englis', [":english"]),
|
||||
(':deutschla', [":deutschland"]),
|
||||
(':new_zea', [":new_zealand"]),
|
||||
(':zh-', [':zh-cn', ':zh-hk', ':zh-tw']),
|
||||
]
|
||||
)
|
||||
def test_autocomplete(self, query: str, autocomplete_list: list):
|
||||
query = RawTextQuery(query, [])
|
||||
self.assertEqual(query.autocomplete_list, autocomplete_list)
|
||||
|
||||
|
||||
class TestTimeoutParser(SearxTestCase): # pylint:disable=missing-class-docstring
|
||||
def test_timeout_below100(self):
|
||||
query_text = '<3 the query'
|
||||
@parameterized.expand(
|
||||
[
|
||||
('<3 the query', 3),
|
||||
('<350 the query', 0.35),
|
||||
('<3500 the query', 3.5),
|
||||
]
|
||||
)
|
||||
def test_timeout_limit(self, query_text: str, timeout_limit: float):
|
||||
query = RawTextQuery(query_text, [])
|
||||
|
||||
self.assertEqual(query.getFullQuery(), query_text)
|
||||
self.assertEqual(len(query.query_parts), 1)
|
||||
self.assertEqual(query.timeout_limit, 3)
|
||||
self.assertFalse(query.specific)
|
||||
|
||||
def test_timeout_above100(self):
|
||||
query_text = '<350 the query'
|
||||
query = RawTextQuery(query_text, [])
|
||||
|
||||
self.assertEqual(query.getFullQuery(), query_text)
|
||||
self.assertEqual(len(query.query_parts), 1)
|
||||
self.assertEqual(query.timeout_limit, 0.35)
|
||||
self.assertFalse(query.specific)
|
||||
|
||||
def test_timeout_above1000(self):
|
||||
query_text = '<3500 the query'
|
||||
query = RawTextQuery(query_text, [])
|
||||
|
||||
self.assertEqual(query.getFullQuery(), query_text)
|
||||
self.assertEqual(len(query.query_parts), 1)
|
||||
self.assertEqual(query.timeout_limit, 3.5)
|
||||
self.assertEqual(query.timeout_limit, timeout_limit)
|
||||
self.assertFalse(query.specific)
|
||||
|
||||
def test_timeout_invalid(self):
|
||||
|
@ -182,7 +166,7 @@ class TestTimeoutParser(SearxTestCase): # pylint:disable=missing-class-docstrin
|
|||
self.assertEqual(query.getFullQuery(), query_text)
|
||||
self.assertEqual(len(query.query_parts), 0)
|
||||
self.assertEqual(query.getQuery(), query_text)
|
||||
self.assertEqual(query.timeout_limit, None)
|
||||
self.assertIsNone(query.timeout_limit)
|
||||
self.assertFalse(query.specific)
|
||||
|
||||
def test_timeout_autocomplete(self):
|
||||
|
@ -193,7 +177,7 @@ class TestTimeoutParser(SearxTestCase): # pylint:disable=missing-class-docstrin
|
|||
self.assertEqual(query.getFullQuery(), query_text)
|
||||
self.assertEqual(len(query.query_parts), 0)
|
||||
self.assertEqual(query.getQuery(), query_text)
|
||||
self.assertEqual(query.timeout_limit, None)
|
||||
self.assertIsNone(query.timeout_limit)
|
||||
self.assertFalse(query.specific)
|
||||
self.assertEqual(query.autocomplete_list, ['<3', '<850'])
|
||||
|
||||
|
@ -212,7 +196,7 @@ class TestExternalBangParser(SearxTestCase): # pylint:disable=missing-class-doc
|
|||
query = RawTextQuery(query_text, [])
|
||||
|
||||
self.assertEqual(query.getFullQuery(), query_text)
|
||||
self.assertEqual(query.external_bang, None)
|
||||
self.assertIsNone(query.external_bang)
|
||||
self.assertFalse(query.specific)
|
||||
|
||||
def test_external_bang_autocomplete(self):
|
||||
|
@ -239,23 +223,22 @@ class TestBang(SearxTestCase): # pylint:disable=missing-class-docstring
|
|||
def tearDown(self):
|
||||
load_engines([])
|
||||
|
||||
def test_bang(self):
|
||||
@parameterized.expand(SPECIFIC_BANGS)
|
||||
def test_bang(self, bang: str):
|
||||
with self.subTest(msg="Check bang", bang=bang):
|
||||
query_text = TestBang.THE_QUERY + ' ' + bang
|
||||
query = RawTextQuery(query_text, [])
|
||||
|
||||
for bang in TestBang.SPECIFIC_BANGS:
|
||||
with self.subTest(msg="Check bang", bang=bang):
|
||||
query_text = TestBang.THE_QUERY + ' ' + bang
|
||||
query = RawTextQuery(query_text, [])
|
||||
self.assertEqual(query.getFullQuery(), bang + ' ' + TestBang.THE_QUERY)
|
||||
self.assertEqual(query.query_parts, [bang])
|
||||
self.assertEqual(query.user_query_parts, TestBang.THE_QUERY.split(' '))
|
||||
|
||||
self.assertEqual(query.getFullQuery(), bang + ' ' + TestBang.THE_QUERY)
|
||||
self.assertEqual(query.query_parts, [bang])
|
||||
self.assertEqual(query.user_query_parts, TestBang.THE_QUERY.split(' '))
|
||||
|
||||
def test_specific(self):
|
||||
for bang in TestBang.SPECIFIC_BANGS:
|
||||
with self.subTest(msg="Check bang is specific", bang=bang):
|
||||
query_text = TestBang.THE_QUERY + ' ' + bang
|
||||
query = RawTextQuery(query_text, [])
|
||||
self.assertTrue(query.specific)
|
||||
@parameterized.expand(SPECIFIC_BANGS)
|
||||
def test_specific(self, bang: str):
|
||||
with self.subTest(msg="Check bang is specific", bang=bang):
|
||||
query_text = TestBang.THE_QUERY + ' ' + bang
|
||||
query = RawTextQuery(query_text, [])
|
||||
self.assertTrue(query.specific)
|
||||
|
||||
def test_bang_not_found(self):
|
||||
query = RawTextQuery('the query !bang_not_found', [])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue