From 111180705b6f3b142732eb6325de1346f6372828 Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Thu, 8 Apr 2021 18:44:29 +0200 Subject: [PATCH] [fix] test: avoid HTTP requests patch engine initialization to skip HTTP request (engine_init function in searx.engines.initialize_engines) --- tests/unit/test_engines_init.py | 2 +- tests/unit/test_query.py | 15 ++++++++++----- tests/unit/test_webapp.py | 7 ++++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/unit/test_engines_init.py b/tests/unit/test_engines_init.py index cf4d50309..c75637f20 100644 --- a/tests/unit/test_engines_init.py +++ b/tests/unit/test_engines_init.py @@ -13,7 +13,7 @@ class TestEnginesInit(SearxTestCase): engine_list = [{'engine': 'dummy', 'name': 'engine1', 'shortcut': 'e1'}, {'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2'}] - engines.initialize_engines(engine_list) + engines.load_engines(engine_list) self.assertEqual(len(engines.engines), 2) self.assertIn('engine1', engines.engines) self.assertIn('engine2', engines.engines) diff --git a/tests/unit/test_query.py b/tests/unit/test_query.py index 6bbfdb1e2..7a79ce242 100644 --- a/tests/unit/test_query.py +++ b/tests/unit/test_query.py @@ -1,7 +1,11 @@ +from mock import patch + from searx.search import initialize from searx.query import RawTextQuery from searx.testing import SearxTestCase +import searx.engines + TEST_ENGINES = [ { @@ -277,9 +281,10 @@ class TestBang(SearxTestCase): self.assertEqual(query.getQuery(), '!dum the query') def test_bang_autocomplete_empty(self): - initialize() - query = RawTextQuery('the query !', []) - self.assertEqual(query.autocomplete_list, ['!images', '!wikipedia', '!osm']) + with patch.object(searx.engines, 'initialize_engines', searx.engines.load_engines): + initialize() + query = RawTextQuery('the query !', []) + self.assertEqual(query.autocomplete_list, ['!images', '!wikipedia', '!osm']) - query = RawTextQuery('the query ?', ['osm']) - self.assertEqual(query.autocomplete_list, ['?images', '?wikipedia']) + query = RawTextQuery('the query ?', ['osm']) + self.assertEqual(query.autocomplete_list, ['?images', '?wikipedia']) diff --git a/tests/unit/test_webapp.py b/tests/unit/test_webapp.py index 7d7a04fdc..d3498f51a 100644 --- a/tests/unit/test_webapp.py +++ b/tests/unit/test_webapp.py @@ -3,14 +3,19 @@ import json from urllib.parse import ParseResult from mock import Mock -from searx import webapp from searx.testing import SearxTestCase from searx.search import Search +import searx.engines class ViewsTestCase(SearxTestCase): def setUp(self): + # skip init function (no external HTTP request) + self.setattr4test(searx.engines, 'initialize_engines', searx.engines.load_engines) + + from searx import webapp # pylint disable=import-outside-toplevel + webapp.app.config['TESTING'] = True # to get better error messages self.app = webapp.app.test_client()