NuCon/test/test.py

75 lines
3.5 KiB
Python
Raw Normal View History

2024-10-02 16:25:45 +02:00
import pytest
2024-10-02 16:51:45 +02:00
import warnings
2024-10-02 16:25:45 +02:00
from nucon import Nucon, NuconConfig, PumpStatus, PumpDryStatus, PumpOverloadStatus, BreakerStatus
WARN_FLOAT_COULD_BE_INT = False
2024-10-02 16:25:45 +02:00
@pytest.fixture(scope="module")
def nucon_setup():
2024-10-02 16:51:45 +02:00
Nucon.set_dummy_mode(False) # Assume the game is running
Nucon.set_base_url("http://localhost:8785/")
2024-10-02 16:25:45 +02:00
yield
2024-10-02 16:51:45 +02:00
Nucon.set_dummy_mode(True)
2024-10-02 16:25:45 +02:00
def test_read_all_parameters(nucon_setup):
all_params = Nucon.get_all()
assert len(all_params) == len(Nucon)
for param, value in all_params.items():
2024-10-02 16:51:45 +02:00
assert isinstance(value, param.param_type), f"Parameter {param.name} has incorrect type. Expected {param.param_type}, got {type(value)}"
if param.param_type == float and value.is_integer() and WARN_FLOAT_COULD_BE_INT:
2024-10-02 16:51:45 +02:00
warnings.warn(f"Parameter {param.name} is a float but has an integer value: {value}")
2024-10-02 16:25:45 +02:00
def test_write_writable_parameters(nucon_setup):
writable_params = Nucon.get_all_writable()
for param in writable_params:
current_value = param.value
param.value = current_value
2024-10-02 16:51:45 +02:00
assert param.value == current_value, f"Failed to write to parameter {param.name}"
def test_non_writable_parameters(nucon_setup):
non_writable_params = [param for param in Nucon if not param.is_writable]
for param in non_writable_params:
# Test that normal set raises an error
with pytest.raises(ValueError, match=f"Parameter {param.name} is not writable"):
param.value = param.value # Attempt to write the current value
# Test that force_set is refused by the webserver
current_value = param.value
with pytest.raises(Exception, match=f"Failed to set parameter {param.name}"):
Nucon.set(param, current_value, force=True)
2024-10-02 16:25:45 +02:00
def test_enum_parameters(nucon_setup):
pump_status = Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_STATUS.value
assert isinstance(pump_status, PumpStatus)
dry_status = Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_DRY_STATUS.value
assert isinstance(dry_status, PumpDryStatus)
overload_status = Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_OVERLOAD_STATUS.value
assert isinstance(overload_status, PumpOverloadStatus)
breaker_status = Nucon.GENERATOR_0_BREAKER.value
assert isinstance(breaker_status, BreakerStatus)
def test_custom_truthy_values(nucon_setup):
2024-10-02 16:51:45 +02:00
assert bool(Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_STATUS.value) == (Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_STATUS.value in [PumpStatus.ACTIVE_NO_SPEED_REACHED, PumpStatus.ACTIVE_SPEED_REACHED])
assert bool(Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_DRY_STATUS.value) == (Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_DRY_STATUS.value == PumpDryStatus.INACTIVE_OR_ACTIVE_WITH_FLUID)
assert bool(Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_OVERLOAD_STATUS.value) == (Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_OVERLOAD_STATUS.value == PumpOverloadStatus.INACTIVE_OR_ACTIVE_NO_OVERLOAD)
assert bool(Nucon.GENERATOR_0_BREAKER.value) == (Nucon.GENERATOR_0_BREAKER.value == BreakerStatus.OPEN)
2024-10-02 16:25:45 +02:00
def test_get_multiple_parameters(nucon_setup):
params_to_get = [Nucon.CORE_TEMP, Nucon.CORE_PRESSURE, Nucon.RODS_POS_ACTUAL]
multiple_params = Nucon.get_multiple(params_to_get)
assert len(multiple_params) == len(params_to_get)
for param, value in multiple_params.items():
assert isinstance(value, param.param_type)
def test_base_url_setting():
original_url = NuconConfig.base_url
new_url = "http://newlocalhost:9090/"
Nucon.set_base_url(new_url)
assert NuconConfig.base_url == new_url
Nucon.set_base_url(original_url)
if __name__ == "__main__":
pytest.main()