81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
from fastapi.testclient import TestClient
|
|
from main import app
|
|
|
|
client = TestClient(app)
|
|
|
|
def test_outlier_detection_univariate():
|
|
# Data with one clear outlier (100)
|
|
data = [
|
|
{"val": 10}, {"val": 11}, {"val": 12}, {"val": 10}, {"val": 100}
|
|
]
|
|
request_data = {
|
|
"data": data,
|
|
"columns": ["val"],
|
|
"method": "univariate"
|
|
}
|
|
|
|
response = client.post("/api/v1/analysis/detect-outliers", json=request_data)
|
|
assert response.status_code == 200
|
|
json_resp = response.json()
|
|
assert json_resp["total_count"] == 1
|
|
assert json_resp["outliers"][0]["index"] == 4
|
|
assert "IQR bounds" in json_resp["outliers"][0]["reasons"][0]
|
|
|
|
def test_outlier_detection_multivariate():
|
|
# Data with a multivariate anomaly
|
|
data = [
|
|
{"x": 1, "y": 1}, {"x": 1.1, "y": 0.9}, {"x": 0.9, "y": 1.1},
|
|
{"x": 10, "y": 10} # Anomaly
|
|
]
|
|
request_data = {
|
|
"data": data,
|
|
"columns": ["x", "y"],
|
|
"method": "multivariate"
|
|
}
|
|
|
|
response = client.post("/api/v1/analysis/detect-outliers", json=request_data)
|
|
assert response.status_code == 200
|
|
json_resp = response.json()
|
|
assert json_resp["total_count"] >= 1
|
|
|
|
def test_feature_importance():
|
|
data = [
|
|
{"x1": 1, "x2": 10, "y": 2},
|
|
{"x1": 2, "x2": 20, "y": 4},
|
|
{"x1": 3, "x2": 30, "y": 6},
|
|
{"x1": 4, "x2": 40, "y": 8},
|
|
{"x1": 5, "x2": 50, "y": 10}
|
|
]
|
|
request_data = {
|
|
"data": data,
|
|
"features": ["x1", "x2"],
|
|
"target": "y"
|
|
}
|
|
response = client.post("/api/v1/analysis/feature-importance", json=request_data)
|
|
assert response.status_code == 200
|
|
json_resp = response.json()
|
|
assert len(json_resp["importances"]) == 2
|
|
assert "feature" in json_resp["importances"][0]
|
|
assert "score" in json_resp["importances"][0]
|
|
|
|
def test_run_regression():
|
|
data = [
|
|
{"x": 1, "y": 2.1},
|
|
{"x": 2, "y": 3.9},
|
|
{"x": 3, "y": 6.2},
|
|
{"x": 4, "y": 8.1},
|
|
{"x": 5, "y": 10.3}
|
|
]
|
|
request_data = {
|
|
"data": data,
|
|
"x_features": ["x"],
|
|
"y_target": "y",
|
|
"model_type": "linear"
|
|
}
|
|
response = client.post("/api/v1/analysis/run-regression", json=request_data)
|
|
assert response.status_code == 200
|
|
json_resp = response.json()
|
|
assert "results" in json_resp
|
|
assert json_resp["results"]["r_squared"] > 0.9
|
|
assert "const" in json_resp["results"]["coefficients"]
|