Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bv_test and pre-commit hook #28

Merged
merged 18 commits into from
Oct 7, 2024
Merged
9 changes: 8 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
pip install -e .
- uses: pre-commit/[email protected]
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
11 changes: 7 additions & 4 deletions src/bilifm/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"
Expand Down
63 changes: 63 additions & 0 deletions src/bilifm/test.py
Original file line number Diff line number Diff line change
@@ -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()
Loading