1
0

test_utils.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import pytest
  2. from myanimebot.utils import 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. # Testing for ANIME
  6. assert MediaType.ANIME == MediaType.from_str('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_list')
  10. assert MediaType.ANIME == MediaType.from_str('ANIME_LIST')
  11. assert MediaType.ANIME == MediaType.from_str('ANiMe_LiST')
  12. # Testing for MANGA
  13. assert MediaType.MANGA == MediaType.from_str('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_list')
  17. assert MediaType.MANGA == MediaType.from_str('MANGA_LIST')
  18. assert MediaType.MANGA == MediaType.from_str('ManGA_LiSt')
  19. # Testing incorrect MediaType
  20. with pytest.raises(NotImplementedError, match='Cannot convert "TEST_LIST" to a MediaType'):
  21. MediaType.from_str('TEST_LIST')
  22. with pytest.raises(NotImplementedError, match='Cannot convert "Blabla" to a MediaType'):
  23. MediaType.from_str('Blabla')
  24. with pytest.raises(NotImplementedError, match='Cannot convert "Toto" to a MediaType'):
  25. MediaType.from_str('Toto')
  26. with pytest.raises(NotImplementedError, match='Cannot convert "ANIMU" to a MediaType'):
  27. MediaType.from_str('ANIMU')
  28. with pytest.raises(NotImplementedError, match='Cannot convert "mango" to a MediaType'):
  29. MediaType.from_str('mango')
  30. def test_Service_from_str():
  31. # Testing for MAL
  32. assert Service.MAL == Service.from_str('MAL')
  33. assert Service.MAL == Service.from_str('MYANIMELIST')
  34. assert Service.MAL == Service.from_str(SERVICE_MAL)
  35. assert Service.MAL == Service.from_str('MaL')
  36. assert Service.MAL == Service.from_str('mal')
  37. assert Service.MAL == Service.from_str('myanimelist')
  38. assert Service.MAL == Service.from_str('mYANimEliST')
  39. # Testing for Anilist
  40. assert Service.ANILIST == Service.from_str('AniList')
  41. assert Service.ANILIST == Service.from_str('AL')
  42. assert Service.ANILIST == Service.from_str(SERVICE_ANILIST)
  43. assert Service.ANILIST == Service.from_str('ANILIST')
  44. assert Service.ANILIST == Service.from_str('anilist')
  45. assert Service.ANILIST == Service.from_str('al')
  46. assert Service.ANILIST == Service.from_str('Al')
  47. # Testing incorrect Services
  48. with pytest.raises(NotImplementedError, match='Cannot convert "Kitsu" to a Service'):
  49. Service.from_str('Kitsu')
  50. with pytest.raises(NotImplementedError, match='Cannot convert "toto" to a Service'):
  51. Service.from_str('toto')
  52. with pytest.raises(NotImplementedError, match='Cannot convert "ani list" to a Service'):
  53. Service.from_str('ani list')
  54. with pytest.raises(NotImplementedError, match='Cannot convert "mla" to a Service'):
  55. Service.from_str('mla')
  56. def test_replace_all():
  57. with pytest.raises(AttributeError):
  58. replace_all("texte", []) == "texte"
  59. assert replace_all("texte", {}) == "texte"
  60. assert replace_all("I is a string", {"is": "am"}) == "I am a string"
  61. assert replace_all("abcdef abcdef 123", {
  62. "a": "z",
  63. "2": "5",
  64. "c": "-"
  65. }) == "zb-def zb-def 153"
  66. assert replace_all("toto", {
  67. "to": "ta",
  68. "z": "ZUUU"
  69. }) == "tata"
  70. assert replace_all("", {
  71. "to": "ta",
  72. "z": "ZUUU"
  73. }) == ""
  74. assert replace_all("abcdcba", {
  75. "a": "0",
  76. "b": "1",
  77. "0": "z"
  78. }) == "z1cdc1z"
  79. def test_filter_name():
  80. assert filter_name("Bonjour") == "Bonjour"
  81. assert filter_name("") == ""
  82. assert filter_name("Bonjour ♥") == "Bonjour \♥"
  83. assert filter_name("♥ Bonjour ♥") == "\♥ Bonjour \♥"
  84. assert filter_name("♥♪☆♂☆♀♀ ♥") == "\♥\♪\☆\♂\☆\♀\♀ \♥"
  85. assert filter_name("♣") == "♣"
  86. def test_truncate_end_show():
  87. assert truncate_end_show("Toto - TV") == "Toto"
  88. assert truncate_end_show("Toto - Movie") == "Toto"
  89. assert truncate_end_show("Toto - Special") == "Toto"
  90. assert truncate_end_show("Toto - OVA") == "Toto"
  91. assert truncate_end_show("Toto - ONA") == "Toto"
  92. assert truncate_end_show("Toto - Manga") == "Toto"
  93. assert truncate_end_show("Toto - Manhua") == "Toto"
  94. assert truncate_end_show("Toto - Manhwa") == "Toto"
  95. assert truncate_end_show("Toto - Novel") == "Toto"
  96. assert truncate_end_show("Toto - One-Shot") == "Toto"
  97. assert truncate_end_show("Toto - Doujinshi") == "Toto"
  98. assert truncate_end_show("Toto - Music") == "Toto"
  99. assert truncate_end_show("Toto - OEL") == "Toto"
  100. assert truncate_end_show("Toto - Unknown") == "Toto"
  101. assert truncate_end_show("Toto- TV") == "Toto"
  102. assert truncate_end_show("Toto- Music") == "Toto"
  103. assert truncate_end_show("Titi-Music") == "Titi-Music"
  104. assert truncate_end_show("- Music") == ""