209 lines
6.9 KiB
Python
209 lines
6.9 KiB
Python
"""Unit tests for reporting data models."""
|
|
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
import pandas as pd
|
|
import pytest
|
|
|
|
from py_dvt_ate.data.models import TestResult, TestRun, TestStatus
|
|
from py_dvt_ate.reporting.models import ReportConfig, ReportData
|
|
|
|
|
|
class TestReportConfig:
|
|
"""Tests for ReportConfig dataclass."""
|
|
|
|
def test_default_values(self) -> None:
|
|
"""Test default configuration values."""
|
|
config = ReportConfig()
|
|
|
|
assert config.company_name == "py_dvt_ate"
|
|
assert config.logo_path is None
|
|
assert config.include_charts is True
|
|
assert config.chart_dpi == 150
|
|
|
|
def test_custom_values(self) -> None:
|
|
"""Test configuration with custom values."""
|
|
config = ReportConfig(
|
|
company_name="Test Company",
|
|
logo_path=Path("/path/to/logo.png"),
|
|
include_charts=False,
|
|
chart_dpi=300,
|
|
)
|
|
|
|
assert config.company_name == "Test Company"
|
|
assert config.logo_path == Path("/path/to/logo.png")
|
|
assert config.include_charts is False
|
|
assert config.chart_dpi == 300
|
|
|
|
|
|
class TestReportData:
|
|
"""Tests for ReportData dataclass."""
|
|
|
|
@pytest.fixture
|
|
def sample_run(self) -> TestRun:
|
|
"""Create a sample test run."""
|
|
return TestRun(
|
|
id="12345678-1234-1234-1234-123456789abc",
|
|
test_name="tempco",
|
|
started_at=datetime(2024, 1, 15, 10, 30, 0),
|
|
completed_at=datetime(2024, 1, 15, 10, 35, 0),
|
|
status=TestStatus.PASSED,
|
|
config_json='{"temperatures": [-40, 25, 85]}',
|
|
operator="test_user",
|
|
description="Test description",
|
|
)
|
|
|
|
@pytest.fixture
|
|
def sample_results(self) -> list[TestResult]:
|
|
"""Create sample test results."""
|
|
return [
|
|
TestResult(
|
|
id="result-1",
|
|
test_run_id="12345678-1234-1234-1234-123456789abc",
|
|
parameter="tempco",
|
|
value=45.0,
|
|
unit="ppm/C",
|
|
measured_at=datetime(2024, 1, 15, 10, 35, 0),
|
|
lower_limit=None,
|
|
upper_limit=100.0,
|
|
),
|
|
TestResult(
|
|
id="result-2",
|
|
test_run_id="12345678-1234-1234-1234-123456789abc",
|
|
parameter="output_voltage_25c",
|
|
value=3.3001,
|
|
unit="V",
|
|
measured_at=datetime(2024, 1, 15, 10, 33, 0),
|
|
lower_limit=3.2,
|
|
upper_limit=3.4,
|
|
),
|
|
]
|
|
|
|
def test_basic_report_data(
|
|
self, sample_run: TestRun, sample_results: list[TestResult]
|
|
) -> None:
|
|
"""Test creating basic report data."""
|
|
data = ReportData(run=sample_run, results=sample_results)
|
|
|
|
assert data.run == sample_run
|
|
assert data.results == sample_results
|
|
assert data.measurements is None
|
|
assert data.charts == {}
|
|
|
|
def test_passed_count(
|
|
self, sample_run: TestRun, sample_results: list[TestResult]
|
|
) -> None:
|
|
"""Test passed_count property."""
|
|
data = ReportData(run=sample_run, results=sample_results)
|
|
|
|
# Both results should pass (within limits)
|
|
assert data.passed_count == 2
|
|
|
|
def test_failed_count(self, sample_run: TestRun) -> None:
|
|
"""Test failed_count property with failed results."""
|
|
failed_results = [
|
|
TestResult(
|
|
id="result-1",
|
|
test_run_id="12345678-1234-1234-1234-123456789abc",
|
|
parameter="tempco",
|
|
value=150.0, # Exceeds upper limit
|
|
unit="ppm/C",
|
|
measured_at=datetime(2024, 1, 15, 10, 35, 0),
|
|
lower_limit=None,
|
|
upper_limit=100.0,
|
|
),
|
|
]
|
|
|
|
data = ReportData(run=sample_run, results=failed_results)
|
|
|
|
assert data.failed_count == 1
|
|
assert data.passed_count == 0
|
|
|
|
def test_overall_status_pass(
|
|
self, sample_run: TestRun, sample_results: list[TestResult]
|
|
) -> None:
|
|
"""Test overall_status when all tests pass."""
|
|
data = ReportData(run=sample_run, results=sample_results)
|
|
|
|
assert data.overall_status == "PASS"
|
|
|
|
def test_overall_status_fail(self, sample_run: TestRun) -> None:
|
|
"""Test overall_status when tests fail."""
|
|
failed_results = [
|
|
TestResult(
|
|
id="result-1",
|
|
test_run_id="12345678-1234-1234-1234-123456789abc",
|
|
parameter="tempco",
|
|
value=150.0, # Exceeds upper limit
|
|
unit="ppm/C",
|
|
measured_at=datetime(2024, 1, 15, 10, 35, 0),
|
|
lower_limit=None,
|
|
upper_limit=100.0,
|
|
),
|
|
]
|
|
|
|
data = ReportData(run=sample_run, results=failed_results)
|
|
|
|
assert data.overall_status == "FAIL"
|
|
|
|
def test_overall_status_error(self) -> None:
|
|
"""Test overall_status when run status is error."""
|
|
error_run = TestRun(
|
|
id="12345678-1234-1234-1234-123456789abc",
|
|
test_name="tempco",
|
|
started_at=datetime(2024, 1, 15, 10, 30, 0),
|
|
status=TestStatus.ERROR,
|
|
config_json="{}",
|
|
)
|
|
|
|
data = ReportData(run=error_run, results=[])
|
|
|
|
assert data.overall_status == "ERROR"
|
|
|
|
def test_with_measurements(
|
|
self, sample_run: TestRun, sample_results: list[TestResult]
|
|
) -> None:
|
|
"""Test report data with measurements DataFrame."""
|
|
measurements = pd.DataFrame(
|
|
{
|
|
"timestamp": [0.0, 1.0, 2.0],
|
|
"parameter": ["output_voltage", "output_voltage", "output_voltage"],
|
|
"value": [3.30, 3.31, 3.30],
|
|
"unit": ["V", "V", "V"],
|
|
"temperature": [25.0, 25.0, 25.0],
|
|
}
|
|
)
|
|
|
|
data = ReportData(
|
|
run=sample_run, results=sample_results, measurements=measurements
|
|
)
|
|
|
|
assert data.measurements is not None
|
|
assert len(data.measurements) == 3
|
|
|
|
def test_with_charts(
|
|
self, sample_run: TestRun, sample_results: list[TestResult]
|
|
) -> None:
|
|
"""Test report data with chart images."""
|
|
charts = {
|
|
"Voltage vs Temperature": "base64_encoded_image_data",
|
|
"Results Summary": "another_base64_image",
|
|
}
|
|
|
|
data = ReportData(run=sample_run, results=sample_results, charts=charts)
|
|
|
|
assert len(data.charts) == 2
|
|
assert "Voltage vs Temperature" in data.charts
|
|
|
|
def test_with_custom_config(
|
|
self, sample_run: TestRun, sample_results: list[TestResult]
|
|
) -> None:
|
|
"""Test report data with custom configuration."""
|
|
config = ReportConfig(company_name="Test Company", include_charts=False)
|
|
|
|
data = ReportData(run=sample_run, results=sample_results, config=config)
|
|
|
|
assert data.config.company_name == "Test Company"
|
|
assert data.config.include_charts is False
|