def test_optional(self): data_def = OptionalDefinition(IntegerDefinition()) data_val = build_data_value(10, data_def) self.assertEqual(data_val, OptionalValue(value=IntegerValue(10))) py_val = build_py_value(data_val, data_def) self.assertEqual(py_val, 10) self.assertTrue(isinstance(py_val, six.integer_types)) data_val = build_data_value(None, data_def) self.assertEqual(data_val, OptionalValue()) py_val = build_py_value(data_val, data_def) self.assertEqual(py_val, None)
def test_boolean(self): # Boolean Conversion data_def = BooleanDefinition() data_val = build_data_value(True, data_def) self.assertEqual(data_val, BooleanValue(True)) py_val = build_py_value(data_val, data_def) self.assertEqual(py_val, True) self.assertTrue(isinstance(py_val, bool)) data_def = BooleanDefinition() data_val = build_data_value(False, data_def) self.assertEqual(data_val, BooleanValue(False)) py_val = build_py_value(data_val, data_def) self.assertEqual(py_val, False) self.assertTrue(isinstance(py_val, bool))
def test_opaque(self): data_def = OpaqueDefinition() data_val = IntegerValue(10) py_val = build_py_value(data_val, data_def) self.assertEqual(py_val, data_val) py_val2 = build_data_value(10, data_def) self.assertEqual(py_val2, 10)
def test_void(self): # Void Conversion data_def = VoidDefinition() data_val = build_data_value(None, data_def) self.assertEqual(data_val, VoidValue()) py_val = build_py_value(data_val, data_def) self.assertEqual(py_val, None)
def test_dynamic_structure(self): data_def = DynamicStructDefinition() py_dict_val = { 'int_val': 10, 'str_val': 'testing', 'bool_val': True, 'opt_val': None, 'list_val': [10, 20, 30], 'struct_val': { 'int_val': 100, 'bool_val': False } } data_valc = build_data_value(py_dict_val, data_def) data_val = StructValue(DYNAMIC_STRUCTURE) data_val.set_field('int_val', IntegerValue(10)) data_val.set_field('str_val', StringValue('testing')) data_val.set_field('bool_val', BooleanValue(True)) data_val.set_field('opt_val', OptionalValue()) list_val = ListValue() list_val.add(IntegerValue(10)) list_val.add(IntegerValue(20)) list_val.add(IntegerValue(30)) data_val.set_field('list_val', list_val) struct_val = StructValue(DYNAMIC_STRUCTURE) struct_val.set_field('int_val', IntegerValue(100)) struct_val.set_field('bool_val', BooleanValue(False)) data_val.set_field('struct_val', struct_val) self.assertEqual(data_valc, data_val)
def test_structure(self): fields = [ ('int_val', IntegerDefinition()), ('str_val', StringDefinition()), ('bool_val', BooleanDefinition()), ('opt_val', OptionalDefinition(IntegerDefinition())), ('list_val', ListDefinition(IntegerDefinition())), ] data_def = StructDefinition('Properties', fields) py_val = TestPyConverter.Properties(10, 'testing', True, 10, [10, 20, 30]) data_valc = build_data_value(py_val, data_def) data_val = data_def.new_value() data_val.set_field('int_val', IntegerValue(10)) data_val.set_field('str_val', StringValue('testing')) data_val.set_field('bool_val', BooleanValue(True)) data_val.set_field('opt_val', OptionalValue(value=IntegerValue(10))) list_val = ListValue() list_val.add(IntegerValue(10)) list_val.add(IntegerValue(20)) list_val.add(IntegerValue(30)) data_val.set_field('list_val', list_val) self.assertEqual(data_valc, data_val) py_valc = build_py_value(data_valc, data_def, impl=self) self.assertEqual(py_val, py_valc) # Negative tests self.assertRaises( CoreException, build_data_value, TestPyConverter.Properties(None, 'testing', True, 10, [10, 20, 30]), data_def) self.assertRaises( CoreException, build_data_value, TestPyConverter.Properties(5.33, 'testing', True, 10, [10, 20, 30]), data_def)
def test_string(self): # String Conversion data_def = StringDefinition() data_val = build_data_value('test', data_def) self.assertEqual(data_val, StringValue('test')) py_val = build_py_value(data_val, data_def) self.assertEqual(py_val, 'test') self.assertTrue(isinstance(py_val, str))
def test_double(self): # Double Conversion data_def = DoubleDefinition() data_val = build_data_value(11.11, data_def) self.assertEqual(data_val, DoubleValue(11.11)) py_val = build_py_value(data_val, data_def) self.assertEqual(py_val, 11.11) self.assertTrue(isinstance(py_val, float))
def test_integer(self): # Integer Conversion data_def = IntegerDefinition() data_val = build_data_value(10, data_def) self.assertEqual(data_val, IntegerValue(10)) py_val = build_py_value(data_val, data_def) self.assertEqual(py_val, 10) self.assertTrue(isinstance(py_val, six.integer_types))
def visit_dynamic_struct(self, typ): """ Visit any struct value :type typ: :class:`vmware.vapi.bindings.type.DynamicStructType` :param typ: Binding type of the value """ if self._in_value.__class__.__name__ == 'VapiStruct': self._out_value = self._in_value.get_struct_value() elif self._in_value.__class__.__name__ == 'VapiError': self._out_value = self._in_value.get_error_value() elif isinstance(self._in_value, VapiStruct): self._visit_vapi_struct(self._in_value.get_binding_type()) elif isinstance(self._in_value, StructValue): self._out_value = self._in_value elif isinstance(self._in_value, dict): self._out_value = build_data_value(self._in_value, typ.definition) else: msg = message_factory.get_message( 'vapi.bindings.typeconverter.unexpected.python.type', StructValue.__name__, type(self._in_value).__name__) logger.debug(msg) raise CoreException(msg)