예제 #1
0
def test_set_source_cache_changes_delegate_cache(simple_param):
    """ Setting the cached value of the source parameter changes the
    delegate parameter cache accordingly.

    """
    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.cache.get() == (new_source_value - offset) / scale
예제 #2
0
def test_scaling(simple_param, numeric_val):
    scale = 5
    offset = 3
    d = DelegateParameter('test_delegate_parameter',
                          simple_param,
                          offset=offset,
                          scale=scale)

    simple_param(numeric_val)
    assert d() == (numeric_val - offset) / scale

    d(numeric_val)
    assert simple_param() == numeric_val * scale + offset
예제 #3
0
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
예제 #4
0
def test_cache_no_source():
    d = DelegateParameter('test_delegate_parameter', source=None)

    assert d.cache.valid is False
    assert d.cache.timestamp is None
    assert d.cache.max_val_age is None

    with pytest.raises(TypeError,
                       match="Cannot get the cache of a "
                       "DelegateParameter that delegates to None"):
        d.cache.get()

    d.cache.invalidate()
예제 #5
0
def test_gettable_settable_snapshotget_delegate_parameter(
        gettable, get_cmd, settable, set_cmd, snapshot_value):
    """
    Test that gettable, settable and snapshot_get are correctly reflected
    in the DelegateParameter
    """
    source_param = Parameter("source",
                             get_cmd=get_cmd,
                             set_cmd=set_cmd,
                             snapshot_value=snapshot_value)
    delegate_param = DelegateParameter("delegate", source=source_param)
    assert delegate_param.gettable is gettable
    assert delegate_param.settable is settable
    assert delegate_param._snapshot_value is snapshot_value
예제 #6
0
    def test_raw_value_scaling(self, make_observable_parameter):
        p = Parameter('testparam',
                      set_cmd=None,
                      get_cmd=None,
                      offset=1,
                      scale=2)
        d = DelegateParameter('test_delegate_parameter', p, offset=3, scale=5)

        val = 1
        p(val)
        assert d() == (val - 3) / 5

        d(val)
        assert d.raw_value == val * 5 + 3
        assert d.raw_value == p()
예제 #7
0
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
예제 #8
0
def test_raw_value_scaling(make_observable_parameter):
    """
    The :attr:`raw_value` will be deprecated soon,
    so other tests should not use it.
    """

    p = Parameter('testparam', set_cmd=None, get_cmd=None, offset=1, scale=2)
    d = DelegateParameter('test_delegate_parameter', p, offset=3, scale=5)

    val = 1
    p(val)
    assert d() == (val - 3) / 5

    d(val)
    assert d.raw_value == val * 5 + 3
    assert d.raw_value == p()
예제 #9
0
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 == ""
예제 #10
0
def test_snapshot():
    p = Parameter('testparam',
                  set_cmd=None,
                  get_cmd=None,
                  offset=1,
                  scale=2,
                  initial_value=1)
    d = DelegateParameter('test_delegate_parameter',
                          p,
                          offset=3,
                          scale=5,
                          initial_value=2)

    delegate_snapshot = d.snapshot()
    source_snapshot = delegate_snapshot.pop('source_parameter')
    assert source_snapshot == p.snapshot()
    assert delegate_snapshot['value'] == 2
    assert source_snapshot['value'] == 13
예제 #11
0
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"
예제 #12
0
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
예제 #13
0
def test_cache_invalidation():
    value = 10
    p = BetterGettableParam('testparam', set_cmd=None, get_cmd=None)
    d = DelegateParameter('test_delegate_parameter',
                          p,
                          initial_cache_value=value)
    assert p._get_count == 0
    assert d.cache.get() == value
    assert p._get_count == 0

    assert d.cache.valid is True
    assert p.cache.valid is True

    d.cache.invalidate()

    assert d.cache.valid is False
    assert p.cache.valid is False

    d.cache.get()
    assert p._get_count == 1

    assert d.cache.valid is True
    assert p.cache.valid is True
예제 #14
0
def test_delegate_cache_pristine_if_not_set():
    p = Parameter('test')
    d = DelegateParameter('delegate', p)
    gotten_delegate_cache = d.cache.get(get_if_invalid=False)
    assert gotten_delegate_cache is None
예제 #15
0
def test_scaling_initial_value(simple_param, numeric_val):
    scale = 5
    offset = 3
    d = DelegateParameter(
        'test_delegate_parameter', simple_param, offset=offset, scale=scale)
    assert d() == (simple_param() - offset) / scale
예제 #16
0
def test_overwritten_label_on_init(simple_param):
    d = DelegateParameter('test_delegate_parameter', simple_param,
                          label='Physical parameter')
    assert d.unit == simple_param.unit
    assert not d.label == simple_param.label
    assert d.label == 'Physical parameter'
예제 #17
0
def test_delegate_parameter_with_none_source_works_as_expected():
    delegate_param = DelegateParameter(name='delegate',
                                       source=None,
                                       scale=2,
                                       offset=1)
    _assert_none_source_is_correct(delegate_param)
예제 #18
0
def test_same_value(simple_param):
    d = DelegateParameter('test_delegate_parameter', simple_param)
    assert d() == simple_param()
예제 #19
0
def test_overwritten_unit_on_init(simple_param):
    d = DelegateParameter('test_delegate_parameter', simple_param, unit='Ohm')
    assert d.label == simple_param.label
    assert not d.unit == simple_param.unit
    assert d.unit == 'Ohm'
예제 #20
0
def test_setting_initial_value_delegate_parameter():
    value = 10
    p = Parameter('testparam', set_cmd=None, get_cmd=None)
    d = DelegateParameter('test_delegate_parameter', p, initial_value=value)
    assert p.cache.get(get_if_invalid=False) == value
    assert d.cache.get(get_if_invalid=False) == value