Browse Source

Flickr Noapi unit test

Cqoicebordel 10 years ago
parent
commit
c6535dd65e

+ 1
- 1
searx/engines/flickr_noapi.py View File

@@ -17,7 +17,7 @@ import re
17 17
 categories = ['images']
18 18
 
19 19
 url = 'https://secure.flickr.com/'
20
-search_url = url+'search/?{query}&page={page}'
20
+search_url = url + 'search/?{query}&page={page}'
21 21
 photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
22 22
 regex = re.compile(r"\"search-photos-models\",\"photos\":(.*}),\"totalItems\":", re.DOTALL)
23 23
 image_sizes = ('o', 'k', 'h', 'b', 'c', 'z', 'n', 'm', 't', 'q', 's')

+ 442
- 0
searx/tests/engines/test_flickr_noapi.py View File

@@ -0,0 +1,442 @@
1
+from collections import defaultdict
2
+import mock
3
+from searx.engines import flickr_noapi
4
+from searx.testing import SearxTestCase
5
+
6
+
7
+class TestFlickrNoapiEngine(SearxTestCase):
8
+
9
+    def test_build_flickr_url(self):
10
+        url = flickr_noapi.build_flickr_url("uid", "pid")
11
+        self.assertIn("uid", url)
12
+        self.assertIn("pid", url)
13
+
14
+    def test_request(self):
15
+        query = 'test_query'
16
+        dicto = defaultdict(dict)
17
+        dicto['pageno'] = 1
18
+        params = flickr_noapi.request(query, dicto)
19
+        self.assertIn('url', params)
20
+        self.assertIn(query, params['url'])
21
+        self.assertIn('flickr.com', params['url'])
22
+
23
+    def test_response(self):
24
+        self.assertRaises(AttributeError, flickr_noapi.response, None)
25
+        self.assertRaises(AttributeError, flickr_noapi.response, [])
26
+        self.assertRaises(AttributeError, flickr_noapi.response, '')
27
+        self.assertRaises(AttributeError, flickr_noapi.response, '[]')
28
+
29
+        response = mock.Mock(text='"search-photos-models","photos":{},"totalItems":')
30
+        self.assertEqual(flickr_noapi.response(response), [])
31
+
32
+        response = mock.Mock(text='search-photos-models","photos":{"data": []},"totalItems":')
33
+        self.assertEqual(flickr_noapi.response(response), [])
34
+
35
+        json = """
36
+        "search-photos-models","photos":
37
+        {
38
+          "_data": [
39
+            {
40
+              "_flickrModelRegistry": "photo-models",
41
+              "title": "This is the title",
42
+              "sizes": {
43
+                "c": {
44
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_c.jpg",
45
+                  "width": 541,
46
+                  "height": 800,
47
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_c.jpg",
48
+                  "key": "c"
49
+                },
50
+                "h": {
51
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_761d32237a_h.jpg",
52
+                  "width": 1081,
53
+                  "height": 1600,
54
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_761d32237a_h.jpg",
55
+                  "key": "h"
56
+                },
57
+                "k": {
58
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_f145a2c11a_k.jpg",
59
+                  "width": 1383,
60
+                  "height": 2048,
61
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_f145a2c11a_k.jpg",
62
+                  "key": "k"
63
+                },
64
+                "l": {
65
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_b.jpg",
66
+                  "width": 692,
67
+                  "height": 1024,
68
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_b.jpg",
69
+                  "key": "l"
70
+                },
71
+                "m": {
72
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777.jpg",
73
+                  "width": 338,
74
+                  "height": 500,
75
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777.jpg",
76
+                  "key": "m"
77
+                },
78
+                "n": {
79
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_n.jpg",
80
+                  "width": 216,
81
+                  "height": 320,
82
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_n.jpg",
83
+                  "key": "n"
84
+                },
85
+                "q": {
86
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_q.jpg",
87
+                  "width": 150,
88
+                  "height": 150,
89
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_q.jpg",
90
+                  "key": "q"
91
+                },
92
+                "s": {
93
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_m.jpg",
94
+                  "width": 162,
95
+                  "height": 240,
96
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_m.jpg",
97
+                  "key": "s"
98
+                },
99
+                "sq": {
100
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_s.jpg",
101
+                  "width": 75,
102
+                  "height": 75,
103
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_s.jpg",
104
+                  "key": "sq"
105
+                },
106
+                "t": {
107
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_t.jpg",
108
+                  "width": 68,
109
+                  "height": 100,
110
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_t.jpg",
111
+                  "key": "t"
112
+                },
113
+                "z": {
114
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_z.jpg",
115
+                  "width": 433,
116
+                  "height": 640,
117
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_z.jpg",
118
+                  "key": "z"
119
+                }
120
+              },
121
+              "canComment": false,
122
+              "rotation": 0,
123
+              "owner": {
124
+                "_flickrModelRegistry": "person-models",
125
+                "pathAlias": "klink692",
126
+                "username": "Owner",
127
+                "buddyicon": {
128
+                  "retina": null,
129
+                  "large": null,
130
+                  "medium": null,
131
+                  "small": null,
132
+                  "default": "//c1.staticflickr.com/9/8108/buddyicons/59729010@N00.jpg?1361642376#59729010@N00"
133
+                },
134
+                "isPro": true,
135
+                "id": "59729010@N00"
136
+              },
137
+              "engagement": {
138
+                "_flickrModelRegistry": "photo-engagement-models",
139
+                "ownerNsid": "59729010@N00",
140
+                "faveCount": 21,
141
+                "commentCount": 14,
142
+                "viewCount": 10160,
143
+                "id": "14001294434"
144
+              },
145
+              "description": "Description",
146
+              "isHD": false,
147
+              "secret": "410f653777",
148
+              "canAddMeta": false,
149
+              "license": 0,
150
+              "oWidth": 1803,
151
+              "oHeight": 2669,
152
+              "safetyLevel": 0,
153
+              "id": "14001294434"
154
+            }
155
+          ],
156
+          "fetchedStart": true,
157
+          "fetchedEnd": false,
158
+          "totalItems": "4386039"
159
+        },"totalItems":
160
+        """
161
+        json = json.replace('\r\n', '').replace('\n', '').replace('\r', '')
162
+        response = mock.Mock(text=json)
163
+        results = flickr_noapi.response(response)
164
+        self.assertEqual(type(results), list)
165
+        self.assertEqual(len(results), 1)
166
+        self.assertEqual(results[0]['title'], 'This is the title')
167
+        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
168
+        self.assertIn('k.jpg', results[0]['img_src'])
169
+        self.assertIn('n.jpg', results[0]['thumbnail_src'])
170
+        self.assertIn('Owner', results[0]['content'])
171
+        self.assertIn('Description', results[0]['content'])
172
+
173
+        json = """
174
+        "search-photos-models","photos":
175
+        {
176
+          "_data": [
177
+            {
178
+              "_flickrModelRegistry": "photo-models",
179
+              "title": "This is the title",
180
+              "sizes": {
181
+                "z": {
182
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_z.jpg",
183
+                  "width": 433,
184
+                  "height": 640,
185
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_z.jpg",
186
+                  "key": "z"
187
+                }
188
+              },
189
+              "canComment": false,
190
+              "rotation": 0,
191
+              "owner": {
192
+                "_flickrModelRegistry": "person-models",
193
+                "pathAlias": "klink692",
194
+                "username": "Owner",
195
+                "buddyicon": {
196
+                  "retina": null,
197
+                  "large": null,
198
+                  "medium": null,
199
+                  "small": null,
200
+                  "default": "//c1.staticflickr.com/9/8108/buddyicons/59729010@N00.jpg?1361642376#59729010@N00"
201
+                },
202
+                "isPro": true,
203
+                "id": "59729010@N00"
204
+              },
205
+              "engagement": {
206
+                "_flickrModelRegistry": "photo-engagement-models",
207
+                "ownerNsid": "59729010@N00",
208
+                "faveCount": 21,
209
+                "commentCount": 14,
210
+                "viewCount": 10160,
211
+                "id": "14001294434"
212
+              },
213
+              "description": "Description",
214
+              "isHD": false,
215
+              "secret": "410f653777",
216
+              "canAddMeta": false,
217
+              "license": 0,
218
+              "oWidth": 1803,
219
+              "oHeight": 2669,
220
+              "safetyLevel": 0,
221
+              "id": "14001294434"
222
+            }
223
+          ],
224
+          "fetchedStart": true,
225
+          "fetchedEnd": false,
226
+          "totalItems": "4386039"
227
+        },"totalItems":
228
+        """
229
+        response = mock.Mock(text=json)
230
+        results = flickr_noapi.response(response)
231
+        self.assertEqual(type(results), list)
232
+        self.assertEqual(len(results), 1)
233
+        self.assertEqual(results[0]['title'], 'This is the title')
234
+        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
235
+        self.assertIn('z.jpg', results[0]['img_src'])
236
+        self.assertIn('z.jpg', results[0]['thumbnail_src'])
237
+        self.assertIn('Owner', results[0]['content'])
238
+        self.assertIn('Description', results[0]['content'])
239
+
240
+        json = """
241
+        "search-photos-models","photos":
242
+        {
243
+          "_data": [
244
+            {
245
+              "_flickrModelRegistry": "photo-models",
246
+              "title": "This is the title",
247
+              "sizes": {
248
+                "o": {
249
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_o.jpg",
250
+                  "width": 433,
251
+                  "height": 640,
252
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_o.jpg",
253
+                  "key": "o"
254
+                }
255
+              },
256
+              "canComment": false,
257
+              "rotation": 0,
258
+              "owner": {
259
+                "_flickrModelRegistry": "person-models",
260
+                "pathAlias": "klink692",
261
+                "username": "Owner",
262
+                "buddyicon": {
263
+                  "retina": null,
264
+                  "large": null,
265
+                  "medium": null,
266
+                  "small": null,
267
+                  "default": "//c1.staticflickr.com/9/8108/buddyicons/59729010@N00.jpg?1361642376#59729010@N00"
268
+                },
269
+                "isPro": true,
270
+                "id": "59729010@N00"
271
+              },
272
+              "engagement": {
273
+                "_flickrModelRegistry": "photo-engagement-models",
274
+                "ownerNsid": "59729010@N00",
275
+                "faveCount": 21,
276
+                "commentCount": 14,
277
+                "viewCount": 10160,
278
+                "id": "14001294434"
279
+              },
280
+              "isHD": false,
281
+              "secret": "410f653777",
282
+              "canAddMeta": false,
283
+              "license": 0,
284
+              "oWidth": 1803,
285
+              "oHeight": 2669,
286
+              "safetyLevel": 0,
287
+              "id": "14001294434"
288
+            }
289
+          ],
290
+          "fetchedStart": true,
291
+          "fetchedEnd": false,
292
+          "totalItems": "4386039"
293
+        },"totalItems":
294
+        """
295
+        response = mock.Mock(text=json)
296
+        results = flickr_noapi.response(response)
297
+        self.assertEqual(type(results), list)
298
+        self.assertEqual(len(results), 1)
299
+        self.assertEqual(results[0]['title'], 'This is the title')
300
+        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
301
+        self.assertIn('o.jpg', results[0]['img_src'])
302
+        self.assertIn('o.jpg', results[0]['thumbnail_src'])
303
+        self.assertIn('Owner', results[0]['content'])
304
+
305
+        json = """
306
+        "search-photos-models","photos":
307
+        {
308
+          "_data": [
309
+            {
310
+              "_flickrModelRegistry": "photo-models",
311
+              "title": "This is the title",
312
+              "sizes": {
313
+              },
314
+              "canComment": false,
315
+              "rotation": 0,
316
+              "owner": {
317
+                "_flickrModelRegistry": "person-models",
318
+                "pathAlias": "klink692",
319
+                "username": "Owner",
320
+                "buddyicon": {
321
+                  "retina": null,
322
+                  "large": null,
323
+                  "medium": null,
324
+                  "small": null,
325
+                  "default": "//c1.staticflickr.com/9/8108/buddyicons/59729010@N00.jpg?1361642376#59729010@N00"
326
+                },
327
+                "isPro": true,
328
+                "id": "59729010@N00"
329
+              },
330
+              "engagement": {
331
+                "_flickrModelRegistry": "photo-engagement-models",
332
+                "ownerNsid": "59729010@N00",
333
+                "faveCount": 21,
334
+                "commentCount": 14,
335
+                "viewCount": 10160,
336
+                "id": "14001294434"
337
+              },
338
+              "description": "Description",
339
+              "isHD": false,
340
+              "secret": "410f653777",
341
+              "canAddMeta": false,
342
+              "license": 0,
343
+              "oWidth": 1803,
344
+              "oHeight": 2669,
345
+              "safetyLevel": 0,
346
+              "id": "14001294434"
347
+            }
348
+          ],
349
+          "fetchedStart": true,
350
+          "fetchedEnd": false,
351
+          "totalItems": "4386039"
352
+        },"totalItems":
353
+        """
354
+        response = mock.Mock(text=json)
355
+        results = flickr_noapi.response(response)
356
+        self.assertEqual(type(results), list)
357
+        self.assertEqual(len(results), 0)
358
+
359
+        json = """
360
+        "search-photos-models","photos":
361
+        {
362
+          "_data": [null],
363
+          "fetchedStart": true,
364
+          "fetchedEnd": false,
365
+          "totalItems": "4386039"
366
+        },"totalItems":
367
+        """
368
+        response = mock.Mock(text=json)
369
+        results = flickr_noapi.response(response)
370
+        self.assertEqual(type(results), list)
371
+        self.assertEqual(len(results), 0)
372
+
373
+        json = """
374
+        "search-photos-models","photos":
375
+        {
376
+          "_data": [
377
+            {
378
+              "_flickrModelRegistry": "photo-models",
379
+              "title": "This is the title",
380
+              "sizes": {
381
+                "o": {
382
+                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_o.jpg",
383
+                  "width": 433,
384
+                  "height": 640,
385
+                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_o.jpg",
386
+                  "key": "o"
387
+                }
388
+              },
389
+              "canComment": false,
390
+              "rotation": 0,
391
+              "owner": {
392
+                "_flickrModelRegistry": "person-models",
393
+                "pathAlias": "klink692",
394
+                "username": "Owner",
395
+                "buddyicon": {
396
+                  "retina": null,
397
+                  "large": null,
398
+                  "medium": null,
399
+                  "small": null,
400
+                  "default": "//c1.staticflickr.com/9/8108/buddyicons/59729010@N00.jpg?1361642376#59729010@N00"
401
+                },
402
+                "isPro": true
403
+              },
404
+              "engagement": {
405
+                "_flickrModelRegistry": "photo-engagement-models",
406
+                "ownerNsid": "59729010@N00",
407
+                "faveCount": 21,
408
+                "commentCount": 14,
409
+                "viewCount": 10160,
410
+                "id": "14001294434"
411
+              },
412
+              "description": "Description",
413
+              "isHD": false,
414
+              "secret": "410f653777",
415
+              "canAddMeta": false,
416
+              "license": 0,
417
+              "oWidth": 1803,
418
+              "oHeight": 2669,
419
+              "safetyLevel": 0,
420
+              "id": "14001294434"
421
+            }
422
+          ],
423
+          "fetchedStart": true,
424
+          "fetchedEnd": false,
425
+          "totalItems": "4386039"
426
+        },"totalItems":
427
+        """
428
+        response = mock.Mock(text=json)
429
+        results = flickr_noapi.response(response)
430
+        self.assertEqual(type(results), list)
431
+        self.assertEqual(len(results), 0)
432
+
433
+        json = """
434
+        {"toto":[
435
+            {"id":200,"name":"Artist Name",
436
+            "link":"http:\/\/www.flickr.com\/artist\/1217","type":"artist"}
437
+        ]}
438
+        """
439
+        response = mock.Mock(text=json)
440
+        results = flickr_noapi.response(response)
441
+        self.assertEqual(type(results), list)
442
+        self.assertEqual(len(results), 0)

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

@@ -8,6 +8,7 @@ from searx.tests.engines.test_deviantart import *  # noqa
8 8
 from searx.tests.engines.test_digg import *  # noqa
9 9
 from searx.tests.engines.test_dummy import *  # noqa
10 10
 from searx.tests.engines.test_flickr import *  # noqa
11
+from searx.tests.engines.test_flickr_noapi import *  # noqa
11 12
 from searx.tests.engines.test_github import *  # noqa
12 13
 from searx.tests.engines.test_google_images import *  # noqa
13 14
 from searx.tests.engines.test_google_news import *  # noqa