71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
|
|
"""Tests for /sources endpoints."""
|
||
|
|
from tests.conftest import AUTH, BAD_AUTH
|
||
|
|
|
||
|
|
|
||
|
|
def create_source(client, name="Doc Watson"):
|
||
|
|
r = client.post("/sources/", json={"name": name}, headers=AUTH)
|
||
|
|
assert r.status_code == 201
|
||
|
|
return r.get_json()
|
||
|
|
|
||
|
|
|
||
|
|
class TestSources:
|
||
|
|
def test_create(self, client):
|
||
|
|
s = create_source(client)
|
||
|
|
assert s["name"] == "Doc Watson"
|
||
|
|
assert "id" in s
|
||
|
|
|
||
|
|
def test_list(self, client):
|
||
|
|
create_source(client, "Doc Watson")
|
||
|
|
create_source(client, "Tony Rice")
|
||
|
|
r = client.get("/sources/")
|
||
|
|
assert r.status_code == 200
|
||
|
|
names = [s["name"] for s in r.get_json()]
|
||
|
|
assert "Doc Watson" in names
|
||
|
|
assert "Tony Rice" in names
|
||
|
|
|
||
|
|
def test_list_is_public(self, client):
|
||
|
|
r = client.get("/sources/")
|
||
|
|
assert r.status_code == 200
|
||
|
|
|
||
|
|
def test_create_requires_auth(self, client):
|
||
|
|
r = client.post("/sources/", json={"name": "test"})
|
||
|
|
assert r.status_code == 401
|
||
|
|
|
||
|
|
def test_update(self, client):
|
||
|
|
s = create_source(client)
|
||
|
|
r = client.patch(f'/sources/{s["id"]}', json={"name": "Doc Watson Jr."}, headers=AUTH)
|
||
|
|
assert r.status_code == 200
|
||
|
|
assert r.get_json()["name"] == "Doc Watson Jr."
|
||
|
|
|
||
|
|
def test_delete(self, client):
|
||
|
|
s = create_source(client)
|
||
|
|
r = client.delete(f'/sources/{s["id"]}', headers=AUTH)
|
||
|
|
assert r.status_code == 200
|
||
|
|
remaining = [x["id"] for x in client.get("/sources/").get_json()]
|
||
|
|
assert s["id"] not in remaining
|
||
|
|
|
||
|
|
def test_tune_with_source(self, client):
|
||
|
|
s = create_source(client, "Clarence Ashley")
|
||
|
|
r = client.post("/tunes/", json={"name": "Coo Coo Bird", "key": "D", "source_id": s["id"]}, headers=AUTH)
|
||
|
|
assert r.status_code == 201
|
||
|
|
tune = r.get_json()
|
||
|
|
assert tune["source"]["name"] == "Clarence Ashley"
|
||
|
|
assert tune["source_id"] == s["id"]
|
||
|
|
|
||
|
|
def test_filter_by_source(self, client):
|
||
|
|
s = create_source(client, "Clarence Ashley")
|
||
|
|
client.post("/tunes/", json={"name": "Coo Coo Bird", "source_id": s["id"]}, headers=AUTH)
|
||
|
|
client.post("/tunes/", json={"name": "Salt Creek"}, headers=AUTH)
|
||
|
|
r = client.get(f'/tunes/?source_id={s["id"]}')
|
||
|
|
results = r.get_json()
|
||
|
|
assert len(results) == 1
|
||
|
|
assert results[0]["name"] == "Coo Coo Bird"
|
||
|
|
|
||
|
|
def test_filter_by_modal(self, client):
|
||
|
|
client.post("/tunes/", json={"name": "Modal Tune", "modal": True}, headers=AUTH)
|
||
|
|
client.post("/tunes/", json={"name": "Regular Tune", "modal": False}, headers=AUTH)
|
||
|
|
r = client.get("/tunes/?modal=true")
|
||
|
|
names = [t["name"] for t in r.get_json()]
|
||
|
|
assert "Modal Tune" in names
|
||
|
|
assert "Regular Tune" not in names
|