finally made tests work, added get_by_name

This commit is contained in:
CheezyGarlicBread 2024-04-24 00:10:42 -04:00 committed by pmoharana-cmd
parent bb39dd6686
commit 0ad8b5059d
2 changed files with 27 additions and 13 deletions

View File

@ -36,6 +36,16 @@ class ServiceService:
return entity.to_model()
def get_service_by_name(self, name: str) -> Service:
"""Service method getting services by id."""
query = select(ServiceEntity).filter(ServiceEntity.name == name)
entity = self._session.scalars(query).one_or_none()
if entity is None:
raise ServiceNotFoundException(f"Service with name: {name} does not exist")
return entity.to_model()
def get_service_by_user(self, subject: User):
"""Service method getting all of the services that a user has access to based on role"""
if subject.role != UserTypeEnum.VOLUNTEER:
@ -50,7 +60,7 @@ class ServiceService:
query = select(ServiceEntity).filter(ServiceEntity.program == program)
entities = self._session.scalars(query)
services.append(entities)
return [service.to_model() for service in services]
return [service.to_model() for service in entities]
def get_all(self, subject: User) -> list[Service]:
"""Service method retrieving all of the services in the table."""

View File

@ -1,15 +1,17 @@
from backend.models.service_model import Service
from backend.models.user_model import User
from backend.entities.service_entity import ServiceEntity
from backend.models.enum_for_models import ProgramTypeEnum
from ...models.enum_for_models import ProgramTypeEnum
from backend.services.service import ServiceService
from backend.services.exceptions import ServiceNotFoundException
from ..services import service_test_data
from ..services import user_test_data
from . import service_test_data
from . import user_test_data
from .fixtures import service_svc, user_svc
from backend.models.service_model import Service
import pytest
def test_list(service_svc: ServiceService):
service = service_svc.get_all()
service = service_svc.get_all(user_test_data.admin)
assert len(service) == len(service_test_data.services)
assert isinstance(service[0], Service)
@ -33,7 +35,7 @@ def test_get_service_by_user_admin(service_svc: ServiceService):
def test_get_service_by_user_volun(service_svc: ServiceService):
service = service_svc.get_service_by_user(user_test_data.volunteer)
assert len(service) == 2
assert len(service) == 3
def test_get_by_program(service_svc: ServiceService):
@ -44,13 +46,13 @@ def test_get_by_program(service_svc: ServiceService):
def test_create(service_svc: ServiceService):
service = service_svc.create(service_test_data.service_7)
service = service_svc.create(user_test_data.admin, service_test_data.service_7)
assert service.name == service_test_data.service_7.name
assert isinstance(service, Service)
def test_update(service_svc: ServiceService):
service = service_svc.update(service_test_data.service_6_edit)
service = service_svc.update(user_test_data.admin, service_test_data.service_6_edit)
assert service.status == service_test_data.service_6_edit.status
assert service.requirements == service_test_data.service_6_edit.requirements
assert isinstance(service, Service)
@ -58,17 +60,19 @@ def test_update(service_svc: ServiceService):
def test_update_not_found(service_svc: ServiceService):
with pytest.raises(ServiceNotFoundException):
service = service_svc.update(service_test_data.new_service)
service = service_svc.update(
user_test_data.admin, service_test_data.new_service
)
pytest.fail()
def test_delete(service_svc: ServiceService):
service = service_svc.delete("service 1")
services = service_svc.get_all()
service_svc.delete(user_test_data.admin, service_test_data.service_6)
services = service_svc.get_all(user_test_data.admin)
assert len(services) == len(service_test_data.services) - 1
def test_delete_not_found(service_svc: ServiceService):
with pytest.raises(ServiceNotFoundException):
service_svc.delete("service 10")
service_svc.delete(user_test_data.admin, service_test_data.service_10)
pytest.fail()