test_utils.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import pytest
  2. from myanimebot.utils import Media, MediaStatus, MediaType, Service, filter_name, replace_all, truncate_end_show
  3. from myanimebot.globals import SERVICE_MAL, SERVICE_ANILIST
  4. def test_MediaType_from_str():
  5. try:
  6. # Testing for ANIME
  7. assert MediaType.ANIME == MediaType.from_str('ANIME')
  8. assert MediaType.ANIME == MediaType.from_str('anime')
  9. assert MediaType.ANIME == MediaType.from_str('ANiMe')
  10. assert MediaType.ANIME == MediaType.from_str('anime_list')
  11. assert MediaType.ANIME == MediaType.from_str('ANIME_LIST')
  12. assert MediaType.ANIME == MediaType.from_str('ANiMe_LiST')
  13. # Testing for MANGA
  14. assert MediaType.MANGA == MediaType.from_str('MANGA')
  15. assert MediaType.MANGA == MediaType.from_str('manga')
  16. assert MediaType.MANGA == MediaType.from_str('ManGA')
  17. assert MediaType.MANGA == MediaType.from_str('manga_list')
  18. assert MediaType.MANGA == MediaType.from_str('MANGA_LIST')
  19. assert MediaType.MANGA == MediaType.from_str('ManGA_LiSt')
  20. except Exception as e:
  21. pytest.fail("Unexpected Exception : {}".format(e))
  22. # Testing incorrect MediaType
  23. with pytest.raises(NotImplementedError, match='Cannot convert "TEST_LIST" to a MediaType'):
  24. MediaType.from_str('TEST_LIST')
  25. with pytest.raises(NotImplementedError, match='Cannot convert "Blabla" to a MediaType'):
  26. MediaType.from_str('Blabla')
  27. with pytest.raises(NotImplementedError, match='Cannot convert "Toto" to a MediaType'):
  28. MediaType.from_str('Toto')
  29. with pytest.raises(NotImplementedError, match='Cannot convert "ANIMU" to a MediaType'):
  30. MediaType.from_str('ANIMU')
  31. with pytest.raises(NotImplementedError, match='Cannot convert "mango" to a MediaType'):
  32. MediaType.from_str('mango')
  33. with pytest.raises(NotImplementedError):
  34. MediaType.from_str('')
  35. with pytest.raises(TypeError):
  36. MediaType.from_str(None)
  37. def test_Service_from_str():
  38. try:
  39. # Testing for MAL
  40. assert Service.MAL == Service.from_str('MAL')
  41. assert Service.MAL == Service.from_str('MYANIMELIST')
  42. assert Service.MAL == Service.from_str(SERVICE_MAL)
  43. assert Service.MAL == Service.from_str('MaL')
  44. assert Service.MAL == Service.from_str('mal')
  45. assert Service.MAL == Service.from_str('myanimelist')
  46. assert Service.MAL == Service.from_str('mYANimEliST')
  47. # Testing for Anilist
  48. assert Service.ANILIST == Service.from_str('AniList')
  49. assert Service.ANILIST == Service.from_str('AL')
  50. assert Service.ANILIST == Service.from_str(SERVICE_ANILIST)
  51. assert Service.ANILIST == Service.from_str('ANILIST')
  52. assert Service.ANILIST == Service.from_str('anilist')
  53. assert Service.ANILIST == Service.from_str('al')
  54. assert Service.ANILIST == Service.from_str('Al')
  55. except Exception as e:
  56. pytest.fail("Unexpected Exception : {}".format(e))
  57. # Testing incorrect Services
  58. with pytest.raises(NotImplementedError, match='Cannot convert "Kitsu" to a Service'):
  59. Service.from_str('Kitsu')
  60. with pytest.raises(NotImplementedError, match='Cannot convert "toto" to a Service'):
  61. Service.from_str('toto')
  62. with pytest.raises(NotImplementedError, match='Cannot convert "ani list" to a Service'):
  63. Service.from_str('ani list')
  64. with pytest.raises(NotImplementedError, match='Cannot convert "mla" to a Service'):
  65. Service.from_str('mla')
  66. with pytest.raises(TypeError):
  67. Service.from_str(None)
  68. def test_replace_all():
  69. with pytest.raises(AttributeError):
  70. replace_all("texte", [])
  71. assert replace_all(None, {}) == None
  72. assert replace_all("toto", None) == "toto"
  73. assert replace_all("texte", {}) == "texte"
  74. assert replace_all("I is a string", {"is": "am"}) == "I am a string"
  75. assert replace_all("abcdef abcdef 123", {
  76. "a": "z",
  77. "2": "5",
  78. "c": "-"
  79. }) == "zb-def zb-def 153"
  80. assert replace_all("toto", {
  81. "to": "ta",
  82. "z": "ZUUU"
  83. }) == "tata"
  84. assert replace_all("", {
  85. "to": "ta",
  86. "z": "ZUUU"
  87. }) == ""
  88. assert replace_all("abcdcba", {
  89. "a": "0",
  90. "b": "1",
  91. "0": "z"
  92. }) == "z1cdc1z"
  93. def test_filter_name():
  94. assert filter_name("Bonjour") == "Bonjour"
  95. assert filter_name("") == ""
  96. assert filter_name("Bonjour ♥") == "Bonjour \♥"
  97. assert filter_name("♥ Bonjour ♥") == "\♥ Bonjour \♥"
  98. assert filter_name("♥♪☆♂☆♀♀ ♥") == "\♥\♪\☆\♂\☆\♀\♀ \♥"
  99. assert filter_name("♣") == "♣"
  100. assert filter_name(None) == None
  101. def test_truncate_end_show():
  102. assert truncate_end_show("Toto - TV") == "Toto"
  103. assert truncate_end_show("Toto - Movie") == "Toto"
  104. assert truncate_end_show("Toto - Special") == "Toto"
  105. assert truncate_end_show("Toto - OVA") == "Toto"
  106. assert truncate_end_show("Toto - ONA") == "Toto"
  107. assert truncate_end_show("Toto - Manga") == "Toto"
  108. assert truncate_end_show("Toto - Manhua") == "Toto"
  109. assert truncate_end_show("Toto - Manhwa") == "Toto"
  110. assert truncate_end_show("Toto - Novel") == "Toto"
  111. assert truncate_end_show("Toto - One-Shot") == "Toto"
  112. assert truncate_end_show("Toto - Doujinshi") == "Toto"
  113. assert truncate_end_show("Toto - Music") == "Toto"
  114. assert truncate_end_show("Toto - OEL") == "Toto"
  115. assert truncate_end_show("Toto - Unknown") == "Toto"
  116. assert truncate_end_show("Toto- TV") == "Toto"
  117. assert truncate_end_show("Toto- Music") == "Toto"
  118. assert truncate_end_show("Titi-Music") == "Titi-Music"
  119. assert truncate_end_show("- Music") == ""
  120. assert truncate_end_show(None) == None
  121. def test_media_status():
  122. print('testing')
  123. # Testing Current
  124. try:
  125. current = MediaStatus.CURRENT
  126. assert MediaStatus.from_str('read') == current
  127. assert MediaStatus.from_str('READ') == current
  128. assert MediaStatus.from_str('ReaD') == current
  129. assert MediaStatus.from_str('Reading') == current
  130. assert MediaStatus.from_str('READING') == current
  131. assert MediaStatus.from_str('reading') == current
  132. assert MediaStatus.from_str('watched') == current
  133. assert MediaStatus.from_str('WATCHING') == current
  134. assert MediaStatus.from_str('WATCHED') == current
  135. assert MediaStatus.from_str('watChing') == current
  136. except Exception as e:
  137. pytest.fail("Unexpected Exception : {}".format(e))
  138. with pytest.raises(NotImplementedError):
  139. MediaStatus.from_str('Watchh')
  140. with pytest.raises(NotImplementedError):
  141. MediaStatus.from_str('Watches')
  142. with pytest.raises(NotImplementedError):
  143. MediaStatus.from_str('Red')
  144. with pytest.raises(NotImplementedError):
  145. MediaStatus.from_str('Watc hed')
  146. # Testing Planning
  147. try:
  148. planning = MediaStatus.PLANNING
  149. assert MediaStatus.from_str('PLANS') == planning
  150. assert MediaStatus.from_str('plans') == planning
  151. assert MediaStatus.from_str('PlAns') == planning
  152. assert MediaStatus.from_str('Plan') == planning
  153. assert MediaStatus.from_str('plan') == planning
  154. assert MediaStatus.from_str('PLAN') == planning
  155. except Exception as e:
  156. pytest.fail("Unexpected Exception : {}".format(e))
  157. with pytest.raises(NotImplementedError):
  158. MediaStatus.from_str('planned')
  159. with pytest.raises(NotImplementedError):
  160. MediaStatus.from_str('Pla')
  161. with pytest.raises(NotImplementedError):
  162. MediaStatus.from_str('pla n')
  163. # Testing Completed
  164. try:
  165. completed = MediaStatus.COMPLETED
  166. assert MediaStatus.from_str('Completed') == completed
  167. assert MediaStatus.from_str('COMPLETED') == completed
  168. assert MediaStatus.from_str('completed') == completed
  169. except Exception as e:
  170. pytest.fail("Unexpected Exception : {}".format(e))
  171. with pytest.raises(NotImplementedError):
  172. MediaStatus.from_str('Complete')
  173. with pytest.raises(NotImplementedError):
  174. MediaStatus.from_str('Compl eted')
  175. # Testing Dropped
  176. try:
  177. dropped = MediaStatus.DROPPED
  178. assert MediaStatus.from_str('DroPPed') == dropped
  179. assert MediaStatus.from_str('DROPPED') == dropped
  180. assert MediaStatus.from_str('dropped') == dropped
  181. except Exception as e:
  182. pytest.fail("Unexpected Exception : {}".format(e))
  183. with pytest.raises(NotImplementedError):
  184. MediaStatus.from_str('Drop')
  185. with pytest.raises(NotImplementedError):
  186. MediaStatus.from_str('Drops')
  187. with pytest.raises(NotImplementedError):
  188. MediaStatus.from_str(' Dropped')
  189. # Testing Paused
  190. try:
  191. paused = MediaStatus.PAUSED
  192. assert MediaStatus.from_str('PAUSED') == paused
  193. assert MediaStatus.from_str('paused') == paused
  194. assert MediaStatus.from_str('PaUSed') == paused
  195. assert MediaStatus.from_str('ON-HOLD') == paused
  196. assert MediaStatus.from_str('on-hold') == paused
  197. assert MediaStatus.from_str('ON-hold') == paused
  198. assert MediaStatus.from_str('on-HOLD') == paused
  199. except Exception as e:
  200. pytest.fail("Unexpected Exception : {}".format(e))
  201. with pytest.raises(NotImplementedError):
  202. MediaStatus.from_str('pauses')
  203. with pytest.raises(NotImplementedError):
  204. MediaStatus.from_str('on hold')
  205. with pytest.raises(NotImplementedError):
  206. MediaStatus.from_str('onhold')
  207. # Testing Repeating
  208. try:
  209. repeating = MediaStatus.REPEATING
  210. assert MediaStatus.from_str('reread') == repeating
  211. assert MediaStatus.from_str('REREAD') == repeating
  212. assert MediaStatus.from_str('reReaD') == repeating
  213. assert MediaStatus.from_str('reReading') == repeating
  214. assert MediaStatus.from_str('REREADING') == repeating
  215. assert MediaStatus.from_str('rereading') == repeating
  216. assert MediaStatus.from_str('rewatched') == repeating
  217. assert MediaStatus.from_str('REWATCHING') == repeating
  218. assert MediaStatus.from_str('reWATCHED') == repeating
  219. assert MediaStatus.from_str('RewatChing') == repeating
  220. except Exception as e:
  221. pytest.fail("Unexpected Exception : {}".format(e))
  222. with pytest.raises(NotImplementedError):
  223. MediaStatus.from_str('rreread')
  224. with pytest.raises(NotImplementedError):
  225. MediaStatus.from_str('rewatches')
  226. with pytest.raises(NotImplementedError):
  227. MediaStatus.from_str('re read')
  228. # Testing incorrect uses cases
  229. with pytest.raises(NotImplementedError):
  230. MediaStatus.from_str('')
  231. with pytest.raises(TypeError):
  232. MediaStatus.from_str(None)