81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
import sqlite3
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from music_server.app import create_app
|
|
from tests.support import auth_headers
|
|
|
|
|
|
class PlayerRouteTests(unittest.TestCase):
|
|
def _prepare_player_db(self, db_path: Path) -> None:
|
|
conn = sqlite3.connect(db_path)
|
|
conn.execute(
|
|
"""
|
|
create table favorite_tracks (
|
|
track_id integer primary key,
|
|
added_at text not null
|
|
)
|
|
"""
|
|
)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
def test_favorite_track_put_then_list(self):
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
db_path = Path(tmpdir) / "player.db"
|
|
self._prepare_player_db(db_path)
|
|
|
|
with patch.dict(
|
|
"os.environ",
|
|
{
|
|
"PLAYER_DB_PATH": str(db_path),
|
|
},
|
|
clear=False,
|
|
):
|
|
client = TestClient(create_app())
|
|
put_response = client.put(
|
|
"/player/v1/me/favorites/tracks/3476",
|
|
headers=auth_headers(db_path),
|
|
)
|
|
get_response = client.get(
|
|
"/player/v1/me/favorites/tracks",
|
|
headers=auth_headers(db_path),
|
|
)
|
|
|
|
self.assertEqual(204, put_response.status_code)
|
|
self.assertEqual(200, get_response.status_code)
|
|
self.assertEqual({"items": [{"track_id": 3476}]}, get_response.json())
|
|
|
|
def test_favorite_track_routes_work_with_fresh_empty_db(self):
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
db_path = Path(tmpdir) / "player.db"
|
|
|
|
with patch.dict(
|
|
"os.environ",
|
|
{
|
|
"PLAYER_DB_PATH": str(db_path),
|
|
},
|
|
clear=False,
|
|
):
|
|
client = TestClient(create_app())
|
|
put_response = client.put(
|
|
"/player/v1/me/favorites/tracks/8888",
|
|
headers=auth_headers(db_path),
|
|
)
|
|
get_response = client.get(
|
|
"/player/v1/me/favorites/tracks",
|
|
headers=auth_headers(db_path),
|
|
)
|
|
|
|
self.assertEqual(204, put_response.status_code)
|
|
self.assertEqual(200, get_response.status_code)
|
|
self.assertEqual({"items": [{"track_id": 8888}]}, get_response.json())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|