Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
feijooso committed May 23, 2024
1 parent b5bd287 commit 6b66224
Showing 1 changed file with 87 additions and 1 deletion.
88 changes: 87 additions & 1 deletion app/tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from repository.Users import UsersRepository
from schemas.Schemas import CreateNotificationSchema, CreateUserSchema, UserSchema
from service.Users import UsersService
from exceptions.UserException import InvalidData, ResourceNotFound, InvalidURL
from exceptions.UserException import ForbiddenUser, InvalidData, ResourceNotFound, InvalidURL


@pytest.fixture
Expand Down Expand Up @@ -199,9 +199,67 @@ def test_create_notification(self):
mock_db.create_notification.assert_called_once()
mock_db.rollback.assert_not_called()

def test_delete_notification(self):
attr_db = {
"delete_notification.return_value": None,
"get_notification_owner.return_value": 1,
"rollback.return_value": None
}
mock_db = get_mock(UsersRepository, attr_db)
service = UsersService(mock_db)
service.delete_notification(1, 1)

mock_db.delete_notification.assert_called_once()
mock_db.rollback.assert_not_called()

def test_delete_notification_fails_if_not_authorized(self):
attr_db = {
"delete_notification.return_value": None,
"get_notification_owner.return_value": 2,
"rollback.return_value": None
}
mock_db = get_mock(UsersRepository, attr_db)
service = UsersService(mock_db)

with self.assertRaises(ForbiddenUser):
service.delete_notification(1, 1)

def test_delete_fails_if_notification_doesnt_exist(self):
attr_db = {
"delete_notification.return_value": None,
"get_notification_owner.return_value": None,
"rollback.return_value": None
}
mock_db = get_mock(UsersRepository, attr_db)
service = UsersService(mock_db)

with self.assertRaises(ResourceNotFound):
service.delete_notification(1, 1)

def test_update_notification(self):
attr_db = {
"edit_notification.return_value": None,
"get_notification_owner.return_value": 1,
"rollback.return_value": None
}
mock_db = get_mock(UsersRepository, attr_db)
service = UsersService(mock_db)
service.update_notification(
1,
1,
CreateNotificationSchema(
date_time=datetime(2023, 6, 17, 10, 30),
content="Alarm"
)
)

mock_db.edit_notification.assert_called_once()
mock_db.rollback.assert_not_called()

def test_create_notification_rollback_if_failed(self):
attr_db = {
"create_notification.side_effect": Exception(),
"get_notification_owner.return_value": 1,
"rollback.return_value": None
}
mock_db = get_mock(UsersRepository, attr_db)
Expand All @@ -216,3 +274,31 @@ def test_create_notification_rollback_if_failed(self):
)

mock_db.rollback.assert_called_once()

def test_delete_notification_rollback_if_failed(self):
attr_db = {
"delete_notification.side_effect": Exception(),
"get_notification_owner.return_value": 1,
"rollback.return_value": None
}
mock_db = get_mock(UsersRepository, attr_db)
service = UsersService(mock_db)

with self.assertRaises(Exception):
service.delete_notification(1, 1)

mock_db.rollback.assert_called_once()

def test_update_notification_rollback_if_failed(self):
attr_db = {
"edit_notification.side_effect": Exception(),
"get_notification_owner.return_value": 1,
"rollback.return_value": None
}
mock_db = get_mock(UsersRepository, attr_db)
service = UsersService(mock_db)

with self.assertRaises(Exception):
service.update_notification(1, 1, {})

mock_db.rollback.assert_called_once()

0 comments on commit 6b66224

Please sign in to comment.