summaryrefslogtreecommitdiff
path: root/tests/unit/engines/test_flickr.py
blob: be97647ce7834a2b31b03c578f801622f61628d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from collections import defaultdict
import mock
from searx.engines import flickr
from searx.testing import SearxTestCase


class TestFlickrEngine(SearxTestCase):

    def test_request(self):
        query = 'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 0
        params = flickr.request(query, dicto)
        self.assertTrue('url' in params)
        self.assertTrue(query in params['url'])
        self.assertTrue('flickr.com' in params['url'])

    def test_response(self):
        self.assertRaises(AttributeError, flickr.response, None)
        self.assertRaises(AttributeError, flickr.response, [])
        self.assertRaises(AttributeError, flickr.response, '')
        self.assertRaises(AttributeError, flickr.response, '[]')

        response = mock.Mock(text='{}')
        self.assertEqual(flickr.response(response), [])

        response = mock.Mock(text='{"data": []}')
        self.assertEqual(flickr.response(response), [])

        json = r"""
        { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
            "photo": [
            { "id": "15751017054", "owner": "66847915@N08",
            "secret": "69c22afc40", "server": "7285", "farm": 8,
            "title": "Photo title", "ispublic": 1,
            "isfriend": 0, "isfamily": 0,
            "description": { "_content": "Description" },
            "ownername": "Owner",
            "url_o": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_9178e0f963_o.jpg",
            "height_o": "2100", "width_o": "2653",
            "url_n": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_n.jpg",
            "height_n": "253", "width_n": "320",
            "url_z": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_z.jpg",
            "height_z": "507", "width_z": "640" }
        ] }, "stat": "ok" }
        """
        response = mock.Mock(text=json)
        results = flickr.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'Photo title')
        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
        self.assertTrue('o.jpg' in results[0]['img_src'])
        self.assertTrue('n.jpg' in results[0]['thumbnail_src'])
        self.assertTrue('Owner' in results[0]['author'])
        self.assertTrue('Description' in results[0]['content'])

        json = r"""
        { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
            "photo": [
            { "id": "15751017054", "owner": "66847915@N08",
            "secret": "69c22afc40", "server": "7285", "farm": 8,
            "title": "Photo title", "ispublic": 1,
            "isfriend": 0, "isfamily": 0,
            "description": { "_content": "Description" },
            "ownername": "Owner",
            "url_z": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_z.jpg",
            "height_z": "507", "width_z": "640" }
        ] }, "stat": "ok" }
        """
        response = mock.Mock(text=json)
        results = flickr.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'Photo title')
        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
        self.assertTrue('z.jpg' in results[0]['img_src'])
        self.assertTrue('z.jpg' in results[0]['thumbnail_src'])
        self.assertTrue('Owner' in results[0]['author'])
        self.assertTrue('Description' in results[0]['content'])

        json = r"""
        { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
            "photo": [
            { "id": "15751017054", "owner": "66847915@N08",
            "secret": "69c22afc40", "server": "7285", "farm": 8,
            "title": "Photo title", "ispublic": 1,
            "isfriend": 0, "isfamily": 0,
            "description": { "_content": "Description" },
            "ownername": "Owner",
            "url_o": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_9178e0f963_o.jpg",
            "height_o": "2100", "width_o": "2653" }
        ] }, "stat": "ok" }
        """
        response = mock.Mock(text=json)
        results = flickr.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'Photo title')
        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
        self.assertTrue('o.jpg' in results[0]['img_src'])
        self.assertTrue('o.jpg' in results[0]['thumbnail_src'])
        self.assertTrue('Owner' in results[0]['author'])
        self.assertTrue('Description' in results[0]['content'])

        json = r"""
        { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
            "photo": [
            { "id": "15751017054", "owner": "66847915@N08",
            "secret": "69c22afc40", "server": "7285", "farm": 8,
            "title": "Photo title", "ispublic": 1,
            "isfriend": 0, "isfamily": 0,
            "description": { "_content": "Description" },
            "ownername": "Owner",
            "url_n": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_n.jpg",
            "height_n": "253", "width_n": "320" }
        ] }, "stat": "ok" }
        """
        response = mock.Mock(text=json)
        results = flickr.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        json = """
        { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
            "toto": [] }, "stat": "ok" }
        """
        response = mock.Mock(text=json)
        results = flickr.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        json = r"""
        {"toto":[
            {"id":200,"name":"Artist Name",
            "link":"http:\/\/www.flickr.com\/artist\/1217","type":"artist"}
        ]}
        """
        response = mock.Mock(text=json)
        results = flickr.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)