def _create_control_server(self, control_server): if control_server is None: return None return ControlServer( { "device": ExposedObject( self._device, exclude_inherited=True, lock=self._adapters.device_lock, ), "simulation": ExposedObject( self, exclude=("start", "control_server", "log"), exclude_inherited=True, ), "interface": ExposedObject( self._adapters, exclude=( "device_lock", "add_adapter", "remove_adapter", "handle", "log", ), exclude_inherited=True, ), }, control_server, )
def test_get_api(self): obj = DummyObject() rpc_object = ExposedObject(obj, ["a"]) api = rpc_object.get_api() self.assertTrue("class" in api) self.assertEqual(api["class"], type(obj).__name__) self.assertTrue("methods" in api) self.assertEqual(set(api["methods"]), {":api", "a:set", "a:get"})
def test_get_api(self): obj = DummyObject() rpc_object = ExposedObject(obj, ['a']) api = rpc_object.get_api() self.assertTrue('class' in api) self.assertEqual(api['class'], type(obj).__name__) self.assertTrue('methods' in api) self.assertEqual(set(api['methods']), {':api', 'a:set', 'a:get'})
def test_method_wrapper_calls(self): obj = DummyObject() rpc_object = ExposedObject(obj) rpc_object["getTest"](45, 56) obj.getTest.assert_called_with(45, 56)
def test_excluded_methods_not_exposed(self): rpc_object = ExposedObject(DummyObject(), exclude=("a", "setTest")) expected_methods = [":api", "b:get", "b:set", "getTest"] self.assertEqual(len(rpc_object), len(expected_methods)) for method in expected_methods: self.assertTrue(method in rpc_object)
def test_inherited_exposed(self): rpc_object = ExposedObject(DummyObjectChild(), members=("a", "c")) expected_methods = [":api", "a:get", "a:set", "c:get", "c:set"] self.assertEqual(len(rpc_object), len(expected_methods)) for method in expected_methods: self.assertTrue(method in rpc_object)
def test_inherited_exposed(self): rpc_object = ExposedObject(DummyObjectChild(), members=('a', 'c')) expected_methods = [':api', 'a:get', 'a:set', 'c:get', 'c:set'] self.assertEqual(len(rpc_object), len(expected_methods)) for method in expected_methods: self.assertTrue(method in rpc_object)
def test_excluded_methods_not_exposed(self): rpc_object = ExposedObject(DummyObject(), exclude=('a', 'setTest')) expected_methods = [':api', 'b:get', 'b:set', 'getTest'] self.assertEqual(len(rpc_object), len(expected_methods)) for method in expected_methods: self.assertTrue(method in rpc_object)
def test_select_methods_exposed(self): rpc_object = ExposedObject(DummyObject(), ('a', 'getTest')) expected_methods = [':api', 'a:get', 'a:set', 'getTest'] self.assertEqual(len(rpc_object), len(expected_methods)) for method in expected_methods: self.assertTrue(method in rpc_object)
def test_select_methods_exposed(self): rpc_object = ExposedObject(DummyObject(), ("a", "getTest")) expected_methods = [":api", "a:get", "a:set", "getTest"] self.assertEqual(len(rpc_object), len(expected_methods)) for method in expected_methods: self.assertTrue(method in rpc_object)
def test_attribute_wrapper_sets_value(self): obj = DummyObject() obj.a = 233 rpc_object = ExposedObject(obj) self.assertEqual(obj.a, 233) rpc_object["a:set"](20) self.assertEqual(obj.a, 20)
def test_add_exposed_object(self): exposed_objects = ExposedObjectCollection({}) obj = DummyObject() assertRaisesNothing(self, exposed_objects.add_object, ExposedObject(obj, ('setTest', 'getTest')), 'testObject') exposed_objects['testObject.getTest'](41, 11) obj.getTest.assert_called_once_with(41, 11)
def test_selected_and_excluded_methods(self): rpc_object = ExposedObject(DummyObject(), members=("a", "getTest"), exclude=("a")) expected_methods = [":api", "getTest"] self.assertEqual(len(rpc_object), len(expected_methods)) for method in expected_methods: self.assertTrue(method in rpc_object)
def test_selected_and_excluded_methods(self): rpc_object = ExposedObject(DummyObject(), members=('a', 'getTest'), exclude=('a')) expected_methods = [':api', 'getTest'] self.assertEqual(len(rpc_object), len(expected_methods)) for method in expected_methods: self.assertTrue(method in rpc_object)
def test_all_methods_exposed(self): rpc_object = ExposedObject(TestObject()) expected_methods = [ ':api', 'a:get', 'a:set', 'b:get', 'b:set', 'getTest', 'setTest' ] self.assertEqual(len(rpc_object), len(expected_methods)) for method in expected_methods: self.assertTrue(method in rpc_object)
def _create_control_server(self, control_server): if control_server is None: return None return ControlServer( { 'device': self._device, 'simulation': ExposedObject(self, exclude=('start', 'control_server')) }, control_server)
def _create_control_server(self, control_server): if control_server is None: return None return ControlServer( { 'device': ExposedObject(self._device, exclude_inherited=True, lock=self._adapters.device_lock), 'simulation': ExposedObject(self, exclude=('start', 'control_server', 'log'), exclude_inherited=True), 'interface': ExposedObject(self._adapters, exclude=('device_lock', 'add_adapter', 'remove_adapter', 'handle', 'log'), exclude_inherited=True) }, control_server)
def test_lock_is_used_if_supplied(self): mock_lock = Mock() mock_lock.__enter__ = Mock() mock_lock.__exit__ = Mock() obj = DummyObject() exposed_object = ExposedObject(obj, ["a"], lock=mock_lock) self.assertEqual(exposed_object["a:get"](), obj.a) mock_lock.__enter__.assert_called_once() mock_lock.__exit__.assert_called_once()
def test_add_exposed_object(self): exposed_objects = ExposedObjectCollection({}) obj = DummyObject() assertRaisesNothing( self, exposed_objects.add_object, ExposedObject(obj, ("setTest", "getTest")), "testObject", ) exposed_objects["testObject.getTest"](41, 11) obj.getTest.assert_called_once_with(41, 11)
def test_attribute_wrapper_gets_value(self): obj = DummyObject() obj.a = 233 rpc_object = ExposedObject(obj) self.assertEqual(rpc_object["a:get"](), obj.a)
def test_attribute_wrapper_argument_number(self): rpc_object = ExposedObject(DummyObject()) self.assertRaises(TypeError, rpc_object["a:get"], 20) self.assertRaises(TypeError, rpc_object["a:set"]) self.assertRaises(TypeError, rpc_object["a:set"], 40, 30)