test_utils.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import pytest
  2. from myanimebot.utils import *
  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. # Testing Current
  123. try:
  124. current = MediaStatus.CURRENT
  125. assert MediaStatus.from_str('read') == current
  126. assert MediaStatus.from_str('READ') == current
  127. assert MediaStatus.from_str('ReaD') == current
  128. assert MediaStatus.from_str('Reading') == current
  129. assert MediaStatus.from_str('READING') == current
  130. assert MediaStatus.from_str('reading') == current
  131. assert MediaStatus.from_str('watched') == current
  132. assert MediaStatus.from_str('WATCHING') == current
  133. assert MediaStatus.from_str('WATCHED') == current
  134. assert MediaStatus.from_str('watChing') == current
  135. assert current.value == CURRENT_COLOR
  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. assert planning.value == PLANNING_COLOR
  156. except Exception as e:
  157. pytest.fail("Unexpected Exception : {}".format(e))
  158. with pytest.raises(NotImplementedError):
  159. MediaStatus.from_str('planned')
  160. with pytest.raises(NotImplementedError):
  161. MediaStatus.from_str('Pla')
  162. with pytest.raises(NotImplementedError):
  163. MediaStatus.from_str('pla n')
  164. # Testing Completed
  165. try:
  166. completed = MediaStatus.COMPLETED
  167. assert MediaStatus.from_str('Completed') == completed
  168. assert MediaStatus.from_str('COMPLETED') == completed
  169. assert MediaStatus.from_str('completed') == completed
  170. assert completed.value == COMPLETED_COLOR
  171. except Exception as e:
  172. pytest.fail("Unexpected Exception : {}".format(e))
  173. with pytest.raises(NotImplementedError):
  174. MediaStatus.from_str('Complete')
  175. with pytest.raises(NotImplementedError):
  176. MediaStatus.from_str('Compl eted')
  177. # Testing Dropped
  178. try:
  179. dropped = MediaStatus.DROPPED
  180. assert MediaStatus.from_str('DroPPed') == dropped
  181. assert MediaStatus.from_str('DROPPED') == dropped
  182. assert MediaStatus.from_str('dropped') == dropped
  183. assert dropped.value == DROPPED_COLOR
  184. except Exception as e:
  185. pytest.fail("Unexpected Exception : {}".format(e))
  186. with pytest.raises(NotImplementedError):
  187. MediaStatus.from_str('Drop')
  188. with pytest.raises(NotImplementedError):
  189. MediaStatus.from_str('Drops')
  190. with pytest.raises(NotImplementedError):
  191. MediaStatus.from_str(' Dropped')
  192. # Testing Paused
  193. try:
  194. paused = MediaStatus.PAUSED
  195. assert MediaStatus.from_str('PAUSED') == paused
  196. assert MediaStatus.from_str('paused') == paused
  197. assert MediaStatus.from_str('PaUSed') == paused
  198. assert MediaStatus.from_str('ON-HOLD') == paused
  199. assert MediaStatus.from_str('on-hold') == paused
  200. assert MediaStatus.from_str('ON-hold') == paused
  201. assert MediaStatus.from_str('on-HOLD') == paused
  202. assert paused.value == PAUSED_COLOR
  203. except Exception as e:
  204. pytest.fail("Unexpected Exception : {}".format(e))
  205. with pytest.raises(NotImplementedError):
  206. MediaStatus.from_str('pauses')
  207. with pytest.raises(NotImplementedError):
  208. MediaStatus.from_str('on hold')
  209. with pytest.raises(NotImplementedError):
  210. MediaStatus.from_str('onhold')
  211. # Testing Repeating
  212. try:
  213. repeating = MediaStatus.REPEATING
  214. assert MediaStatus.from_str('reread') == repeating
  215. assert MediaStatus.from_str('REREAD') == repeating
  216. assert MediaStatus.from_str('reReaD') == repeating
  217. assert MediaStatus.from_str('reReading') == repeating
  218. assert MediaStatus.from_str('REREADING') == repeating
  219. assert MediaStatus.from_str('rereading') == repeating
  220. assert MediaStatus.from_str('rewatched') == repeating
  221. assert MediaStatus.from_str('REWATCHING') == repeating
  222. assert MediaStatus.from_str('reWATCHED') == repeating
  223. assert MediaStatus.from_str('RewatChing') == repeating
  224. assert MediaStatus.from_str('Re-watChing') == repeating
  225. assert MediaStatus.from_str('Re-watChed') == repeating
  226. assert MediaStatus.from_str('Re-readiNg') == repeating
  227. assert MediaStatus.from_str('Re-Read') == repeating
  228. assert repeating.value == REPEATING_COLOR
  229. except Exception as e:
  230. pytest.fail("Unexpected Exception : {}".format(e))
  231. with pytest.raises(NotImplementedError):
  232. MediaStatus.from_str('rreread')
  233. with pytest.raises(NotImplementedError):
  234. MediaStatus.from_str('rewatches')
  235. with pytest.raises(NotImplementedError):
  236. MediaStatus.from_str('re read')
  237. # Testing incorrect uses cases
  238. with pytest.raises(NotImplementedError):
  239. MediaStatus.from_str('')
  240. with pytest.raises(TypeError):
  241. MediaStatus.from_str(None)
  242. def test_feed_get_status_str():
  243. user = User(id=0, service_id=0, name='test', servers=[])
  244. media = Media(id=None,
  245. name='Random anime',
  246. url=None,
  247. episodes='?',
  248. image=None,
  249. type=MediaType.ANIME)
  250. feed = Feed(service=Service.MAL,
  251. date_publication=None,
  252. user=user,
  253. status=MediaStatus.COMPLETED,
  254. description=None,
  255. media=media,
  256. progress='?',
  257. score=None,
  258. score_format=None)
  259. assert feed.get_status_str() == 'Completed'
  260. feed.status = MediaStatus.PLANNING
  261. assert feed.get_status_str() == 'Plans to watch'
  262. feed.status = MediaStatus.DROPPED
  263. assert feed.get_status_str() == 'Dropped'
  264. feed.status = MediaStatus.PAUSED
  265. assert feed.get_status_str() == 'Paused'
  266. feed.status = MediaStatus.CURRENT
  267. assert feed.get_status_str() == 'Watching'
  268. feed.status = MediaStatus.REPEATING
  269. assert feed.get_status_str() == 'Re-watching'
  270. feed.media.type = MediaType.MANGA
  271. assert feed.get_status_str() == 'Re-reading'
  272. feed.status = MediaStatus.COMPLETED
  273. assert feed.get_status_str() == 'Completed'
  274. feed.status = MediaStatus.PLANNING
  275. assert feed.get_status_str() == 'Plans to read'
  276. feed.status = MediaStatus.DROPPED
  277. assert feed.get_status_str() == 'Dropped'
  278. feed.status = MediaStatus.PAUSED
  279. assert feed.get_status_str() == 'Paused'
  280. feed.status = MediaStatus.CURRENT
  281. assert feed.get_status_str() == 'Reading'