Test writing to unwritable

This commit is contained in:
Dominik Moritz Roth 2024-10-02 16:51:45 +02:00
parent 2c58571d62
commit 7f92a323c7

View File

@ -1,25 +1,40 @@
import pytest import pytest
import warnings
from nucon import Nucon, NuconConfig, PumpStatus, PumpDryStatus, PumpOverloadStatus, BreakerStatus from nucon import Nucon, NuconConfig, PumpStatus, PumpDryStatus, PumpOverloadStatus, BreakerStatus
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def nucon_setup(): def nucon_setup():
Nucon.set_dummy_mode(True) Nucon.set_dummy_mode(False) # Assume the game is running
Nucon.set_base_url("http://localhost:8080/") Nucon.set_base_url("http://localhost:8080/")
yield yield
Nucon.set_dummy_mode(False) Nucon.set_dummy_mode(True)
def test_read_all_parameters(nucon_setup): def test_read_all_parameters(nucon_setup):
all_params = Nucon.get_all() all_params = Nucon.get_all()
assert len(all_params) == len(Nucon) assert len(all_params) == len(Nucon)
for param, value in all_params.items(): for param, value in all_params.items():
assert isinstance(value, param.param_type) 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():
warnings.warn(f"Parameter {param.name} is a float but has an integer value: {value}")
def test_write_writable_parameters(nucon_setup): def test_write_writable_parameters(nucon_setup):
writable_params = Nucon.get_all_writable() writable_params = Nucon.get_all_writable()
for param in writable_params: for param in writable_params:
current_value = param.value current_value = param.value
param.value = current_value param.value = current_value
assert param.value == current_value 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)
def test_enum_parameters(nucon_setup): def test_enum_parameters(nucon_setup):
pump_status = Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_STATUS.value pump_status = Nucon.COOLANT_CORE_CIRCULATION_PUMP_0_STATUS.value
@ -35,18 +50,10 @@ def test_enum_parameters(nucon_setup):
assert isinstance(breaker_status, BreakerStatus) assert isinstance(breaker_status, BreakerStatus)
def test_custom_truthy_values(nucon_setup): def test_custom_truthy_values(nucon_setup):
assert bool(PumpStatus.ACTIVE_NO_SPEED_REACHED) == True 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(PumpStatus.ACTIVE_SPEED_REACHED) == True 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(PumpStatus.INACTIVE) == False 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)
assert bool(PumpDryStatus.INACTIVE_OR_ACTIVE_WITH_FLUID) == True
assert bool(PumpDryStatus.ACTIVE_WITHOUT_FLUID) == False
assert bool(PumpOverloadStatus.INACTIVE_OR_ACTIVE_NO_OVERLOAD) == True
assert bool(PumpOverloadStatus.ACTIVE_AND_OVERLOAD) == False
assert bool(BreakerStatus.OPEN) == True
assert bool(BreakerStatus.CLOSED) == False
def test_get_multiple_parameters(nucon_setup): def test_get_multiple_parameters(nucon_setup):
params_to_get = [Nucon.CORE_TEMP, Nucon.CORE_PRESSURE, Nucon.RODS_POS_ACTUAL] params_to_get = [Nucon.CORE_TEMP, Nucon.CORE_PRESSURE, Nucon.RODS_POS_ACTUAL]
@ -55,16 +62,6 @@ def test_get_multiple_parameters(nucon_setup):
for param, value in multiple_params.items(): for param, value in multiple_params.items():
assert isinstance(value, param.param_type) assert isinstance(value, param.param_type)
def test_dummy_mode(nucon_setup):
Nucon.set_dummy_mode(False)
Nucon.set_base_url("http://localhost:8081/") # Change to a non-existent URL to trigger an exception
with pytest.raises(Exception):
Nucon.CORE_TEMP.value
Nucon.set_dummy_mode(True)
Nucon.set_base_url("http://localhost:8080/")
assert isinstance(Nucon.CORE_TEMP.value, Nucon.CORE_TEMP.param_type)
def test_base_url_setting(): def test_base_url_setting():
original_url = NuconConfig.base_url original_url = NuconConfig.base_url
new_url = "http://newlocalhost:9090/" new_url = "http://newlocalhost:9090/"
@ -72,9 +69,5 @@ def test_base_url_setting():
assert NuconConfig.base_url == new_url assert NuconConfig.base_url == new_url
Nucon.set_base_url(original_url) Nucon.set_base_url(original_url)
def test_error_on_writing_readonly_parameter(nucon_setup):
with pytest.raises(ValueError):
Nucon.CORE_TEMP.value = 100.0
if __name__ == "__main__": if __name__ == "__main__":
pytest.main() pytest.main()