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_change_source_reflected_in_label_and_unit(): delegate_param = DelegateParameter("delegate", source=None) source_param_1 = Parameter("source1", label="source 1", unit="unit1") source_param_2 = Parameter("source2", label="source 2", unit="unit2") assert delegate_param.label == "delegate" assert delegate_param.unit == "" delegate_param.source = source_param_1 assert delegate_param.label == "source 1" assert delegate_param.unit == "unit1" delegate_param.source = source_param_2 assert delegate_param.label == "source 2" assert delegate_param.unit == "unit2" delegate_param.source = None assert delegate_param.label == "delegate" assert delegate_param.unit == ""
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_fixed_label_unit_unchanged(): delegate_param = DelegateParameter("delegate", label="delegatelabel", unit="delegateunit", source=None) source_param_1 = Parameter("source1", label="source 1", unit="unit1") source_param_2 = Parameter("source2", label="source 2", unit="unit2") assert delegate_param.label == "delegatelabel" assert delegate_param.unit == "delegateunit" delegate_param.source = source_param_1 assert delegate_param.label == "delegatelabel" assert delegate_param.unit == "delegateunit" delegate_param.source = source_param_2 assert delegate_param.label == "delegatelabel" assert delegate_param.unit == "delegateunit" delegate_param.source = None assert delegate_param.label == "delegatelabel" assert delegate_param.unit == "delegateunit"
def test_gettable_settable_snapshotget_delegate_parameter_2( gettable, get_cmd, settable, set_cmd, snapshot_value): """ Test that gettable/settable and snapshot_get are updated correctly when source changes """ source_param = Parameter("source", get_cmd=get_cmd, set_cmd=set_cmd, snapshot_value=snapshot_value) delegate_param = DelegateParameter("delegate", source=None) delegate_param.source = source_param assert delegate_param.gettable is gettable assert delegate_param.settable is settable assert delegate_param._snapshot_value is snapshot_value
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