Fix bugs regarding service test data insertion

This commit is contained in:
pmoharana-cmd 2024-04-22 21:13:30 -04:00
parent a107337737
commit f4f816b94c
5 changed files with 94 additions and 87 deletions

View File

@ -8,20 +8,24 @@ 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, UserTypeEnum
from backend.services.exceptions import ServiceNotFoundException, ProgramNotAssignedException
from backend.services.exceptions import (
ServiceNotFoundException,
ProgramNotAssignedException,
)
class ServiceService:
def __init__(self, session: Session = Depends(db_session)):
self._session = session
def get_service_by_program(self, program: ProgramTypeEnum) -> list[Service]:
"""Service method getting services belonging to a particular program."""
query = select(ServiceEntity).filter(ServiceEntity.program == program)
entities = self._session.scalars(query)
return [entity.to_model() for entity in entities]
def get_service_by_id(self, id: int) -> Service:
"""Service method getting services by id."""
query = select(ServiceEntity).filter(ServiceEntity.id == id)
@ -39,14 +43,15 @@ class ServiceService:
for program in programs:
query = select(ServiceEntity).filter(ServiceEntity.program == program)
entities = self._session.scalars(query)
all.append(entities)
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:
raise ProgramNotAssignedException(f"User is not {UserTypeEnum.ADMIN}, cannot get all")
raise ProgramNotAssignedException(
f"User is not {UserTypeEnum.ADMIN}, cannot get all"
)
query = select(ServiceEntity)
entities = self._session.scalars(query).all()
@ -56,23 +61,29 @@ class ServiceService:
def create(self, subject: User, service: Service) -> Service:
"""Creates/adds a service to the table."""
if subject.role != UserTypeEnum.ADMIN:
raise ProgramNotAssignedException(f"User is not {UserTypeEnum.ADMIN}, cannot create service")
raise ProgramNotAssignedException(
f"User is not {UserTypeEnum.ADMIN}, cannot create service"
)
service_entity = ServiceEntity.from_model(service)
self._session.add(service_entity)
self._session.commit()
return service_entity.to_model()
def update(self, subject: User, service: Service) -> Service:
"""Updates a service if in the table."""
if subject.role != UserTypeEnum.ADMIN:
raise ProgramNotAssignedException(f"User is not {UserTypeEnum.ADMIN}, cannot update service")
raise ProgramNotAssignedException(
f"User is not {UserTypeEnum.ADMIN}, cannot update service"
)
service_entity = self._session.get(ServiceEntity, service.id)
if service_entity is None:
raise ServiceNotFoundException("The service you are searching for does not exist.")
raise ServiceNotFoundException(
"The service you are searching for does not exist."
)
service_entity.name = service.name
service_entity.status = service.status
service_entity.summary = service.summary
@ -90,7 +101,9 @@ class ServiceService:
service_entity = self._session.get(ServiceEntity, service.id)
if service_entity is None:
raise ServiceNotFoundException("The service you are searching for does not exist.")
raise ServiceNotFoundException(
"The service you are searching for does not exist."
)
self._session.delete(service_entity)
self._session.commit()
self._session.commit()

View File

@ -4,7 +4,7 @@ import pytest
from sqlalchemy import Engine, create_engine, text
from sqlalchemy.orm import Session
from sqlalchemy.exc import OperationalError
from .services import user_test_data, tag_test_data
from .services import user_test_data, tag_test_data, service_test_data
from ..database import _engine_str
from ..env import getenv
@ -56,5 +56,6 @@ def session(test_engine: Engine):
def setup_insert_data_fixture(session: Session):
user_test_data.insert_fake_data(session)
tag_test_data.insert_fake_data(session)
service_test_data.insert_fake_data(session)
session.commit()
yield

View File

@ -2,19 +2,21 @@ 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
def test_get_all(service_svc: ServiceService):
service = service_svc.get_all(user_test_data.admin)
assert len(service) == len(service_test_data.services)
def test_get_by_id(service_svc: ServiceService):
if service_test_data.service_1.id != None:
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
@ -47,7 +49,9 @@ 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(
user_test_data.admin, service_test_data.new_service
)
pytest.fail()
@ -60,4 +64,4 @@ def test_delete(service_svc: ServiceService):
def test_delete_not_found(service_svc: ServiceService):
with pytest.raises(ServiceNotFoundException):
service_svc.delete(user_test_data.admin, service_test_data.service_7)
pytest.fail()
pytest.fail()

View File

@ -1,88 +1,89 @@
import pytest
from sqlalchemy.orm import Session
from backend.entities.service_entity import ServiceEntity
from backend.models.service_model import Service
from backend.models.enum_for_models import ProgramTypeEnum
from ...entities import ServiceEntity
from ...models.enum_for_models import ProgramTypeEnum
from ...models.service_model import Service
service_1 = Service(
id = 1,
name = "service 1",
status= "open",
summary= "presentation educating community on domestic violence",
requirements= [""],
program= ProgramTypeEnum.COMMUNITY
id=1,
name="service 1",
status="open",
summary="presentation educating community on domestic violence",
requirements=[""],
program=ProgramTypeEnum.COMMUNITY,
)
service_2 = Service(
id = 2,
name = "service 2",
status= "closed",
summary= "service finding safe places to stay",
requirements= [""],
program= ProgramTypeEnum.DOMESTIC
id=2,
name="service 2",
status="closed",
summary="service finding safe places to stay",
requirements=[""],
program=ProgramTypeEnum.DOMESTIC,
)
service_3 = Service(
id = 3,
name = "service 3",
status= "open",
summary= "",
requirements= [""],
program= ProgramTypeEnum.DOMESTIC
id=3,
name="service 3",
status="open",
summary="",
requirements=[""],
program=ProgramTypeEnum.DOMESTIC,
)
service_4 = Service(
id = 4,
name = "service 4",
status= "waitlist",
summary= "community event",
requirements= [""],
program= ProgramTypeEnum.COMMUNITY
id=4,
name="service 4",
status="waitlist",
summary="community event",
requirements=[""],
program=ProgramTypeEnum.COMMUNITY,
)
service_5 = Service(
id = 5,
name = "service 5",
status= "open",
summary= "talk circle for victims of domestic violence",
requirements= ["18+"],
program= ProgramTypeEnum.COMMUNITY
id=5,
name="service 5",
status="open",
summary="talk circle for victims of domestic violence",
requirements=["18+"],
program=ProgramTypeEnum.COMMUNITY,
)
service_6 = Service(
id = 6,
name = "service 6",
status= "waitlist",
summary= "program offering economic assistance",
requirements= [""],
program= ProgramTypeEnum.ECONOMIC
id=6,
name="service 6",
status="waitlist",
summary="program offering economic assistance",
requirements=[""],
program=ProgramTypeEnum.ECONOMIC,
)
service_6_edit = Service(
id = 6,
name = "service 6",
status= "open",
summary= "program offering economic assistance",
requirements= ["18+"],
program= ProgramTypeEnum.ECONOMIC
id=6,
name="service 6",
status="open",
summary="program offering economic assistance",
requirements=["18+"],
program=ProgramTypeEnum.ECONOMIC,
)
service_7 = Service(
id = 7,
name = "service 7",
status= "waitlist",
summary= "insert generic description",
requirements= [""],
program= ProgramTypeEnum.ECONOMIC
id=7,
name="service 7",
status="waitlist",
summary="insert generic description",
requirements=[""],
program=ProgramTypeEnum.ECONOMIC,
)
new_service = Service(
id = 8,
name = "new service",
status= "open",
summary= "insert other generic description",
requirements= [""],
program= ProgramTypeEnum.DOMESTIC
id=8,
name="new service",
status="open",
summary="insert other generic description",
requirements=[""],
program=ProgramTypeEnum.DOMESTIC,
)
services = [service_1, service_2, service_3, service_4, service_5, service_6]
@ -115,7 +116,6 @@ def reset_table_id_seq(
def insert_fake_data(session: Session):
"""Inserts fake organization data into the test session."""
global services
# Create entities for test organization data
@ -130,14 +130,3 @@ def insert_fake_data(session: Session):
# Commit all changes
session.commit()
@pytest.fixture(autouse=True)
def fake_data_fixture(session: Session):
"""Insert fake data the session automatically when test is run.
Note:
This function runs automatically due to the fixture property `autouse=True`.
"""
insert_fake_data(session)
session.commit()
yield

View File

@ -71,7 +71,7 @@ toDelete = User(
group="none",
program=[programs.COMMUNITY],
created_at=datetime.now(),
role=roles.VOLUNTEER
role=roles.VOLUNTEER,
)
users = [volunteer, employee, admin, toDelete]