File size: 6,089 Bytes
2c29579 1120492 2c29579 1120492 2c29579 807485b 1120492 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | import json
import math
import os
from infra.database import DatasetModel, ExperimentRun, JobModel
from tests.conftest import TestingSessionLocal
def test_health_check(client):
response = client.get("/health")
assert response.status_code == 200
assert response.json()["status"] == "ok"
def test_get_jobs_empty(client):
# Depending on DB setup, it might be empty
response = client.get("/api/jobs")
assert response.status_code == 200
assert isinstance(response.json(), list)
def test_get_experiments(client):
response = client.get("/api/experiments")
assert response.status_code == 200
assert isinstance(response.json(), list)
def test_get_leaderboard(client):
response = client.get("/api/leaderboard")
assert response.status_code == 200
assert isinstance(response.json(), list)
def test_404_not_found(client):
response = client.get("/api/this_does_not_exist")
assert response.status_code == 404
def test_status_endpoint_sanitizes_nan_payloads(client):
with TestingSessionLocal() as db:
db.add(
JobModel(
id="job-with-nan",
dataset_id="ds-1",
status="completed",
history_json=json.dumps([{"time": "Final", "metric": math.nan}]),
results_json='{"best_model":"LGBM","score":NaN,"leaderboard":[{"model":"LGBM","score":NaN}]}',
insights_json='{"confidence":NaN}',
reasoning_json=json.dumps(["done"]),
)
)
db.commit()
response = client.get("/api/status/job-with-nan")
assert response.status_code == 200
body = response.json()
assert body["history"] == [{"time": "Final", "metric": None}]
assert body["results"]["score"] == 0.0
assert body["results"]["leaderboard"] == [{"model": "LGBM", "score": None}]
assert body["insights"] == {"confidence": None}
def test_experiments_endpoint_sanitizes_nan_payloads(client):
with TestingSessionLocal() as db:
db.add(
ExperimentRun(
id="exp-with-nan",
job_id="job-exp-nan",
dataset_id="ds-1",
model_name="LGBM",
metric_name="Accuracy",
score=math.nan,
task_type="classification",
mode="Balanced",
goal="Performance",
feature_count=5,
row_count=100,
hyperparams_json='{"depth": 8, "lr": NaN}',
metrics_json='{"precision": NaN, "recall": 88.1}',
leaderboard_json='[{"model":"LGBM","score":NaN}]',
)
)
db.commit()
response = client.get("/api/experiments")
assert response.status_code == 200
rows = response.json()
target = next(row for row in rows if row["id"] == "exp-with-nan")
assert target["score"] is None
assert target["hyperparams"] == {"depth": 8, "lr": None}
assert target["metrics"] == {"precision": None, "recall": 88.1}
assert target["leaderboard"] == [{"model": "LGBM", "score": None}]
def test_workspace_latest_restores_dataset_and_job(client, tmp_path):
csv_path = tmp_path / "workspace.csv"
csv_path.write_text("feature,target\n1,yes\n2,no\n", encoding="utf-8")
with TestingSessionLocal() as db:
db.add(
DatasetModel(
id="workspace-ds",
file_path=os.fspath(csv_path),
profile_json=json.dumps(
{
"rows": 2,
"cols": 2,
"columns": ["feature", "target"],
"suggested_target": "target",
"task_type": "classification",
}
),
source_type="upload",
)
)
db.add(
JobModel(
id="workspace-job",
dataset_id="workspace-ds",
status="completed",
history_json=json.dumps([{"time": "Final", "metric": 93.2}]),
results_json=json.dumps({"best_model": "LiteGBM", "score": 93.2}),
)
)
db.commit()
response = client.get("/api/workspace/latest")
assert response.status_code == 200
body = response.json()
assert body["dataset"]["id"] == "workspace-ds"
assert body["dataset"]["profile"]["suggested_target"] == "target"
assert body["dataset"]["preview_records"][0]["feature"] == 1
assert body["job"]["id"] == "workspace-job"
def test_workspace_restore_honors_explicit_job_id(client):
with TestingSessionLocal() as db:
if not db.query(DatasetModel).filter(DatasetModel.id == "workspace-ds").first():
db.add(
DatasetModel(
id="workspace-ds",
file_path="tests/data/dummy_data.csv",
profile_json=json.dumps(
{
"rows": 2,
"cols": 2,
"columns": ["feature", "target"],
"suggested_target": "target",
"task_type": "classification",
}
),
source_type="upload",
)
)
if not db.query(JobModel).filter(JobModel.id == "workspace-job").first():
db.add(
JobModel(
id="workspace-job",
dataset_id="workspace-ds",
status="completed",
history_json=json.dumps([{"time": "Final", "metric": 93.2}]),
results_json=json.dumps({"best_model": "LiteGBM", "score": 93.2}),
)
)
db.commit()
response = client.get("/api/workspace/restore", params={"job_id": "workspace-job"})
assert response.status_code == 200
body = response.json()
assert body["job"]["id"] == "workspace-job"
assert body["dataset"]["id"] == "workspace-ds"
|