From f27c4f8fdb92fec1c2aebfd46f104ecb7210c719 Mon Sep 17 00:00:00 2001 From: "mauro.cortellazzi" Date: Fri, 26 Jul 2024 12:16:02 +0200 Subject: [PATCH] added unit tests --- api/app/db/dao/model_dao.py | 1 - api/app/routes/model_route.py | 6 ++--- api/tests/dao/model_dao_test.py | 10 +++++++++ api/tests/routes/model_route_test.py | 28 ++++++++++++++++++++++++ api/tests/services/model_service_test.py | 14 ++++++++++++ 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/api/app/db/dao/model_dao.py b/api/app/db/dao/model_dao.py index 287aae66..a83b362e 100644 --- a/api/app/db/dao/model_dao.py +++ b/api/app/db/dao/model_dao.py @@ -41,7 +41,6 @@ def update(self, uuid: UUID, model: Model): ) return session.execute(query).rowcount - def delete(self, uuid: UUID) -> int: with self.db.begin_session() as session: deleted_at = datetime.datetime.now(tz=datetime.UTC) diff --git a/api/app/routes/model_route.py b/api/app/routes/model_route.py index fd2b0da9..56419dd6 100644 --- a/api/app/routes/model_route.py +++ b/api/app/routes/model_route.py @@ -1,9 +1,8 @@ import logging from typing import Annotated, List, Optional from uuid import UUID -from requests import Response -from fastapi import APIRouter +from fastapi import APIRouter, Response from fastapi.params import Query from fastapi_pagination import Page, Params @@ -56,7 +55,6 @@ def delete_model(model_uuid: UUID): def update_model_by_uuid(model_uuid: UUID, model_in: ModelIn): if model_service.update_model_by_uuid(model_uuid, model_in): return Response(status_code=200) - else: - return Response(status_code=404) + return Response(status_code=404) return router diff --git a/api/tests/dao/model_dao_test.py b/api/tests/dao/model_dao_test.py index 5d7f14d2..da776dbe 100644 --- a/api/tests/dao/model_dao_test.py +++ b/api/tests/dao/model_dao_test.py @@ -27,6 +27,16 @@ def test_get_by_uuid_empty(self): retrieved = self.model_dao.get_by_uuid(uuid.uuid4()) assert retrieved is None + def test_update(self): + model = db_mock.get_sample_model() + self.model_dao.insert(model) + model_to_update = model + model_to_update.name = 'updated_name' + rows = self.model_dao.update(model.uuid, model_to_update) + retrieved = self.model_dao.get_by_uuid(model.uuid) + assert rows == 1 + assert retrieved.name == 'updated_name' + def test_delete(self): model = db_mock.get_sample_model() self.model_dao.insert(model) diff --git a/api/tests/routes/model_route_test.py b/api/tests/routes/model_route_test.py index 6f5917e9..726a57fb 100644 --- a/api/tests/routes/model_route_test.py +++ b/api/tests/routes/model_route_test.py @@ -50,6 +50,34 @@ def test_create_model(self): assert jsonable_encoder(model_out) == res.json() self.model_service.create_model.assert_called_once_with(model_in) + def test_update_model_ok(self): + model_in = db_mock.get_sample_model_in() + self.model_service.update_model_by_uuid = MagicMock(return_value=True) + + res = self.client.post( + f'{self.prefix}/{db_mock.MODEL_UUID}', + json=jsonable_encoder(model_in), + ) + + assert res.status_code == 200 + self.model_service.update_model_by_uuid.assert_called_once_with( + db_mock.MODEL_UUID, model_in + ) + + def test_update_model_ko(self): + model_in = db_mock.get_sample_model_in() + self.model_service.update_model_by_uuid = MagicMock(return_value=False) + + res = self.client.post( + f'{self.prefix}/{db_mock.MODEL_UUID}', + json=jsonable_encoder(model_in), + ) + + assert res.status_code == 404 + self.model_service.update_model_by_uuid.assert_called_once_with( + db_mock.MODEL_UUID, model_in + ) + def test_get_model_by_uuid(self): model = db_mock.get_sample_model() model_out = ModelOut.from_model(model) diff --git a/api/tests/services/model_service_test.py b/api/tests/services/model_service_test.py index 02e6a669..dfa6274c 100644 --- a/api/tests/services/model_service_test.py +++ b/api/tests/services/model_service_test.py @@ -66,6 +66,20 @@ def test_get_model_by_uuid_not_found(self): ) self.model_dao.get_by_uuid.assert_called_once() + def test_update_model_ok(self): + model_in = db_mock.get_sample_model_in() + self.model_dao.update = MagicMock(return_value=1) + res = self.model_service.update_model_by_uuid(model_uuid, model_in) + + assert res is True + + def test_update_model_ko(self): + model_in = db_mock.get_sample_model_in() + self.model_dao.update = MagicMock(return_value=0) + res = self.model_service.update_model_by_uuid(model_uuid, model_in) + + assert res is False + def test_delete_model_ok(self): model = db_mock.get_sample_model() self.model_dao.get_by_uuid = MagicMock(return_value=model)