1
0
mirror of https://github.com/searx/searx synced 2024-12-12 08:46:26 +01:00

Merge pull request #2309 from dalf/mod_search_repr

[mod] searx.search: EngineRef, SearchQuery: add __repr__ and __eq__ methods
This commit is contained in:
Alexandre Flament 2020-11-14 13:23:44 +01:00 committed by GitHub
commit 46b454277f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 5 deletions

View File

@ -57,8 +57,11 @@ class EngineRef:
self.category = category self.category = category
self.from_bang = from_bang self.from_bang = from_bang
def __str__(self): def __repr__(self):
return "(" + self.name + "," + self.category + "," + str(self.from_bang) + ")" return "EngineRef({!r}, {!r}, {!r})".format(self.name, self.category, self.from_bang)
def __eq__(self, other):
return self.name == other.name and self.category == other.category and self.from_bang == other.from_bang
class SearchQuery: class SearchQuery:
@ -87,8 +90,21 @@ class SearchQuery:
self.timeout_limit = timeout_limit self.timeout_limit = timeout_limit
self.external_bang = external_bang self.external_bang = external_bang
def __str__(self): def __repr__(self):
return self.query + ";" + str(self.engineref_list) return "SearchQuery({!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r})".\
format(self.query, self.engineref_list, self.categories, self.lang, self.safesearch,
self.pageno, self.time_range, self.timeout_limit, self.external_bang)
def __eq__(self, other):
return self.query == other.query\
and self.engineref_list == other.engineref_list\
and self.categories == self.categories\
and self.lang == other.lang\
and self.safesearch == other.safesearch\
and self.pageno == other.pageno\
and self.time_range == other.time_range\
and self.timeout_limit == other.timeout_limit\
and self.external_bang == other.external_bang
def send_http_request(engine, request_params): def send_http_request(engine, request_params):

View File

@ -21,6 +21,20 @@ TEST_ENGINES = [
] ]
class SearchQueryTestCase(SearxTestCase):
def test_repr(self):
s = SearchQuery('test', [EngineRef('bing', 'general', False)], ['general'], 'all', 0, 1, '1', 5.0, 'g')
self.assertEqual(repr(s),
"SearchQuery('test', [EngineRef('bing', 'general', False)], ['general'], 'all', 0, 1, '1', 5.0, 'g')") # noqa
def test_eq(self):
s = SearchQuery('test', [EngineRef('bing', 'general', False)], ['general'], 'all', 0, 1, None, None, None)
t = SearchQuery('test', [EngineRef('google', 'general', False)], ['general'], 'all', 0, 1, None, None, None)
self.assertEqual(s, s)
self.assertNotEqual(s, t)
class SearchTestCase(SearxTestCase): class SearchTestCase(SearxTestCase):
@classmethod @classmethod

View File

@ -7,6 +7,7 @@ import sys
from mock import Mock, patch from mock import Mock, patch
from nose2.tools import params from nose2.tools import params
from searx.search import SearchQuery
from searx.testing import SearxTestCase from searx.testing import SearxTestCase
@ -91,7 +92,7 @@ class StandaloneSearx(SearxTestCase):
args = sas.parse_argument(['rain', ]) args = sas.parse_argument(['rain', ])
search_q = sas.get_search_query(args) search_q = sas.get_search_query(args)
self.assertTrue(search_q) self.assertTrue(search_q)
self.assertEqual(str(search_q), 'rain;[]') self.assertEqual(search_q, SearchQuery('rain', [], ['general'], 'all', 0, 1, None, None, None))
def test_no_parsed_url(self): def test_no_parsed_url(self):
"""test no_parsed_url func""" """test no_parsed_url func"""