Add rover_startup/tests/test_core.py
This commit is contained in:
parent
66d582c9ba
commit
1563de4e21
1 changed files with 78 additions and 0 deletions
78
rover_startup/tests/test_core.py
Normal file
78
rover_startup/tests/test_core.py
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
import pytest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
import types
|
||||||
|
from rover_startup import main
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def fake_sensor_data():
|
||||||
|
return main.SensorData(
|
||||||
|
light=123.4,
|
||||||
|
infrared=36.7,
|
||||||
|
sound=55.2,
|
||||||
|
core_temp=40.3,
|
||||||
|
battery_status=87.5,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_sensor_data_model_fields(fake_sensor_data):
|
||||||
|
# Ensure all expected fields exist and have correct types
|
||||||
|
assert isinstance(fake_sensor_data.light, float)
|
||||||
|
assert isinstance(fake_sensor_data.infrared, float)
|
||||||
|
assert isinstance(fake_sensor_data.sound, float)
|
||||||
|
assert isinstance(fake_sensor_data.core_temp, float)
|
||||||
|
assert isinstance(fake_sensor_data.battery_status, float)
|
||||||
|
|
||||||
|
|
||||||
|
def test_init_sensors_returns_sensor_data(monkeypatch):
|
||||||
|
# Mock hardware-related calls to prevent actual hardware interaction
|
||||||
|
fake = main.SensorData(
|
||||||
|
light=10.0,
|
||||||
|
infrared=20.0,
|
||||||
|
sound=30.0,
|
||||||
|
core_temp=40.0,
|
||||||
|
battery_status=50.0,
|
||||||
|
)
|
||||||
|
|
||||||
|
monkeypatch.setattr(main, "SensorData", lambda **kwargs: fake)
|
||||||
|
|
||||||
|
result = main.init_sensors()
|
||||||
|
assert isinstance(result, main.SensorData)
|
||||||
|
assert result.light == 10.0
|
||||||
|
assert result.battery_status == 50.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_init_sensors_value_ranges(monkeypatch):
|
||||||
|
# Ensure the initialized sensor data returns realistic ranges
|
||||||
|
result = main.init_sensors()
|
||||||
|
|
||||||
|
assert 0.0 <= result.light <= 100000.0
|
||||||
|
assert -50.0 <= result.infrared <= 150.0
|
||||||
|
assert 0.0 <= result.sound <= 150.0
|
||||||
|
assert -20.0 <= result.core_temp <= 100.0
|
||||||
|
assert 0.0 <= result.battery_status <= 100.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_init_sensors_with_mocked_time(monkeypatch):
|
||||||
|
# Mock time.sleep to ensure no delays during test
|
||||||
|
monkeypatch.setattr(main.time, "sleep", lambda s: None)
|
||||||
|
result = main.init_sensors()
|
||||||
|
assert isinstance(result, main.SensorData)
|
||||||
|
|
||||||
|
|
||||||
|
def test_init_sensors_json_serializable():
|
||||||
|
# Ensure that the return data can be converted to JSON
|
||||||
|
result = main.init_sensors()
|
||||||
|
import json
|
||||||
|
|
||||||
|
data_dict = {
|
||||||
|
"light": result.light,
|
||||||
|
"infrared": result.infrared,
|
||||||
|
"sound": result.sound,
|
||||||
|
"core_temp": result.core_temp,
|
||||||
|
"battery_status": result.battery_status,
|
||||||
|
}
|
||||||
|
json_str = json.dumps(data_dict)
|
||||||
|
assert isinstance(json_str, str)
|
||||||
|
assert "light" in json_str
|
||||||
Loading…
Reference in a new issue