Add off_by_3_fix/tests/test_core.py
This commit is contained in:
parent
e585141214
commit
fadace632c
1 changed files with 66 additions and 0 deletions
66
off_by_3_fix/tests/test_core.py
Normal file
66
off_by_3_fix/tests/test_core.py
Normal file
|
|
@ -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'])
|
||||||
Loading…
Reference in a new issue