Refactor: unittest like

This commit is contained in:
Colin Goutte 2023-08-26 17:24:58 +02:00
parent 8b8a1ed30a
commit 6e77545ef9
1 changed files with 26 additions and 27 deletions

View File

@ -50,39 +50,38 @@ def rand_name():
return name
def test_get_movie_404_if_not_found():
response = client.get("/movies/-1")
assert response.status_code == 404
class BaseCrud(unittest.TestCase):
def test_get_movie_404_if_not_found(self):
response = client.get("/movies/-1")
assert response.status_code == 404
def test_list_movies(self):
response = client.get("/movies/")
# assert response.json() == []
def test_list_movies():
response = client.get("/movies/")
# assert response.json() == []
N = 10
names = []
for _ in range(N):
name = rand_name()
N = 10
names = []
for _ in range(N):
name = rand_name()
names.append(name)
response = client.post("/movies/", json={"title": name})
assert response.status_code == 200
names.append(name)
movies = client.get("/movies/")
movies_by_title = {m["title"]: m for m in movies.json()}
found = list(movies_by_title[title] for title in names)
assert all(movies_by_title[title] for title in names)
def test_create_movie_api(self):
name = f"rand_{random.randint(1, 1000)}"
response = client.post("/movies/", json={"title": name})
assert response.status_code == 200
movies = client.get("/movies/")
movies_by_title = {m["title"]: m for m in movies.json()}
found = list(movies_by_title[title] for title in names)
assert all(movies_by_title[title] for title in names)
def test_create_movie_api():
name = f"rand_{random.randint(1, 1000)}"
response = client.post("/movies/", json={"title": name})
assert response.status_code == 200
movie_id = response.json()["id"]
assert f"Created {name}" in response.json()["message"]
response = client.get(f"/movies/{movie_id}")
assert response.json()["title"] == name
movie_id = response.json()["id"]
assert f"Created {name}" in response.json()["message"]
response = client.get(f"/movies/{movie_id}")
assert response.json()["title"] == name
class ApiTestCase(unittest.TestCase):