mitai-jinkendo/backend/tests/test_prompt_output_compact.py
Lars 7676897fda
All checks were successful
Deploy Development / deploy (push) Successful in 50s
Build Test / pytest-backend (push) Successful in 4s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 17s
feat: enhance normalization of metric values for improved handling
- Updated `_normalize_metric_value_for_read` to compact numeric strings and ensure consistent formatting for string data types.
- Enhanced `normalize_prompt_number` to handle numeric strings and non-finite float values effectively.
- Improved unit tests to validate the new normalization behavior for session metrics and scalar formatting.
2026-04-18 10:43:21 +02:00

90 lines
2.2 KiB
Python

"""Tests für data_layer.prompt_output_compact (KI-Platzhalter, Token)."""
import pytest
from data_layer.prompt_output_compact import (
compact_float_for_prompt,
compact_json_payload_for_prompts,
format_scalar_for_prompt_text,
normalize_prompt_number,
session_metrics_list_to_key_value_compact,
)
@pytest.mark.parametrize(
"x,expected",
[
(0.0, 0),
(123.456, 123),
(45.67, 46),
(9.876, 9.88),
(0.99, 0.99),
(0.055, 0.055),
(0.01234, 0.012),
],
)
def test_compact_float_for_prompt(x, expected):
out = compact_float_for_prompt(x)
if isinstance(expected, float):
assert abs(float(out) - expected) < 0.0001
else:
assert out == expected
def test_compact_json_nested():
raw = {"a": 12.345678, "b": {"c": 0.0666}, "d": [1.111, 2.0]}
out = compact_json_payload_for_prompts(raw)
assert out["a"] == 12
assert abs(out["b"]["c"] - 0.067) < 0.001
assert out["d"][0] == 1.11
def test_format_scalar_no_long_float_tail():
s = format_scalar_for_prompt_text(51.5818181818181818)
assert "181818" not in s
assert len(s) <= 8
def test_format_scalar_numeric_string_no_long_tail():
s = format_scalar_for_prompt_text("51.581818181818181818")
assert "181818" not in s
def test_session_metrics_string_dtype_compacts_numeric_strings():
sm = [
{
"key": "temp_c",
"data_type": "string",
"value": "22.333333333333336",
},
{
"key": "kcal_per_km",
"data_type": "string",
"value": "51.581818181818181818",
},
]
out = session_metrics_list_to_key_value_compact(sm)
assert out["temp_c"] == 22
assert out["kcal_per_km"] == 52
def test_session_metrics_key_value_only():
sm = [
{
"key": "rpe",
"data_type": "integer",
"value": 7,
"name_de": "RPE",
"description_de": "lang",
},
{
"key": "watts",
"data_type": "float",
"value": 199.999,
"unit": "W",
},
]
out = session_metrics_list_to_key_value_compact(sm)
assert out == {"rpe": 7, "watts": 200}
assert "name_de" not in str(out)