Browse Source

Digg's unit test

Cqoicebordel 10 years ago
parent
commit
1d255061c7
3 changed files with 59 additions and 1 deletions
  1. 1
    1
      searx/engines/digg.py
  2. 57
    0
      searx/tests/engines/test_digg.py
  3. 1
    0
      searx/tests/test_engines.py

+ 1
- 1
searx/engines/digg.py View File

@@ -44,7 +44,7 @@ def response(resp):
44 44
 
45 45
     search_result = loads(resp.text)
46 46
 
47
-    if search_result['html'] == '':
47
+    if 'html' not in search_result or search_result['html'] == '':
48 48
         return results
49 49
 
50 50
     dom = html.fromstring(search_result['html'])

+ 57
- 0
searx/tests/engines/test_digg.py View File

@@ -0,0 +1,57 @@
1
+from collections import defaultdict
2
+import mock
3
+from searx.engines import digg
4
+from searx.testing import SearxTestCase
5
+
6
+
7
+class TestDiggEngine(SearxTestCase):
8
+
9
+    def test_request(self):
10
+        query = 'test_query'
11
+        dicto = defaultdict(dict)
12
+        dicto['pageno'] = 1
13
+        params = digg.request(query, dicto)
14
+        self.assertIn('url', params)
15
+        self.assertIn(query, params['url'])
16
+        self.assertIn('digg.com', params['url'])
17
+
18
+    def test_response(self):
19
+        self.assertRaises(AttributeError, digg.response, None)
20
+        self.assertRaises(AttributeError, digg.response, [])
21
+        self.assertRaises(AttributeError, digg.response, '')
22
+        self.assertRaises(AttributeError, digg.response, '[]')
23
+
24
+        response = mock.Mock(text='{}')
25
+        self.assertEqual(digg.response(response), [])
26
+
27
+        response = mock.Mock(text='{"data": []}')
28
+        self.assertEqual(digg.response(response), [])
29
+
30
+        json = """
31
+        {
32
+        "status": "ok",
33
+        "num": 10,
34
+        "next_position": 20,
35
+        "html": "<article itemscope itemtype=\\"http://schema.org/Article\\" class=\\"story-container digg-story-el hentry entry story-1sRANah col-1\\" data-content-id=\\"1sRANah\\" data-contenturl=\\"http://url.of.link\\" data-position=\\"0\\" data-diggs=\\"24\\" data-tweets=\\"69\\" data-digg-score=\\"1190\\"> <div class=\\"story-image story-image-thumb\\"> <a data-position=\\"0\\" data-content-id=\\"1sRANah\\" class=\\"story-link\\" href=\\"http://www.thedailybeast.com/\\" target=\\"_blank\\"><img class=\\"story-image-img\\" src=\\"http://url.of.image.jpeg\\" width=\\"312\\" height=\\"170\\" alt=\\"\\" /> </a> </div> <div class=\\"story-content\\"><header class=\\"story-header\\"> <div itemprop=\\"alternativeHeadline\\" class=\\"story-kicker\\" >Kicker</div> <h2 itemprop=\\"headline\\" class=\\"story-title entry-title\\"><a class=\\"story-title-link story-link\\" rel=\\"bookmark\\" itemprop=\\"url\\" href=\\"http://www.thedailybeast.com/\\" target=\\"_blank\\">Title of article</h2> <div class=\\"story-meta\\"><div class=\\"story-score \\"> <div class=\\"story-score-diggscore diggscore-1sRANah\\">1190</div> <div class=\\"story-score-details\\"> <div class=\\"arrow\\"></div><ul class=\\"story-score-details-list\\"> <li class=\\"story-score-detail story-score-diggs\\"><span class=\\"label\\">Diggs:</span> <span class=\\"count diggs-1sRANah\\">24</span></li> <li class=\\"story-score-detail story-score-twitter\\"><span class=\\"label\\">Tweets:</span> <span class=\\"count tweets-1sRANah\\">69</span></li> <li class=\\"story-score-detail story-score-facebook\\"><span class=\\"label\\">Facebook Shares:</span> <span class=\\"count fb_shares-1sRANah\\">1097</span></li> </ul> </div> </div> <span class=\\"story-meta-item story-source\\"> <a itemprop=\\"publisher copyrightHolder sourceOrganization provider\\" class=\\"story-meta-item-link story-source-link\\" href=\\"/source/thedailybeast.com\\">The Daily Beast </a> </span> <span class=\\"story-meta-item story-tag first-tag\\"> <a itemprop=\\"keywords\\" rel=\\"tag\\" class=\\"story-meta-item-link story-tag-link\\" href=\\"/tag/news\\">News</a> </span> <abbr class=\\"published story-meta-item story-timestamp\\" title=\\"2014-10-18 14:53:45\\"> <time datetime=\\"2014-10-18 14:53:45\\">18 Oct 2014</time> </abbr> </div> </header> </div> <ul class=\\"story-actions\\"> <li class=\\"story-action story-action-digg btn-story-action-container\\"><a class=\\"target digg-1sRANah\\" href=\\"#\\">Digg</a></li> <li class=\\"story-action story-action-save btn-story-action-container\\"><a class=\\"target save-1sRANah\\" href=\\"#\\">Save</a></li> <li class=\\"story-action story-action-share\\"><a class=\\"target share-facebook\\" href=\\"https://www.facebook.com/\\">Facebook</a></li> <li class=\\"story-action story-action-share\\"><a class=\\"target share-twitter\\" href=\\"https://twitter.com/\\">Twitter</a></li> </ul> </article>"
36
+        }
37
+        """
38
+        response = mock.Mock(text=json)
39
+        results = digg.response(response)
40
+        self.assertEqual(type(results), list)
41
+        self.assertEqual(len(results), 1)
42
+        self.assertEqual(results[0]['title'], 'Title of article')
43
+        self.assertEqual(results[0]['url'], 'http://url.of.link')
44
+        self.assertEqual(results[0]['thumbnail'], 'http://url.of.image.jpeg')
45
+        self.assertEqual(results[0]['content'], '')
46
+
47
+        json = """
48
+        {
49
+        "status": "error",
50
+        "num": 10,
51
+        "next_position": 20
52
+        }
53
+        """
54
+        response = mock.Mock(text=json)
55
+        results = digg.response(response)
56
+        self.assertEqual(type(results), list)
57
+        self.assertEqual(len(results), 0)

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

@@ -1,6 +1,7 @@
1 1
 from searx.tests.engines.test_bing import *  # noqa
2 2
 from searx.tests.engines.test_dailymotion import *  # noqa
3 3
 from searx.tests.engines.test_deezer import *  # noqa
4
+from searx.tests.engines.test_digg import *  # noqa
4 5
 from searx.tests.engines.test_dummy import *  # noqa
5 6
 from searx.tests.engines.test_flickr import *  # noqa
6 7
 from searx.tests.engines.test_github import *  # noqa