134 lines
5.9 KiB
Python
134 lines
5.9 KiB
Python
|
|
"""Tests for notes, references, and musicians sub-resource endpoints."""
|
||
|
|
from tests.conftest import AUTH
|
||
|
|
|
||
|
|
|
||
|
|
def make_tune(client):
|
||
|
|
r = client.post("/tunes/", json={"name": "Sub Test Tune", "key": "G"}, headers=AUTH)
|
||
|
|
assert r.status_code == 201
|
||
|
|
return r.get_json()
|
||
|
|
|
||
|
|
|
||
|
|
def make_musician(client, name="Doc Watson"):
|
||
|
|
r = client.post("/musicians/", json={"name": name}, headers=AUTH)
|
||
|
|
assert r.status_code == 201
|
||
|
|
return r.get_json()
|
||
|
|
|
||
|
|
|
||
|
|
# ── Notes ──────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
class TestNotes:
|
||
|
|
def test_add_note(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
r = client.post(f'/tunes/{tune["id"]}/notes',
|
||
|
|
json={"note": "remember the B part"}, headers=AUTH)
|
||
|
|
assert r.status_code == 201
|
||
|
|
assert r.get_json()["note"] == "remember the B part"
|
||
|
|
|
||
|
|
def test_list_notes(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
client.post(f'/tunes/{tune["id"]}/notes', json={"note": "note 1"}, headers=AUTH)
|
||
|
|
client.post(f'/tunes/{tune["id"]}/notes', json={"note": "note 2"}, headers=AUTH)
|
||
|
|
r = client.get(f'/tunes/{tune["id"]}/notes', headers=AUTH)
|
||
|
|
assert len(r.get_json()) == 2
|
||
|
|
|
||
|
|
def test_update_note(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
note = client.post(f'/tunes/{tune["id"]}/notes',
|
||
|
|
json={"note": "old"}, headers=AUTH).get_json()
|
||
|
|
r = client.patch(f'/tunes/{tune["id"]}/notes/{note["id"]}',
|
||
|
|
json={"note": "new"}, headers=AUTH)
|
||
|
|
assert r.get_json()["note"] == "new"
|
||
|
|
|
||
|
|
def test_delete_note(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
note = client.post(f'/tunes/{tune["id"]}/notes',
|
||
|
|
json={"note": "to delete"}, headers=AUTH).get_json()
|
||
|
|
r = client.delete(f'/tunes/{tune["id"]}/notes/{note["id"]}', headers=AUTH)
|
||
|
|
assert r.status_code == 200
|
||
|
|
|
||
|
|
def test_note_on_missing_tune_returns_404(self, client):
|
||
|
|
r = client.post("/tunes/99999/notes", json={"note": "x"}, headers=AUTH)
|
||
|
|
assert r.status_code == 404
|
||
|
|
|
||
|
|
|
||
|
|
# ── References ────────────────────────────────────────────────
|
||
|
|
|
||
|
|
class TestReferences:
|
||
|
|
def test_add_reference(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
r = client.post(f'/tunes/{tune["id"]}/references',
|
||
|
|
json={"link": "https://youtube.com/abc", "site": "YouTube"},
|
||
|
|
headers=AUTH)
|
||
|
|
assert r.status_code == 201
|
||
|
|
assert r.get_json()["musicians"] == []
|
||
|
|
|
||
|
|
def test_add_reference_with_musicians(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
m1 = make_musician(client, "Doc Watson")
|
||
|
|
m2 = make_musician(client, "Merle Watson")
|
||
|
|
r = client.post(f'/tunes/{tune["id"]}/references',
|
||
|
|
json={"link": "https://example.com", "site": "YouTube",
|
||
|
|
"musicians": [{"id": m1["id"]}, {"id": m2["id"]}]},
|
||
|
|
headers=AUTH)
|
||
|
|
assert r.status_code == 201
|
||
|
|
names = [m["name"] for m in r.get_json()["musicians"]]
|
||
|
|
assert "Doc Watson" in names
|
||
|
|
assert "Merle Watson" in names
|
||
|
|
|
||
|
|
def test_list_references(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
client.post(f'/tunes/{tune["id"]}/references',
|
||
|
|
json={"link": "https://a.com"}, headers=AUTH)
|
||
|
|
client.post(f'/tunes/{tune["id"]}/references',
|
||
|
|
json={"link": "https://b.com"}, headers=AUTH)
|
||
|
|
r = client.get(f'/tunes/{tune["id"]}/references', headers=AUTH)
|
||
|
|
assert len(r.get_json()) == 2
|
||
|
|
|
||
|
|
def test_update_reference(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
ref = client.post(f'/tunes/{tune["id"]}/references',
|
||
|
|
json={"link": "https://old.com", "site": "Old"},
|
||
|
|
headers=AUTH).get_json()
|
||
|
|
r = client.patch(f'/tunes/{tune["id"]}/references/{ref["id"]}',
|
||
|
|
json={"site": "New"}, headers=AUTH)
|
||
|
|
assert r.get_json()["site"] == "New"
|
||
|
|
|
||
|
|
def test_delete_reference(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
ref = client.post(f'/tunes/{tune["id"]}/references',
|
||
|
|
json={"link": "https://del.com"}, headers=AUTH).get_json()
|
||
|
|
r = client.delete(f'/tunes/{tune["id"]}/references/{ref["id"]}', headers=AUTH)
|
||
|
|
assert r.status_code == 200
|
||
|
|
|
||
|
|
|
||
|
|
# ── Musicians on references ───────────────────────────────────
|
||
|
|
|
||
|
|
class TestReferenceMusicians:
|
||
|
|
def _make_ref(self, client, tune_id):
|
||
|
|
r = client.post(f'/tunes/{tune_id}/references',
|
||
|
|
json={"link": "https://example.com"}, headers=AUTH)
|
||
|
|
return r.get_json()
|
||
|
|
|
||
|
|
def test_add_musician_to_reference(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
ref = self._make_ref(client, tune["id"])
|
||
|
|
m = make_musician(client, "Tony Rice")
|
||
|
|
r = client.post(f'/tunes/{tune["id"]}/references/{ref["id"]}/musicians',
|
||
|
|
json={"musician_id": m["id"]}, headers=AUTH)
|
||
|
|
assert r.status_code == 200
|
||
|
|
names = [x["name"] for x in r.get_json()["musicians"]]
|
||
|
|
assert "Tony Rice" in names
|
||
|
|
|
||
|
|
def test_remove_musician_from_reference(self, client):
|
||
|
|
tune = make_tune(client)
|
||
|
|
m = make_musician(client, "Tony Rice")
|
||
|
|
ref = self._make_ref(client, tune["id"])
|
||
|
|
# add musician via the join endpoint
|
||
|
|
client.post(f'/tunes/{tune["id"]}/references/{ref["id"]}/musicians',
|
||
|
|
json={"musician_id": m["id"]}, headers=AUTH)
|
||
|
|
r = client.delete(
|
||
|
|
f'/tunes/{tune["id"]}/references/{ref["id"]}/musicians/{m["id"]}',
|
||
|
|
headers=AUTH)
|
||
|
|
assert r.status_code == 200
|
||
|
|
assert r.get_json()["musicians"] == []
|