summaryrefslogtreecommitdiff
path: root/tests/unit/engines/test_faroo.py
blob: acebdda863c734061c0c050145bed8e6951bf40e (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
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import faroo
from searx.testing import SearxTestCase


class TestFarooEngine(SearxTestCase):

    def test_request(self):
        query = 'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 1
        dicto['language'] = 'fr_FR'
        dicto['category'] = 'general'
        params = faroo.request(query, dicto)
        self.assertIn('url', params)
        self.assertIn(query, params['url'])
        self.assertIn('faroo.com', params['url'])
        self.assertIn('en', params['url'])
        self.assertIn('web', params['url'])

        dicto['language'] = 'all'
        params = faroo.request(query, dicto)
        self.assertIn('en', params['url'])

        dicto['language'] = 'de_DE'
        params = faroo.request(query, dicto)
        self.assertIn('de', params['url'])

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

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

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

        response = mock.Mock(text='{"data": []}', status_code=401)
        self.assertRaises(Exception, faroo.response, response)

        response = mock.Mock(text='{"data": []}', status_code=429)
        self.assertRaises(Exception, faroo.response, response)

        json = """
        {
          "results": [
            {
              "title": "This is the title",
              "kwic": "This is the content",
              "content": "",
              "url": "http://this.is.the.url/",
              "iurl": "",
              "domain": "css3test.com",
              "author": "Jim Dalrymple",
              "news": true,
              "votes": "10",
              "date": 1360622563000,
              "related": []
            },
            {
              "title": "This is the title2",
              "kwic": "This is the content2",
              "content": "",
              "url": "http://this.is.the.url2/",
              "iurl": "",
              "domain": "css3test.com",
              "author": "Jim Dalrymple",
              "news": false,
              "votes": "10",
              "related": []
            },
            {
              "title": "This is the title3",
              "kwic": "This is the content3",
              "content": "",
              "url": "http://this.is.the.url3/",
              "iurl": "http://upload.wikimedia.org/optimized.jpg",
              "domain": "css3test.com",
              "author": "Jim Dalrymple",
              "news": false,
              "votes": "10",
              "related": []
            }
          ],
          "query": "test",
          "suggestions": [],
          "count": 100,
          "start": 1,
          "length": 10,
          "time": "15"
        }
        """
        response = mock.Mock(text=json)
        results = faroo.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 4)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
        self.assertEqual(results[0]['content'], 'This is the content')
        self.assertEqual(results[1]['title'], 'This is the title2')
        self.assertEqual(results[1]['url'], 'http://this.is.the.url2/')
        self.assertEqual(results[1]['content'], 'This is the content2')
        self.assertEqual(results[3]['img_src'], 'http://upload.wikimedia.org/optimized.jpg')

        json = """
        {}
        """
        response = mock.Mock(text=json)
        results = faroo.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)