Files
calminer/tests/test_import_api.py
zwitschi ce9c174b53
Some checks failed
CI / lint (push) Failing after 1m14s
CI / test (push) Has been skipped
CI / build (push) Has been skipped
feat: Enhance project and scenario creation with monitoring metrics
- Added monitoring metrics for project creation success and error handling in `ProjectRepository`.
- Implemented similar monitoring for scenario creation in `ScenarioRepository`.
- Refactored `run_monte_carlo` function in `simulation.py` to include timing and success/error metrics.
- Introduced new CSS styles for headers, alerts, and navigation buttons in `main.css` and `projects.css`.
- Created a new JavaScript file for navigation logic to handle chevron buttons.
- Updated HTML templates to include new navigation buttons and improved styling for buttons.
- Added tests for reporting service and routes to ensure proper functionality and access control.
- Removed unused imports and optimized existing test files for better clarity and performance.
2025-11-12 10:36:24 +01:00

102 lines
3.0 KiB
Python

from __future__ import annotations
from fastapi.testclient import TestClient
from models.project import MiningOperationType, Project
def test_project_import_preview_and_commit_flow(
client: TestClient,
unit_of_work_factory,
) -> None:
with unit_of_work_factory() as uow:
assert uow.projects is not None
existing = Project(
name="Existing Project",
location="Chile",
operation_type=MiningOperationType.OPEN_PIT,
)
uow.projects.create(existing)
csv_content = (
"name,location,operation_type\n"
"Existing Project,Peru,underground\n"
"New Project,Canada,open pit\n"
)
preview_response = client.post(
"/imports/projects/preview",
files={"file": ("projects.csv", csv_content, "text/csv")},
)
assert preview_response.status_code == 200
preview_data = preview_response.json()
assert preview_data["summary"]["accepted"] == 2
token = preview_data["stage_token"]
assert token
commit_response = client.post(
"/imports/projects/commit",
json={"token": token},
)
assert commit_response.status_code == 200
commit_data = commit_response.json()
assert commit_data["summary"] == {"created": 1, "updated": 1}
with unit_of_work_factory() as uow:
assert uow.projects is not None
projects = {project.name: project for project in uow.projects.list()}
assert "Existing Project" in projects and "New Project" in projects
assert (
projects["Existing Project"].operation_type
== MiningOperationType.UNDERGROUND
)
repeat_commit = client.post(
"/imports/projects/commit",
json={"token": token},
)
assert repeat_commit.status_code == 404
def test_scenario_import_commit_invalid_token_returns_404(
client: TestClient,
) -> None:
response = client.post(
"/imports/scenarios/commit",
json={"token": "missing-token"},
)
assert response.status_code == 404
assert "Unknown scenario import token" in response.json()["detail"]
def test_scenario_import_preview_rejects_invalid_currency(
client: TestClient,
unit_of_work_factory,
) -> None:
with unit_of_work_factory() as uow:
assert uow.projects is not None
project = Project(
name="Import Currency Project",
operation_type=MiningOperationType.OPEN_PIT,
)
uow.projects.create(project)
csv_content = (
"project_name,name,currency\n"
"Import Currency Project,Invalid Currency,US\n"
)
response = client.post(
"/imports/scenarios/preview",
files={"file": ("scenarios.csv", csv_content, "text/csv")},
)
assert response.status_code == 200
payload = response.json()
assert payload["summary"]["accepted"] == 0
assert payload["summary"]["errored"] == 1
assert payload["parser_errors"]
parser_error = payload["parser_errors"][0]
assert parser_error["field"] == "currency"
assert "Invalid currency code" in parser_error["message"]