mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-03 19:40:16 -04:00
changes to tests and services
This commit is contained in:
parent
f4f816b94c
commit
c3385b0f0f
|
@ -38,19 +38,25 @@ class ServiceService:
|
|||
|
||||
def get_service_by_user(self, subject: User):
|
||||
"""Service method getting all of the services that a user has access to based on role"""
|
||||
programs = subject.program
|
||||
services = []
|
||||
for program in programs:
|
||||
query = select(ServiceEntity).filter(ServiceEntity.program == program)
|
||||
entities = self._session.scalars(query)
|
||||
services.append(entities)
|
||||
return [service.to_model() for service in services]
|
||||
if subject.role != UserTypeEnum.VOLUNTEER:
|
||||
query = select(ServiceEntity)
|
||||
entities = self._session.scalars(query).all()
|
||||
|
||||
return [service.to_model() for service in entities]
|
||||
else:
|
||||
programs = subject.program
|
||||
services = []
|
||||
for program in programs:
|
||||
query = select(ServiceEntity).filter(ServiceEntity.program == program)
|
||||
entities = self._session.scalars(query)
|
||||
services.append(entities)
|
||||
return [service.to_model() for service in services]
|
||||
|
||||
def get_all(self, subject: User) -> list[Service]:
|
||||
"""Service method retrieving all of the services in the table."""
|
||||
if subject.role != UserTypeEnum.ADMIN:
|
||||
if subject.role == UserTypeEnum.VOLUNTEER:
|
||||
raise ProgramNotAssignedException(
|
||||
f"User is not {UserTypeEnum.ADMIN}, cannot get all"
|
||||
f"User is not {UserTypeEnum.ADMIN} or {UserTypeEnum.VOLUNTEER}, cannot get all"
|
||||
)
|
||||
|
||||
query = select(ServiceEntity)
|
||||
|
|
|
@ -1,32 +1,41 @@
|
|||
import pytest
|
||||
|
||||
from ...services import ServiceService
|
||||
from .fixtures import service_svc
|
||||
from ...models.service_model import Service
|
||||
from ...models.enum_for_models import ProgramTypeEnum
|
||||
|
||||
from . import user_test_data
|
||||
from . import service_test_data
|
||||
from ...services.exceptions import ServiceNotFoundException, ProgramNotAssignedException
|
||||
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 backend.services.service import ServiceService
|
||||
from backend.services.exceptions import ServiceNotFoundException
|
||||
from ..services import service_test_data
|
||||
from ..services import user_test_data
|
||||
|
||||
|
||||
def test_get_all(service_svc: ServiceService):
|
||||
service = service_svc.get_all(user_test_data.admin)
|
||||
def test_list(service_svc: ServiceService):
|
||||
service = service_svc.get_all()
|
||||
assert len(service) == len(service_test_data.services)
|
||||
assert isinstance(service[0], Service)
|
||||
|
||||
|
||||
def test_get_by_id(service_svc: ServiceService):
|
||||
if service_test_data.service_1.id != None:
|
||||
service = service_svc.get_service_by_id(service_test_data.service_1.id)
|
||||
assert service.id == service_test_data.service_1.id
|
||||
def test_get_by_name(service_svc: ServiceService):
|
||||
service = service_svc.get_service_by_name("service 1")
|
||||
assert service.name == service_test_data.service_1.name
|
||||
assert isinstance(service, Service)
|
||||
|
||||
|
||||
def test_get_by_name_not_found(service_svc: ServiceService):
|
||||
with pytest.raises(ServiceNotFoundException):
|
||||
service_svc.get_service_by_id(12)
|
||||
service = service_svc.get_service_by_name("service 12")
|
||||
pytest.fail()
|
||||
|
||||
|
||||
def test_get_service_by_user_admin(service_svc: ServiceService):
|
||||
service = service_svc.get_service_by_user(user_test_data.admin)
|
||||
assert len(service) == len(service_test_data.services)
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
def test_get_by_program(service_svc: ServiceService):
|
||||
services = service_svc.get_service_by_program(ProgramTypeEnum.COMMUNITY)
|
||||
for service in services:
|
||||
|
@ -35,13 +44,13 @@ def test_get_by_program(service_svc: ServiceService):
|
|||
|
||||
|
||||
def test_create(service_svc: ServiceService):
|
||||
service = service_svc.create(user_test_data.admin, service_test_data.service_7)
|
||||
service = service_svc.create(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(user_test_data.admin, service_test_data.service_6_edit)
|
||||
service = service_svc.update(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)
|
||||
|
@ -49,19 +58,17 @@ def test_update(service_svc: ServiceService):
|
|||
|
||||
def test_update_not_found(service_svc: ServiceService):
|
||||
with pytest.raises(ServiceNotFoundException):
|
||||
service = service_svc.update(
|
||||
user_test_data.admin, service_test_data.new_service
|
||||
)
|
||||
service = service_svc.update(service_test_data.new_service)
|
||||
pytest.fail()
|
||||
|
||||
|
||||
def test_delete(service_svc: ServiceService):
|
||||
service_svc.delete(user_test_data.admin, service_test_data.service_1)
|
||||
services = service_svc.get_all(user_test_data.admin)
|
||||
service = service_svc.delete("service 1")
|
||||
services = service_svc.get_all()
|
||||
assert len(services) == len(service_test_data.services) - 1
|
||||
|
||||
|
||||
def test_delete_not_found(service_svc: ServiceService):
|
||||
with pytest.raises(ServiceNotFoundException):
|
||||
service_svc.delete(user_test_data.admin, service_test_data.service_7)
|
||||
service_svc.delete("service 10")
|
||||
pytest.fail()
|
||||
|
|
Loading…
Reference in New Issue
Block a user