Add data_analysis/tests/test_core.py
This commit is contained in:
parent
b5a6fbc92a
commit
ef4e10c314
1 changed files with 54 additions and 0 deletions
54
data_analysis/tests/test_core.py
Normal file
54
data_analysis/tests/test_core.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import io
|
||||
import pandas as pd
|
||||
import pytest
|
||||
from unittest import mock
|
||||
|
||||
import src.data_analysis.core as core
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_csv():
|
||||
data = """timestamp,temperature,humidity,lux
|
||||
2024-07-01T22:00:00,23.5,45.1,150.0
|
||||
2024-07-01T23:00:00,22.9,46.0,120.0
|
||||
2024-07-02T00:00:00,22.0,47.5,90.0
|
||||
"""
|
||||
return io.StringIO(data)
|
||||
|
||||
|
||||
def test_load_data_valid(monkeypatch, sample_csv):
|
||||
monkeypatch.setattr(pd, 'read_csv', lambda f, parse_dates=None: pd.read_csv(sample_csv, parse_dates=parse_dates))
|
||||
|
||||
df = core.load_data('fake_path.csv')
|
||||
|
||||
assert not df.empty, "Datenrahmen sollte nicht leer sein"
|
||||
expected_columns = {'timestamp', 'temperature', 'humidity', 'lux'}
|
||||
assert expected_columns.issubset(df.columns), f"Erwartete Spalten fehlen: {expected_columns - set(df.columns)}"
|
||||
assert pd.api.types.is_datetime64_any_dtype(df['timestamp']), "timestamp-Spalte sollte datetime enthalten"
|
||||
assert df['temperature'].dtype == float, "Temperatur sollte Float sein"
|
||||
|
||||
|
||||
def test_load_data_invalid_path():
|
||||
with pytest.raises(FileNotFoundError):
|
||||
core.load_data('nonexistent.csv')
|
||||
|
||||
|
||||
def test_plot_data_creates_plot(monkeypatch):
|
||||
df = pd.DataFrame({
|
||||
'timestamp': pd.date_range('2024-07-01', periods=3, freq='H'),
|
||||
'temperature': [23.5, 22.9, 22.0],
|
||||
'humidity': [45.1, 46.0, 47.5],
|
||||
'lux': [150.0, 120.0, 90.0],
|
||||
})
|
||||
|
||||
mock_savefig = mock.Mock()
|
||||
with mock.patch('matplotlib.pyplot.savefig', mock_savefig):
|
||||
core.plot_data(df)
|
||||
|
||||
mock_savefig.assert_called(), "Plot sollte gespeichert werden"
|
||||
|
||||
|
||||
def test_plot_data_with_empty_dataframe():
|
||||
df = pd.DataFrame(columns=['timestamp', 'temperature', 'humidity', 'lux'])
|
||||
with pytest.raises(ValueError):
|
||||
core.plot_data(df)
|
||||
Loading…
Reference in a new issue