Browse Source

Searchcode code's test unit

Cqoicebordel 10 years ago
parent
commit
eca5de73a7
2 changed files with 76 additions and 0 deletions
  1. 75
    0
      searx/tests/engines/test_searchcode_code.py
  2. 1
    0
      searx/tests/test_engines.py

+ 75
- 0
searx/tests/engines/test_searchcode_code.py View File

@@ -0,0 +1,75 @@
1
+from collections import defaultdict
2
+import mock
3
+from searx.engines import searchcode_code
4
+from searx.testing import SearxTestCase
5
+
6
+
7
+class TestSearchcodeCodeEngine(SearxTestCase):
8
+
9
+    def test_request(self):
10
+        query = 'test_query'
11
+        dicto = defaultdict(dict)
12
+        dicto['pageno'] = 0
13
+        params = searchcode_code.request(query, dicto)
14
+        self.assertIn('url', params)
15
+        self.assertIn(query, params['url'])
16
+        self.assertIn('searchcode.com', params['url'])
17
+
18
+    def test_response(self):
19
+        self.assertRaises(AttributeError, searchcode_code.response, None)
20
+        self.assertRaises(AttributeError, searchcode_code.response, [])
21
+        self.assertRaises(AttributeError, searchcode_code.response, '')
22
+        self.assertRaises(AttributeError, searchcode_code.response, '[]')
23
+
24
+        response = mock.Mock(text='{}')
25
+        self.assertEqual(searchcode_code.response(response), [])
26
+
27
+        response = mock.Mock(text='{"data": []}')
28
+        self.assertEqual(searchcode_code.response(response), [])
29
+
30
+        json = """
31
+        {
32
+        "matchterm": "test",
33
+        "previouspage": null,
34
+        "searchterm": "test",
35
+        "query": "test",
36
+        "total": 1000,
37
+        "page": 0,
38
+        "nextpage": 1,
39
+        "results": [
40
+            {
41
+            "repo": "https://repo",
42
+            "linescount": 1044,
43
+            "location": "/tests",
44
+            "name": "Name",
45
+            "url": "https://url",
46
+            "md5hash": "ecac6e479edd2b9406c9e08603cec655",
47
+            "lines": {
48
+                "1": "// Test 011",
49
+                "2": "// Source: "
50
+            },
51
+            "id": 51223527,
52
+            "filename": "File.CPP"
53
+            }
54
+        ]
55
+        }
56
+        """
57
+        response = mock.Mock(text=json)
58
+        results = searchcode_code.response(response)
59
+        self.assertEqual(type(results), list)
60
+        self.assertEqual(len(results), 1)
61
+        self.assertEqual(results[0]['title'], 'Name - File.CPP')
62
+        self.assertEqual(results[0]['url'], 'https://url')
63
+        self.assertEqual(results[0]['repository'], 'https://repo')
64
+        self.assertEqual(results[0]['code_language'], 'cpp')
65
+
66
+        json = """
67
+        {"toto":[
68
+            {"id":200,"name":"Artist Name",
69
+            "link":"http:\/\/www.searchcode_code.com\/artist\/1217","type":"artist"}
70
+        ]}
71
+        """
72
+        response = mock.Mock(text=json)
73
+        results = searchcode_code.response(response)
74
+        self.assertEqual(type(results), list)
75
+        self.assertEqual(len(results), 0)

+ 1
- 0
searx/tests/test_engines.py View File

@@ -4,4 +4,5 @@ from searx.tests.engines.test_dummy import *  # noqa
4 4
 from searx.tests.engines.test_flickr import *  # noqa
5 5
 from searx.tests.engines.test_github import *  # noqa
6 6
 from searx.tests.engines.test_mixcloud import *  # noqa
7
+from searx.tests.engines.test_searchcode_code import *  # noqa
7 8
 from searx.tests.engines.test_youtube import *  # noqa