Skip to content

Commit

Permalink
write tests confirming session scoping
Browse files Browse the repository at this point in the history
  • Loading branch information
JLoveUOA committed Dec 9, 2024
1 parent 1f2f6cb commit 94679e5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
27 changes: 13 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
from models.services import ResearchDriveService
from models.submission import DriveOffboardSubmission

ROLES = prepopulate_roles()


def random_role() -> Role:
"Choose a random role from the prepoulated roles"
return random.choice(ROLES)


@pytest.fixture(name="session")
def session_fixture() -> Session:
Expand Down Expand Up @@ -50,14 +57,6 @@ def submission() -> dict[str, Any]:
}


ROLES = prepopulate_roles()


def random_role() -> Role:
"Choose a random role from the prepoulated roles"
return random.choice(ROLES)


@pytest.fixture
def person_factory(session: Session) -> SQLAlchemyModelFactory:
"fixture for person factories"
Expand All @@ -67,7 +66,7 @@ class PersonFactory(SQLAlchemyModelFactory):

class Meta:
model = Person
sqlalchemy_session_factory = session
sqlalchemy_session = session

id: int = factory.sequence(lambda n: n)
email = random.choice([factory.Faker("email"), None])
Expand All @@ -88,7 +87,7 @@ class DriveOffboardSubmissionFactory(SQLAlchemyModelFactory):

class Meta:
model = DriveOffboardSubmission
sqlalchemy_session_factory = session
sqlalchemy_session = session

retention_period_years = random.choice(
[6, 10, 20, 26, random.randrange(7, 100)]
Expand All @@ -115,7 +114,7 @@ class ResearchDriveServiceFactory(SQLAlchemyModelFactory):

class Meta:
model = ResearchDriveService
sqlalchemy_session_factory = session
sqlalchemy_session = session

id: int = factory.sequence(lambda n: n)

Expand Down Expand Up @@ -147,7 +146,7 @@ class MemberFactory(SQLAlchemyModelFactory):

class Meta:
model = Member
sqlalchemy_session_factory = session
sqlalchemy_session = session

role = random_role()
project = None
Expand All @@ -165,7 +164,7 @@ class CodeFactory(SQLAlchemyModelFactory):

class Meta:
model = Code
sqlalchemy_session_factory = session
sqlalchemy_session = session

code = factory.Faker("bothify", text="????????#####")

Expand All @@ -186,7 +185,7 @@ class ProjectFactory(SQLAlchemyModelFactory):

class Meta:
model = Project
sqlalchemy_session_factory = session
sqlalchemy_session = session

title = factory.Faker("sentence")
description = factory.Faker("paragraph")
Expand Down
32 changes: 28 additions & 4 deletions tests/test_factories.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@

"functions and classes for testing the factories for populating classes"
from factory.alchemy import SQLAlchemyModelFactory
from sqlmodel import Session, select

from models.project import Project


def test_factories_all(# pylint: disable=too-many-arguments,too-many-positional-arguments
def test_factories_all( # pylint: disable=too-many-arguments,too-many-positional-arguments
person_factory: SQLAlchemyModelFactory,
member_factory: SQLAlchemyModelFactory,
code_factory: SQLAlchemyModelFactory,
research_drive_service_factory: SQLAlchemyModelFactory,
drive_offboard_submission_factory: SQLAlchemyModelFactory,
project_factory: SQLAlchemyModelFactory
) -> None:
project_factory: SQLAlchemyModelFactory,
) -> None:
"Test the basic functionality of all factories"
person_factory.build()
member_factory.build()
code_factory.build()
research_drive_service_factory.build()
drive_offboard_submission_factory.build()
project_factory.build()


def test_project_retrieval(
project_factory: SQLAlchemyModelFactory, session: Session
) -> None:
"test factories create within DB correctly"
project = project_factory.create(title="test12345")

project_query = select(Project).where(Project.title == "test12345")
project_found = session.exec(project_query).first()
assert project_found == project


def test_project_session_scope(
project_factory: SQLAlchemyModelFactory, session: Session
) -> None:
"check session scope is only within each unit test for factories"
project_factory.create(title="test54321")

project_query = select(Project).where(Project.title == "test12345")
project_found = session.exec(project_query).first()
assert project_found is None

0 comments on commit 94679e5

Please sign in to comment.