summaryrefslogtreecommitdiff
path: root/searx/tests/engines/test_google_images.py
blob: 9bef692d4d771f06fa411ac6754c0783e28c61af (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
from collections import defaultdict
import mock
from searx.engines import google_images
from searx.testing import SearxTestCase


class TestGoogleImagesEngine(SearxTestCase):

    def test_request(self):
        query = 'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 1
        params = google_images.request(query, dicto)
        self.assertIn('url', params)
        self.assertIn(query, params['url'])
        self.assertIn('googleapis.com', params['url'])
        self.assertIn('safe=on', params['url'])

        dicto['safesearch'] = 0
        params = google_images.request(query, dicto)
        self.assertIn('safe=off', params['url'])

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

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

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

        json = """
        {
        "responseData": {
            "results": [
            {
                "GsearchResultClass": "GimageSearch",
                "width": "400",
                "height": "400",
                "imageId": "ANd9GcQbYb9FJuAbG_hT4i8FeC0O0x-P--EHdzgRIF9ao97nHLl7C2mREn6qTQ",
                "tbWidth": "124",
                "tbHeight": "124",
                "unescapedUrl": "http://unescaped.url.jpg",
                "url": "http://image.url.jpg",
                "visibleUrl": "insolitebuzz.fr",
                "title": "This is the title",
                "titleNoFormatting": "Petit test sympa qui rend fou tout le monde ! A faire",
                "originalContextUrl": "http://this.is.the.url",
                "content": "<b>test</b>",
                "contentNoFormatting": "test",
                "tbUrl": "http://thumbnail.url"
            }
            ]
        },
        "responseDetails": null,
        "responseStatus": 200
        }
        """
        response = mock.Mock(text=json)
        results = google_images.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'http://this.is.the.url')
        self.assertEqual(results[0]['thumbnail_src'], 'https://thumbnail.url')
        self.assertEqual(results[0]['img_src'], 'http://image.url.jpg')
        self.assertEqual(results[0]['content'], '<b>test</b>')

        json = """
        {
        "responseData": {
            "results": [
            {
                "GsearchResultClass": "GimageSearch",
                "width": "400",
                "height": "400",
                "imageId": "ANd9GcQbYb9FJuAbG_hT4i8FeC0O0x-P--EHdzgRIF9ao97nHLl7C2mREn6qTQ",
                "tbWidth": "124",
                "tbHeight": "124",
                "unescapedUrl": "http://unescaped.url.jpg",
                "visibleUrl": "insolitebuzz.fr",
                "title": "This is the title",
                "titleNoFormatting": "Petit test sympa qui rend fou tout le monde ! A faire",
                "originalContextUrl": "http://this.is.the.url",
                "content": "<b>test</b>",
                "contentNoFormatting": "test",
                "tbUrl": "http://thumbnail.url"
            }
            ]
        },
        "responseDetails": null,
        "responseStatus": 200
        }
        """
        response = mock.Mock(text=json)
        results = google_images.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        json = """
        {
        "responseData": {},
        "responseDetails": null,
        "responseStatus": 200
        }
        """
        response = mock.Mock(text=json)
        results = google_images.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)