def test_delegate_parameter_get_and_snapshot_with_none_source(): """ Test that a delegate parameter returns None on get and snapshot if the source has a value of None and an offset or scale is used. And returns a value if the source is remapped to a real parameter. """ none_param = Parameter("None") source_param = Parameter('source', get_cmd=None, set_cmd=None, initial_value=2) delegate_param = DelegateParameter(name='delegate', source=none_param) delegate_param.offset = 4 assert delegate_param.get() is None assert delegate_param.snapshot()['value'] is None delegate_param.offset = None delegate_param.scale = 2 assert delegate_param.get() is None assert delegate_param.snapshot()['value'] is None assert delegate_param.cache._parameter.source.cache is none_param.cache delegate_param.source = source_param assert delegate_param.get() == 1 assert delegate_param.snapshot()['value'] == 1 assert delegate_param.cache._parameter.source.cache is source_param.cache
def test_delegate_parameter_with_changed_source_snapshot_matches_value( value, scale, offset): delegate_param = DelegateParameter(name="delegate", source=None, scale=scale, offset=offset) source_parameter = Parameter(name="source", get_cmd=None, set_cmd=None, initial_value=value) _assert_none_source_is_correct(delegate_param) delegate_param.source = source_parameter calc_value = (value - offset) / scale assert delegate_param.cache.get(get_if_invalid=False) == calc_value assert delegate_param.source.cache.get(get_if_invalid=False) == value snapshot = delegate_param.snapshot() # disregard timestamp that might be slightly different snapshot["source_parameter"].pop("ts") source_snapshot = source_parameter.snapshot() source_snapshot.pop("ts") assert snapshot["source_parameter"] == source_snapshot assert snapshot["value"] == calc_value assert delegate_param.get() == calc_value # now remove the source again delegate_param.source = None _assert_none_source_is_correct(delegate_param) _assert_delegate_cache_none_source(delegate_param)
def test_delegate_parameter_get_and_snapshot_raises_with_none(): """ Test that a delegate parameter raises on get and snapshot if the source has a value of None and a scale is used. But works correctly if the source is remapped to a real parameter. """ none_param = Parameter("None") source_param = Parameter('source', get_cmd=None, set_cmd=None, initial_value=2) delegate_param = DelegateParameter(name='delegate', source=none_param) delegate_param.scale = 2 with pytest.raises(TypeError): delegate_param.get() with pytest.raises(TypeError): delegate_param.snapshot() assert delegate_param.cache._parameter.source.cache is none_param.cache delegate_param.source = source_param assert delegate_param.get() == 1 assert delegate_param.snapshot()['value'] == 1 assert delegate_param.cache._parameter.source.cache is source_param.cache
def test_set_source_cache_changes_delegate_get(simple_param): """ When the delegate parameter's ``get`` is called, the new value of the source propagates. """ offset = 4 scale = 5 d = DelegateParameter('d', simple_param, offset=offset, scale=scale) new_source_value = 3 simple_param.cache.set(new_source_value) assert d.get() == (new_source_value - offset) / scale