test_currency_convert.py 1.94 KB
Newer Older
1
2
3
4
from collections import defaultdict
from datetime import datetime
import mock
from searx.engines import currency_convert
Nicolas Gelot's avatar
Nicolas Gelot committed
5
from unittest import TestCase
6
7


Nicolas Gelot's avatar
Nicolas Gelot committed
8
class TestCurrencyConvertEngine(TestCase):
9
10

    def test_request(self):
Nicolas Gelot's avatar
Nicolas Gelot committed
11
        query = 'test_query'
12
13
14
15
16
        dicto = defaultdict(dict)
        dicto['pageno'] = 1
        params = currency_convert.request(query, dicto)
        self.assertNotIn('url', params)

Nicolas Gelot's avatar
Nicolas Gelot committed
17
        query = 'convert 10 Pound Sterlings to United States Dollars'
18
19
        params = currency_convert.request(query, dicto)
        self.assertIn('url', params)
rinpatch's avatar
rinpatch committed
20
        self.assertIn('duckduckgo.com', params['url'])
21
        self.assertIn('GBP', params['url'])
22
23
24
25
        self.assertIn('USD', params['url'])

    def test_response(self):
        dicto = defaultdict(dict)
pyrrh0n1c's avatar
pyrrh0n1c committed
26
        dicto['amount'] = float(10)
27
        dicto['from'] = "GBP"
28
        dicto['to'] = "USD"
29
        dicto['from_name'] = "pound sterling"
30
        dicto['to_name'] = "United States dollar"
31
32
        response = mock.Mock(text='a,b,c,d', search_params=dicto)
        self.assertEqual(currency_convert.response(response), [])
rinpatch's avatar
rinpatch committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        body = """ddg_spice_currency(
            {
                "conversion":{
                    "converted-amount": "0.5"
                },
                "topConversions":[
                    {
                    },
                    {
                    }
                ]
            }
        );
        """
pyrrh0n1c's avatar
pyrrh0n1c committed
47
        response = mock.Mock(text=body, search_params=dicto)
48
49
50
        results = currency_convert.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
51
52
53
54
55
        self.assertEqual(
            results[0]['answer'],
            ('10.0 GBP = 5.0 USD, 1 GBP (pound sterling)'
             ' = 0.5 USD (United States dollar)')
        )
pyrrh0n1c's avatar
pyrrh0n1c committed
56

rinpatch's avatar
rinpatch committed
57
        target_url = 'https://duckduckgo.com/js/spice/currency/1/{}/{}'.format(
pyrrh0n1c's avatar
pyrrh0n1c committed
58
59
            dicto['from'], dicto['to'])
        self.assertEqual(results[0]['url'], target_url)