mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-09 14:00:15 -04:00
102 lines
2.5 KiB
Python
102 lines
2.5 KiB
Python
|
|
import pytest
|
|
from sqlalchemy.orm import Session
|
|
from ...models.user_model import User
|
|
from ...entities.program_enum import ProgramEnum
|
|
from ...entities.user_enum import RoleEnum
|
|
from ...entities.user_entity import UserEntity
|
|
|
|
|
|
programs = ProgramEnum
|
|
roles = RoleEnum
|
|
|
|
volunteer = User(
|
|
id = 1,
|
|
username="volunteer",
|
|
email="volunteer@compass.com",
|
|
experience="1 year",
|
|
group="volunteers",
|
|
programtype=[programs.ECONOMIC],
|
|
usertype=roles.VOLUNTEER,
|
|
)
|
|
|
|
employee = User(
|
|
id = 2,
|
|
username="employee",
|
|
email="employee@compass.com",
|
|
experience="5 years",
|
|
group="employees",
|
|
programtype=[programs.DOMESTIC, programs.COMMUNITY],
|
|
usertype=roles.EMPLOYEE,
|
|
)
|
|
|
|
admin = User(
|
|
id = 3,
|
|
username="admin",
|
|
email="admin@compass.com",
|
|
experience="10 years",
|
|
group="admin",
|
|
programtype=[programs.DOMESTIC, programs.COMMUNITY, programs.ECONOMIC],
|
|
usertype=roles.ADMIN,
|
|
)
|
|
|
|
users=[volunteer, employee, admin]
|
|
|
|
|
|
|
|
from sqlalchemy import text
|
|
from sqlalchemy.orm import Session, DeclarativeBase, InstrumentedAttribute
|
|
|
|
|
|
def reset_table_id_seq(
|
|
session: Session,
|
|
entity: type[DeclarativeBase],
|
|
entity_id_column: InstrumentedAttribute[int],
|
|
next_id: int,
|
|
) -> None:
|
|
"""Reset the ID sequence of an entity table.
|
|
|
|
Args:
|
|
session (Session) - A SQLAlchemy Session
|
|
entity (DeclarativeBase) - The SQLAlchemy Entity table to target
|
|
entity_id_column (MappedColumn) - The ID column (should be an int column)
|
|
next_id (int) - Where the next inserted, autogenerated ID should begin
|
|
|
|
Returns:
|
|
None"""
|
|
table = entity.__table__
|
|
id_column_name = entity_id_column.name
|
|
sql = text(f"ALTER SEQUENCe {table}_{id_column_name}_seq RESTART WITH {next_id}")
|
|
session.execute(sql)
|
|
|
|
|
|
def insert_fake_data(session: Session):
|
|
"""Inserts fake organization data into the test session."""
|
|
|
|
global users
|
|
|
|
# Create entities for test organization data
|
|
entities = []
|
|
for user in users:
|
|
entity = UserEntity.from_model(user)
|
|
session.add(entity)
|
|
entities.append(entity)
|
|
|
|
# Reset table IDs to prevent ID conflicts
|
|
reset_table_id_seq(
|
|
session, UserEntity, UserEntity.id, len(users) + 1
|
|
)
|
|
|
|
# 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 |