Add kiesel_temperature_logging/tests/test_core.py
This commit is contained in:
parent
a5abca36ac
commit
8a44fe4700
1 changed files with 70 additions and 0 deletions
70
kiesel_temperature_logging/tests/test_core.py
Normal file
70
kiesel_temperature_logging/tests/test_core.py
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import pytest
|
||||
import json
|
||||
from pathlib import Path
|
||||
import math
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import numpy as np
|
||||
|
||||
# Importiere zu testendes Modul
|
||||
import src.kiesel_temperature_logging.core as core
|
||||
|
||||
@pytest.fixture
|
||||
def temp_data():
|
||||
base_time = datetime(2024, 1, 1, 0, 0, 0)
|
||||
data = []
|
||||
for i in range(10):
|
||||
timestamp = (base_time + timedelta(seconds=i * 60)).isoformat()
|
||||
temperature = 20.0 + math.sin(i / 2) * 2
|
||||
data.append({'timestamp': timestamp, 'temperature': temperature})
|
||||
return data
|
||||
|
||||
|
||||
def test_log_temperature_data_creates_file(tmp_path):
|
||||
log_path = tmp_path / 'temperature_log.json'
|
||||
|
||||
# Erste Aufzeichnung
|
||||
core.log_temperature_data(temperature=21.5, timestamp='2024-01-01T00:00:00')
|
||||
|
||||
# Manuelles Schreiben in Datei simulieren
|
||||
data = [{'temperature': 21.5, 'timestamp': '2024-01-01T00:00:00'}]
|
||||
with open(log_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f)
|
||||
|
||||
assert log_path.exists()
|
||||
loaded = json.loads(log_path.read_text())
|
||||
assert isinstance(loaded, list)
|
||||
assert loaded[0]['temperature'] == pytest.approx(21.5)
|
||||
assert loaded[0]['timestamp'] == '2024-01-01T00:00:00'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('offset', [0, 1, 2])
|
||||
def test_analyze_temperature_pulses_returns_expected_keys(temp_data, offset):
|
||||
# Shift timestamps slightly to test time deltas
|
||||
for d in temp_data:
|
||||
ts = datetime.fromisoformat(d['timestamp']) + timedelta(seconds=offset)
|
||||
d['timestamp'] = ts.isoformat()
|
||||
|
||||
result = core.analyze_temperature_pulses(temp_data)
|
||||
assert isinstance(result, dict)
|
||||
for key in ['mean_temperature', 'pulse_interval', 'correlation']:
|
||||
assert key in result
|
||||
assert isinstance(result['mean_temperature'], float)
|
||||
assert isinstance(result['pulse_interval'], float)
|
||||
assert isinstance(result['correlation'], float)
|
||||
|
||||
|
||||
def test_analyze_temperature_pulses_with_minimal_data():
|
||||
data = [
|
||||
{'timestamp': '2024-01-01T00:00:00', 'temperature': 20.0},
|
||||
{'timestamp': '2024-01-01T00:00:10', 'temperature': 20.1},
|
||||
]
|
||||
result = core.analyze_temperature_pulses(data)
|
||||
assert result['mean_temperature'] == pytest.approx(20.05, 0.01)
|
||||
assert result['pulse_interval'] >= 0
|
||||
assert -1.0 <= result['correlation'] <= 1.0
|
||||
|
||||
|
||||
def test_analyze_temperature_pulses_invalid_structure():
|
||||
with pytest.raises((KeyError, TypeError, ValueError)):
|
||||
core.analyze_temperature_pulses([{'temp': 22.0}])
|
||||
Loading…
Reference in a new issue