Add spike_finder/tests/test_core.py
This commit is contained in:
parent
4ee336d3cd
commit
c9c8a48d8f
1 changed files with 61 additions and 0 deletions
61
spike_finder/tests/test_core.py
Normal file
61
spike_finder/tests/test_core.py
Normal file
|
|
@ -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
|
||||
Loading…
Reference in a new issue