From f3302e2132fb61bc2c4061ce7fc847be70441822 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Fri, 30 Dec 2022 13:29:43 +0100 Subject: [PATCH] Feat: add put and delete path to api --- backend/api/main.py | 37 +++++++++-- backend/repository/tribe_sqlite_repository.py | 8 +-- tests/e2e/test_api_tribes.py | 64 +++++++++++++++++++ 3 files changed, 101 insertions(+), 8 deletions(-) diff --git a/backend/api/main.py b/backend/api/main.py index e90faba..aceb53b 100644 --- a/backend/api/main.py +++ b/backend/api/main.py @@ -1,7 +1,7 @@ import sqlite3 from fastapi import FastAPI, status -from fastapi.responses import JSONResponse +from fastapi.responses import JSONResponse, Response from backend.adapters.sqlite import create_db from backend.api.model import StudentModel, TribeModel @@ -37,21 +37,50 @@ app = FastAPI() @app.post("/tribes", status_code=status.HTTP_201_CREATED, response_model=TribeModel) async def post_tribe(item: TribeModel): - tribe = Tribe(**item.dict()) try: - services.add_tribe( + tribe = services.add_tribe( name=item.name, level=item.level, tribe_repo=tribe_repo, conn=conn ) except TribeExists: return JSONResponse( status_code=status.HTTP_409_CONFLICT, - content=f"The tribe {tribe.name} already exists", + content=f"The tribe {item.name} already exists", ) return tribe.to_dict() +@app.put("/tribes/{name}", status_code=status.HTTP_200_OK, response_model=TribeModel) +async def put_tribe(name: str, item: TribeModel): + try: + tribe = services.update_tribe( + name=item.name, level=item.level, tribe_repo=tribe_repo, conn=conn + ) + except TribeDoesNotExist: + return JSONResponse( + status_code=status.HTTP_409_CONFLICT, + content=f"The tribe {name} does not exists", + ) + + return tribe.to_dict() + + +@app.delete("/tribes/{name}") +async def delete_tribe(name: str): + try: + services.delete_tribe(name=name, tribe_repo=tribe_repo, conn=conn) + except TribeDoesNotExist: + return JSONResponse( + status_code=status.HTTP_409_CONFLICT, + content=f"The tribe {name} does not exists", + ) + + return Response( + status_code=status.HTTP_204_NO_CONTENT, + ) + + @app.get("/tribes", response_model=list[TribeModel]) async def list_tribes(): tribes = tribe_repo.list() diff --git a/backend/repository/tribe_sqlite_repository.py b/backend/repository/tribe_sqlite_repository.py index 1b9c2fd..4f735fa 100644 --- a/backend/repository/tribe_sqlite_repository.py +++ b/backend/repository/tribe_sqlite_repository.py @@ -72,11 +72,11 @@ class TribeSQLiteRepository(AbstractRepository): rows = cursor.fetchall() return [Tribe(*r) for r in rows] - def delete(self, tribe: Tribe) -> None: + def delete(self, name: str) -> None: tribes = self.list() - if tribe.name not in map(lambda x: x.name, tribes): + if name not in map(lambda x: x.name, tribes): raise TribeRepositoryError( - f"The tribe {tribe.name} doesn't exists. Can't delete it." + f"The tribe {name} doesn't exists. Can't delete it." ) self.conn.execute( @@ -84,6 +84,6 @@ class TribeSQLiteRepository(AbstractRepository): DELETE FROM tribes WHERE name=:name """, { - "name": tribe.name, + "name": name, }, ) diff --git a/tests/e2e/test_api_tribes.py b/tests/e2e/test_api_tribes.py index b027aa9..b1b1604 100644 --- a/tests/e2e/test_api_tribes.py +++ b/tests/e2e/test_api_tribes.py @@ -35,6 +35,70 @@ def test_api_post_tribe_already_exists(): assert r.json() == f"The tribe {data['name']} already exists" +@pytest.mark.usefixtures("restart_api") +@pytest.mark.usefixtures("clean_db") +def test_api_put_tribe(): + tribe = build_tribes(1)[0] + + url = config.get_api_url() + r = requests.post(f"{url}/tribes", json=tribe.to_dict()) + assert r.status_code == 201 + + mod_tribe = tribe + mod_tribe.level = "other level" + r = requests.put(f"{url}/tribes/{tribe.name}", json=mod_tribe.to_dict()) + assert r.status_code == 200 + + r = requests.get(f"{url}/tribes") + assert [t["name"] for t in r.json()] == [mod_tribe.name] + assert [t["level"] for t in r.json()] == [mod_tribe.level] + + +@pytest.mark.usefixtures("restart_api") +@pytest.mark.usefixtures("clean_db") +def test_api_put_tribe_doesnt_exists(): + tribe = build_tribes(1)[0] + + url = config.get_api_url() + r = requests.put(f"{url}/tribes/{tribe.name}", json=tribe.to_dict()) + assert r.status_code == 409 + + +@pytest.mark.usefixtures("restart_api") +@pytest.mark.usefixtures("clean_db") +def test_api_delete_tribe(): + tribe = build_tribes(1)[0] + + url = config.get_api_url() + r = requests.post(f"{url}/tribes", json=tribe.to_dict()) + + assert r.status_code == 201 + + r = requests.delete(f"{url}/tribes/{tribe.name}") + assert r.status_code == 204 + + r = requests.get(f"{url}/tribes") + assert r.json() == [] + + +@pytest.mark.usefixtures("restart_api") +@pytest.mark.usefixtures("clean_db") +def test_api_delete_tribe_doesnt_exists(): + tribe = build_tribes(1)[0] + + url = config.get_api_url() + r = requests.post(f"{url}/tribes", json=tribe.to_dict()) + + assert r.status_code == 201 + + r = requests.delete(f"{url}/tribes/notexisting") + assert r.status_code == 409 + + r = requests.get(f"{url}/tribes") + assert [t["name"] for t in r.json()] == [tribe.name] + assert [t["level"] for t in r.json()] == [tribe.level] + + @pytest.mark.usefixtures("restart_api") @pytest.mark.usefixtures("clean_db") def test_api_post_list_tribe():