Files
anorandClaude Opus 4.8 3055c67ec9 feat: Фаза 4.5 — приём замеров со стенда обратно в базу
bench.py: BenchMeasurement (pydantic) → карточка с провенансом
стенд+дата+прибор + расхождение с симуляцией. record() пишет в
data/bench/measurements.json. record_bench.py CLI. Bench-слой
интегрирован в индекс (iter_bench_chunks, citation «стенд <дата>
(<прибор>)»). Граница: реальные замеры делает человек — модуль
только принимает, ничего не выдумывает. Тесты +7.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 12:41:42 +10:00

72 lines
3.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Тесты Фазы 4.5 (железо-контур). Замер → валидация → карточка → попадание в
индекс через bench-слой. Без приборов: проверяем инфраструктуру приёма данных.
"""
import json
import pytest
from pydantic import ValidationError
from rag.index import iter_bench_chunks
from rag.schemas import BenchMeasurement, record
def test_measurement_validation_rejects_empty():
with pytest.raises(ValidationError):
BenchMeasurement(date="2026-06-10", instrument="", quantity="f",
value=1.6, unit="кГц") # instrument пуст
def test_deviation_pct():
m = BenchMeasurement(date="2026-06-10", instrument="частотомер",
quantity="f0", value=1.58, unit="кГц", predicted=1.60)
assert m.deviation_pct() == pytest.approx(1.25, abs=0.01)
def test_deviation_none_without_prediction():
m = BenchMeasurement(date="2026-06-10", instrument="ч", quantity="f0",
value=1.6, unit="кГц")
assert m.deviation_pct() is None
def test_to_card_shape_and_provenance():
m = BenchMeasurement(date="2026-06-10", instrument="осциллограф Rigol",
quantity="резонансная частота", value=1.58, unit="кГц",
predicted=1.60, design_ref="lc_tank f0=1600")
card = m.to_card()
assert card["type"] == "номинал"
assert "1.58 кГц" in card["statement"]
assert "расхождение" in card["statement"]
assert card["sources"][0]["instrument"] == "осциллограф Rigol"
assert card["sources"][0]["date"] == "2026-06-10"
def test_record_appends_and_validates(tmp_path):
path = tmp_path / "measurements.json"
m1 = BenchMeasurement(date="2026-06-10", instrument="ч", quantity="f0",
value=1.58, unit="кГц")
record(m1, path=path)
m2 = BenchMeasurement(date="2026-06-11", instrument="ч", quantity="ток",
value=2.1, unit="А")
record(m2, path=path)
data = json.loads(path.read_text(encoding="utf-8"))
assert len(data["cards"]) == 2
assert data["bench_id"] == "stand_romanov"
def test_bench_chunk_integrates_into_index(tmp_path):
m = BenchMeasurement(date="2026-06-10", instrument="осциллограф",
quantity="резонансная частота контура", value=1.58,
unit="кГц", predicted=1.60)
record(m, path=tmp_path / "measurements.json")
chunks = list(iter_bench_chunks(tmp_path))
assert len(chunks) == 1
c = chunks[0]
assert c.layer == "bench"
assert c.citation() == "стенд 2026-06-10 (осциллограф)"
assert "1.58" in c.text
def test_iter_bench_empty_dir_is_safe(tmp_path):
# каталога ещё нет / пуст — не падаем (стенд не снимал)
assert list(iter_bench_chunks(tmp_path / "missing")) == []