Skip to content

Commit

Permalink
add bv_test and pre-commit hook (#28)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* test: temp

Signed-off-by: jingfelix <[email protected]>

* tests: temp GitHub Action

Signed-off-by: jingfelix <[email protected]>

* [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 <[email protected]>

* fix: uid for testing

Signed-off-by: jingfelix <[email protected]>

---------

Signed-off-by: jingfelix <[email protected]>
Co-authored-by: jingfelix <[email protected]>
  • Loading branch information
npc0vo and jingfelix authored Oct 7, 2024
1 parent 209f894 commit 807eb59
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
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()

0 comments on commit 807eb59

Please sign in to comment.