|
| 1 | +import httpx |
| 2 | +from fastapi import status |
| 3 | + |
| 4 | +from main import app |
| 5 | +from src.models.movies import MovieModel |
| 6 | + |
| 7 | + |
| 8 | +async def test_api_create_movie() -> None: |
| 9 | + # when |
| 10 | + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client: |
| 11 | + response = await client.post( |
| 12 | + "/movies", |
| 13 | + json={ |
| 14 | + "title": (title := "test"), |
| 15 | + "playtime": (playtime := 240), |
| 16 | + "genre": (genre := ["SF", "Romance", "Action"]), |
| 17 | + }, |
| 18 | + ) |
| 19 | + |
| 20 | + # then |
| 21 | + assert response.status_code == status.HTTP_201_CREATED |
| 22 | + response_json = response.json() |
| 23 | + assert response_json["title"] == title |
| 24 | + assert response_json["playtime"] == playtime |
| 25 | + assert response_json["genre"] == genre |
| 26 | + |
| 27 | + |
| 28 | +async def test_api_get_movies_when_query_param_is_nothing() -> None: |
| 29 | + # given |
| 30 | + MovieModel.create_dummy() |
| 31 | + |
| 32 | + # when |
| 33 | + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client: |
| 34 | + response = await client.get("/movies") |
| 35 | + |
| 36 | + # then |
| 37 | + assert response.status_code == status.HTTP_200_OK |
| 38 | + response_json = response.json() |
| 39 | + assert len(response_json) == 10 |
| 40 | + assert response_json[0]["id"] == MovieModel._data[0].id |
| 41 | + assert response_json[0]["title"] == MovieModel._data[0].title |
| 42 | + assert response_json[0]["playtime"] == MovieModel._data[0].playtime |
| 43 | + assert response_json[0]["genre"] == MovieModel._data[0].genre |
| 44 | + |
| 45 | + |
| 46 | +async def test_api_get_movies_when_query_param_is_not_none() -> None: |
| 47 | + # given |
| 48 | + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client: |
| 49 | + await client.post( |
| 50 | + "/movies", |
| 51 | + json={"title": (title := "test"), "playtime": 240, "genre": (genre := ["SF", "Romance", "Action"])}, |
| 52 | + ) |
| 53 | + |
| 54 | + # when |
| 55 | + response = await client.get("/movies", params={"title": title, "genre": genre[0]}) |
| 56 | + |
| 57 | + # then |
| 58 | + assert response.status_code == status.HTTP_200_OK |
| 59 | + response_json = response.json() |
| 60 | + assert len(response_json) == 1 |
| 61 | + assert response_json[0]["title"] == title |
| 62 | + assert response_json[0]["genre"] == genre |
| 63 | + |
| 64 | + |
| 65 | +async def test_api_get_movie() -> None: |
| 66 | + # given |
| 67 | + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client: |
| 68 | + create_response = await client.post( |
| 69 | + "/movies", |
| 70 | + json={ |
| 71 | + "title": (title := "test"), |
| 72 | + "playtime": (playtime := 240), |
| 73 | + "genre": (genre := ["SF", "Romance", "Action"]), |
| 74 | + }, |
| 75 | + ) |
| 76 | + movie_id = create_response.json()["id"] |
| 77 | + # when |
| 78 | + response = await client.get(f"/movies/{movie_id}") |
| 79 | + |
| 80 | + # then |
| 81 | + assert response.status_code == status.HTTP_200_OK |
| 82 | + response_json = response.json() |
| 83 | + assert response_json["title"] == title |
| 84 | + assert response_json["playtime"] == playtime |
| 85 | + assert response_json["genre"] == genre |
| 86 | + |
| 87 | + |
| 88 | +async def test_api_get_movie_when_movie_id_is_invalid() -> None: |
| 89 | + # when |
| 90 | + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client: |
| 91 | + response = await client.get("/movies/1232131311") |
| 92 | + |
| 93 | + # then |
| 94 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 95 | + |
| 96 | + |
| 97 | +async def test_api_update_movie() -> None: |
| 98 | + # when |
| 99 | + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client: |
| 100 | + create_response = await client.post( |
| 101 | + "/movies", json={"title": "test", "playtime": 240, "genre": ["SF", "Romance", "Action"]} |
| 102 | + ) |
| 103 | + movie_id = create_response.json()["id"] |
| 104 | + |
| 105 | + # when |
| 106 | + response = await client.patch( |
| 107 | + f"/movies/{movie_id}", |
| 108 | + json={ |
| 109 | + "title": (updated_title := "updated_title"), |
| 110 | + "playtime": (updated_playtime := 180), |
| 111 | + "genre": (updated_genre := ["Fantasy", "Romance", "Adventure"]), |
| 112 | + }, |
| 113 | + ) |
| 114 | + |
| 115 | + # then |
| 116 | + assert response.status_code == status.HTTP_200_OK |
| 117 | + response_json = response.json() |
| 118 | + assert response_json["title"] == updated_title |
| 119 | + assert response_json["playtime"] == updated_playtime |
| 120 | + assert response_json["genre"] == updated_genre |
| 121 | + |
| 122 | + |
| 123 | +async def test_api_update_movie_when_movie_id_is_invalid() -> None: |
| 124 | + # when |
| 125 | + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client: |
| 126 | + response = await client.patch( |
| 127 | + "/movies/1232131311", |
| 128 | + json={"title": "updated_title", "playtime": 180, "genre": ["Fantasy", "Romance", "Adventure"]}, |
| 129 | + ) |
| 130 | + |
| 131 | + # then |
| 132 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 133 | + |
| 134 | + |
| 135 | +async def test_api_delete_movie() -> None: |
| 136 | + # when |
| 137 | + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client: |
| 138 | + create_response = await client.post( |
| 139 | + "/movies", json={"title": "test", "playtime": 240, "genre": ["SF", "Romance", "Action"]} |
| 140 | + ) |
| 141 | + movie_id = create_response.json()["id"] |
| 142 | + |
| 143 | + # when |
| 144 | + response = await client.delete(f"/movies/{movie_id}") |
| 145 | + |
| 146 | + # then |
| 147 | + assert response.status_code == status.HTTP_204_NO_CONTENT |
| 148 | + |
| 149 | + |
| 150 | +async def test_api_delete_movie_when_movie_id_is_invalid() -> None: |
| 151 | + # when |
| 152 | + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client: |
| 153 | + response = await client.delete("/movies/1232131311") |
| 154 | + |
| 155 | + # then |
| 156 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
0 commit comments