Matej Cotman пре 11 година
родитељ
комит
2bcc949abe
4 измењених фајлова са 181 додато и 10 уклоњено
  1. 0
    10
      searx/tests/test_unit.py
  2. 167
    0
      searx/tests/test_webapp.py
  3. 13
    0
      searx/webapp.py
  4. 1
    0
      setup.py

+ 0
- 10
searx/tests/test_unit.py Прегледај датотеку

@@ -1,10 +0,0 @@
1
-# -*- coding: utf-8 -*-
2
-
3
-from searx.testing import SearxTestCase
4
-
5
-
6
-class UnitTestCase(SearxTestCase):
7
-
8
-    def test_flask(self):
9
-        import flask
10
-        self.assertIn('Flask', dir(flask))

+ 167
- 0
searx/tests/test_webapp.py Прегледај датотеку

@@ -0,0 +1,167 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+from mock import patch
4
+from searx import webapp
5
+from searx.testing import SearxTestCase
6
+from urlparse import ParseResult
7
+
8
+
9
+import json
10
+
11
+
12
+class ViewsTestCase(SearxTestCase):
13
+
14
+    def setUp(self):
15
+        webapp.app.config['TESTING'] = True  # to get better error messages
16
+        self.app = webapp.app.test_client()
17
+
18
+        # set some defaults
19
+        self.test_results = [
20
+            {
21
+                'content': 'first test content',
22
+                'title': 'First Test',
23
+                'url': 'http://first.test.xyz',
24
+                'engines': ['youtube', 'startpage'],
25
+                'engine': 'startpage',
26
+                'parsed_url': ParseResult(scheme='http', netloc='first.test.xyz', path='/', params='', query='', fragment=''),  # noqa
27
+            }, {
28
+                'content': 'second test content',
29
+                'title': 'Second Test',
30
+                'url': 'http://second.test.xyz',
31
+                'engines': ['youtube', 'startpage'],
32
+                'engine': 'youtube',
33
+                'parsed_url': ParseResult(scheme='http', netloc='second.test.xyz', path='/', params='', query='', fragment=''),  # noqa
34
+            },
35
+        ]
36
+
37
+        self.maxDiff = None  # to see full diffs
38
+
39
+    def test_index_empty(self):
40
+        result = self.app.post('/')
41
+        self.assertEqual(result.status_code, 200)
42
+        self.assertIn('<div class="title"><h1>searx</h1></div>', result.data)
43
+
44
+    @patch('searx.webapp.search')
45
+    def test_index_html(self, search):
46
+        search.return_value = (
47
+            self.test_results,
48
+            set()
49
+        )
50
+        result = self.app.post('/', data={'q': 'test'})
51
+        self.assertIn(
52
+            '<h3 class="result_title"><a href="http://first.test.xyz">First <b>Test</b></a></h3>',  # noqa
53
+            result.data
54
+        )
55
+        self.assertIn(
56
+            '<p class="content">first <b>test</b> content<br /></p>',
57
+            result.data
58
+        )
59
+
60
+    @patch('searx.webapp.search')
61
+    def test_index_json(self, search):
62
+        search.return_value = (
63
+            self.test_results,
64
+            set()
65
+        )
66
+        result = self.app.post('/', data={'q': 'test', 'format': 'json'})
67
+
68
+        result_dict = json.loads(result.data)
69
+
70
+        self.assertEqual('test', result_dict['query'])
71
+        self.assertEqual(
72
+            result_dict['results'][0]['content'], 'first test content')
73
+        self.assertEqual(
74
+            result_dict['results'][0]['url'], 'http://first.test.xyz')
75
+
76
+    @patch('searx.webapp.search')
77
+    def test_index_csv(self, search):
78
+        search.return_value = (
79
+            self.test_results,
80
+            set()
81
+        )
82
+        result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
83
+
84
+        self.assertEqual(
85
+            'title,url,content,host,engine,score\r\n'
86
+            'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,\r\n'
87
+            'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,\r\n',
88
+            result.data
89
+        )
90
+
91
+    @patch('searx.webapp.search')
92
+    def test_index_rss(self, search):
93
+        search.return_value = (
94
+            self.test_results,
95
+            set()
96
+        )
97
+        result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
98
+
99
+        self.assertIn(
100
+            '<description>Search results for "test" - searx</description>',
101
+            result.data
102
+        )
103
+
104
+        self.assertIn(
105
+            '<opensearch:totalResults>2</opensearch:totalResults>',
106
+            result.data
107
+        )
108
+
109
+        self.assertIn(
110
+            '<title>First Test</title>',
111
+            result.data
112
+        )
113
+
114
+        self.assertIn(
115
+            '<link>http://first.test.xyz</link>',
116
+            result.data
117
+        )
118
+
119
+        self.assertIn(
120
+            '<description>first test content</description>',
121
+            result.data
122
+        )
123
+
124
+    def test_about(self):
125
+        result = self.app.get('/about')
126
+        self.assertEqual(result.status_code, 200)
127
+        self.assertIn('<h1>About <a href="/">searx</a></h1>', result.data)
128
+
129
+    def test_engines(self):
130
+        result = self.app.get('/engines')
131
+        self.assertEqual(result.status_code, 200)
132
+        self.assertIn('<h2>Currently used search engines</h2>', result.data)
133
+
134
+    def test_preferences(self):
135
+        result = self.app.get('/preferences')
136
+        self.assertEqual(result.status_code, 200)
137
+        self.assertIn(
138
+            '<form method="post" action="/preferences" id="search_form">',
139
+            result.data
140
+        )
141
+        self.assertIn(
142
+            '<legend>Default categories</legend>',
143
+            result.data
144
+        )
145
+        self.assertIn(
146
+            '<legend>Interface language</legend>',
147
+            result.data
148
+        )
149
+
150
+    def test_stats(self):
151
+        result = self.app.get('/stats')
152
+        self.assertEqual(result.status_code, 200)
153
+        self.assertIn('<h2>Engine stats</h2>', result.data)
154
+
155
+    def test_robots_txt(self):
156
+        result = self.app.get('/robots.txt')
157
+        self.assertEqual(result.status_code, 200)
158
+        self.assertIn('Allow: /', result.data)
159
+
160
+    def test_opensearch_xml(self):
161
+        result = self.app.get('/opensearch.xml')
162
+        self.assertEqual(result.status_code, 200)
163
+        self.assertIn('<Description>Search searx</Description>', result.data)
164
+
165
+    def test_favicon(self):
166
+        result = self.app.get('/favicon.ico')
167
+        self.assertEqual(result.status_code, 200)

+ 13
- 0
searx/webapp.py Прегледај датотеку

@@ -117,6 +117,10 @@ def parse_query(query):
117 117
 
118 118
 @app.route('/', methods=['GET', 'POST'])
119 119
 def index():
120
+    """Render index page.
121
+
122
+    Supported outputs: html, json, csv, rss.
123
+    """
120 124
     paging = False
121 125
     lang = 'all'
122 126
 
@@ -231,17 +235,25 @@ def index():
231 235
 
232 236
 @app.route('/about', methods=['GET'])
233 237
 def about():
238
+    """Render about page"""
234 239
     return render('about.html')
235 240
 
236 241
 
237 242
 @app.route('/engines', methods=['GET'])
238 243
 def list_engines():
244
+    """Render engines page.
245
+
246
+    List of all supported engines.
247
+    """
239 248
     global categories
240 249
     return render('engines.html', categs=categories.items())
241 250
 
242 251
 
243 252
 @app.route('/preferences', methods=['GET', 'POST'])
244 253
 def preferences():
254
+    """Render preferences page.
255
+
256
+    Settings that are going to be saved as cookies."""
245 257
     lang = None
246 258
 
247 259
     if request.cookies.get('language')\
@@ -296,6 +308,7 @@ def preferences():
296 308
 
297 309
 @app.route('/stats', methods=['GET'])
298 310
 def stats():
311
+    """Render engine statistics page."""
299 312
     global categories
300 313
     stats = get_engines_stats()
301 314
     return render('stats.html', stats=stats)

+ 1
- 0
setup.py Прегледај датотеку

@@ -40,6 +40,7 @@ setup(
40 40
         'test': [
41 41
             'coverage',
42 42
             'flake8',
43
+            'mock',
43 44
             'plone.testing',
44 45
             'robotframework',
45 46
             'robotframework-debuglibrary',