diff --git a/data_analysis/tests/test_core.py b/data_analysis/tests/test_core.py new file mode 100644 index 0000000..0c3c748 --- /dev/null +++ b/data_analysis/tests/test_core.py @@ -0,0 +1,61 @@ +import pytest +import math +from data_analysis import core + +@pytest.fixture +def sample_raw_data(): + return [ + {"intensity": 100.0, "background_noise": 10.0}, + {"intensity": 150.0, "background_noise": 15.0}, + {"intensity": 200.0, "background_noise": 20.0} + ] + +@pytest.fixture +def sample_reference_data(): + return [ + {"intensity": 5.0, "background_noise": 0.0}, + {"intensity": 10.0, "background_noise": 0.0}, + {"intensity": 15.0, "background_noise": 0.0} + ] + +def test_correct_flourescence_basic(sample_raw_data): + corrected = core.correct_flourescence(sample_raw_data) + assert isinstance(corrected, list) + assert all(isinstance(p, dict) for p in corrected) + assert len(corrected) == len(sample_raw_data) + + for p in corrected: + assert 'intensity' in p and 'background_noise' in p + assert p['intensity'] <= 200.0 + +def test_correct_flourescence_input_validation(): + with pytest.raises((TypeError, ValueError)): + core.correct_flourescence(None) + with pytest.raises((TypeError, ValueError)): + core.correct_flourescence([{"intensity": "bad", "background_noise": 1.0}]) + +def test_substract_reference_valid(sample_raw_data, sample_reference_data): + corr = core.correct_flourescence(sample_raw_data) + result = core.substract_reference(corr, sample_reference_data) + assert isinstance(result, list) + assert len(result) == len(sample_reference_data) + + for dp in result: + assert 'intensity' in dp + assert isinstance(dp['intensity'], float) + assert not math.isnan(dp['intensity']) + + +def test_substract_reference_error_cases(sample_raw_data): + with pytest.raises((TypeError, ValueError)): + core.substract_reference(sample_raw_data, None) + with pytest.raises((TypeError, ValueError)): + core.substract_reference(None, sample_raw_data) + + +def test_substract_reference_results_decrease(sample_raw_data, sample_reference_data): + corr = core.correct_flourescence(sample_raw_data) + result = core.substract_reference(corr, sample_reference_data) + for original, ref, res in zip(corr, sample_reference_data, result): + expected = original['intensity'] - ref['intensity'] + assert pytest.approx(res['intensity'], rel=1e-6) == expected