test_search.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import pytest
  2. from tests import TestSearchResult
  3. from yandex_music import Search, SearchResult
  4. @pytest.fixture(scope='class')
  5. def search_result(results, types):
  6. def make_search_result(param):
  7. return SearchResult(
  8. types[param], TestSearchResult.total, TestSearchResult.per_page, TestSearchResult.order, [results[param]]
  9. )
  10. return make_search_result
  11. @pytest.fixture(scope='class')
  12. def search(best, search_result):
  13. return Search(
  14. TestSearch.search_request_id,
  15. TestSearch.text,
  16. best,
  17. search_result(3),
  18. search_result(2),
  19. search_result(4),
  20. search_result(1),
  21. search_result(5),
  22. search_result(13),
  23. search_result(14),
  24. search_result(15),
  25. TestSearch.type_,
  26. TestSearch.page,
  27. TestSearch.per_page,
  28. TestSearch.misspell_result,
  29. TestSearch.misspell_original,
  30. TestSearch.misspell_corrected,
  31. TestSearch.nocorrect,
  32. )
  33. class TestSearch:
  34. search_request_id = (
  35. 'myt1-0261-c2e-msk-myt-music-st-e72-18274.gencfg-c.yandex.net-1573323135801461'
  36. '-3742331365077765411-1573323135819 '
  37. )
  38. text = 'NCS'
  39. type_ = 'artist'
  40. page = 0
  41. per_page = 10
  42. misspell_result = 'era ameno'
  43. misspell_original = 'ero amen'
  44. misspell_corrected = False
  45. nocorrect = False
  46. def test_expected_values(self, search, best, search_result):
  47. assert search.search_request_id == self.search_request_id
  48. assert search.text == self.text
  49. assert search.best == best
  50. assert search.albums == search_result(3)
  51. assert search.artists == search_result(2)
  52. assert search.playlists == search_result(4)
  53. assert search.tracks == search_result(1)
  54. assert search.videos == search_result(5)
  55. assert search.users == search_result(13)
  56. assert search.podcasts == search_result(14)
  57. assert search.podcast_episodes == search_result(15)
  58. assert search.type == self.type_
  59. assert search.page == self.page
  60. assert search.per_page == self.per_page
  61. assert search.misspell_result == self.misspell_result
  62. assert search.misspell_original == self.misspell_original
  63. assert search.misspell_corrected == self.misspell_corrected
  64. assert search.nocorrect == self.nocorrect
  65. def test_de_json_none(self, client):
  66. assert Search.de_json({}, client) is None
  67. def test_de_json_required(self, client, best, search_result):
  68. json_dict = {
  69. 'search_request_id': self.search_request_id,
  70. 'text': self.text,
  71. 'best': best.to_dict(),
  72. 'albums': search_result(3).to_dict(),
  73. 'artists': search_result(2).to_dict(),
  74. 'playlists': search_result(4).to_dict(),
  75. 'tracks': search_result(1).to_dict(),
  76. 'videos': search_result(5).to_dict(),
  77. 'users': search_result(13).to_dict(),
  78. 'podcasts': search_result(14).to_dict(),
  79. 'podcast_episodes': search_result(15).to_dict(),
  80. }
  81. search = Search.de_json(json_dict, client)
  82. assert search.search_request_id == self.search_request_id
  83. assert search.text == self.text
  84. assert search.best == best
  85. assert search.albums == search_result(3)
  86. assert search.artists == search_result(2)
  87. assert search.playlists == search_result(4)
  88. assert search.tracks == search_result(1)
  89. assert search.videos == search_result(5)
  90. assert search.users == search_result(13)
  91. assert search.podcasts == search_result(14)
  92. assert search.podcast_episodes == search_result(15)
  93. def test_de_json_all(self, client, best, search_result):
  94. json_dict = {
  95. 'search_request_id': self.search_request_id,
  96. 'text': self.text,
  97. 'best': best.to_dict(),
  98. 'albums': search_result(3).to_dict(),
  99. 'artists': search_result(2).to_dict(),
  100. 'playlists': search_result(4).to_dict(),
  101. 'tracks': search_result(1).to_dict(),
  102. 'videos': search_result(5).to_dict(),
  103. 'users': search_result(13).to_dict(),
  104. 'podcasts': search_result(14).to_dict(),
  105. 'podcast_episodes': search_result(15).to_dict(),
  106. 'misspell_corrected': self.misspell_corrected,
  107. 'nocorrect': self.nocorrect,
  108. 'type': self.type_,
  109. 'page': self.page,
  110. 'per_page': self.per_page,
  111. 'misspell_result': self.misspell_result,
  112. 'misspell_original': self.misspell_original,
  113. }
  114. search = Search.de_json(json_dict, client)
  115. assert search.search_request_id == self.search_request_id
  116. assert search.text == self.text
  117. assert search.best == best
  118. assert search.albums == search_result(3)
  119. assert search.artists == search_result(2)
  120. assert search.playlists == search_result(4)
  121. assert search.tracks == search_result(1)
  122. assert search.videos == search_result(5)
  123. assert search.users == search_result(13)
  124. assert search.podcasts == search_result(14)
  125. assert search.podcast_episodes == search_result(15)
  126. assert search.type == self.type_
  127. assert search.page == self.page
  128. assert search.per_page == self.per_page
  129. assert search.misspell_result == self.misspell_result
  130. assert search.misspell_original == self.misspell_original
  131. assert search.misspell_corrected == self.misspell_corrected
  132. assert search.nocorrect == self.nocorrect
  133. def test_equality(self, best, search_result):
  134. a = Search(
  135. self.search_request_id,
  136. self.text,
  137. best,
  138. search_result(3),
  139. search_result(2),
  140. search_result(4),
  141. search_result(1),
  142. search_result(5),
  143. search_result(13),
  144. search_result(14),
  145. search_result(15),
  146. )
  147. b = Search(
  148. self.search_request_id,
  149. '',
  150. best,
  151. search_result(3),
  152. None,
  153. search_result(4),
  154. search_result(1),
  155. search_result(5),
  156. search_result(13),
  157. None,
  158. search_result(15),
  159. )
  160. c = Search(
  161. self.search_request_id,
  162. self.text,
  163. best,
  164. search_result(3),
  165. search_result(2),
  166. search_result(4),
  167. search_result(1),
  168. search_result(5),
  169. search_result(13),
  170. search_result(14),
  171. search_result(15),
  172. )
  173. assert a != b
  174. assert hash(a) != hash(b)
  175. assert a is not b
  176. assert a == c