From 80ba6f22fafb24c97c5df80e22d913aaefbda644 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Wed, 11 Mar 2015 21:23:28 +0100 Subject: [fix] webapp tests --- searx/tests/test_webapp.py | 42 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) (limited to 'searx/tests') diff --git a/searx/tests/test_webapp.py b/searx/tests/test_webapp.py index 8bbe5d056..32eff5fa5 100644 --- a/searx/tests/test_webapp.py +++ b/searx/tests/test_webapp.py @@ -2,7 +2,6 @@ import json from urlparse import ParseResult -from mock import patch from searx import webapp from searx.testing import SearxTestCase @@ -33,6 +32,11 @@ class ViewsTestCase(SearxTestCase): }, ] + def search_mock(search_self, *args): + search_self.results = self.test_results + + webapp.Search.search = search_mock + self.maxDiff = None # to see full diffs def test_index_empty(self): @@ -40,14 +44,7 @@ class ViewsTestCase(SearxTestCase): self.assertEqual(result.status_code, 200) self.assertIn('

searx

', result.data) - @patch('searx.search.Search.search') - def test_index_html(self, search): - search.return_value = ( - self.test_results, - set(), - set(), - set() - ) + def test_index_html(self): result = self.app.post('/', data={'q': 'test'}) self.assertIn( '

youtubeSecond Test

', # noqa @@ -58,14 +55,7 @@ class ViewsTestCase(SearxTestCase): result.data ) - @patch('searx.search.Search.search') - def test_index_json(self, search): - search.return_value = ( - self.test_results, - set(), - set(), - set() - ) + def test_index_json(self): result = self.app.post('/', data={'q': 'test', 'format': 'json'}) result_dict = json.loads(result.data) @@ -76,14 +66,7 @@ class ViewsTestCase(SearxTestCase): self.assertEqual( result_dict['results'][0]['url'], 'http://first.test.xyz') - @patch('searx.search.Search.search') - def test_index_csv(self, search): - search.return_value = ( - self.test_results, - set(), - set(), - set() - ) + def test_index_csv(self): result = self.app.post('/', data={'q': 'test', 'format': 'csv'}) self.assertEqual( @@ -93,14 +76,7 @@ class ViewsTestCase(SearxTestCase): result.data ) - @patch('searx.search.Search.search') - def test_index_rss(self, search): - search.return_value = ( - self.test_results, - set(), - set(), - set() - ) + def test_index_rss(self): result = self.app.post('/', data={'q': 'test', 'format': 'rss'}) self.assertIn( -- cgit v1.2.3 From f57149f912eece7dab1069e078f6bfe54ffd46e1 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Sat, 14 Mar 2015 22:35:29 +0100 Subject: [enh] plugin tests --- searx/tests/test_plugins.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 searx/tests/test_plugins.py (limited to 'searx/tests') diff --git a/searx/tests/test_plugins.py b/searx/tests/test_plugins.py new file mode 100644 index 000000000..19a02c7b1 --- /dev/null +++ b/searx/tests/test_plugins.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- + +from searx.testing import SearxTestCase +from searx import plugins +from mock import Mock + + +class PluginStoreTest(SearxTestCase): + + def test_PluginStore_init(self): + store = plugins.PluginStore() + self.assertTrue(isinstance(store.plugins, list) and len(store.plugins) == 0) + + def test_PluginStore_register(self): + store = plugins.PluginStore() + testplugin = plugins.Plugin() + store.register(testplugin) + + self.assertTrue(len(store.plugins) == 1) + + def test_PluginStore_call(self): + store = plugins.PluginStore() + testplugin = plugins.Plugin() + store.register(testplugin) + setattr(testplugin, 'asdf', Mock()) + request = Mock(user_plugins=[]) + store.call('asdf', request, Mock()) + + self.assertFalse(testplugin.asdf.called) + + request.user_plugins.append(testplugin) + store.call('asdf', request, Mock()) + + self.assertTrue(testplugin.asdf.called) + + +class SelfIPTest(SearxTestCase): + + def test_PluginStore_init(self): + store = plugins.PluginStore() + store.register(plugins.self_ip) + + self.assertTrue(len(store.plugins) == 1) + + request = Mock(user_plugins=store.plugins, + remote_addr='127.0.0.1') + request.headers.getlist.return_value = [] + ctx = {'search': Mock(answers=set(), + query='ip')} + store.call('pre_search', request, ctx) + self.assertTrue('127.0.0.1' in ctx['search'].answers) -- cgit v1.2.3