diff --git a/data_analysis_tool/tests/test_core.py b/data_analysis_tool/tests/test_core.py new file mode 100644 index 0000000..b7094f9 --- /dev/null +++ b/data_analysis_tool/tests/test_core.py @@ -0,0 +1,50 @@ +import pytest +import pandas as pd +from pathlib import Path +from data_analysis_tool import core + + +@pytest.fixture +def tmp_csv(tmp_path): + data = pd.DataFrame({ + 'timestamp': ['2024-05-01T00:00:00Z', '2024-05-01T00:01:00Z', '2024-05-01T00:02:00Z'], + 'Bx': [10.0, 20.0, 30.0], + 'By': [5.0, 7.0, 9.0], + 'Bz': [1.0, 2.0, 3.0] + }) + csv_path = tmp_path / 'sample.csv' + data.to_csv(csv_path, index=False) + return csv_path + + +def test_analyze_data_returns_analysisresults(tmp_csv): + result = core.analyze_data(str(tmp_csv)) + assert isinstance(result, core.AnalysisResults) + + +def test_analyze_data_correct_means_and_variance(tmp_csv): + result = core.analyze_data(str(tmp_csv)) + expected_mean_Bx = pytest.approx((10.0 + 20.0 + 30.0) / 3, rel=1e-3) + expected_mean_By = pytest.approx((5.0 + 7.0 + 9.0) / 3, rel=1e-3) + values_Bx = [10.0, 20.0, 30.0] + mean_Bx = sum(values_Bx) / 3 + expected_variance_Bx = pytest.approx(sum((x - mean_Bx) ** 2 for x in values_Bx) / len(values_Bx), rel=1e-3) + + assert result.mean_Bx == expected_mean_Bx + assert result.mean_By == expected_mean_By + assert result.variance_Bx == expected_variance_Bx + + +def test_to_json_output_contains_expected_keys(tmp_csv): + result = core.analyze_data(str(tmp_csv)) + result_json = result.to_json() + assert isinstance(result_json, str) + + for key in ["mean_Bx", "mean_By", "variance_Bx", "spikes"]: + assert key in result_json + + +def test_invalid_file_path_raises(tmp_path): + bad_path = tmp_path / 'does_not_exist.csv' + with pytest.raises((FileNotFoundError, pd.errors.EmptyDataError, ValueError)): + core.analyze_data(str(bad_path)) \ No newline at end of file