def test_write_read_spectrum_attribute(tango_test, writable_spectrum_attribute): "Check that writable spectrum attributes can be written and read" attr_name = writable_spectrum_attribute config = tango_test.get_attribute_config(attr_name, wait=True) use_all_elements = True if is_bool_type(config.data_type): write_values = [True, False] elif is_int_type(config.data_type): write_values = [76, 77] elif is_float_type(config.data_type): write_values = [-28.2, 44.3] elif is_str_type(config.data_type): # string spectrum attributes don't reduce their x dimension # when written to, so we only compare the values written use_all_elements = False write_values = ["hello", "hola"] else: pytest.xfail("Not currently testing this type") tango_test.write_attribute(attr_name, write_values, wait=True) read_attr = tango_test.read_attribute(attr_name, wait=True) if use_all_elements: read_values = read_attr.value else: read_values = read_attr.value[0:len(write_values)] assert_close(read_values, write_values)
def test_command_string(tango_test): cmd_name = 'DevString' bytes_big = 100000 * b'big data ' str_big = bytes_big.decode('latin-1') values = [b'', '', 'Hello, World!', b'Hello, World!', bytes_devstring, str_devstring, bytes_big, str_big] if PY3: expected_values = ['', '', 'Hello, World!', 'Hello, World!', str_devstring, str_devstring, str_big, str_big] else: expected_values = ['', '', 'Hello, World!', 'Hello, World!', bytes_devstring, bytes_devstring, bytes_big, bytes_big] for value, expected_value in zip(values, expected_values): result = tango_test.command_inout(cmd_name, value, wait=True) assert result == expected_value cmd_name = 'DevVarStringArray' for value, expected_value in zip(values, expected_values): result = tango_test.command_inout(cmd_name, [value, value], wait=True) assert result == [expected_value, expected_value] cmd_name = 'DevVarLongStringArray' for value, expected_value in zip(values, expected_values): result = tango_test.command_inout(cmd_name, [[-10, 200], [value, value]], wait=True) assert len(result) == 2 assert_close(result[0], [-10, 200]) assert_close(result[1], [expected_value, expected_value])
def test_identity_command(typed_values, server_green_mode): dtype, values = typed_values if dtype == (bool, ): pytest.xfail('Not supported for some reasons') class TestDevice(Device): green_mode = server_green_mode @command(dtype_in=dtype, dtype_out=dtype) def identity(self, arg): return arg with DeviceTestContext(TestDevice) as proxy: for value in values: assert_close(proxy.identity(value), value)
def test_read_write_attribute(typed_values, server_green_mode): dtype, values = typed_values class TestDevice(Device): green_mode = server_green_mode @attribute(dtype=dtype, max_dim_x=10, access=AttrWriteType.READ_WRITE) def attr(self): return self.attr_value @attr.write def attr(self, value): self.attr_value = value with DeviceTestContext(TestDevice) as proxy: for value in values: proxy.attr = value assert_close(proxy.attr, value)
def test_device_property_no_default(typed_values, server_green_mode): dtype, values = typed_values patched_dtype = dtype if dtype != (bool, ) else (int, ) default = values[0] value = values[1] class TestDevice(Device): green_mode = server_green_mode prop = device_property(dtype=dtype) @command(dtype_out=patched_dtype) def get_prop(self): return default if self.prop is None else self.prop with DeviceTestContext(TestDevice, process=True) as proxy: assert_close(proxy.get_prop(), default) with DeviceTestContext(TestDevice, properties={'prop': value}, process=True) as proxy: assert_close(proxy.get_prop(), value)
def test_read_write_dynamic_attribute(typed_values, server_green_mode): dtype, values, expected = typed_values class TestDevice(Device): green_mode = server_green_mode def __init__(self, *args, **kwargs): super(TestDevice, self).__init__(*args, **kwargs) self.attr_value = None @command def add_dyn_attr(self): attr = attribute( name="dyn_attr", dtype=dtype, max_dim_x=10, access=AttrWriteType.READ_WRITE, fget=self.read, fset=self.write) self.add_attribute(attr) @command def delete_dyn_attr(self): self.remove_attribute("dyn_attr") def read(self, attr): attr.set_value(self.attr_value) def write(self, attr): self.attr_value = attr.get_write_value() with DeviceTestContext(TestDevice) as proxy: proxy.add_dyn_attr() for value in values: proxy.dyn_attr = value assert_close(proxy.dyn_attr, expected(value)) proxy.delete_dyn_attr() assert "dyn_attr" not in proxy.get_attribute_list()
def test_mandatory_device_property(typed_values, server_green_mode): dtype, values = typed_values patched_dtype = dtype if dtype != (bool, ) else (int, ) default, value = values[:2] class TestDevice(Device): green_mode = server_green_mode prop = device_property(dtype=dtype, mandatory=True) @command(dtype_out=patched_dtype) def get_prop(self): return self.prop with DeviceTestContext(TestDevice, properties={'prop': value}, process=True) as proxy: assert_close(proxy.get_prop(), value) with pytest.raises(DevFailed) as context: with DeviceTestContext(TestDevice, process=True) as proxy: pass assert 'Device property prop is mandatory' in str(context.value)
def test_device_property_with_default_value(typed_values, server_green_mode): dtype, values, expected = typed_values patched_dtype = dtype if dtype != (bool, ) else (int, ) default = values[0] value = values[1] class TestDevice(Device): green_mode = server_green_mode prop = device_property(dtype=dtype, default_value=default) @command(dtype_out=patched_dtype) def get_prop(self): print(self.prop) return self.prop with DeviceTestContext(TestDevice, process=True) as proxy: assert_close(proxy.get_prop(), expected(default)) with DeviceTestContext(TestDevice, properties={'prop': value}, process=True) as proxy: assert_close(proxy.get_prop(), expected(value))