Download tests/test_user_features.py from binhugg2/api: direct link, hf CLI and curl.
- Browser
- Download file 6.64 kB
-
https://huggingface.co/spaces/binhugg2/api/resolve/main/tests/test_user_features.py
- Command line
-
hf download hf://spaces/binhugg2/api/tests/test_user_features.py
-
curl -L -o test_user_features.py https://huggingface.co/spaces/binhugg2/api/resolve/main/tests/test_user_features.py
6.64 kB
| """ | |
| 测试用户收藏和播放历史功能 | |
| """ | |
| import pytest | |
| async def test_add_favorite(client, user_token, db_session): | |
| from app.models.vod import Vod | |
| from app.models.category import Category | |
| cat = Category(name="电影", sort_order=1, is_active=True) | |
| db_session.add(cat) | |
| await db_session.flush() | |
| vod = Vod(name="收藏测试影片", category_id=cat.id, is_hidden=False) | |
| db_session.add(vod) | |
| await db_session.commit() | |
| resp = await client.post( | |
| "/user/favorite", | |
| json={"vod_id": vod.id}, | |
| headers={"Authorization": f"Bearer {user_token}"}, | |
| ) | |
| assert resp.status_code == 200 | |
| assert resp.json()["code"] == 0 | |
| async def test_add_favorite_duplicate(client, user_token, db_session): | |
| from app.models.vod import Vod | |
| from app.models.category import Category | |
| cat = Category(name="剧集", sort_order=2, is_active=True) | |
| db_session.add(cat) | |
| await db_session.flush() | |
| vod = Vod(name="重复收藏影片", category_id=cat.id, is_hidden=False) | |
| db_session.add(vod) | |
| await db_session.commit() | |
| headers = {"Authorization": f"Bearer {user_token}"} | |
| await client.post("/user/favorite", json={"vod_id": vod.id}, headers=headers) | |
| resp = await client.post("/user/favorite", json={"vod_id": vod.id}, headers=headers) | |
| assert resp.json()["code"] == 40002 | |
| async def test_list_favorites(client, user_token, db_session): | |
| from app.models.vod import Vod | |
| from app.models.category import Category | |
| cat = Category(name="综艺", sort_order=3, is_active=True) | |
| db_session.add(cat) | |
| await db_session.flush() | |
| vod = Vod(name="列表收藏影片", category_id=cat.id, is_hidden=False) | |
| db_session.add(vod) | |
| await db_session.commit() | |
| headers = {"Authorization": f"Bearer {user_token}"} | |
| await client.post("/user/favorite", json={"vod_id": vod.id}, headers=headers) | |
| resp = await client.get("/user/favorite", headers=headers) | |
| assert resp.status_code == 200 | |
| body = resp.json() | |
| assert body["code"] == 0 | |
| assert body["data"]["total"] >= 1 | |
| async def test_remove_favorite(client, user_token, db_session): | |
| from app.models.vod import Vod | |
| from app.models.category import Category | |
| cat = Category(name="动漫", sort_order=4, is_active=True) | |
| db_session.add(cat) | |
| await db_session.flush() | |
| vod = Vod(name="删除收藏影片", category_id=cat.id, is_hidden=False) | |
| db_session.add(vod) | |
| await db_session.commit() | |
| headers = {"Authorization": f"Bearer {user_token}"} | |
| await client.post("/user/favorite", json={"vod_id": vod.id}, headers=headers) | |
| resp = await client.delete(f"/user/favorite/{vod.id}", headers=headers) | |
| assert resp.status_code == 200 | |
| assert resp.json()["code"] == 0 | |
| async def test_remove_favorite_not_found(client, user_token): | |
| resp = await client.delete( | |
| "/user/favorite/99999", | |
| headers={"Authorization": f"Bearer {user_token}"}, | |
| ) | |
| assert resp.json()["code"] == 40401 | |
| async def test_record_history(client, user_token, db_session): | |
| from app.models.vod import Vod | |
| from app.models.category import Category | |
| from app.models.source_site import SourceSite | |
| cat = Category(name="纪录片", sort_order=5, is_active=True) | |
| db_session.add(cat) | |
| await db_session.flush() | |
| vod = Vod(name="历史记录影片", category_id=cat.id, is_hidden=False) | |
| db_session.add(vod) | |
| source = SourceSite(name="测试源", code="test_src", base_url="http://test.com", sync_interval=60, sort_order=0, last_sync_count=0, total_count=0) | |
| db_session.add(source) | |
| await db_session.commit() | |
| resp = await client.post( | |
| "/user/history", | |
| json={ | |
| "vod_id": vod.id, | |
| "source_id": source.id, | |
| "episode": "第1集", | |
| "play_url": "http://test.com/play.m3u8", | |
| "progress": 120, | |
| "duration": 3600, | |
| }, | |
| headers={"Authorization": f"Bearer {user_token}"}, | |
| ) | |
| assert resp.status_code == 200 | |
| assert resp.json()["code"] == 0 | |
| async def test_list_history(client, user_token, db_session): | |
| from app.models.vod import Vod | |
| from app.models.category import Category | |
| from app.models.source_site import SourceSite | |
| cat = Category(name="短片", sort_order=6, is_active=True) | |
| db_session.add(cat) | |
| await db_session.flush() | |
| vod = Vod(name="历史列表影片", category_id=cat.id, is_hidden=False) | |
| db_session.add(vod) | |
| source = SourceSite(name="测试源2", code="test_src2", base_url="http://test2.com", sync_interval=60, sort_order=0, last_sync_count=0, total_count=0) | |
| db_session.add(source) | |
| await db_session.commit() | |
| headers = {"Authorization": f"Bearer {user_token}"} | |
| await client.post("/user/history", json={ | |
| "vod_id": vod.id, "source_id": source.id, | |
| "episode": "第1集", "play_url": "http://test.com/1.m3u8", | |
| "progress": 60, "duration": 1800, | |
| }, headers=headers) | |
| resp = await client.get("/user/history", headers=headers) | |
| assert resp.status_code == 200 | |
| body = resp.json() | |
| assert body["code"] == 0 | |
| assert body["data"]["total"] >= 1 | |
| async def test_delete_history(client, user_token, db_session): | |
| from app.models.vod import Vod | |
| from app.models.category import Category | |
| from app.models.source_site import SourceSite | |
| from app.models.play_history import PlayHistory | |
| from app.models.user import User | |
| from sqlalchemy import select | |
| cat = Category(name="MV", sort_order=7, is_active=True) | |
| db_session.add(cat) | |
| await db_session.flush() | |
| vod = Vod(name="删除历史影片", category_id=cat.id, is_hidden=False) | |
| db_session.add(vod) | |
| source = SourceSite(name="测试源3", code="test_src3", base_url="http://test3.com", sync_interval=60, sort_order=0, last_sync_count=0, total_count=0) | |
| db_session.add(source) | |
| await db_session.commit() | |
| headers = {"Authorization": f"Bearer {user_token}"} | |
| await client.post("/user/history", json={ | |
| "vod_id": vod.id, "source_id": source.id, | |
| "episode": "第1集", "play_url": "http://test.com/1.m3u8", | |
| "progress": 30, "duration": 900, | |
| }, headers=headers) | |
| result = await db_session.execute(select(PlayHistory).order_by(PlayHistory.id.desc()).limit(1)) | |
| history = result.scalar_one() | |
| resp = await client.delete(f"/user/history/{history.id}", headers=headers) | |
| assert resp.status_code == 200 | |
| assert resp.json()["code"] == 0 | |