From 807eb59d166f65ebae009a4d2ee7edd19a379de2 Mon Sep 17 00:00:00 2001 From: Orz <136583853+npc0vo@users.noreply.github.com> Date: Mon, 7 Oct 2024 17:46:39 +0800 Subject: [PATCH] add bv_test and pre-commit hook (#28) * test * pre commit hook * pre-commmit hook * Add test_bv unit And Add pre-commit-hook * update testunit * Fix github action envs,update unittest * test github action * Update github action again * tests: temp fix GitHub Action Signed-off-by: jingfelix * test: temp Signed-off-by: jingfelix * tests: temp GitHub Action Signed-off-by: jingfelix * [test]try to fix ttl * [test]try again to fix ttl * [test]try again to fix ttl * [test]try again to fix ttl * [test]try again to fix ttl * fix: skip run-tests in GitHub Action Signed-off-by: jingfelix * fix: uid for testing Signed-off-by: jingfelix --------- Signed-off-by: jingfelix Co-authored-by: jingfelix --- .github/workflows/check.yml | 9 +++++- .pre-commit-config.yaml | 8 +++++ src/bilifm/audio.py | 11 ++++--- src/bilifm/test.py | 63 +++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 src/bilifm/test.py diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 168f684..3b0ccf9 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -5,10 +5,17 @@ on: branches: [ main ] pull_request: +env: + SKIP: run-tests + jobs: pre_commit: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-python@v3 - - uses: pre-commit/action@v3.0.0 \ No newline at end of file + - name: Install dependencies + run: | + python3 -m pip install --upgrade pip + pip install -e . + - uses: pre-commit/action@v3.0.0 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 488c389..f42d0cb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,3 +26,11 @@ repos: files: ^pdm.lock$ - id: pdm-lock-check - id: pdm-sync +- repo: local + hooks: + - id: run-tests + name: Run tests + entry: python3 -m unittest src/bilifm/test.py + language: system + types: [python] + stages: [commit] \ No newline at end of file diff --git a/src/bilifm/audio.py b/src/bilifm/audio.py index 56416e0..dbfa54f 100644 --- a/src/bilifm/audio.py +++ b/src/bilifm/audio.py @@ -83,9 +83,11 @@ def download(self): "cid": cid, } ) - json = requests.get( - self.playUrl, params=params, headers=self.headers - ).json() + res = requests.get( + self.playUrl, params=params, headers=self.headers, timeout=60 + ) + + json = res.json() if json["data"] is None: console.print( @@ -158,11 +160,12 @@ def download(self): except Exception as e: console.print( Panel( - f"[bold red]下载失败[/bold red]\n错误: {str(e)}", + f"[bold red]下载失败[/bold red]\n Code: {res.status_code} 错误: {str(e)}", title="异常", expand=False, ) ) + raise e def __get_cid_title(self, bvid: str): url = "https://api.bilibili.com/x/web-interface/view" diff --git a/src/bilifm/test.py b/src/bilifm/test.py new file mode 100644 index 0000000..756eafd --- /dev/null +++ b/src/bilifm/test.py @@ -0,0 +1,63 @@ +import os +import unittest + +from .command import bv, season, series, uid +from .util import AudioQualityEnums, change_directory + + +class TestBiliFM(unittest.TestCase): + @classmethod + def setUpClass(cls) -> None: + cls.cpwd = os.getcwd() + change_directory("tests") + + @classmethod # 清理测试文件 + def tearDownClass(cls) -> None: + change_directory(cls.cpwd) + import shutil + + folder_name = "tests" + try: + shutil.rmtree(folder_name) + print(f"文件夹 {folder_name} 已被删除。") + except FileNotFoundError: + print(f"文件夹 {folder_name} 不存在。") + except Exception as e: + print(f"删除文件夹 {folder_name} 时出错: {e}") + + def setUp(self): + # 设置测试环境 + self.test_bv = "BV1yt4y1Q7SS" + self.audioquality = AudioQualityEnums.k64 + self.test_fav = "69361944" # 暂时没有找到比较好的测试fav + self.test_cookies_path = "" + self.media_id = "69361944" + self.test_series_uid = "1918016728" + self.test_series_sid = "4049063" + self.uid = "7590247" + self.test_season_uid = "48484716" + self.test_season_sid = "28722" + + def test_bv_command(self): + self.assertIsNone(bv(self.test_bv, None, self.audioquality)) + + def test_uid_command(self): + self.assertIsNone(uid(self.uid, None, self.audioquality)) + + def test_fav_command(self): + # 需要cookies 感觉不太能测吧 + pass + + def test_season_command(self): + self.assertIsNone( + season(self.test_season_uid, self.test_season_sid, None, self.audioquality) + ) + + def test_series_command(self): + self.assertIsNone( + series(self.test_series_uid, self.test_series_sid, None, self.audioquality) + ) + + +if __name__ == "__main__": + unittest.main()