From 7f92a323c72fea36be4c6f2cfd950992adb1af4f Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Wed, 2 Oct 2024 16:51:45 +0200 Subject: [PATCH] Test writing to unwritable --- test/test.py | 53 +++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/test/test.py b/test/test.py index 528b776..407ce60 100644 --- a/test/test.py +++ b/test/test.py @@ -1,25 +1,40 @@ import pytest +import warnings from nucon import Nucon, NuconConfig, PumpStatus, PumpDryStatus, PumpOverloadStatus, BreakerStatus @pytest.fixture(scope="module") 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/") yield - Nucon.set_dummy_mode(False) + Nucon.set_dummy_mode(True) 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(): - 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): writable_params = Nucon.get_all_writable() for param in writable_params: current_value = param.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): 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) def test_custom_truthy_values(nucon_setup): - assert bool(PumpStatus.ACTIVE_NO_SPEED_REACHED) == True - assert bool(PumpStatus.ACTIVE_SPEED_REACHED) == True - assert bool(PumpStatus.INACTIVE) == False - - 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 + 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) def test_get_multiple_parameters(nucon_setup): 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(): 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(): original_url = NuconConfig.base_url new_url = "http://newlocalhost:9090/" @@ -72,9 +69,5 @@ def test_base_url_setting(): assert NuconConfig.base_url == new_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__": pytest.main() \ No newline at end of file