Ejemplo n.º 1
0
 def test_len(self):
     m = Map(self.meta)
     self.assertEqual(0, len(m))
     m.a = 1
     self.assertEqual(1, len(m))
     m.b = 1
     self.assertEqual(2, len(m))
Ejemplo n.º 2
0
 def test_len(self):
     m = Map(self.meta)
     self.assertEqual(0, len(m))
     m.a = 1
     self.assertEqual(1, len(m))
     m.b = 1
     self.assertEqual(2, len(m))
Ejemplo n.º 3
0
 def test_update_raises_on_invalid_key(self):
     m = Map(self.nmeta, {"a": 1})
     d = {"a": 2, "b": 2, "c": 2}
     with self.assertRaises(ValueError):
         m.update(d)
     self.assertEqual(1, m.a)
     with self.assertRaises(AttributeError):
         m.b
     with self.assertRaises(AttributeError):
         m.c
Ejemplo n.º 4
0
 def test_update_raises_on_invalid_key(self):
     m = Map(self.nmeta, {"a":1})
     d = {"a":2, "b":2, "c":2}
     with self.assertRaises(ValueError):
         m.update(d)
     self.assertEqual(1, m.a)
     with self.assertRaises(AttributeError):
         m.b
     with self.assertRaises(AttributeError):
         m.c
Ejemplo n.º 5
0
 def test_init(self):
     b_mock = MagicMock()
     m = Map(self.meta, {"a": "test", "b": b_mock})
     self.assertEqual(self.meta, m.meta)
     self.assertEqual("test", m.a)
     self.assertIs(str(b_mock), m.b)
     self.assertEqual("malcolm:core/Map:1.0", m.typeid)
Ejemplo n.º 6
0
    def test_to_dict(self):
        a_mock = MagicMock()
        s = StringMeta(description="a string")
        meta = Meta()
        meta.elements = OrderedDict()
        meta.elements["b"] = s
        meta.elements["c"] = s
        meta.elements["d"] = NumberMeta("int32")
        meta.elements["e"] = s
        m = Map(meta, {"b": "test", "d": 123, "e": "e"})

        expected = OrderedDict()
        expected["typeid"] = "malcolm:core/Map:1.0"
        expected["b"] = "test"
        expected["d"] = 123
        expected["e"] = "e"
        self.assertEquals(expected, m.to_dict())
Ejemplo n.º 7
0
 def test_set_item(self):
     m = Map(self.meta, {"a": 1})
     a_mock = MagicMock()
     b_mock = MagicMock()
     m["a"] = a_mock
     m["b"] = b_mock
     self.assertEqual(str(a_mock), m.a)
     self.assertEqual(str(b_mock), m.b)
Ejemplo n.º 8
0
    def test_to_dict(self):
        a_mock = MagicMock()
        s = StringMeta(description="a string")
        meta = Meta()
        meta.elements = OrderedDict()
        meta.elements["b"] = s
        meta.elements["c"] = s
        meta.elements["d"] = NumberMeta("int32")
        meta.elements["e"] = s
        m = Map(meta, {"b":"test", "d":123, "e":"e"})

        expected = OrderedDict()
        expected["typeid"] = "malcolm:core/Map:1.0"
        expected["b"] = "test"
        expected["d"] = 123
        expected["e"] = "e"
        self.assertEquals(expected, m.to_dict())
Ejemplo n.º 9
0
    def test_from_dict(self):
        map_meta = MagicMock()
        map_meta.elements = {"a": Mock(), "b": Mock(), "c": Mock()}
        map_meta.elements["a"].validate.return_value = 124
        map_meta.elements["b"].validate.return_value = "Something"
        map_meta.required = ["a"]

        d = {"a": 123, "b": {"typeid": "mock_typeid"}}
        m = Map(map_meta, d)
        self.assertEquals(124, m.a)
        self.assertEquals("Something", m.b)
Ejemplo n.º 10
0
    def test_equals_maps(self):
        self.meta.to_dict = MagicMock()
        m1 = Map(self.meta, {"a":"test"})
        m2 = Map(self.meta, {"a":"test2"})
        self.assertFalse(m1 == m2)
        self.assertTrue(m1 != m2)
        m2.a = "test"
        self.assertTrue(m1 == m2)
        self.assertFalse(m1 != m2)

        m2 = Map(self.meta, {"a":"test", "b":"test"})
        self.assertFalse(m1 == m2)
        m1["b"] = "test"
        self.assertTrue(m1 == m2)

        s = StringMeta(description="a string")
        meta2 = Meta()
        meta2.elements = {"a":s, "b":s}
        meta2.required = ["a"]
        meta2.to_dict = self.meta.to_dict
        m2 = Map(meta2, {"a":"test", "b":"test"})
        self.assertTrue(m1 == m2)
Ejemplo n.º 11
0
    def create_part(self, params=None):
        if params is None:
            params = dict(
                name="attrname",
                description="desc",
                pv="pv",
                rbv_suff="2"
            )

        @capart_takes()
        class MyCAPart(CAPart):
            create_meta = MagicMock(return_value=NumberMeta("int32"))
            get_datatype = MagicMock()

        # TODO: defaults?
        mparams = Map(MyCAPart.Method.takes, MyCAPart.Method.defaults)
        mparams.update(params)
        mparams.check_valid()

        p = MyCAPart(mparams, MagicMock())
        p.set_logger_name("something")
        list(p.create_attributes())
        return p
Ejemplo n.º 12
0
    def call_post_function(self, post_function, param_dict, *args):
        need_params = bool(self.takes.elements)
        need_ret = bool(self.returns.elements)
        args = list(args)
        # Prepare input map
        if need_params:
            if param_dict is None:
                param_dict = {}
            params = self.prepare_input_map(**param_dict)
            args.append(params)

        # Prepare output map
        if need_ret:
            ret = Map(self.returns)
            args.append(ret)

        self.log_debug("Calling with %s" % (args,))

        result = post_function(*args)
        if need_ret:
            result = Map(self.returns, result)
            result.check_valid()

        return result
Ejemplo n.º 13
0
    def call_post_function(self, post_function, param_dict, *args):
        need_params = bool(self.takes.elements)
        need_ret = bool(self.returns.elements)
        args = list(args)
        # Prepare input map
        if need_params:
            if param_dict is None:
                param_dict = {}
            params = self.prepare_input_map(**param_dict)
            args.append(params)

        # Prepare output map
        if need_ret:
            ret = Map(self.returns)
            args.append(ret)

        self.log_debug("Calling with %s" % (args, ))

        result = post_function(*args)
        if need_ret:
            result = Map(self.returns, result)
            result.check_valid()

        return result
Ejemplo n.º 14
0
    def test_equals_dicts(self):
        m = Map(self.meta, {"a": "test"})
        d = {"a": "test"}
        self.assertTrue(m == d)
        self.assertFalse(m != d)

        m["b"] = "test"
        self.assertFalse(m == d)
        self.assertTrue(m != d)

        d["b"] = "test2"
        self.assertFalse(m == d)
        self.assertTrue(m != d)

        d["b"] = "test"
        self.assertTrue(m == d)
        self.assertFalse(m != d)
Ejemplo n.º 15
0
    def call_function(self, parameters_dict):
        """
        Validate function parameters, call function and validate the response

        Args:
            parameters_dict(dict): Dictionary of parameter names and values

        Returns:
            Map: Return values
        """

        if not self.writeable:
            raise ValueError("Cannot call a method that is not writeable")

        for arg in self.defaults:
            if arg not in parameters_dict.keys():
                parameters_dict[arg] = self.defaults[arg]

        parameters = Map(self.takes, parameters_dict)
        parameters.check_valid()
        expected_response = Map(self.returns)

        if len(self.takes.elements) > 0:
            if len(self.returns.elements) > 0:
                return_val = self.func(parameters, expected_response)
            else:
                return_val = self.func(parameters)
        else:
            if len(self.returns.elements) > 0:
                return_val = self.func(expected_response)
            else:
                return_val = self.func()

        if len(self.returns.elements) > 0:
            return_val = Map(self.returns, return_val)
            return_val.check_valid()

        return return_val
Ejemplo n.º 16
0
 def _update_future(self, response):
     """Called when a future is filled. Updates the future accordingly and
     removes it from the futures list
     """
     self.log_debug("future %d filled", response.id)
     f = self._futures.pop(response.id)
     method = self._methods.pop(response.id, None)
     if isinstance(response, Error):
         f.set_exception(ResponseError(response.message))
     elif isinstance(response, Return):
         result = response.value
         if method and result:
             result = Map(method.returns, result)
         f.set_result(result)
     else:
         raise UnexpectedError("Future received unexpected response: %r" %
                               response)
     return f
Ejemplo n.º 17
0
    def test_equals_maps(self):
        self.meta.to_dict = MagicMock()
        m1 = Map(self.meta, {"a": "test"})
        m2 = Map(self.meta, {"a": "test2"})
        self.assertFalse(m1 == m2)
        self.assertTrue(m1 != m2)
        m2.a = "test"
        self.assertTrue(m1 == m2)
        self.assertFalse(m1 != m2)

        m2 = Map(self.meta, {"a": "test", "b": "test"})
        self.assertFalse(m1 == m2)
        m1["b"] = "test"
        self.assertTrue(m1 == m2)

        s = StringMeta(description="a string")
        meta2 = Meta()
        meta2.elements = {"a": s, "b": s}
        meta2.required = ["a"]
        meta2.to_dict = self.meta.to_dict
        m2 = Map(meta2, {"a": "test", "b": "test"})
        self.assertTrue(m1 == m2)
Ejemplo n.º 18
0
    def create_part(self, params=None):
        if params is None:
            params = dict(name="attrname",
                          description="desc",
                          pv="pv",
                          rbv_suff="2")

        @capart_takes()
        class MyCAPart(CAPart):
            create_meta = MagicMock(return_value=NumberMeta("int32"))
            get_datatype = MagicMock()

        # TODO: defaults?
        mparams = Map(MyCAPart.Method.takes, MyCAPart.Method.defaults)
        mparams.update(params)
        mparams.check_valid()

        p = MyCAPart(mparams, MagicMock())
        p.set_logger_name("something")
        list(p.create_attributes())
        return p
Ejemplo n.º 19
0
 def test_update(self):
     m = Map(self.nmeta, {"a":1})
     d = {"a":2, "b":2}
     m.update(d)
     self.assertEqual(2, m.a)
     self.assertEqual(2, m.b)
Ejemplo n.º 20
0
 def test_get_item_fails_if_non_key(self):
     m = Map(self.meta)
     with self.assertRaises(KeyError):
         m["__init__"]
Ejemplo n.º 21
0
 def test_get_item(self):
     m = Map(self.meta, {"a":"test"})
     self.assertEqual("test", m["a"])
     m.a = "test_2"
     self.assertEqual("test_2", m["a"])
Ejemplo n.º 22
0
 def test_values(self):
     m = Map(self.nmeta, {"a": 1})
     self.assertEqual([1], m.values())
     m.b = 2
     self.assertEqual({1, 2}, set(m.values()))
Ejemplo n.º 23
0
 def test_contains(self):
     m = Map(self.meta, {"a": "test"})
     self.assertTrue("a" in m)
     self.assertFalse("b" in m)
     # TODO GK when did Map not have __init__ ???
     self.assertFalse("__init__" in m)
Ejemplo n.º 24
0
 def test_items(self):
     m = Map(self.nmeta, {"b":2})
     self.assertEqual([("b", 2)], m.items())
     m.a = 1
     self.assertEqual({("a", 1), ("b", 2)}, set(m.items()))
Ejemplo n.º 25
0
    def test_keys(self):

        m = Map(self.nmeta, {"a":1})
        self.assertEqual(["a"], m.keys())
        m.b = 1
        self.assertEqual({"a", "b"}, set(m.keys()))
Ejemplo n.º 26
0
 def test_update(self):
     m = Map(self.nmeta, {"a": 1})
     d = {"a": 2, "b": 2}
     m.update(d)
     self.assertEqual(2, m.a)
     self.assertEqual(2, m.b)
Ejemplo n.º 27
0
 def test_clear(self):
     m = Map(self.nmeta, {"a": 1})
     m.clear()
     with self.assertRaises(AttributeError):
         m.a
Ejemplo n.º 28
0
    def test_keys(self):

        m = Map(self.nmeta, {"a": 1})
        self.assertEqual(["a"], m.keys())
        m.b = 1
        self.assertEqual({"a", "b"}, set(m.keys()))
Ejemplo n.º 29
0
 def prepare_input_map(self, **param_dict):
     params = Map(self.takes, self.defaults)
     if param_dict:
         params.update(param_dict)
     params.check_valid()
     return params
Ejemplo n.º 30
0
 def test_init_raises_on_bad_key(self):
     with self.assertRaises(ValueError):
         m = Map(self.meta, {"bad_key": MagicMock()})
Ejemplo n.º 31
0
 def test_get_item_raises_if_no_key(self):
     m = Map(self.meta, {"a": "test"})
     with self.assertRaises(KeyError):
         m["b"]
Ejemplo n.º 32
0
 def test_clear(self):
     m = Map(self.nmeta, {"a":1})
     m.clear()
     with self.assertRaises(AttributeError):
         m.a
Ejemplo n.º 33
0
 def prepare_input_map(self, **param_dict):
     params = Map(self.takes, self.defaults)
     if param_dict:
         params.update(param_dict)
     params.check_valid()
     return params
Ejemplo n.º 34
0
 def test_values(self):
     m = Map(self.nmeta, {"a":1})
     self.assertEqual([1], m.values())
     m.b = 2
     self.assertEqual({1, 2}, set(m.values()))
Ejemplo n.º 35
0
 def test_iter(self):
     m = Map(self.meta, {"a": "x", "b": "y"})
     self.assertEqual({"a", "b"}, {x for x in m})
Ejemplo n.º 36
0
 def test_get_item(self):
     m = Map(self.meta, {"a": "test"})
     self.assertEqual("test", m["a"])
     m.a = "test_2"
     self.assertEqual("test_2", m["a"])
Ejemplo n.º 37
0
 def test_set_item_raises_invalid_key(self):
     m = Map(self.meta)
     with self.assertRaises(ValueError):
         m["c"] = MagicMock()
Ejemplo n.º 38
0
 def test_empty_init(self):
     m = Map(self.meta, None)
     self.assertEqual(self.meta, m.meta)
     with self.assertRaises(AttributeError):
         m.a
Ejemplo n.º 39
0
 def test_items(self):
     m = Map(self.nmeta, {"b": 2})
     self.assertEqual([("b", 2)], m.items())
     m.a = 1
     self.assertEqual({("a", 1), ("b", 2)}, set(m.items()))