|
@@ -0,0 +1,91 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+from collections import defaultdict
|
|
3
|
+import mock
|
|
4
|
+from searx.engines import base
|
|
5
|
+from searx.testing import SearxTestCase
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+class TestBaseEngine(SearxTestCase):
|
|
9
|
+
|
|
10
|
+ def test_request(self):
|
|
11
|
+ query = 'test_query'
|
|
12
|
+ dicto = defaultdict(dict)
|
|
13
|
+ dicto['pageno'] = 1
|
|
14
|
+ params = base.request(query, dicto)
|
|
15
|
+ self.assertIn('url', params)
|
|
16
|
+ self.assertIn('base-search.net', params['url'])
|
|
17
|
+
|
|
18
|
+ def test_response(self):
|
|
19
|
+ self.assertRaises(AttributeError, base.response, None)
|
|
20
|
+ self.assertRaises(AttributeError, base.response, [])
|
|
21
|
+ self.assertRaises(AttributeError, base.response, '')
|
|
22
|
+ self.assertRaises(AttributeError, base.response, '[]')
|
|
23
|
+
|
|
24
|
+ response = mock.Mock(text='<response></response>')
|
|
25
|
+ self.assertEqual(base.response(response), [])
|
|
26
|
+
|
|
27
|
+ xml_mock = """<?xml version="1.0"?>
|
|
28
|
+<response>
|
|
29
|
+ <lst name="responseHeader">
|
|
30
|
+ <int name="status">0</int>
|
|
31
|
+ <int name="QTime">1</int>
|
|
32
|
+ </lst>
|
|
33
|
+ <result name="response" numFound="1" start="0">
|
|
34
|
+ <doc>
|
|
35
|
+ <date name="dchdate">2000-01-01T01:01:01Z</date>
|
|
36
|
+ <str name="dcdocid">1</str>
|
|
37
|
+ <str name="dccontinent">cna</str>
|
|
38
|
+ <str name="dccountry">us</str>
|
|
39
|
+ <str name="dccollection">ftciteseerx</str>
|
|
40
|
+ <str name="dcprovider">CiteSeerX</str>
|
|
41
|
+ <str name="dctitle">Science and more</str>
|
|
42
|
+ <arr name="dccreator">
|
|
43
|
+ <str>Someone</str>
|
|
44
|
+ </arr>
|
|
45
|
+ <arr name="dcperson">
|
|
46
|
+ <str>Someone</str>
|
|
47
|
+ </arr>
|
|
48
|
+ <arr name="dcsubject">
|
|
49
|
+ <str>Science and more</str>
|
|
50
|
+ </arr>
|
|
51
|
+ <str name="dcdescription">Science, and even more.</str>
|
|
52
|
+ <arr name="dccontributor">
|
|
53
|
+ <str>The neighbour</str>
|
|
54
|
+ </arr>
|
|
55
|
+ <str name="dcdate">2001</str>
|
|
56
|
+ <int name="dcyear">2001</int>
|
|
57
|
+ <arr name="dctype">
|
|
58
|
+ <str>text</str>
|
|
59
|
+ </arr>
|
|
60
|
+ <arr name="dctypenorm">
|
|
61
|
+ <str>1</str>
|
|
62
|
+ </arr>
|
|
63
|
+ <arr name="dcformat">
|
|
64
|
+ <str>application/pdf</str>
|
|
65
|
+ </arr>
|
|
66
|
+ <arr name="dccontenttype">
|
|
67
|
+ <str>application/pdf</str>
|
|
68
|
+ </arr>
|
|
69
|
+ <arr name="dcidentifier">
|
|
70
|
+ <str>http://example.org/</str>
|
|
71
|
+ </arr>
|
|
72
|
+ <str name="dclink">http://example.org</str>
|
|
73
|
+ <str name="dcsource">http://example.org</str>
|
|
74
|
+ <arr name="dclanguage">
|
|
75
|
+ <str>en</str>
|
|
76
|
+ </arr>
|
|
77
|
+ <str name="dcrights">Under the example.org licence</str>
|
|
78
|
+ <int name="dcoa">1</int>
|
|
79
|
+ <arr name="dclang">
|
|
80
|
+ <str>eng</str>
|
|
81
|
+ </arr>
|
|
82
|
+ </doc>
|
|
83
|
+ </result>
|
|
84
|
+</response>"""
|
|
85
|
+
|
|
86
|
+ response = mock.Mock(text=xml_mock.encode('utf-8'))
|
|
87
|
+ results = base.response(response)
|
|
88
|
+ self.assertEqual(type(results), list)
|
|
89
|
+ self.assertEqual(len(results), 1)
|
|
90
|
+ self.assertEqual(results[0]['title'], 'Science and more')
|
|
91
|
+ self.assertEqual(results[0]['content'], 'Science, and even more.')
|