Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maocorte committed Jul 26, 2024
1 parent 7125df4 commit f27c4f8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
1 change: 0 additions & 1 deletion api/app/db/dao/model_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions api/app/routes/model_route.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions api/tests/dao/model_dao_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions api/tests/routes/model_route_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions api/tests/services/model_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f27c4f8

Please sign in to comment.