From fadace632c768cd466a68e7f0c256ff63e334845 Mon Sep 17 00:00:00 2001 From: Mika Date: Thu, 11 Dec 2025 11:56:26 +0000 Subject: [PATCH] Add off_by_3_fix/tests/test_core.py --- off_by_3_fix/tests/test_core.py | 66 +++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 off_by_3_fix/tests/test_core.py diff --git a/off_by_3_fix/tests/test_core.py b/off_by_3_fix/tests/test_core.py new file mode 100644 index 0000000..898ca02 --- /dev/null +++ b/off_by_3_fix/tests/test_core.py @@ -0,0 +1,66 @@ +import json +import pytest +from pathlib import Path + +from off_by_3_fix import core + + +def load_sample_trace(): + data_path = Path('tests/data/sample_trace.json') + with data_path.open('r', encoding='utf-8') as f: + return json.load(f) + + +def validate_trace_data(data): + assert isinstance(data, list) + for item in data: + assert isinstance(item, dict) + assert 'group' in item + assert 'bucket' in item + assert 'value' in item + assert isinstance(item['group'], str) + assert isinstance(item['bucket'], int) + assert isinstance(item['value'], (int, float)) + + +def test_fix_off_by_3_integer_buckets(tmp_path): + # Lade Beispiel-Trace-Daten + trace_data = load_sample_trace() + validate_trace_data(trace_data) + + # Wende die Korrektur an + corrected = core.fix_off_by_3(trace_data) + + # Validierung der Struktur + validate_trace_data(corrected) + + # Prüfe, dass alle Buckets integerbasiert und aufgerundet sind + for item in corrected: + assert isinstance(item['bucket'], int) + # Keine Off-By-3-Verschiebung mehr + assert item['bucket'] % 1 == 0 + + # Beispielausgabe für Debug / CI-Review + output_file = tmp_path / 'fixed_trace.json' + with output_file.open('w', encoding='utf-8') as f: + json.dump(corrected, f, indent=2) + assert output_file.exists() + + +def test_fix_off_by_3_consistency(): + # Simulierte kleine Eingabe mit Float-Buckets + example_input = [ + {'group': 'A', 'bucket': 10.999, 'value': 42.0}, + {'group': 'B', 'bucket': 23.001, 'value': 11.5} + ] + result = core.fix_off_by_3(example_input) + + # Typprüfung + validate_trace_data(result) + + # Erwartung: Buckets als Integer und nahe am Input-Wert + for original, fixed in zip(example_input, result): + assert isinstance(fixed['bucket'], int) + assert abs(fixed['bucket'] - int(round(original['bucket']))) <= 1 + assert fixed['group'] == original['group'] + assert fixed['value'] == pytest.approx(original['value']) \ No newline at end of file