Beispiel #1
0
 def create_children(self):
     for name in self.ref:
         child = self.ref[name]
         if isinstance(child, Attribute):
             attr = child
             item = AttributeItem(self.endpoint + (name, ), attr)
             group_name = self._get_group_name(attr)
             if group_name is None:
                 parent_item = self
             else:
                 parent_endpoint = self.endpoint + (group_name, )
                 parent_item = self.items[parent_endpoint]
             parent_item.add_child(item)
             item.create_children()
         elif isinstance(child, MethodMeta):
             method = child
             item = MethodItem(self.endpoint + (name, ), method)
             self.add_child(item)
             item.create_children()
Beispiel #2
0
class TestMethodItem(unittest.TestCase):

    def setUp(self):
        ref = MagicMock()
        MethodItem.items.clear()
        self.item = MethodItem(("endpoint",), ref)

    def test_get_writeable(self):
        assert self.item.get_writeable() == self.item.ref.writeable

    def test_ref_children(self):
        self.item.ref.takes.elements = dict(a=1, b=2)
        assert self.item.ref_children() == 2

    @patch("malcolm.gui.methoditem.ParameterItem")
    def test_create_children(self, parameter_mock):
        # Load up items to create
        pi1, pi2 = MagicMock(), MagicMock()
        parameter_mock.side_effect = [pi1, pi2]
        # Load up refs to get
        self.item.ref.takes.elements = OrderedDict((("p", 1), ("q", 2)))
        self.item.ref.defaults = dict(p=4)
        self.item.create_children()
        # Check it made the right thing
        assert len(self.item.children) == 2
        parameter_mock.assert_any_call(("endpoint", "takes", "elements", "p"), 1, 4)
        assert self.item.children[0] == pi1
        parameter_mock.assert_any_call(("endpoint", "takes", "elements", "q"), 2, None)
        assert self.item.children[1] == pi2

    def test_set_value(self):
        p1 = MagicMock()
        p2 = MagicMock()
        self.item.children = [p1, p2]
        p1.get_value.return_value = 43
        p1.endpoint = ("endpoint", "p1")
        p2.get_value.return_value = 1
        p2.endpoint = ("endpoint", "p2")
        request = self.item.set_value("anything")
        p1.reset_value.assert_called_once_with()
        p2.reset_value.assert_called_once_with()
        assert self.item.get_state() == self.item.RUNNING
        assert request.parameters == dict(p1=43, p2=1)
        assert request.path == ["endpoint"]
        self.assertIsInstance(request, Post)

    def test_handle_response_error(self):
        response = Error(message="bad")
        self.item.handle_response(response)
        assert self.item.get_state() == self.item.ERROR

    def test_handle_response_return(self):
        response = Return(value="yay")
        self.item.handle_response(response)
        assert self.item.get_state() == self.item.IDLE

    def test_handle_response_unknown(self):
        response = Delta(changes=[])
        with self.assertRaises(TypeError):
            self.item.handle_response(response)
Beispiel #3
0
 def setUp(self):
     ref = MagicMock()
     MethodItem.items.clear()
     self.item = MethodItem(("endpoint",), ref)
 def setUp(self):
     ref = MagicMock()
     MethodItem.items.clear()
     self.item = MethodItem(("endpoint",), ref)
class TestMethodItem(unittest.TestCase):

    def setUp(self):
        ref = MagicMock()
        MethodItem.items.clear()
        self.item = MethodItem(("endpoint",), ref)

    def test_get_writeable(self):
        self.assertEqual(self.item.get_writeable(), self.item.ref.writeable)

    def test_ref_children(self):
        self.item.ref.takes.elements = dict(a=1, b=2)
        self.assertEqual(self.item.ref_children(), 2)

    @patch("malcolm.gui.methoditem.ParameterItem")
    def test_create_children(self, parameter_mock):
        # Load up items to create
        pi1, pi2 = MagicMock(), MagicMock()
        parameter_mock.side_effect = [pi1, pi2]
        # Load up refs to get
        self.item.ref.takes.elements = OrderedDict((("p", 1), ("q", 2)))
        self.item.ref.defaults = dict(p=4)
        self.item.create_children()
        # Check it made the right thing
        self.assertEqual(len(self.item.children), 2)
        parameter_mock.assert_any_call(("endpoint", "takes", "elements", "p"), 1, 4)
        self.assertEqual(self.item.children[0], pi1)
        parameter_mock.assert_any_call(("endpoint", "takes", "elements", "q"), 2, None)
        self.assertEqual(self.item.children[1], pi2)

    def test_set_value(self):
        p1 = MagicMock()
        p2 = MagicMock()
        self.item.children = [p1, p2]
        p1.get_value.return_value = 43
        p1.endpoint = ("endpoint", "p1")
        p2.get_value.return_value = 1
        p2.endpoint = ("endpoint", "p2")
        request = self.item.set_value("anything")
        p1.reset_value.assert_called_once_with()
        p2.reset_value.assert_called_once_with()
        self.assertEqual(self.item.get_state(), self.item.RUNNING)
        self.assertEqual(request.parameters, dict(p1=43, p2=1))
        self.assertEqual(request.endpoint, ["endpoint"])
        self.assertIsInstance(request, Post)

    def test_handle_response_error(self):
        response = Error(None, None, "bad")
        self.item.handle_response(response)
        self.assertEqual(self.item.get_state(), self.item.ERROR)

    def test_handle_response_return(self):
        response = Return(None, None, "yay")
        self.item.handle_response(response)
        self.assertEqual(self.item.get_state(), self.item.IDLE)

    def test_handle_response_unknown(self):
        response = Delta(None, None, [])
        self.assertRaises(TypeError, self.item.handle_response, response)