From c9c8a48d8f9aafce6ccc2ab96290b0ea49065ae7 Mon Sep 17 00:00:00 2001 From: Mika Date: Fri, 9 Jan 2026 14:49:07 +0000 Subject: [PATCH] Add spike_finder/tests/test_core.py --- spike_finder/tests/test_core.py | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 spike_finder/tests/test_core.py diff --git a/spike_finder/tests/test_core.py b/spike_finder/tests/test_core.py new file mode 100644 index 0000000..c79a746 --- /dev/null +++ b/spike_finder/tests/test_core.py @@ -0,0 +1,61 @@ +import pytest +from spike_finder import core + + +def test_find_spikes_basic_case(): + # Synthetic log data with values around and above the threshold + log_data = [ + {"timestamp": "2024-01-01T00:00:00", "value": 0.5}, + {"timestamp": "2024-01-01T00:01:00", "value": 1.2}, + {"timestamp": "2024-01-01T00:02:00", "value": 0.7}, + {"timestamp": "2024-01-01T00:03:00", "value": 1.5} + ] + threshold = 1.0 + + spikes = core.find_spikes(log_data, threshold) + + # Verify the number of detected spikes + assert isinstance(spikes, list) + assert len(spikes) == 2 + + # Check each spike event's structure + for event in spikes: + assert isinstance(event, dict) + assert 'timestamp' in event + assert 'value' in event + assert 'context_window' in event + assert isinstance(event['timestamp'], str) + assert isinstance(event['value'], float) + assert isinstance(event['context_window'], list) + assert all(isinstance(x, float) for x in event['context_window']) + + +def test_find_spikes_no_spikes(): + # All values below threshold + log_data = [ + {"timestamp": "2024-01-01T00:00:00", "value": 0.2}, + {"timestamp": "2024-01-01T00:01:00", "value": 0.3}, + {"timestamp": "2024-01-01T00:02:00", "value": 0.4} + ] + threshold = 1.0 + + spikes = core.find_spikes(log_data, threshold) + + assert isinstance(spikes, list) + assert len(spikes) == 0 + + +def test_find_spikes_edge_context(): + # Ensure context window properly handles edges + log_data = [ + {"timestamp": "2024-01-01T00:00:00", "value": 2.0}, + {"timestamp": "2024-01-01T00:01:00", "value": 0.5}, + {"timestamp": "2024-01-01T00:02:00", "value": 2.1} + ] + threshold = 1.0 + + spikes = core.find_spikes(log_data, threshold) + + assert len(spikes) == 2 + for event in spikes: + assert len(event['context_window']) >= 1 # Each should have some context \ No newline at end of file