88 lines
2.5 KiB
Python
88 lines
2.5 KiB
Python
"""Tests for the web backend."""
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from pathlib import Path
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
from disk_checker.web.server import app
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
"""Create test client."""
|
|
return TestClient(app)
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_csv_content():
|
|
"""Sample CSV content for testing."""
|
|
return """nsec,read_clat_ns_count,read_clat_ns_cumulative,read_clat_ns_percentile
|
|
19791872,1,1,0.00016775708773695687
|
|
22413312,4,5,0.0008387854386847844
|
|
22675456,9,14,0.0023485992283173965
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_csv_path(tmp_path, sample_csv_content):
|
|
"""Create sample CSV file."""
|
|
path = tmp_path / "test.csv"
|
|
path.write_text(sample_csv_content)
|
|
return path
|
|
|
|
|
|
def test_health_check(client):
|
|
"""Test health check endpoint."""
|
|
response = client.get("/health")
|
|
assert response.status_code == 200
|
|
assert response.json()["status"] == "ok"
|
|
|
|
|
|
def test_scan_csv(client, sample_csv_path):
|
|
"""Test CSV scan endpoint."""
|
|
with open(sample_csv_path, "rb") as f:
|
|
response = client.post("/api/v1/scan", files={"file": f})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "metrics" in data
|
|
assert len(data["metrics"]) > 0
|
|
|
|
|
|
def test_plot_endpoint(client, sample_csv_path):
|
|
"""Test plot generation endpoint."""
|
|
with open(sample_csv_path, "rb") as f:
|
|
response = client.post(
|
|
"/api/v1/plot",
|
|
files={"file": (sample_csv_path.name, f, "text/csv")},
|
|
data={"unit": "us"},
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.headers["content-type"] == "image/png"
|
|
|
|
|
|
def test_plot_with_options(client, sample_csv_path):
|
|
"""Test plot generation with options."""
|
|
with open(sample_csv_path, "rb") as f:
|
|
response = client.post(
|
|
"/api/v1/plot",
|
|
files={"file": (sample_csv_path.name, f, "text/csv")},
|
|
data={
|
|
"metric": "read_clat",
|
|
"unit": "ms",
|
|
"log_x": "true",
|
|
"cdf": "true",
|
|
},
|
|
)
|
|
assert response.status_code == 200
|
|
|
|
|
|
def test_invalid_metric(client, sample_csv_path):
|
|
"""Test invalid metric returns error."""
|
|
with open(sample_csv_path, "rb") as f:
|
|
response = client.post(
|
|
"/api/v1/plot",
|
|
files={"file": (sample_csv_path.name, f, "text/csv")},
|
|
data={"metric": "invalid_metric", "unit": "us"},
|
|
)
|
|
assert response.status_code == 400 |