Parcourir la source

Fix truncate_end_show and filter_name, added tests

Lucas Villeneuve il y a 5 ans
Parent
commit
d0f3f9e59f
2 fichiers modifiés avec 66 ajouts et 11 suppressions
  1. 17 9
      myanimebot/utils.py
  2. 49 2
      tests/test_utils.py

+ 17 - 9
myanimebot/utils.py

@@ -105,15 +105,18 @@ def getThumbnail(urlParam):
 	
 	return thumbnail
 
-# Replace multiple substrings from a string
-def replace_all(text : str, dic : dict) -> str:
-	for i, j in dic.items():
-		text = text.replace(i, j)
+
+def replace_all(text : str, replace_dic : dict) -> str:
+	''' Replace multiple substrings from a string '''
+	
+	for replace_key, replace_value in replace_dic.items():
+		text = text.replace(replace_key, replace_value)
 	return text
 
 
-# Escape special characters
-def filter_name(name):
+def filter_name(name : str) -> str:
+	''' Escapes special characters from name '''
+
 	dic = {
         "♥": "\♥",
         "♀": "\♀",
@@ -126,7 +129,7 @@ def filter_name(name):
 
 # Check if the show's name ends with a show type and truncate it
 def truncate_end_show(show):
-	SHOW_TYPES = (
+	show_types = (
         '- TV',
 		'- Movie',
 		'- Special',
@@ -143,8 +146,13 @@ def truncate_end_show(show):
 		'- Unknown'
     )
     
-	if show.endswith(SHOW_TYPES):
-		return show[:show.rindex('-') - 1]
+	for show_type in show_types:
+		if show.endswith(show_type):
+			new_show = show[:-len(show_type)]
+			# Check if space at the end
+			if new_show.endswith(' '):
+				new_show = new_show[:-1]
+			return new_show
 	return show
 
 

+ 49 - 2
tests/test_utils.py

@@ -1,6 +1,6 @@
 import pytest
 
-from myanimebot.utils import MediaType, Service, filter_name, replace_all
+from myanimebot.utils import MediaType, Service, filter_name, replace_all, truncate_end_show
 from myanimebot.globals import SERVICE_MAL, SERVICE_ANILIST
 
 def test_MediaType_from_str():
@@ -69,7 +69,54 @@ def test_replace_all():
 
     assert replace_all("texte", {}) == "texte"
     assert replace_all("I is a string", {"is": "am"}) == "I am a string"
+    assert replace_all("abcdef abcdef 123", {
+        "a": "z",
+        "2": "5",
+        "c": "-"
+        }) == "zb-def zb-def 153"
+
+    assert replace_all("toto", {
+        "to": "ta",
+        "z": "ZUUU"
+        }) == "tata"
+    assert replace_all("", {
+        "to": "ta",
+        "z": "ZUUU"
+        }) == ""
+    assert replace_all("abcdcba", {
+        "a": "0",
+        "b": "1",
+        "0": "z"
+        }) == "z1cdc1z"
 
 
 def test_filter_name():
-    assert filter_name("") != "toto"
+    assert filter_name("Bonjour") == "Bonjour"
+    assert filter_name("") == ""
+    assert filter_name("Bonjour ♥") == "Bonjour \♥"
+    assert filter_name("♥ Bonjour ♥") == "\♥ Bonjour \♥"
+    assert filter_name("♥♪☆♂☆♀♀ ♥") == "\♥\♪\☆\♂\☆\♀\♀ \♥"
+    assert filter_name("♣") == "♣"
+
+
+def test_truncate_end_show():
+    assert truncate_end_show("Toto - TV") == "Toto"
+    assert truncate_end_show("Toto - Movie") == "Toto"
+    assert truncate_end_show("Toto - Special") == "Toto"
+    assert truncate_end_show("Toto - OVA") == "Toto"
+    assert truncate_end_show("Toto - ONA") == "Toto"
+    assert truncate_end_show("Toto - Manga") == "Toto"
+    assert truncate_end_show("Toto - Manhua") == "Toto"
+    assert truncate_end_show("Toto - Manhwa") == "Toto"
+    assert truncate_end_show("Toto - Novel") == "Toto"
+    assert truncate_end_show("Toto - One-Shot") == "Toto"
+    assert truncate_end_show("Toto - Doujinshi") == "Toto"
+    assert truncate_end_show("Toto - Music") == "Toto"
+    assert truncate_end_show("Toto - OEL") == "Toto"
+    assert truncate_end_show("Toto - Unknown") == "Toto"
+    
+    assert truncate_end_show("Toto- TV") == "Toto"
+    assert truncate_end_show("Toto- Music") == "Toto"
+    assert truncate_end_show("Titi-Music") == "Titi-Music"
+    assert truncate_end_show("- Music") == ""
+