FastAPI app that parses CMM inspection reports (PDF/Excel/CSV), computes SPC metrics (Cp/Cpk/Pp/Ppk, control limits, Shapiro-Wilk), generates interactive Plotly charts, and provides AI-powered quality summaries via Azure OpenAI with graceful fallback. Includes 21 passing tests covering parsers, SPC calculations, and API endpoints.
91 lines
2.3 KiB
Python
91 lines
2.3 KiB
Python
import tempfile
|
|
from pathlib import Path
|
|
|
|
import pandas as pd
|
|
|
|
from app.parsers.base import get_parser, match_column
|
|
from app.parsers.excel_parser import ExcelParser
|
|
from app.parsers.models import MeasurementRecord
|
|
|
|
|
|
def test_match_column_nominal():
|
|
assert match_column("Nominal") == "nominal"
|
|
assert match_column("NOM") == "nominal"
|
|
assert match_column("Target Value") == "nominal"
|
|
|
|
|
|
def test_match_column_actual():
|
|
assert match_column("Actual") == "actual"
|
|
assert match_column("Measured") == "actual"
|
|
|
|
|
|
def test_match_column_unknown():
|
|
assert match_column("random_xyz") is None
|
|
|
|
|
|
def test_get_parser_pdf():
|
|
p = get_parser("report.pdf")
|
|
from app.parsers.pdf_parser import PDFParser
|
|
assert isinstance(p, PDFParser)
|
|
|
|
|
|
def test_get_parser_excel():
|
|
p = get_parser("data.xlsx")
|
|
assert isinstance(p, ExcelParser)
|
|
|
|
|
|
def test_get_parser_csv():
|
|
p = get_parser("data.csv")
|
|
assert isinstance(p, ExcelParser)
|
|
|
|
|
|
def test_get_parser_unsupported():
|
|
try:
|
|
get_parser("image.png")
|
|
assert False, "Should have raised"
|
|
except ValueError:
|
|
pass
|
|
|
|
|
|
def test_measurement_record_properties():
|
|
rec = MeasurementRecord(
|
|
feature_name="D1",
|
|
nominal=10.0,
|
|
tolerance_plus=0.05,
|
|
tolerance_minus=-0.05,
|
|
actual=10.02,
|
|
)
|
|
assert rec.usl == 10.05
|
|
assert rec.lsl == 9.95
|
|
assert rec.in_tolerance is True
|
|
|
|
|
|
def test_measurement_record_out_of_tolerance():
|
|
rec = MeasurementRecord(
|
|
feature_name="D1",
|
|
nominal=10.0,
|
|
tolerance_plus=0.05,
|
|
tolerance_minus=-0.05,
|
|
actual=10.10,
|
|
)
|
|
assert rec.in_tolerance is False
|
|
|
|
|
|
def test_excel_parser_with_standard_headers():
|
|
df = pd.DataFrame({
|
|
"Feature Name": ["D1", "D2", "D3"],
|
|
"Nominal": [10.0, 20.0, 30.0],
|
|
"Actual": [10.02, 19.98, 30.05],
|
|
"Tol+": [0.05, 0.10, 0.10],
|
|
"Tol-": [-0.05, -0.10, -0.10],
|
|
})
|
|
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as f:
|
|
df.to_excel(f.name, index=False)
|
|
parser = ExcelParser()
|
|
report = parser.parse(Path(f.name))
|
|
|
|
assert len(report.measurements) == 3
|
|
assert report.measurements[0].feature_name == "D1"
|
|
assert report.measurements[0].nominal == 10.0
|
|
assert report.measurements[0].actual == 10.02
|