test_genius.py 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. from collections import defaultdict
  2. import mock
  3. from datetime import datetime
  4. from searx.engines import genius
  5. from searx.testing import SearxTestCase
  6. class TestGeniusEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. params = genius.request(query, dicto)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertTrue('genius.com' in params['url'])
  15. def test_response(self):
  16. json_empty = """
  17. {
  18. "meta": {
  19. "status": 200
  20. },
  21. "response": {
  22. "sections": [
  23. {
  24. "type": "top_hit",
  25. "hits": []
  26. },
  27. {
  28. "type": "song",
  29. "hits": []
  30. },
  31. {
  32. "type": "lyric",
  33. "hits": []
  34. },
  35. {
  36. "type": "artist",
  37. "hits": []
  38. },
  39. {
  40. "type": "album",
  41. "hits": []
  42. },
  43. {
  44. "type": "tag",
  45. "hits": []
  46. },
  47. {
  48. "type": "video",
  49. "hits": []
  50. },
  51. {
  52. "type": "article",
  53. "hits": []
  54. },
  55. {
  56. "type": "user",
  57. "hits": []
  58. }
  59. ]
  60. }
  61. }
  62. """
  63. resp = mock.Mock(text=json_empty)
  64. self.assertEqual(genius.response(resp), [])
  65. json = """
  66. {
  67. "meta": {
  68. "status": 200
  69. },
  70. "response": {
  71. "sections": [
  72. {
  73. "type": "lyric",
  74. "hits": [
  75. {
  76. "highlights": [
  77. {
  78. "property": "lyrics",
  79. "value": "Sample lyrics",
  80. "snippet": true,
  81. "ranges": []
  82. }
  83. ],
  84. "index": "lyric",
  85. "type": "song",
  86. "result": {
  87. "_type": "song",
  88. "annotation_count": 45,
  89. "api_path": "/songs/52916",
  90. "full_title": "J't'emmerde by MC Jean Gab'1",
  91. "header_image_thumbnail_url": "https://images.genius.com/xxx.300x300x1.jpg",
  92. "header_image_url": "https://images.genius.com/ef9f736a86df3c3b1772f3fb7fbdb21c.1000x1000x1.jpg",
  93. "id": 52916,
  94. "instrumental": false,
  95. "lyrics_owner_id": 15586,
  96. "lyrics_state": "complete",
  97. "lyrics_updated_at": 1498744545,
  98. "path": "/Mc-jean-gab1-jtemmerde-lyrics",
  99. "pyongs_count": 4,
  100. "song_art_image_thumbnail_url": "https://images.genius.com/xxx.300x300x1.jpg",
  101. "stats": {
  102. "hot": false,
  103. "unreviewed_annotations": 0,
  104. "pageviews": 62490
  105. },
  106. "title": "J't'emmerde",
  107. "title_with_featured": "J't'emmerde",
  108. "updated_by_human_at": 1498744546,
  109. "url": "https://genius.com/Mc-jean-gab1-jtemmerde-lyrics",
  110. "primary_artist": {
  111. "_type": "artist",
  112. "api_path": "/artists/12691",
  113. "header_image_url": "https://images.genius.com/c7847662a58f8c2b0f02a6e217d60907.960x657x1.jpg",
  114. "id": 12691,
  115. "image_url": "https://s3.amazonaws.com/rapgenius/Mc-jean-gab1.jpg",
  116. "index_character": "m",
  117. "is_meme_verified": false,
  118. "is_verified": false,
  119. "name": "MC Jean Gab'1",
  120. "slug": "Mc-jean-gab1",
  121. "url": "https://genius.com/artists/Mc-jean-gab1"
  122. }
  123. }
  124. }
  125. ]
  126. },
  127. {
  128. "type": "artist",
  129. "hits": [
  130. {
  131. "highlights": [],
  132. "index": "artist",
  133. "type": "artist",
  134. "result": {
  135. "_type": "artist",
  136. "api_path": "/artists/191580",
  137. "header_image_url": "https://assets.genius.com/images/default_avatar_300.png?1503090542",
  138. "id": 191580,
  139. "image_url": "https://assets.genius.com/images/default_avatar_300.png?1503090542",
  140. "index_character": "a",
  141. "is_meme_verified": false,
  142. "is_verified": false,
  143. "name": "ASDF Guy",
  144. "slug": "Asdf-guy",
  145. "url": "https://genius.com/artists/Asdf-guy"
  146. }
  147. }
  148. ]
  149. },
  150. {
  151. "type": "album",
  152. "hits": [
  153. {
  154. "highlights": [],
  155. "index": "album",
  156. "type": "album",
  157. "result": {
  158. "_type": "album",
  159. "api_path": "/albums/132332",
  160. "cover_art_thumbnail_url": "https://images.genius.com/xxx.300x300x1.jpg",
  161. "cover_art_url": "https://images.genius.com/xxx.600x600x1.jpg",
  162. "full_title": "ASD by A Skylit Drive",
  163. "id": 132332,
  164. "name": "ASD",
  165. "name_with_artist": "ASD (artist: A Skylit Drive)",
  166. "release_date_components": {
  167. "year": 2015,
  168. "month": null,
  169. "day": null
  170. },
  171. "url": "https://genius.com/albums/A-skylit-drive/Asd",
  172. "artist": {
  173. "_type": "artist",
  174. "api_path": "/artists/48712",
  175. "header_image_url": "https://images.genius.com/814c1551293172c56306d0e310c6aa89.620x400x1.jpg",
  176. "id": 48712,
  177. "image_url": "https://images.genius.com/814c1551293172c56306d0e310c6aa89.620x400x1.jpg",
  178. "index_character": "s",
  179. "is_meme_verified": false,
  180. "is_verified": false,
  181. "name": "A Skylit Drive",
  182. "slug": "A-skylit-drive",
  183. "url": "https://genius.com/artists/A-skylit-drive"
  184. }
  185. }
  186. }
  187. ]
  188. }
  189. ]
  190. }
  191. }
  192. """
  193. resp = mock.Mock(text=json)
  194. results = genius.response(resp)
  195. self.assertEqual(len(results), 3)
  196. self.assertEqual(type(results), list)
  197. # check lyric parsing
  198. r = results[0]
  199. self.assertEqual(r['url'], 'https://genius.com/Mc-jean-gab1-jtemmerde-lyrics')
  200. self.assertEqual(r['title'], "J't'emmerde by MC Jean Gab'1")
  201. self.assertEqual(r['content'], "Sample lyrics")
  202. self.assertEqual(r['template'], 'videos.html')
  203. self.assertEqual(r['thumbnail'], 'https://images.genius.com/xxx.300x300x1.jpg')
  204. created = datetime.fromtimestamp(1498744545)
  205. self.assertEqual(r['publishedDate'], created)
  206. # check artist parsing
  207. r = results[1]
  208. self.assertEqual(r['url'], 'https://genius.com/artists/Asdf-guy')
  209. self.assertEqual(r['title'], "ASDF Guy")
  210. self.assertEqual(r['content'], None)
  211. self.assertEqual(r['template'], 'videos.html')
  212. self.assertEqual(r['thumbnail'], 'https://assets.genius.com/images/default_avatar_300.png?1503090542')
  213. # check album parsing
  214. r = results[2]
  215. self.assertEqual(r['url'], 'https://genius.com/albums/A-skylit-drive/Asd')
  216. self.assertEqual(r['title'], "ASD by A Skylit Drive")
  217. self.assertEqual(r['content'], "Released: 2015")
  218. self.assertEqual(r['template'], 'videos.html')
  219. self.assertEqual(r['thumbnail'], 'https://images.genius.com/xxx.600x600x1.jpg')