|  | @@ -0,0 +1,142 @@
 | 
	
		
			
			|  | 1 | +from collections import defaultdict
 | 
	
		
			
			|  | 2 | +import mock
 | 
	
		
			
			|  | 3 | +from searx.engines import flickr
 | 
	
		
			
			|  | 4 | +from searx.testing import SearxTestCase
 | 
	
		
			
			|  | 5 | +
 | 
	
		
			
			|  | 6 | +
 | 
	
		
			
			|  | 7 | +class TestFlickrEngine(SearxTestCase):
 | 
	
		
			
			|  | 8 | +
 | 
	
		
			
			|  | 9 | +    def test_request(self):
 | 
	
		
			
			|  | 10 | +        query = 'test_query'
 | 
	
		
			
			|  | 11 | +        dicto = defaultdict(dict)
 | 
	
		
			
			|  | 12 | +        dicto['pageno'] = 0
 | 
	
		
			
			|  | 13 | +        params = flickr.request(query, dicto)
 | 
	
		
			
			|  | 14 | +        self.assertTrue('url' in params)
 | 
	
		
			
			|  | 15 | +        self.assertTrue(query in params['url'])
 | 
	
		
			
			|  | 16 | +        self.assertTrue('flickr.com' in params['url'])
 | 
	
		
			
			|  | 17 | +
 | 
	
		
			
			|  | 18 | +    def test_response(self):
 | 
	
		
			
			|  | 19 | +        self.assertRaises(AttributeError, flickr.response, None)
 | 
	
		
			
			|  | 20 | +        self.assertRaises(AttributeError, flickr.response, [])
 | 
	
		
			
			|  | 21 | +        self.assertRaises(AttributeError, flickr.response, '')
 | 
	
		
			
			|  | 22 | +        self.assertRaises(AttributeError, flickr.response, '[]')
 | 
	
		
			
			|  | 23 | +
 | 
	
		
			
			|  | 24 | +        response = mock.Mock(text='{}')
 | 
	
		
			
			|  | 25 | +        self.assertEqual(flickr.response(response), [])
 | 
	
		
			
			|  | 26 | +
 | 
	
		
			
			|  | 27 | +        response = mock.Mock(text='{"data": []}')
 | 
	
		
			
			|  | 28 | +        self.assertEqual(flickr.response(response), [])
 | 
	
		
			
			|  | 29 | +
 | 
	
		
			
			|  | 30 | +        json = """
 | 
	
		
			
			|  | 31 | +        { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
 | 
	
		
			
			|  | 32 | +            "photo": [
 | 
	
		
			
			|  | 33 | +            { "id": "15751017054", "owner": "66847915@N08",
 | 
	
		
			
			|  | 34 | +            "secret": "69c22afc40", "server": "7285", "farm": 8,
 | 
	
		
			
			|  | 35 | +            "title": "Photo title", "ispublic": 1,
 | 
	
		
			
			|  | 36 | +            "isfriend": 0, "isfamily": 0,
 | 
	
		
			
			|  | 37 | +            "description": { "_content": "Description" },
 | 
	
		
			
			|  | 38 | +            "ownername": "Owner",
 | 
	
		
			
			|  | 39 | +            "url_o": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_9178e0f963_o.jpg",
 | 
	
		
			
			|  | 40 | +            "height_o": "2100", "width_o": "2653",
 | 
	
		
			
			|  | 41 | +            "url_n": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_n.jpg",
 | 
	
		
			
			|  | 42 | +            "height_n": "253", "width_n": "320",
 | 
	
		
			
			|  | 43 | +            "url_z": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_z.jpg",
 | 
	
		
			
			|  | 44 | +            "height_z": "507", "width_z": "640" }
 | 
	
		
			
			|  | 45 | +        ] }, "stat": "ok" }
 | 
	
		
			
			|  | 46 | +        """
 | 
	
		
			
			|  | 47 | +        response = mock.Mock(text=json)
 | 
	
		
			
			|  | 48 | +        results = flickr.response(response)
 | 
	
		
			
			|  | 49 | +        self.assertEqual(type(results), list)
 | 
	
		
			
			|  | 50 | +        self.assertEqual(len(results), 1)
 | 
	
		
			
			|  | 51 | +        self.assertEqual(results[0]['title'], 'Photo title')
 | 
	
		
			
			|  | 52 | +        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
 | 
	
		
			
			|  | 53 | +        self.assertTrue('o.jpg' in results[0]['img_src'])
 | 
	
		
			
			|  | 54 | +        self.assertTrue('n.jpg' in results[0]['thumbnail_src'])
 | 
	
		
			
			|  | 55 | +        self.assertTrue('Owner' in results[0]['content'])
 | 
	
		
			
			|  | 56 | +        self.assertTrue('Description' in results[0]['content'])
 | 
	
		
			
			|  | 57 | +
 | 
	
		
			
			|  | 58 | +        json = """
 | 
	
		
			
			|  | 59 | +        { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
 | 
	
		
			
			|  | 60 | +            "photo": [
 | 
	
		
			
			|  | 61 | +            { "id": "15751017054", "owner": "66847915@N08",
 | 
	
		
			
			|  | 62 | +            "secret": "69c22afc40", "server": "7285", "farm": 8,
 | 
	
		
			
			|  | 63 | +            "title": "Photo title", "ispublic": 1,
 | 
	
		
			
			|  | 64 | +            "isfriend": 0, "isfamily": 0,
 | 
	
		
			
			|  | 65 | +            "description": { "_content": "Description" },
 | 
	
		
			
			|  | 66 | +            "ownername": "Owner",
 | 
	
		
			
			|  | 67 | +            "url_z": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_z.jpg",
 | 
	
		
			
			|  | 68 | +            "height_z": "507", "width_z": "640" }
 | 
	
		
			
			|  | 69 | +        ] }, "stat": "ok" }
 | 
	
		
			
			|  | 70 | +        """
 | 
	
		
			
			|  | 71 | +        response = mock.Mock(text=json)
 | 
	
		
			
			|  | 72 | +        results = flickr.response(response)
 | 
	
		
			
			|  | 73 | +        self.assertEqual(type(results), list)
 | 
	
		
			
			|  | 74 | +        self.assertEqual(len(results), 1)
 | 
	
		
			
			|  | 75 | +        self.assertEqual(results[0]['title'], 'Photo title')
 | 
	
		
			
			|  | 76 | +        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
 | 
	
		
			
			|  | 77 | +        self.assertTrue('z.jpg' in results[0]['img_src'])
 | 
	
		
			
			|  | 78 | +        self.assertTrue('z.jpg' in results[0]['thumbnail_src'])
 | 
	
		
			
			|  | 79 | +        self.assertTrue('Owner' in results[0]['content'])
 | 
	
		
			
			|  | 80 | +        self.assertTrue('Description' in results[0]['content'])
 | 
	
		
			
			|  | 81 | +
 | 
	
		
			
			|  | 82 | +        json = """
 | 
	
		
			
			|  | 83 | +        { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
 | 
	
		
			
			|  | 84 | +            "photo": [
 | 
	
		
			
			|  | 85 | +            { "id": "15751017054", "owner": "66847915@N08",
 | 
	
		
			
			|  | 86 | +            "secret": "69c22afc40", "server": "7285", "farm": 8,
 | 
	
		
			
			|  | 87 | +            "title": "Photo title", "ispublic": 1,
 | 
	
		
			
			|  | 88 | +            "isfriend": 0, "isfamily": 0,
 | 
	
		
			
			|  | 89 | +            "description": { "_content": "Description" },
 | 
	
		
			
			|  | 90 | +            "ownername": "Owner",
 | 
	
		
			
			|  | 91 | +            "url_o": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_9178e0f963_o.jpg",
 | 
	
		
			
			|  | 92 | +            "height_o": "2100", "width_o": "2653" }
 | 
	
		
			
			|  | 93 | +        ] }, "stat": "ok" }
 | 
	
		
			
			|  | 94 | +        """
 | 
	
		
			
			|  | 95 | +        response = mock.Mock(text=json)
 | 
	
		
			
			|  | 96 | +        results = flickr.response(response)
 | 
	
		
			
			|  | 97 | +        self.assertEqual(type(results), list)
 | 
	
		
			
			|  | 98 | +        self.assertEqual(len(results), 1)
 | 
	
		
			
			|  | 99 | +        self.assertEqual(results[0]['title'], 'Photo title')
 | 
	
		
			
			|  | 100 | +        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
 | 
	
		
			
			|  | 101 | +        self.assertTrue('o.jpg' in results[0]['img_src'])
 | 
	
		
			
			|  | 102 | +        self.assertTrue('o.jpg' in results[0]['thumbnail_src'])
 | 
	
		
			
			|  | 103 | +        self.assertTrue('Owner' in results[0]['content'])
 | 
	
		
			
			|  | 104 | +        self.assertTrue('Description' in results[0]['content'])
 | 
	
		
			
			|  | 105 | +
 | 
	
		
			
			|  | 106 | +        json = """
 | 
	
		
			
			|  | 107 | +        { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
 | 
	
		
			
			|  | 108 | +            "photo": [
 | 
	
		
			
			|  | 109 | +            { "id": "15751017054", "owner": "66847915@N08",
 | 
	
		
			
			|  | 110 | +            "secret": "69c22afc40", "server": "7285", "farm": 8,
 | 
	
		
			
			|  | 111 | +            "title": "Photo title", "ispublic": 1,
 | 
	
		
			
			|  | 112 | +            "isfriend": 0, "isfamily": 0,
 | 
	
		
			
			|  | 113 | +            "description": { "_content": "Description" },
 | 
	
		
			
			|  | 114 | +            "ownername": "Owner",
 | 
	
		
			
			|  | 115 | +            "url_n": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_n.jpg",
 | 
	
		
			
			|  | 116 | +            "height_n": "253", "width_n": "320" }
 | 
	
		
			
			|  | 117 | +        ] }, "stat": "ok" }
 | 
	
		
			
			|  | 118 | +        """
 | 
	
		
			
			|  | 119 | +        response = mock.Mock(text=json)
 | 
	
		
			
			|  | 120 | +        results = flickr.response(response)
 | 
	
		
			
			|  | 121 | +        self.assertEqual(type(results), list)
 | 
	
		
			
			|  | 122 | +        self.assertEqual(len(results), 0)
 | 
	
		
			
			|  | 123 | +
 | 
	
		
			
			|  | 124 | +        json = """
 | 
	
		
			
			|  | 125 | +        { "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
 | 
	
		
			
			|  | 126 | +            "toto": [] }, "stat": "ok" }
 | 
	
		
			
			|  | 127 | +        """
 | 
	
		
			
			|  | 128 | +        response = mock.Mock(text=json)
 | 
	
		
			
			|  | 129 | +        results = flickr.response(response)
 | 
	
		
			
			|  | 130 | +        self.assertEqual(type(results), list)
 | 
	
		
			
			|  | 131 | +        self.assertEqual(len(results), 0)
 | 
	
		
			
			|  | 132 | +
 | 
	
		
			
			|  | 133 | +        json = """
 | 
	
		
			
			|  | 134 | +        {"toto":[
 | 
	
		
			
			|  | 135 | +            {"id":200,"name":"Artist Name",
 | 
	
		
			
			|  | 136 | +            "link":"http:\/\/www.flickr.com\/artist\/1217","type":"artist"}
 | 
	
		
			
			|  | 137 | +        ]}
 | 
	
		
			
			|  | 138 | +        """
 | 
	
		
			
			|  | 139 | +        response = mock.Mock(text=json)
 | 
	
		
			
			|  | 140 | +        results = flickr.response(response)
 | 
	
		
			
			|  | 141 | +        self.assertEqual(type(results), list)
 | 
	
		
			
			|  | 142 | +        self.assertEqual(len(results), 0)
 |