mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-09 14:00:15 -04:00
96 lines
2.7 KiB
Python
96 lines
2.7 KiB
Python
from sqlalchemy.orm import Session
|
|
from datetime import datetime
|
|
|
|
from ...entities import ResourceEntity
|
|
from ...models.enum_for_models import ProgramTypeEnum
|
|
from ...models.resource_model import Resource
|
|
|
|
resource_1 = Resource(
|
|
id=1,
|
|
name="Resource 1",
|
|
summary="Helpful information for victims of domestic violence",
|
|
link="https://example.com/resource1",
|
|
program=ProgramTypeEnum.DOMESTIC,
|
|
created_at=datetime(2023, 6, 1, 10, 0, 0),
|
|
)
|
|
|
|
resource_2 = Resource(
|
|
id=2,
|
|
name="Resource 2",
|
|
summary="Legal assistance resources",
|
|
link="https://example.com/resource2",
|
|
program=ProgramTypeEnum.COMMUNITY,
|
|
created_at=datetime(2023, 6, 2, 12, 30, 0),
|
|
)
|
|
|
|
resource_3 = Resource(
|
|
id=3,
|
|
name="Resource 3",
|
|
summary="Financial aid resources",
|
|
link="https://example.com/resource3",
|
|
program=ProgramTypeEnum.ECONOMIC,
|
|
created_at=datetime(2023, 6, 3, 15, 45, 0),
|
|
)
|
|
|
|
resource_4 = Resource(
|
|
id=4,
|
|
name="Resource 4",
|
|
summary="Counseling and support groups",
|
|
link="https://example.com/resource4",
|
|
program=ProgramTypeEnum.DOMESTIC,
|
|
created_at=datetime(2023, 6, 4, 9, 15, 0),
|
|
)
|
|
|
|
resource_5 = Resource(
|
|
id=5,
|
|
name="Resource 5",
|
|
summary="Shelter and housing resources",
|
|
link="https://example.com/resource5",
|
|
program=ProgramTypeEnum.DOMESTIC,
|
|
created_at=datetime(2023, 6, 5, 11, 30, 0),
|
|
)
|
|
|
|
resources = [resource_1, resource_2, resource_3, resource_4, resource_5]
|
|
|
|
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 resource data into the test session."""
|
|
global resources
|
|
# Create entities for test resource data
|
|
entities = []
|
|
for resource in resources:
|
|
entity = ResourceEntity.from_model(resource)
|
|
session.add(entity)
|
|
entities.append(entity)
|
|
|
|
# Reset table IDs to prevent ID conflicts
|
|
reset_table_id_seq(session, ResourceEntity, ResourceEntity.id, len(resources) + 1)
|
|
|
|
# Commit all changes
|
|
session.commit()
|