From 873ccd9c553ea246a71fdcb7344254757fe7b91b Mon Sep 17 00:00:00 2001 From: Mika Date: Tue, 31 Mar 2026 13:46:38 +0000 Subject: [PATCH] Add artifact.1/tests/test_core.py --- artifact.1/tests/test_core.py | 57 +++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 artifact.1/tests/test_core.py diff --git a/artifact.1/tests/test_core.py b/artifact.1/tests/test_core.py new file mode 100644 index 0000000..5a0814a --- /dev/null +++ b/artifact.1/tests/test_core.py @@ -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 == {}