def test_scale_and_offset_raw_value_iterable_for_set_cache( values, offsets, scales): p = Parameter(name='test_scale_and_offset_raw_value', set_cmd=None) # test that scale and offset does not change the default behaviour p.cache.set(values) assert p.raw_value == values # test setting scale and offset does not change anything p.scale = scales p.offset = offsets assert p.raw_value == values np_values = np.array(values) np_offsets = np.array(offsets) np_scales = np.array(scales) np_get_latest_values = np.array(p.get_latest()) # Without a call to ``get``, ``get_latest`` will just return old # cached values without applying the set scale and offset np.testing.assert_allclose(np_get_latest_values, np_values) np_get_values = np.array(p.get()) # Now that ``get`` is called, the returned values are the result of # application of the scale and offset. Obviously, calling # ``get_latest`` now will also return the values with the applied # scale and offset np.testing.assert_allclose(np_get_values, (np_values - np_offsets) / np_scales) np_get_latest_values_after_get = np.array(p.get_latest()) np.testing.assert_allclose(np_get_latest_values_after_get, (np_values - np_offsets) / np_scales) # test ``cache.set`` for scalar values if not isinstance(values, Iterable): p.cache.set(values) np.testing.assert_allclose(np.array(p.raw_value), np_values * np_scales + np_offsets) # No set/get cmd performed # testing conversion back and forth p.cache.set(values) np_get_latest_values = np.array(p.get_latest()) # No set/get cmd performed np.testing.assert_allclose(np_get_latest_values, np_values) # adding statistics if isinstance(offsets, Iterable): event('Offset is array') if isinstance(scales, Iterable): event('Scale is array') if isinstance(values, Iterable): event('Value is array') if isinstance(scales, Iterable) and isinstance(offsets, Iterable): event('Scale is array and also offset') if isinstance(scales, Iterable) and not isinstance(offsets, Iterable): event('Scale is array but not offset')
def test_param_cmd_with_parsing(): mem = ParameterMemory() p = Parameter('p_int', get_cmd=mem.get, get_parser=int, set_cmd=mem.set, set_parser=mem.parse_set_p) p(5) assert mem.get() == '5' assert p() == 5 p.cache.set(7) assert p.get_latest() == 7 # Nothing has been passed to the "instrument" at ``cache.set`` # call, hence the following assertions should hold assert mem.get() == '5' assert p() == 5 assert p.get_latest() == 5
def test_get_latest(): time_resolution = time.get_clock_info('time').resolution sleep_delta = 2 * time_resolution # Create a gettable parameter local_parameter = Parameter('test_param', set_cmd=None, get_cmd=None) before_set = datetime.now() time.sleep(sleep_delta) local_parameter.set(1) time.sleep(sleep_delta) after_set = datetime.now() # Check we return last set value, with the correct timestamp assert local_parameter.get_latest() == 1 assert before_set < local_parameter.get_latest.get_timestamp() < after_set # Check that updating the value updates the timestamp time.sleep(sleep_delta) local_parameter.set(2) assert local_parameter.get_latest() == 2 assert local_parameter.get_latest.get_timestamp() > after_set
def test_gettable(): mem = ParameterMemory() p = Parameter('p', get_cmd=mem.get) mem.set(21) assert p() == 21 assert p.get() == 21 with pytest.raises(NotImplementedError): p(10) assert hasattr(p, 'get') assert p.gettable assert not hasattr(p, 'set') assert not p.settable p.cache.set(7) assert p.get_latest() == 7 # Nothing has been passed to the "instrument" at ``cache.set`` # call, hence the following assertions should hold assert mem.get() == 21 assert p() == 21 assert p.get_latest() == 21
def test_get_latest_no_get(): """ Test that get_latest on a parameter that does not have get is handled correctly. """ local_parameter = Parameter('test_param', set_cmd=None, get_cmd=False) # The parameter does not have a get method. with pytest.raises(AttributeError): local_parameter.get() # get_latest will fail as get cannot be called and no cache # is available with pytest.raises(RuntimeError): local_parameter.get_latest() value = 1 local_parameter.set(value) assert local_parameter.get_latest() == value local_parameter2 = Parameter('test_param2', set_cmd=None, get_cmd=False, initial_value=value) with pytest.raises(AttributeError): local_parameter2.get() assert local_parameter2.get_latest() == value
def test_get_latest_raw_value(): # To have a simple distinction between raw value and value of the # parameter lets create a parameter with an offset p = Parameter('p', set_cmd=None, get_cmd=None, offset=42) assert p.get_latest.get_timestamp() is None # Initially, the parameter's raw value is None assert p.get_latest.get_raw_value() is None # After setting the parameter to some value, the # ``.get_latest.get_raw_value()`` call should return the new raw value # of the parameter p(3) assert p.get_latest.get_timestamp() is not None assert p.get_latest.get() == 3 assert p.get_latest() == 3 assert p.get_latest.get_raw_value() == 3 + 42
def test_settable(): mem = ParameterMemory() p = Parameter('p', set_cmd=mem.set, get_cmd=False) p(10) assert mem.get() == 10 with pytest.raises(NotImplementedError): p() assert hasattr(p, 'set') assert p.settable assert not hasattr(p, 'get') assert not p.gettable # For settable-only parameters, using ``cache.set`` may not make # sense, nevertheless, it works p.cache.set(7) assert p.get_latest() == 7