Example #1
0
class CAChoicePart(Part):
    """Defines a choice `Attribute` that talks to a DBR_ENUM mbbo PV"""

    def __init__(self,
                 name,  # type: util.APartName
                 description,  # type: util.AMetaDescription
                 pv="",  # type: util.APv
                 rbv="",  # type: util.ARbv
                 rbv_suffix="",  # type: util.ARbvSuffix
                 min_delta=0.05,  # type: util.AMinDelta
                 timeout=DEFAULT_TIMEOUT,  # type: util.ATimeout
                 sink_port=None,  # type: util.ASinkPort
                 widget=None,  # type: util.AWidget
                 group=None,  # type: util.AGroup
                 config=True,  # type: util.AConfig
                 ):
        # type: (...) -> None
        super(CAChoicePart, self).__init__(name)
        self.meta = ChoiceMeta(description)
        self.caa = util.CAAttribute(
            self.meta, util.catools.DBR_ENUM, pv, rbv, rbv_suffix, min_delta,
            timeout, sink_port, widget, group, config, self.on_connect)

    def on_connect(self, value):
        self.meta.set_choices(value.enums)

    def caput(self, value):
        # Turn the string value int the index of the choice list. We are
        # passed a validated value, so it is guaranteed to be in choices
        value = self.meta.choices.index(value)
        self.caa.caput(value)

    def setup(self, registrar):
        # type: (PartRegistrar) -> None
        self.caa.setup(registrar, self.name, self.register_hooked, self.caput)
Example #2
0
class TestChoiceMeta(unittest.TestCase):
    def setUp(self):
        self.choice_meta = ChoiceMeta(
            "test description", ["a", "b"])
        self.serialized = OrderedDict()
        self.serialized["typeid"] = "malcolm:core/ChoiceMeta:1.0"
        self.serialized["description"] = "desc"
        self.serialized["choices"] = ["a", "b"]
        self.serialized["tags"] = []
        self.serialized["writeable"] = False
        self.serialized["label"] = "name"

    def test_init(self):
        self.choice_meta = ChoiceMeta(
            "test description", ["a", "b"])
        assert (
                   "test description") == self.choice_meta.description
        assert (
                   self.choice_meta.typeid) == "malcolm:core/ChoiceMeta:1.0"
        assert (
                   self.choice_meta.label) == ""
        assert (
                   self.choice_meta.choices) == ["a", "b"]

    def test_given_valid_value_then_return(self):
        response = self.choice_meta.validate("a")
        assert "a" == response

    def test_int_validate(self):
        response = self.choice_meta.validate(1)
        assert "b" == response

    def test_None_valid(self):
        response = self.choice_meta.validate(None)
        assert "a" == response

    def test_given_invalid_value_then_raises(self):
        with self.assertRaises(ValueError):
            self.choice_meta.validate('badname')

    def test_set_choices(self):
        self.choice_meta.set_choices(["4"])

        assert ["4"] == self.choice_meta.choices

    def test_to_dict(self):
        bm = ChoiceMeta("desc", ["a", "b"], label="name")
        assert bm.to_dict() == self.serialized

    def test_from_dict(self):
        bm = ChoiceMeta.from_dict(self.serialized)
        assert type(bm) == ChoiceMeta
        assert bm.description == "desc"
        assert bm.choices == ["a", "b"]
        assert bm.tags == []
        assert not bm.writeable
        assert bm.label == "name"