on master help sign in
[fix] fix monkey patch in test_webapp.py (#1667)

at the end of test_webapp.py, the monkey patch of searx.search.Search was not revert which lead to side effects on other tests close #1663

commit333e54943d945a1afcb2ac5a82dd6edcf052be2d authorAlexandre Flament <alex@al-f.net> dateSat 3 Aug 2019, 13:23 (7 years ago) parentd24e794
2 files changed+19−6
searx/testing.py+12 tests/unit/test_webapp.py+7−6
searx/testing.py+12
@@ -80,6 +80,18 @@ class SearxTestCase(TestCase):
8080
8181 layer = SearxTestLayer
8282
83+ def setattr4test(self, obj, attr, value):
84+ """
85+ setattr(obj, attr, value)
86+ but reset to the previous value in the cleanup.
87+ """
88+ previous_value = getattr(obj, attr)
89+
90+ def cleanup_patch():
91+ setattr(obj, attr, previous_value)
92+ self.addCleanup(cleanup_patch)
93+ setattr(obj, attr, value)
94+
8395
8496if __name__ == '__main__':
8597 import sys
tests/unit/test_webapp.py+7−6
@@ -15,7 +15,7 @@ class ViewsTestCase(SearxTestCase):
1515 self.app = webapp.app.test_client()
1616
1717 # set some defaults
18− self.test_results = [
18+ test_results = [
1919 {
2020 'content': 'first test content',
2121 'title': 'First Test',
@@ -47,25 +47,25 @@ class ViewsTestCase(SearxTestCase):
4747 ]
4848
4949 def search_mock(search_self, *args):
50− search_self.result_container = Mock(get_ordered_results=lambda: self.test_results,
50+ search_self.result_container = Mock(get_ordered_results=lambda: test_results,
5151 answers=set(),
5252 corrections=set(),
5353 suggestions=set(),
5454 infoboxes=[],
5555 unresponsive_engines=set(),
56− results=self.test_results,
56+ results=test_results,
5757 results_number=lambda: 3,
58− results_length=lambda: len(self.test_results),
58+ results_length=lambda: len(test_results),
5959 get_timings=lambda: timings)
6060
61− Search.search = search_mock
61+ self.setattr4test(Search, 'search', search_mock)
6262
6363 def get_current_theme_name_mock(override=None):
6464 if override:
6565 return override
6666 return 'legacy'
6767
68− webapp.get_current_theme_name = get_current_theme_name_mock
68+ self.setattr4test(webapp, 'get_current_theme_name', get_current_theme_name_mock)
6969
7070 self.maxDiff = None # to see full diffs
7171
@@ -91,6 +91,7 @@ class ViewsTestCase(SearxTestCase):
9191 result_dict = json.loads(result.data.decode('utf-8'))
9292
9393 self.assertEqual('test', result_dict['query'])
94+ self.assertEqual(len(result_dict['results']), 2)
9495 self.assertEqual(result_dict['results'][0]['content'], 'first test content')
9596 self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
9697