Add 1_interferenz_metrics_parser/tests/test_core.py
This commit is contained in:
parent
95127b4826
commit
378133d6b4
1 changed files with 59 additions and 0 deletions
59
1_interferenz_metrics_parser/tests/test_core.py
Normal file
59
1_interferenz_metrics_parser/tests/test_core.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import json
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from interferenz_metrics_parser import core
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_log_file(tmp_path):
|
||||
data = [
|
||||
{"run_id": "run_01", "retry_tail_p99": 0.99, "band_width": 120.5, "mix_ratio": 0.3},
|
||||
{"run_id": "run_02", "retry_tail_p99": 1.23, "band_width": 98.7, "mix_ratio": 0.7}
|
||||
]
|
||||
file_path = tmp_path / "sample_log.json"
|
||||
file_path.write_text(json.dumps(data))
|
||||
return file_path
|
||||
|
||||
|
||||
def test_parse_interference_logs_valid(sample_log_file):
|
||||
result = core.parse_interference_logs(str(sample_log_file))
|
||||
assert isinstance(result, list)
|
||||
assert len(result) == 2
|
||||
|
||||
first = result[0]
|
||||
assert hasattr(first, 'run_id')
|
||||
assert hasattr(first, 'retry_tail_p99')
|
||||
assert hasattr(first, 'band_width')
|
||||
assert hasattr(first, 'mix_ratio')
|
||||
|
||||
assert first.run_id == 'run_01'
|
||||
assert pytest.approx(first.retry_tail_p99, rel=1e-3) == 0.99
|
||||
assert pytest.approx(first.band_width, rel=1e-3) == 120.5
|
||||
assert pytest.approx(first.mix_ratio, rel=1e-3) == 0.3
|
||||
|
||||
|
||||
def test_parse_interference_logs_invalid_json(tmp_path):
|
||||
invalid_content = "{ 'bad_json': True, }"
|
||||
file_path = tmp_path / "bad_log.json"
|
||||
file_path.write_text(invalid_content)
|
||||
|
||||
with pytest.raises((json.JSONDecodeError, ValueError)):
|
||||
core.parse_interference_logs(str(file_path))
|
||||
|
||||
|
||||
def test_parse_interference_logs_missing_fields(tmp_path):
|
||||
incomplete_data = [{"run_id": "run_03", "retry_tail_p99": 0.45}]
|
||||
file_path = tmp_path / "incomplete.json"
|
||||
file_path.write_text(json.dumps(incomplete_data))
|
||||
|
||||
result = core.parse_interference_logs(str(file_path))
|
||||
# Sollte leere Liste oder gefilterte gültige Ergebnisse zurückgeben
|
||||
assert isinstance(result, list)
|
||||
assert all(hasattr(item, 'run_id') for item in result)
|
||||
|
||||
|
||||
def test_parse_interference_logs_empty_file(tmp_path):
|
||||
empty_file = tmp_path / "empty.json"
|
||||
empty_file.write_text("")
|
||||
with pytest.raises((json.JSONDecodeError, ValueError)):
|
||||
core.parse_interference_logs(str(empty_file))
|
||||
Loading…
Reference in a new issue