Initial commit
This commit is contained in:
80
backend/tests/test_analysis.py
Normal file
80
backend/tests/test_analysis.py
Normal file
@@ -0,0 +1,80 @@
|
||||
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"]
|
||||
37
backend/tests/test_upload.py
Normal file
37
backend/tests/test_upload.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from fastapi.testclient import TestClient
|
||||
import pandas as pd
|
||||
import io
|
||||
import pyarrow as pa
|
||||
from main import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
def test_health_check():
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"status": "ok", "service": "backend"}
|
||||
|
||||
def test_upload_csv():
|
||||
# Create a dummy CSV
|
||||
csv_content = "name,age\nAlice,30\nBob,25"
|
||||
file = ("test.csv", csv_content, "text/csv")
|
||||
|
||||
response = client.post("/api/v1/upload", files={"file": file})
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"] == "application/vnd.apache.arrow.stream"
|
||||
assert "X-Column-Metadata" in response.headers
|
||||
|
||||
# Verify Arrow data
|
||||
buffer = io.BytesIO(response.content)
|
||||
with pa.ipc.open_stream(buffer) as reader:
|
||||
table = reader.read_all()
|
||||
df = table.to_pandas()
|
||||
assert df.shape == (2, 2)
|
||||
assert list(df.columns) == ["name", "age"]
|
||||
|
||||
def test_upload_invalid_format():
|
||||
file = ("test.txt", "invalid content", "text/plain")
|
||||
response = client.post("/api/v1/upload", files={"file": file})
|
||||
assert response.status_code == 400
|
||||
assert "Only .xlsx, .xls and .csv files are supported" in response.json()["detail"]
|
||||
Reference in New Issue
Block a user