|  | @@ -0,0 +1,104 @@
 | 
	
		
			
			|  | 1 | +from collections import defaultdict
 | 
	
		
			
			|  | 2 | +import mock
 | 
	
		
			
			|  | 3 | +from searx.engines import swisscows
 | 
	
		
			
			|  | 4 | +from searx.testing import SearxTestCase
 | 
	
		
			
			|  | 5 | +
 | 
	
		
			
			|  | 6 | +
 | 
	
		
			
			|  | 7 | +class TestSwisscowsEngine(SearxTestCase):
 | 
	
		
			
			|  | 8 | +
 | 
	
		
			
			|  | 9 | +    def test_request(self):
 | 
	
		
			
			|  | 10 | +        query = 'test_query'
 | 
	
		
			
			|  | 11 | +        dicto = defaultdict(dict)
 | 
	
		
			
			|  | 12 | +        dicto['pageno'] = 1
 | 
	
		
			
			|  | 13 | +        dicto['language'] = 'de_DE'
 | 
	
		
			
			|  | 14 | +        params = swisscows.request(query, dicto)
 | 
	
		
			
			|  | 15 | +        self.assertTrue('url' in params)
 | 
	
		
			
			|  | 16 | +        self.assertTrue(query in params['url'])
 | 
	
		
			
			|  | 17 | +        self.assertTrue('swisscows.ch' in params['url'])
 | 
	
		
			
			|  | 18 | +        self.assertTrue('uiLanguage=de' in params['url'])
 | 
	
		
			
			|  | 19 | +        self.assertTrue('region=de-DE' in params['url'])
 | 
	
		
			
			|  | 20 | +
 | 
	
		
			
			|  | 21 | +        dicto['language'] = 'all'
 | 
	
		
			
			|  | 22 | +        params = swisscows.request(query, dicto)
 | 
	
		
			
			|  | 23 | +        self.assertTrue('uiLanguage=browser' in params['url'])
 | 
	
		
			
			|  | 24 | +        self.assertTrue('region=browser' in params['url'])
 | 
	
		
			
			|  | 25 | +
 | 
	
		
			
			|  | 26 | +    def test_response(self):
 | 
	
		
			
			|  | 27 | +        self.assertRaises(AttributeError, swisscows.response, None)
 | 
	
		
			
			|  | 28 | +        self.assertRaises(AttributeError, swisscows.response, [])
 | 
	
		
			
			|  | 29 | +        self.assertRaises(AttributeError, swisscows.response, '')
 | 
	
		
			
			|  | 30 | +        self.assertRaises(AttributeError, swisscows.response, '[]')
 | 
	
		
			
			|  | 31 | +
 | 
	
		
			
			|  | 32 | +        response = mock.Mock(content='<html></html>')
 | 
	
		
			
			|  | 33 | +        self.assertEqual(swisscows.response(response), [])
 | 
	
		
			
			|  | 34 | +
 | 
	
		
			
			|  | 35 | +        response = mock.Mock(content='<html></html>')
 | 
	
		
			
			|  | 36 | +        self.assertEqual(swisscows.response(response), [])
 | 
	
		
			
			|  | 37 | +
 | 
	
		
			
			|  | 38 | +        html = u"""
 | 
	
		
			
			|  | 39 | +        <script>
 | 
	
		
			
			|  | 40 | +            App.Dispatcher.dispatch("initialize", {
 | 
	
		
			
			|  | 41 | +                html5history: true,
 | 
	
		
			
			|  | 42 | +                initialData: {"Request":
 | 
	
		
			
			|  | 43 | +                    {"Page":1,
 | 
	
		
			
			|  | 44 | +                    "ItemsCount":1,
 | 
	
		
			
			|  | 45 | +                    "Query":"This should ",
 | 
	
		
			
			|  | 46 | +                    "NormalizedQuery":"This should ",
 | 
	
		
			
			|  | 47 | +                    "Region":"de-AT",
 | 
	
		
			
			|  | 48 | +                    "UILanguage":"de"},
 | 
	
		
			
			|  | 49 | +                    "Results":{"items":[
 | 
	
		
			
			|  | 50 | +                            {"Title":"\uE000This should\uE001 be the title",
 | 
	
		
			
			|  | 51 | +                            "Description":"\uE000This should\uE001 be the content.",
 | 
	
		
			
			|  | 52 | +                            "Url":"http://this.should.be.the.link/",
 | 
	
		
			
			|  | 53 | +                            "DisplayUrl":"www.\uE000this.should.be.the\uE001.link",
 | 
	
		
			
			|  | 54 | +                            "Id":"782ef287-e439-451c-b380-6ebc14ba033d"}
 | 
	
		
			
			|  | 55 | +                        ],"TotalCount":55300,
 | 
	
		
			
			|  | 56 | +                        "Query":"This should "
 | 
	
		
			
			|  | 57 | +                    },
 | 
	
		
			
			|  | 58 | +                    "Images":[{"Title":"Datei:This should.svg",
 | 
	
		
			
			|  | 59 | +                        "Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should.png",
 | 
	
		
			
			|  | 60 | +                        "SourceUrl":"http://de.wikipedia.org/wiki/Datei:This should.svg",
 | 
	
		
			
			|  | 61 | +                        "DisplayUrl":"de.wikipedia.org/wiki/Datei:This should.svg",
 | 
	
		
			
			|  | 62 | +                        "Width":1280,
 | 
	
		
			
			|  | 63 | +                        "Height":677,
 | 
	
		
			
			|  | 64 | +                        "FileSize":50053,
 | 
	
		
			
			|  | 65 | +                        "ContentType":"image/png",
 | 
	
		
			
			|  | 66 | +                        "Thumbnail":{"Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should.png",
 | 
	
		
			
			|  | 67 | +                            "ContentType":"image/png",
 | 
	
		
			
			|  | 68 | +                            "Width":300,
 | 
	
		
			
			|  | 69 | +                            "Height":158,
 | 
	
		
			
			|  | 70 | +                            "FileSize":8023},
 | 
	
		
			
			|  | 71 | +                        "Id":"ae230fd8-a06a-47d6-99d5-e74766d8143a"}]},
 | 
	
		
			
			|  | 72 | +                environment: "production"
 | 
	
		
			
			|  | 73 | +            }).then(function (options) {
 | 
	
		
			
			|  | 74 | +                $('#Search_Form').on('submit', function (e) {
 | 
	
		
			
			|  | 75 | +                    if (!Modernizr.history) return;
 | 
	
		
			
			|  | 76 | +                    e.preventDefault();
 | 
	
		
			
			|  | 77 | +
 | 
	
		
			
			|  | 78 | +                    var $form = $(this),
 | 
	
		
			
			|  | 79 | +                        $query = $('#Query'),
 | 
	
		
			
			|  | 80 | +                        query = $.trim($query.val()),
 | 
	
		
			
			|  | 81 | +                        path = App.Router.makePath($form.attr('action'), null, $form.serializeObject())
 | 
	
		
			
			|  | 82 | +
 | 
	
		
			
			|  | 83 | +                    if (query.length) {
 | 
	
		
			
			|  | 84 | +                        options.html5history ?
 | 
	
		
			
			|  | 85 | +                            ReactRouter.HistoryLocation.push(path) :
 | 
	
		
			
			|  | 86 | +                            ReactRouter.RefreshLocation.push(path);
 | 
	
		
			
			|  | 87 | +                    }
 | 
	
		
			
			|  | 88 | +                    else $('#Query').trigger('blur');
 | 
	
		
			
			|  | 89 | +                });
 | 
	
		
			
			|  | 90 | +
 | 
	
		
			
			|  | 91 | +            });
 | 
	
		
			
			|  | 92 | +        </script>
 | 
	
		
			
			|  | 93 | +        """
 | 
	
		
			
			|  | 94 | +        response = mock.Mock(content=html)
 | 
	
		
			
			|  | 95 | +        results = swisscows.response(response)
 | 
	
		
			
			|  | 96 | +        self.assertEqual(type(results), list)
 | 
	
		
			
			|  | 97 | +        self.assertEqual(len(results), 2)
 | 
	
		
			
			|  | 98 | +        self.assertEqual(results[0]['title'], 'This should be the title')
 | 
	
		
			
			|  | 99 | +        self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
 | 
	
		
			
			|  | 100 | +        self.assertEqual(results[0]['content'], 'This should be the content.')
 | 
	
		
			
			|  | 101 | +        self.assertEqual(results[1]['title'], 'Datei:This should.svg')
 | 
	
		
			
			|  | 102 | +        self.assertEqual(results[1]['url'], 'http://de.wikipedia.org/wiki/Datei:This should.svg')
 | 
	
		
			
			|  | 103 | +        self.assertEqual(results[1]['img_src'], 'http://ts2.mm.This/should.png')
 | 
	
		
			
			|  | 104 | +        self.assertEqual(results[1]['template'], 'images.html')
 |