Add artifact.1/tests/test_core.py

This commit is contained in:
Mika 2026-03-31 13:46:38 +00:00
parent 401970b448
commit 873ccd9c55

View file

@ -0,0 +1,57 @@
import json
import pytest
import pandas as pd
from pathlib import Path
from src.artifact_1 import core
@pytest.fixture
def sample_log_file(tmp_path):
data = [
{"timestamp": "2024-03-01T12:00:00", "aux_worker": 1, "p99_tail": 100.0, "band_width": 500.0},
{"timestamp": "2024-03-01T12:02:00", "aux_worker": 1, "p99_tail": 110.0, "band_width": 510.0},
{"timestamp": "2024-03-01T12:04:00", "aux_worker": 2, "p99_tail": 150.0, "band_width": 600.0},
{"timestamp": "2024-03-01T12:06:00", "aux_worker": 2, "p99_tail": 170.0, "band_width": 590.0},
{"timestamp": "2024-03-01T12:08:00", "aux_worker": 3, "p99_tail": 200.0, "band_width": 610.0}
]
file_path = tmp_path / "sample.json"
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(data, f)
return file_path
def test_analyze_logs_returns_dict(sample_log_file):
result = core.analyze_logs(str(sample_log_file))
assert isinstance(result, dict)
assert 1 in result and 2 in result and 3 in result
def test_metrics_computation(sample_log_file):
result = core.analyze_logs(str(sample_log_file))
aux1 = result[1]
aux2 = result[2]
aux3 = result[3]
# For aux1 median of p99_tail = (100+110)/2 = 105
assert pytest.approx(aux1['p99_tail']['median'], 0.001) == 105
# IQR for aux1 = 110 - 100 = 10
assert pytest.approx(aux1['p99_tail']['iqr'], 0.001) == 10
# Bandwidth for aux2 median = (600+590)/2 = 595
assert pytest.approx(aux2['band_width']['median'], 0.001) == 595
# Only one entry for aux3 → IQR = 0
assert aux3['p99_tail']['iqr'] == 0
def test_invalid_input_raises(tmp_path):
# Create invalid JSON file
bad_file = tmp_path / "bad.json"
with open(bad_file, 'w', encoding='utf-8') as f:
f.write('{"invalid": true}')
with pytest.raises(Exception):
core.analyze_logs(str(bad_file))
def test_empty_file(tmp_path):
empty_file = tmp_path / "empty.json"
empty_file.write_text('[]', encoding='utf-8')
result = core.analyze_logs(str(empty_file))
assert result == {}