import pytest import warnings from nucon import Nucon, PumpStatus, PumpDryStatus, PumpOverloadStatus, BreakerStatus WARN_FLOAT_COULD_BE_INT = False @pytest.fixture(scope="module") def nucon_setup(): Nucon.set_dummy_mode(False) Nucon.set_base_url("http://localhost:8785/") 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), f"Parameter {param.id} 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: warnings.warn(f"Parameter {param.id} is a float but has an integer value: {value}") if param.param_type == str: try: float(value) raise ValueError(f"Parameter {param.id} is a string that looks like a number: {value}") except ValueError: pass try: bool(value.lower()) raise ValueError(f"Parameter {param.id} is a string that looks like a boolean: {value}") except ValueError: pass 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, f"Failed to write to parameter {param.id}" 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.id} 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.id}"): Nucon.set(param, current_value, force=True) 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): 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] 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()