def test_returns_dict(self): method_dict = OrderedDict(takes=OrderedDict(one=OrderedDict()), returns=OrderedDict(one=OrderedDict()), defaults=OrderedDict()) m1 = MagicMock() m1.to_dict.return_value = method_dict m2 = MagicMock() m2.to_dict.return_value = method_dict a1 = MagicMock() a1dict = OrderedDict(value="test", meta=MagicMock()) a1.to_dict.return_value = a1dict a2 = MagicMock() a2dict = OrderedDict(value="value", meta=MagicMock()) a2.to_dict.return_value = a2dict block = Block() block.set_parent(MagicMock(), "Test") block.add_method('method_one', m1) block.add_method('method_two', m2) block.add_attribute('attr_one', a1) block.add_attribute('attr_two', a2) m1.reset_mock() m2.reset_mock() a1.reset_mock() a2.reset_mock() expected_dict = OrderedDict() expected_dict['typeid'] = "malcolm:core/Block:1.0" expected_dict['attr_one'] = a1dict expected_dict['attr_two'] = a2dict expected_dict['method_one'] = method_dict expected_dict['method_two'] = method_dict response = block.to_dict() m1.to_dict.assert_called_once_with() m2.to_dict.assert_called_once_with() self.assertEqual(expected_dict, response)
def setUp(self): # Serialized version of the block we want source = Block() HelloController(MagicMock(), source, "blockname") self.serialized = source.to_dict() # Setup client controller prerequisites self.b = Block() self.b.name = "blockname" self.p = MagicMock() self.comms = MagicMock() self.cc = ClientController(self.p, self.b, "blockname") # get process to give us comms self.p.get_client_comms.return_value = self.comms # tell our controller which blocks the process can talk to response = MagicMock(id_=self.cc.REMOTE_BLOCKS_ID, value=["blockname"]) self.cc.put(response) # tell our controller the serialized state of the block response = MagicMock(id_=self.cc.BLOCK_ID, changes=[[[], self.serialized]]) self.cc.put(response)
def test_replace_children(self): b = Block() b.name = "blockname" b.methods["m1"] = 2 b.attributes["a1"] = 3 setattr(b, "m1", 2) setattr(b, "a1", 3) attr_meta = StringMeta(description="desc") attr = Attribute(attr_meta) b.add_attribute('attr', attr) method = Method(description="desc") b.add_method('method', method) b.on_changed = MagicMock(wrap=b.on_changed) b.replace_children({'attr': attr, 'method': method}) self.assertEqual(b.attributes, dict(attr=attr)) self.assertEqual(b.methods, dict(method=method)) b.on_changed.assert_called_once_with([[], b.to_dict()], True) self.assertFalse(hasattr(b, "m1")) self.assertFalse(hasattr(b, "a1"))
def test_replace_children(self): b = Block() b.name = "blockname" b.methods["m1"] = 2 b.attributes["a1"] = 3 setattr(b, "m1", 2) setattr(b, "a1", 3) attr_meta = StringMeta(description="desc") attr = Attribute(attr_meta) b.add_attribute('attr', attr) method = Method(description="desc") b.add_method('method', method) b.on_changed = MagicMock(wrap=b.on_changed) b.replace_children({'attr':attr, 'method':method}) self.assertEqual(b.attributes, dict(attr=attr)) self.assertEqual(b.methods, dict(method=method)) b.on_changed.assert_called_once_with( [[], b.to_dict()], True) self.assertFalse(hasattr(b, "m1")) self.assertFalse(hasattr(b, "a1"))
def test_returns_dict(self): method_dict = OrderedDict(takes=OrderedDict(one=OrderedDict()), returns=OrderedDict(one=OrderedDict()), defaults=OrderedDict()) m1 = MagicMock() m1.name = "method_one" m1.to_dict.return_value = method_dict m2 = MagicMock() m2.name = "method_two" m2.to_dict.return_value = method_dict a1 = MagicMock() a1.name = "attr_one" a1dict = OrderedDict(value="test", meta=MagicMock()) a1.to_dict.return_value = a1dict a2 = MagicMock() a2.name = "attr_two" a2dict = OrderedDict(value="value", meta=MagicMock()) a2.to_dict.return_value = a2dict block = Block("Test") block.add_method(m1) block.add_method(m2) block.add_attribute(a1) block.add_attribute(a2) expected_dict = OrderedDict() expected_dict['attr_one'] = a1dict expected_dict['attr_two'] = a2dict expected_dict['method_one'] = method_dict expected_dict['method_two'] = method_dict response = block.to_dict() m1.to_dict.assert_called_once_with() m2.to_dict.assert_called_once_with() self.assertEqual(expected_dict, response)
class TestHandleRequest(unittest.TestCase): def setUp(self): self.block = Block("TestBlock") self.method = MagicMock() self.method.name = "get_things" self.response = MagicMock() self.block.add_method(self.method) def test_given_request_then_pass_to_correct_method(self): request = MagicMock() request.POST = "Post" request.type_ = "Post" request.endpoint = ["TestBlock", "device", "get_things"] self.block.handle_request(request) self.method.handle_request.assert_called_once_with(request) def test_given_get_then_return_attribute(self): self.block.state = MagicMock() self.block.state.value = "Running" request = MagicMock() request.type_ = "Get" request.endpoint = ["TestBlock", "state", "value"] self.block.handle_request(request) request.respond_with_return.assert_called_once_with("Running") def test_given_get_block_then_return_self(self): request = MagicMock() request.type_ = "Get" request.endpoint = ["TestBlock"] expected_call = self.block.to_dict() self.block.handle_request(request) request.respond_with_return.assert_called_once_with(expected_call)