Browse Source

Bing images' unit test

Cqoicebordel 10 years ago
parent
commit
dad0434f34
3 changed files with 273 additions and 1 deletions
  1. 4
    1
      searx/engines/bing_images.py
  2. 268
    0
      searx/tests/engines/test_bing_images.py
  3. 1
    0
      searx/tests/test_engines.py

+ 4
- 1
searx/engines/bing_images.py View File

@@ -33,7 +33,10 @@ def request(query, params):
33 33
     offset = (params['pageno'] - 1) * 10 + 1
34 34
 
35 35
     # required for cookie
36
-    language = 'en-US'
36
+    if params['language'] == 'all':
37
+        language = 'en-US'
38
+    else:
39
+        language = params['language'].replace('_', '-')
37 40
 
38 41
     search_path = search_string.format(
39 42
         query=urlencode({'q': query}),

+ 268
- 0
searx/tests/engines/test_bing_images.py View File

@@ -0,0 +1,268 @@
1
+# -*- coding: utf-8 -*-
2
+from collections import defaultdict
3
+import mock
4
+from searx.engines import bing_images
5
+from searx.testing import SearxTestCase
6
+
7
+
8
+class TestBingImagesEngine(SearxTestCase):
9
+
10
+    def test_request(self):
11
+        query = 'test_query'
12
+        dicto = defaultdict(dict)
13
+        dicto['pageno'] = 1
14
+        dicto['language'] = 'fr_FR'
15
+        params = bing_images.request(query, dicto)
16
+        self.assertTrue('url' in params)
17
+        self.assertTrue(query in params['url'])
18
+        self.assertTrue('bing.com' in params['url'])
19
+        self.assertTrue('SRCHHPGUSR' in params['cookies'])
20
+        self.assertTrue('fr' in params['cookies']['SRCHHPGUSR'])
21
+
22
+        dicto['language'] = 'all'
23
+        params = bing_images.request(query, dicto)
24
+        self.assertIn('SRCHHPGUSR', params['cookies'])
25
+        self.assertIn('en', params['cookies']['SRCHHPGUSR'])
26
+
27
+    def test_response(self):
28
+        self.assertRaises(AttributeError, bing_images.response, None)
29
+        self.assertRaises(AttributeError, bing_images.response, [])
30
+        self.assertRaises(AttributeError, bing_images.response, '')
31
+        self.assertRaises(AttributeError, bing_images.response, '[]')
32
+
33
+        response = mock.Mock(content='<html></html>')
34
+        self.assertEqual(bing_images.response(response), [])
35
+
36
+        response = mock.Mock(content='<html></html>')
37
+        self.assertEqual(bing_images.response(response), [])
38
+
39
+        html = """
40
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
41
+            <a href="#" ihk="HN.608003696942779811"
42
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
43
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
44
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
45
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
46
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
47
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
48
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
49
+                style="height:144px;" width="178" height="144"/>
50
+            </a>
51
+        </div>
52
+        """
53
+        html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
54
+        response = mock.Mock(content=html)
55
+        results = bing_images.response(response)
56
+        self.assertEqual(type(results), list)
57
+        self.assertEqual(len(results), 1)
58
+        self.assertEqual(results[0]['title'], 'Test Query')
59
+        self.assertEqual(results[0]['url'], 'http://www.page.url/')
60
+        self.assertEqual(results[0]['content'], '')
61
+        self.assertEqual(results[0]['thumbnail_src'], 'http://ts1.mm.bing.net/th?id=HN.608003696942779811')
62
+        self.assertEqual(results[0]['img_src'], 'http://test.url/Test%20Query.jpg')
63
+
64
+        html = """
65
+        <a href="#" ihk="HN.608003696942779811"
66
+            m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
67
+            mid:&quot;59EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
68
+            surl:&quot;http://www.page.url/&quot;,
69
+            imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,oh:&quot;238&quot;,
70
+            tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
71
+            mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
72
+            t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
73
+            <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
74
+            style="height:144px;" width="178" height="144"/>
75
+        </a>
76
+        """
77
+        response = mock.Mock(content=html)
78
+        results = bing_images.response(response)
79
+        self.assertEqual(type(results), list)
80
+        self.assertEqual(len(results), 0)
81
+
82
+        html = """
83
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
84
+            <a href="#" ihk="HN.608003696942779811"
85
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
86
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
87
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
88
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
89
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
90
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
91
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
92
+                style="height:144px;" width="178" height="144"/>
93
+            </a>
94
+        </div>
95
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
96
+            <a href="#" ihk="HN.608003696942779811"
97
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
98
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
99
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
100
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
101
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
102
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
103
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
104
+                style="height:144px;" width="178" height="144"/>
105
+            </a>
106
+        </div>
107
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
108
+            <a href="#" ihk="HN.608003696942779811"
109
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
110
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
111
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
112
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
113
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
114
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
115
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
116
+                style="height:144px;" width="178" height="144"/>
117
+            </a>
118
+        </div>
119
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
120
+            <a href="#" ihk="HN.608003696942779811"
121
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
122
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
123
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
124
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
125
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
126
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
127
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
128
+                style="height:144px;" width="178" height="144"/>
129
+            </a>
130
+        </div>
131
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
132
+            <a href="#" ihk="HN.608003696942779811"
133
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
134
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
135
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
136
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
137
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
138
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
139
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
140
+                style="height:144px;" width="178" height="144"/>
141
+            </a>
142
+        </div>
143
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
144
+            <a href="#" ihk="HN.608003696942779811"
145
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
146
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
147
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
148
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
149
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
150
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
151
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
152
+                style="height:144px;" width="178" height="144"/>
153
+            </a>
154
+        </div>
155
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
156
+            <a href="#" ihk="HN.608003696942779811"
157
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
158
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
159
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
160
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
161
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
162
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
163
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
164
+                style="height:144px;" width="178" height="144"/>
165
+            </a>
166
+        </div>
167
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
168
+            <a href="#" ihk="HN.608003696942779811"
169
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
170
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
171
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
172
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
173
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
174
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
175
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
176
+                style="height:144px;" width="178" height="144"/>
177
+            </a>
178
+        </div>
179
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
180
+            <a href="#" ihk="HN.608003696942779811"
181
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
182
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
183
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
184
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
185
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
186
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
187
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
188
+                style="height:144px;" width="178" height="144"/>
189
+            </a>
190
+        </div>
191
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
192
+            <a href="#" ihk="HN.608003696942779811"
193
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
194
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
195
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
196
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
197
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
198
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
199
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
200
+                style="height:144px;" width="178" height="144"/>
201
+            </a>
202
+        </div>
203
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
204
+            <a href="#" ihk="HN.608003696942779811"
205
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
206
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
207
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
208
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
209
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
210
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
211
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
212
+                style="height:144px;" width="178" height="144"/>
213
+            </a>
214
+        </div>
215
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
216
+            <a href="#" ihk="HN.608003696942779811"
217
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
218
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
219
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
220
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
221
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
222
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
223
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
224
+                style="height:144px;" width="178" height="144"/>
225
+            </a>
226
+        </div>
227
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
228
+            <a href="#" ihk="HN.608003696942779811"
229
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
230
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
231
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
232
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
233
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
234
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
235
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
236
+                style="height:144px;" width="178" height="144"/>
237
+            </a>
238
+        </div>
239
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
240
+            <a href="#" ihk="HN.608003696942779811"
241
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
242
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
243
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
244
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
245
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
246
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
247
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
248
+                style="height:144px;" width="178" height="144"/>
249
+            </a>
250
+        </div>
251
+        <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
252
+            <a href="#" ihk="HN.608003696942779811"
253
+                m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
254
+mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
255
+surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
256
+oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
257
+                mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
258
+                t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
259
+                <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
260
+                style="height:144px;" width="178" height="144"/>
261
+            </a>
262
+        </div>
263
+        """
264
+        html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
265
+        response = mock.Mock(content=html)
266
+        results = bing_images.response(response)
267
+        self.assertEqual(type(results), list)
268
+        self.assertEqual(len(results), 10)

+ 1
- 0
searx/tests/test_engines.py View File

@@ -1,4 +1,5 @@
1 1
 from searx.tests.engines.test_bing import *  # noqa
2
+from searx.tests.engines.test_bing_images import *  # noqa
2 3
 from searx.tests.engines.test_dailymotion import *  # noqa
3 4
 from searx.tests.engines.test_deezer import *  # noqa
4 5
 from searx.tests.engines.test_deviantart import *  # noqa