feat: add export filters for projects and scenarios with filtering capabilities
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator
|
||||
from datetime import datetime, timezone
|
||||
from datetime import date, datetime, timezone
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
@@ -16,6 +16,7 @@ from models import (
|
||||
Project,
|
||||
Scenario,
|
||||
ScenarioStatus,
|
||||
ResourceType,
|
||||
SimulationParameter,
|
||||
StochasticVariable,
|
||||
)
|
||||
@@ -25,6 +26,7 @@ from services.repositories import (
|
||||
ScenarioRepository,
|
||||
SimulationParameterRepository,
|
||||
)
|
||||
from services.export_query import ProjectExportFilters, ScenarioExportFilters
|
||||
from services.unit_of_work import UnitOfWork
|
||||
|
||||
|
||||
@@ -136,6 +138,7 @@ def test_unit_of_work_commit_and_rollback(engine) -> None:
|
||||
|
||||
# Commit path
|
||||
with UnitOfWork(session_factory=TestingSession) as uow:
|
||||
assert uow.projects is not None
|
||||
uow.projects.create(
|
||||
Project(name="Project Delta", operation_type=MiningOperationType.PLACER)
|
||||
)
|
||||
@@ -147,6 +150,7 @@ def test_unit_of_work_commit_and_rollback(engine) -> None:
|
||||
# Rollback path
|
||||
with pytest.raises(RuntimeError):
|
||||
with UnitOfWork(session_factory=TestingSession) as uow:
|
||||
assert uow.projects is not None
|
||||
uow.projects.create(
|
||||
Project(name="Project Epsilon", operation_type=MiningOperationType.OTHER)
|
||||
)
|
||||
@@ -241,4 +245,122 @@ def test_financial_input_repository_latest_created_at(session: Session) -> None:
|
||||
)
|
||||
|
||||
latest = repo.latest_created_at()
|
||||
assert latest == new_timestamp
|
||||
assert latest == new_timestamp
|
||||
|
||||
|
||||
def test_project_repository_filtered_for_export(session: Session) -> None:
|
||||
repo = ProjectRepository(session)
|
||||
|
||||
alpha_created = datetime(2025, 1, 1, 9, 30, tzinfo=timezone.utc)
|
||||
alpha_updated = datetime(2025, 1, 2, 12, 0, tzinfo=timezone.utc)
|
||||
bravo_created = datetime(2025, 2, 1, 9, 30, tzinfo=timezone.utc)
|
||||
bravo_updated = datetime(2025, 2, 2, 12, 0, tzinfo=timezone.utc)
|
||||
|
||||
project_alpha = Project(
|
||||
name="Alpha",
|
||||
location="Nevada",
|
||||
operation_type=MiningOperationType.OPEN_PIT,
|
||||
description="Primary export candidate",
|
||||
)
|
||||
project_alpha.created_at = alpha_created
|
||||
project_alpha.updated_at = alpha_updated
|
||||
|
||||
project_bravo = Project(
|
||||
name="Bravo",
|
||||
location="Ontario",
|
||||
operation_type=MiningOperationType.UNDERGROUND,
|
||||
description="Excluded project",
|
||||
)
|
||||
project_bravo.created_at = bravo_created
|
||||
project_bravo.updated_at = bravo_updated
|
||||
|
||||
scenario_alpha = Scenario(
|
||||
name="Alpha Scenario",
|
||||
project=project_alpha,
|
||||
status=ScenarioStatus.ACTIVE,
|
||||
)
|
||||
|
||||
session.add_all([project_alpha, project_bravo, scenario_alpha])
|
||||
session.flush()
|
||||
|
||||
filters = ProjectExportFilters(
|
||||
ids=(project_alpha.id, project_alpha.id, -5),
|
||||
names=("Alpha", " alpha ", ""),
|
||||
name_contains="alp",
|
||||
locations=(" nevada ", ""),
|
||||
operation_types=(MiningOperationType.OPEN_PIT,),
|
||||
created_from=alpha_created,
|
||||
created_to=alpha_created,
|
||||
updated_from=alpha_updated,
|
||||
updated_to=alpha_updated,
|
||||
)
|
||||
|
||||
results = repo.filtered_for_export(filters, include_scenarios=True)
|
||||
|
||||
assert [project.name for project in results] == ["Alpha"]
|
||||
assert len(results[0].scenarios) == 1
|
||||
assert results[0].scenarios[0].name == "Alpha Scenario"
|
||||
|
||||
|
||||
def test_scenario_repository_filtered_for_export(session: Session) -> None:
|
||||
repo = ScenarioRepository(session)
|
||||
|
||||
project_export = Project(
|
||||
name="Export Project",
|
||||
operation_type=MiningOperationType.PLACER,
|
||||
)
|
||||
project_other = Project(
|
||||
name="Other Project",
|
||||
operation_type=MiningOperationType.OTHER,
|
||||
)
|
||||
|
||||
scenario_match = Scenario(
|
||||
name="Case Alpha",
|
||||
project=project_export,
|
||||
status=ScenarioStatus.ACTIVE,
|
||||
start_date=date(2025, 1, 5),
|
||||
end_date=date(2025, 2, 1),
|
||||
discount_rate=7.5,
|
||||
currency="usd",
|
||||
primary_resource=ResourceType.EXPLOSIVES,
|
||||
)
|
||||
scenario_match.created_at = datetime(2025, 1, 6, tzinfo=timezone.utc)
|
||||
scenario_match.updated_at = datetime(2025, 1, 16, tzinfo=timezone.utc)
|
||||
|
||||
scenario_other = Scenario(
|
||||
name="Case Beta",
|
||||
project=project_other,
|
||||
status=ScenarioStatus.DRAFT,
|
||||
start_date=date(2024, 12, 20),
|
||||
end_date=date(2025, 3, 1),
|
||||
currency="cad",
|
||||
primary_resource=ResourceType.WATER,
|
||||
)
|
||||
scenario_other.created_at = datetime(2024, 12, 25, tzinfo=timezone.utc)
|
||||
scenario_other.updated_at = datetime(2025, 3, 5, tzinfo=timezone.utc)
|
||||
|
||||
session.add_all([project_export, project_other, scenario_match, scenario_other])
|
||||
session.flush()
|
||||
|
||||
filters = ScenarioExportFilters(
|
||||
ids=(scenario_match.id, scenario_match.id, -1),
|
||||
project_ids=(project_export.id, 0),
|
||||
project_names=(" Export Project ", "EXPORT PROJECT"),
|
||||
name_contains="case",
|
||||
statuses=(ScenarioStatus.ACTIVE,),
|
||||
start_date_from=date(2025, 1, 1),
|
||||
start_date_to=date(2025, 1, 31),
|
||||
end_date_from=date(2025, 1, 31),
|
||||
end_date_to=date(2025, 2, 28),
|
||||
created_from=datetime(2025, 1, 1, tzinfo=timezone.utc),
|
||||
created_to=datetime(2025, 1, 31, tzinfo=timezone.utc),
|
||||
updated_from=datetime(2025, 1, 10, tzinfo=timezone.utc),
|
||||
updated_to=datetime(2025, 1, 31, tzinfo=timezone.utc),
|
||||
currencies=(" usd ", "USD"),
|
||||
primary_resources=(ResourceType.EXPLOSIVES,),
|
||||
)
|
||||
|
||||
results = repo.filtered_for_export(filters, include_project=True)
|
||||
|
||||
assert [scenario.name for scenario in results] == ["Case Alpha"]
|
||||
assert results[0].project.name == "Export Project"
|
||||
Reference in New Issue
Block a user