فهرست منبع

Improving hierarchy, adding tests and Travis CI

Lucas Villeneuve 5 سال پیش
والد
کامیت
ca9df512cb
9فایلهای تغییر یافته به همراه146 افزوده شده و 12 حذف شده
  1. 1 0
      .gitignore
  2. 13 0
      .travis.yml
  3. 4 0
      myanimebot/__init__.py
  4. 5 5
      myanimebot/anilist.py
  5. 1 1
      myanimebot/globals.py
  6. 3 3
      myanimebot/myanimebot.py
  7. 2 3
      myanimebot/utils.py
  8. 42 0
      requirements.txt
  9. 75 0
      tests/test_utils.py

+ 1 - 0
.gitignore

@@ -116,6 +116,7 @@ venv/
 ENV/
 env.bak/
 venv.bak/
+myanimebot-env
 
 # Spyder project settings
 .spyderproject

+ 13 - 0
.travis.yml

@@ -0,0 +1,13 @@
+language: python
+python:
+  - 3.7
+before_install:
+  - python --version
+  - pip install -U pip
+  - pip install -U pytest
+  - pip install codecov
+install:
+  - pip install -r requirements.txt
+script: python -m pytest tests # run tests
+after_success:
+  - codecov # submit coverage

+ 4 - 0
myanimebot/__init__.py

@@ -0,0 +1,4 @@
+from .utils import * 
+from .anilist import *
+from .globals import *
+from .myanimebot import*

+ 5 - 5
src/anilist.py → myanimebot/anilist.py

@@ -6,9 +6,9 @@ from typing import Dict, List
 
 import requests
 
-import globals
-import myanimebot
-import utils
+import myanimebot.globals as globals
+import myanimebot.myanimebot as mab
+import myanimebot.utils as utils
 
 ANILIST_GRAPHQL_URL = 'https://graphql.anilist.co'
 
@@ -317,10 +317,10 @@ async def send_embed_to_channels(activity : utils.Feed):
     
         if data_channels is not None:
             for channel in data_channels:
-                await myanimebot.send_embed_wrapper(None,
+                await mab.send_embed_wrapper(None,
                                                     channel["channel"],
                                                     globals.client,
-                                                    myanimebot.build_embed(activity.user.name,
+                                                    mab.build_embed(activity.user.name,
                                                                             activity.media.name,
                                                                             activity.media.url,
                                                                             activity.status,

+ 1 - 1
src/globals.py → myanimebot/globals.py

@@ -128,7 +128,7 @@ Get some information about this bot.
 logger.info("Booting MyAnimeBot " + VERSION + "...")
 logger.debug("DEBUG log: OK")
 
-feedparser.PREFERRED_XML_PARSERS.remove("drv_libxml2")
+# feedparser.PREFERRED_XML_PARSERS.remove("drv_libxml2")
 
 # Initialization of the database
 try:

+ 3 - 3
src/myanimebot.py → myanimebot/myanimebot.py

@@ -27,9 +27,9 @@ from dateutil.parser import parse as parse_datetime
 from html2text import HTML2Text
 
 # Our modules
-import anilist
-import globals
-import utils
+import myanimebot.anilist as anilist
+import myanimebot.globals as globals
+import myanimebot.utils as utils
 
 if not sys.version_info[:2] >= (3, 7):
 	print("ERROR: Requires python 3.7 or newer.")

+ 2 - 3
src/utils.py → myanimebot/utils.py

@@ -6,8 +6,7 @@ from typing import List
 
 from bs4 import BeautifulSoup
 
-import globals
-
+import myanimebot.globals as globals
 
 class Service(Enum):
 	MAL=globals.SERVICE_MAL
@@ -107,7 +106,7 @@ def getThumbnail(urlParam):
 	return thumbnail
 
 # Replace multiple substrings from a string
-def replace_all(text, dic):
+def replace_all(text : str, dic : dict) -> str:
 	for i, j in dic.items():
 		text = text.replace(i, j)
 	return text

+ 42 - 0
requirements.txt

@@ -0,0 +1,42 @@
+aiodns==2.0.0
+aiohttp==3.6.3
+async-timeout==3.0.1
+asyncio==3.4.3
+attrs==20.3.0
+beautifulsoup4==4.9.3
+bs4==0.0.1
+cchardet==2.1.7
+certifi==2020.12.5
+cffi==1.14.4
+chardet==3.0.4
+discord==1.0.1
+discord.py==1.5.1
+feedparser==6.0.2
+html2text==2020.1.16
+idna==2.10
+importlib-metadata==3.3.0
+iniconfig==1.1.1
+mariadb==1.0.5
+multidict==4.7.6
+mysql==0.0.2
+mysql-connector==2.2.9
+mysqlclient==2.0.3
+packaging==20.8
+pluggy==0.13.1
+py==1.10.0
+pycares==3.1.1
+pycparser==2.20
+PyNaCl==1.4.0
+pyparsing==2.4.7
+pytest==6.2.1
+python-dateutil==2.8.1
+pytz==2020.5
+requests==2.25.1
+sgmllib3k==1.0.0
+six==1.15.0
+soupsieve==2.1
+toml==0.10.2
+typing-extensions==3.7.4.3
+urllib3==1.26.2
+yarl==1.5.1
+zipp==3.4.0

+ 75 - 0
tests/test_utils.py

@@ -0,0 +1,75 @@
+import pytest
+
+from myanimebot.utils import MediaType, Service, filter_name, replace_all
+from myanimebot.globals import SERVICE_MAL, SERVICE_ANILIST
+
+def test_MediaType_from_str():
+    # Testing for ANIME
+    assert MediaType.ANIME == MediaType.from_str('ANIME')
+    assert MediaType.ANIME == MediaType.from_str('anime')
+    assert MediaType.ANIME == MediaType.from_str('ANiMe')
+    assert MediaType.ANIME == MediaType.from_str('anime_list')
+    assert MediaType.ANIME == MediaType.from_str('ANIME_LIST')
+    assert MediaType.ANIME == MediaType.from_str('ANiMe_LiST')
+
+    # Testing for MANGA
+    assert MediaType.MANGA == MediaType.from_str('MANGA')
+    assert MediaType.MANGA == MediaType.from_str('manga')
+    assert MediaType.MANGA == MediaType.from_str('ManGA')
+    assert MediaType.MANGA == MediaType.from_str('manga_list')
+    assert MediaType.MANGA == MediaType.from_str('MANGA_LIST')
+    assert MediaType.MANGA == MediaType.from_str('ManGA_LiSt')
+
+    # Testing incorrect MediaType
+    with pytest.raises(NotImplementedError, match='Cannot convert "TEST_LIST" to a MediaType'):
+        MediaType.from_str('TEST_LIST')
+    with pytest.raises(NotImplementedError, match='Cannot convert "Blabla" to a MediaType'):
+        MediaType.from_str('Blabla')
+    with pytest.raises(NotImplementedError, match='Cannot convert "Toto" to a MediaType'):
+        MediaType.from_str('Toto')
+    with pytest.raises(NotImplementedError, match='Cannot convert "ANIMU" to a MediaType'):
+        MediaType.from_str('ANIMU')
+    with pytest.raises(NotImplementedError, match='Cannot convert "mango" to a MediaType'):
+        MediaType.from_str('mango')
+
+
+def test_Service_from_str():
+    # Testing for MAL
+    assert Service.MAL == Service.from_str('MAL')
+    assert Service.MAL == Service.from_str('MYANIMELIST')
+    assert Service.MAL == Service.from_str(SERVICE_MAL)
+    assert Service.MAL == Service.from_str('MaL')
+    assert Service.MAL == Service.from_str('mal')
+    assert Service.MAL == Service.from_str('myanimelist')
+    assert Service.MAL == Service.from_str('mYANimEliST')
+
+    # Testing for Anilist
+    assert Service.ANILIST == Service.from_str('AniList')
+    assert Service.ANILIST == Service.from_str('AL')
+    assert Service.ANILIST == Service.from_str(SERVICE_ANILIST)
+    assert Service.ANILIST == Service.from_str('ANILIST')
+    assert Service.ANILIST == Service.from_str('anilist')
+    assert Service.ANILIST == Service.from_str('al')
+    assert Service.ANILIST == Service.from_str('Al')
+
+    # Testing incorrect Services
+    with pytest.raises(NotImplementedError, match='Cannot convert "Kitsu" to a Service'):
+        Service.from_str('Kitsu')
+    with pytest.raises(NotImplementedError, match='Cannot convert "toto" to a Service'):
+        Service.from_str('toto')
+    with pytest.raises(NotImplementedError, match='Cannot convert "ani list" to a Service'):
+        Service.from_str('ani list')
+    with pytest.raises(NotImplementedError, match='Cannot convert "mla" to a Service'):
+        Service.from_str('mla')
+
+
+def test_replace_all():
+    with pytest.raises(AttributeError):
+        replace_all("texte", []) == "texte"
+
+    assert replace_all("texte", {}) == "texte"
+    assert replace_all("I is a string", {"is": "am"}) == "I am a string"
+
+
+def test_filter_name():
+    assert filter_name("") != "toto"