Skip to content

Commit

Permalink
add unit tests for albums endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardfritz committed Nov 16, 2023
1 parent c5669d3 commit cfa2444
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"ms-python.python",
"ms-vscode-remote.remote-containers",
"charliermarsh.ruff"
"charliermarsh.ruff",
"ms-python.isort"
]
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"tests/"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

![Docker](https://img.shields.io/badge/docker-%230db7ed.svg?style=for-the-badge&logo=docker&logoColor=white)
![FastAPI](https://img.shields.io/badge/FastAPI-005571?style=for-the-badge&logo=fastapi)
![Nginx](https://img.shields.io/badge/nginx-%23009639.svg?style=for-the-badge&logo=nginx&logoColor=white)
![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white)
![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)
![Raspberry Pi](https://img.shields.io/badge/-RaspberryPi-C51A4A?style=for-the-badge&logo=Raspberry-Pi)
Expand Down
3 changes: 2 additions & 1 deletion src/pytunes/crud.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from uuid import UUID
from sqlalchemy import select, and_

from sqlalchemy import and_, select
from sqlalchemy.orm import Session

from . import models, schemas
Expand Down
8 changes: 5 additions & 3 deletions src/pytunes/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from .database import SessionLocal
from functools import lru_cache

from passlib.apache import HtpasswdFile
from redis import Redis
from rq import Queue
from passlib.apache import HtpasswdFile
from functools import lru_cache

from .database import SessionLocal


def get_db():
Expand Down
2 changes: 1 addition & 1 deletion src/pytunes/sync.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import glob
import time

from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer

from . import crud, models
from .database import engine
Expand Down
5 changes: 0 additions & 5 deletions src/pytunes/testing/client.py

This file was deleted.

14 changes: 14 additions & 0 deletions src/pytunes/testing/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from unittest.mock import Mock

from fastapi.testclient import TestClient

from pytunes.dependencies import get_db
from pytunes.main import app, get_current_username

mock_current_username = "current_username"
app.dependency_overrides[get_current_username] = lambda: mock_current_username

mock_db = Mock()
app.dependency_overrides[get_db] = lambda: mock_db

client = TestClient(app)
23 changes: 23 additions & 0 deletions tests/test_albums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from unittest.mock import patch
from uuid import uuid4

from pytunes import models
from pytunes.testing.setup import client


@patch(
"pytunes.routers.albums.crud.get_album_tracks",
return_value=[
models.Track(id=uuid4(), name="foo"),
models.Track(id=uuid4(), name="bar"),
],
)
@patch("pytunes.routers.albums.track_to_playlist_item", return_value="playlist_item")
def test_get_album_playlist(mock_get_album_tracks, mock_track_to_playlist_item):
album_id = uuid4()

response = client.get(f"/albums/{album_id}.m3u")

assert response.status_code == 200
assert response.text == "\n".join(["#EXTM3U", "playlist_item", "playlist_item"])
assert response.headers["content-type"] == "audio/x-mpegurl"
18 changes: 13 additions & 5 deletions tests/test_tracks.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from pytunes.testing.client import client
from uuid import uuid4

from pytunes.testing.setup import client


def test_get_track_playlist(fs):
fs.create_file("/data/.pytunes/0beec7b5-ea3f-0fdb-c95d-0dd47f3c5bc2.m3u8")
response = client.get("/tracks/0beec7b5-ea3f-0fdb-c95d-0dd47f3c5bc2.m3u8")
track_id = uuid4()
fs.create_file(f"/data/.pytunes/{track_id}.m3u8")

response = client.get(f"/tracks/{track_id}.m3u8")

assert response.status_code == 200


def test_get_track_segment(fs):
fs.create_file("/data/.pytunes/0beec7b5-ea3f-0fdb-c95d-0dd47f3c5bc2.0.m4a")
response = client.get("/tracks/0beec7b5-ea3f-0fdb-c95d-0dd47f3c5bc2.0.m4a")
track_id = uuid4()
fs.create_file(f"/data/.pytunes/{track_id}.0.m4a")

response = client.get(f"/tracks/{track_id}.0.m4a")

assert response.status_code == 200
27 changes: 25 additions & 2 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
from uuid import UUID
from pytunes.util import sha1_to_uuid
from unittest.mock import Mock
from uuid import UUID, uuid4

from fastapi.datastructures import URL

from pytunes import models
from pytunes.util import sha1_to_uuid, track_to_playlist_item


def test_sha1_to_uuid():
assert sha1_to_uuid("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33") == UUID(
"0beec7b5-ea3f-0fdb-c95d-0dd47f3c5bc2"
)


def test_track_to_playlist_item():
track_id = uuid4()
track_name = "foo"
artist_name = "bar"
track = models.Track(
id=track_id, name=track_name, artists=[models.Artist(name=artist_name)]
)
track_url = f"http://testserver/tracks/{track_id}.m3u8"
mock_request = Mock()
mock_request.url_for = Mock(return_value=URL(track_url))

playlist_item = track_to_playlist_item(mock_request, track)

assert playlist_item == "\n".join(
[f"#EXTINF:-1,{artist_name} - {track_name}", track_url]
)

0 comments on commit cfa2444

Please sign in to comment.