def test_can_generate_extension(self): xml = """ <xs:schema targetNamespace="http://example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:complexType name="ComplexType"> <xs:complexContent> <xs:extension base="Base"> <xs:sequence> <xs:element maxOccurs="1" minOccurs="0" name="Field2" type="xs:string"> </xs:element> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="Base"> <xs:sequence> <xs:element name="Field1" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:schema> """ xml_element = etree.fromstring(xml) code_string = xsd2py.generate_code_from_xsd(xml_element) schema, new_symbols = generated_symbols(code_string)
def test_can_generate_list_enumeration(self): raise SkipTest('list enumerations are not parsed correctly from xsd') xml = '<xsd:schema elementFormDefault="qualified" targetNamespace="http://example.org/A" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' \ ' <xsd:simpleType name="MyList">' \ ' <xsd:list>' \ ' <xsd:simpleType>' \ ' <xsd:restriction base="xsd:string">' \ ' <xsd:enumeration value="A"/>' \ ' <xsd:enumeration value="B"/>' \ ' <xsd:enumeration value="C"/>' \ ' </xsd:restriction>' \ ' </xsd:simpleType>' \ ' </xsd:list>' \ ' </xsd:simpleType>' \ '</xsd:schema>' xml_element = etree.fromstring(xml) code_string = xsd2py.generate_code_from_xsd(xml_element) schema, new_symbols = generated_symbols(code_string) assert_not_none(schema) assert_length(2, new_symbols) assert_true(issubclass(new_symbols['MyList'], xsd.List)) my_list = new_symbols['MyList']() assert_equals(my_list.accept(['B']), True)
def test_schema_xsd_restriction(self): xml = utils.open_document('tests/assets/generation/restriction.xsd') code = xsd2py.generate_code_from_xsd(xml) if six.PY3: code = code.decode() assert_contains('RestrictedString', code) assert_contains("pattern=r'[a-z]+'", code)
def test_can_generate_code_with_xsd_refs_to_simple_elements(self): xml = utils.open_document('tests/assets/generation/reference_simple.xsd') code = xsd2py.generate_code_from_xsd(xml) schemas, symbols = generated_symbols(code) self.assertTrue(schemas) # somehow we need to be able to have schemas with multiple possible # root elements self.assertEqual(len(symbols), 3) self.assertIn('Name', symbols.keys()) self.assertIn('Job', symbols.keys()) self.assertEqual({'job', 'name'}, list(schemas[0].elements)) Job = symbols['Job'] Name = symbols['Name'] name_ref = Job.name # not sure if these assertions are correct but they should give you # the idea self.assertIsInstance(name_ref, xsd.Ref) self.assertEqual(name_ref._type, Name) job = Job() # Should not raise job.name = 'Foo'
def test_can_generate_code_with_xsd_refs_to_simple_elements(self): xml = utils.open_document( 'tests/assets/generation/reference_simple.xsd') code = xsd2py.generate_code_from_xsd(xml) schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas) # somehow we need to be able to have schemas with multiple possible # root elements assert_length(3, symbols) assert_contains('Name', symbols.keys()) assert_contains('Job', symbols.keys()) assert_equals(set(['name', 'job']), list(schemas[0].elements)) Job = symbols['Job'] Name = symbols['Name'] name_ref = Job.name # not sure if these assertions are correct but they should give you # the idea assert_isinstance(name_ref, xsd.Ref) assert_equals(name_ref._type, Name) job = Job() # Should not raise job.name = u'Foo'
def test_can_generate_code_with_xsd_refs_to_simple_elements(self): xml = utils.open_document('tests/assets/generation/reference_simple.xsd') code = xsd2py.generate_code_from_xsd(xml) schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas) # somehow we need to be able to have schemas with multiple possible # root elements assert_length(3, symbols) assert_contains('Name', symbols.keys()) assert_contains('Job', symbols.keys()) assert_equals(set(['name', 'job']), list(schemas[0].elements)) Job = symbols['Job'] Name = symbols['Name'] name_ref = Job.name # not sure if these assertions are correct but they should give you # the idea assert_isinstance(name_ref, xsd.Ref) assert_equals(name_ref._type, Name) job = Job() # Should not raise job.name = u'Foo'
def test_create_method_list_param(self): xmlelement = etree.fromstring(XSD_LIST_PARAM) code = generate_code_from_xsd(xmlelement) if six.PY3: code = code.decode() assert_contains("def create(cls, Items):", code) assert_contains("instance.Items = Items", code) assert_not_contains("Itemss", code)
def test_can_generate_extension_of_type_with_special_chars(self): xml = utils.open_document( 'tests/assets/generation/extension_with_special_chars.xsd') code = xsd2py.generate_code_from_xsd(xml) schemas, symbols = generated_symbols(code) assert_true('BaseType_with_special_chars_123' in symbols) assert_equals('BaseType_with_special_chars_123', symbols['ComplexType'].__base__.__name__)
def test_schema_xsd_enumeration(self): xml = utils.open_document('tests/assets/generation/enumeration2.xsd') code = xsd2py.generate_code_from_xsd(xml) m = {} try: self._exec(code, m) except SyntaxError as e: self.fail('%s: %s' % (e.__class__.__name__, e))
def test_create_method_list_param(self): xml = utils.open_document('tests/assets/generation/list_param.xsd') code = xsd2py.generate_code_from_xsd(xml) if six.PY3: code = code.decode() assert_contains('def create(cls, Items):', code) assert_contains('instance.Items = Items', code) assert_not_contains('Itemss', code)
def test_schema_xsd_enumeration(self): xml = utils.open_document('tests/assets/generation/enumeration2.xsd') code = xsd2py.generate_code_from_xsd(xml) if six.PY3: code = code.decode() m = {} try: self._exec(code, m) except SyntaxError as e: self.fail('%s: %s' % (e.__class__.__name__, e))
def test_can_generate_code_for_simple_element(self): xml = utils.open_document('tests/assets/generation/simple_element.xsd') code = xsd2py.generate_code_from_xsd(xml) schemas, symbols = generated_symbols(code) self.assertTrue(schemas) self.assertEqual(len([s for s in symbols if s.startswith('Schema_')]), 1) self.assertEqual(['simpleElement'], list(schemas[0].elements)) simple_element = schemas[0].elements['simpleElement'] self.assertIsInstance(simple_element._type, xsd.String)
def test_can_generate_list_enumeration(self): xml = utils.open_document('tests/assets/generation/enumeration.xsd') code = xsd2py.generate_code_from_xsd(xml) schemas, symbols = generated_symbols(code) self.assertTrue(schemas) self.assertEqual(len(symbols), 2) self.assertIs(issubclass(symbols['MyList'], xsd.List), True) my_list = symbols['MyList']() self.assertIs(my_list.accept(['B']), True)
def test_can_generate_list_enumeration(self): xml = utils.open_document('tests/assets/generation/enumeration.xsd') code = xsd2py.generate_code_from_xsd(xml) schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas) assert_length(2, symbols) assert_true(issubclass(symbols['MyList'], xsd.List)) my_list = symbols['MyList']() assert_equals(my_list.accept(['B']), True)
def test_can_generate_code_for_simple_element(self): xml = utils.open_document('tests/assets/generation/simple_element.xsd') code = xsd2py.generate_code_from_xsd(xml) schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas) assert_length(1, symbols) assert_equals(['simpleElement'], list(schemas[0].elements)) simple_element = schemas[0].elements['simpleElement'] assert_isinstance(simple_element._type, xsd.String)
def test_can_generate_code_for_simple_element(self): xml = ('<xs:schema targetNamespace="http://site.example/ws/spec" \n' ' xmlns:example="http://site.example/ws/spec" \n' ' xmlns:xs="http://www.w3.org/2001/XMLSchema" \n' ' elementFormDefault="qualified" attributeFormDefault="unqualified">\n' ' <xs:element name="simpleElement" type="xs:string"/>\n' '</xs:schema>') xml_element = etree.fromstring(xml) code_string = xsd2py.generate_code_from_xsd(xml_element) schema, new_symbols = generated_symbols(code_string) assert_not_none(schema) assert_length(1, new_symbols) assert_equals(['simpleElement'], list(schema.elements)) simple_element = schema.elements['simpleElement'] assert_isinstance(simple_element._type, xsd.String)
def test_can_generate_extension_imported(self): xml = utils.open_document('tests/assets/generation/extension_imported.xsd') code = xsd2py.generate_code_from_xsd(xml, cwd='tests/assets/generation') schemas, symbols = generated_symbols(code) self.assertTrue(schemas) base = symbols['Base'] base.Field1 try: base.Field2 except AttributeError: pass else: self.fail('Unexpected base.Field2') ct = symbols['ComplexType'] ct.Field1 ct.Field2
def test_can_generate_code_for_simple_element(self): xml = ( '<xs:schema targetNamespace="http://site.example/ws/spec" \n' ' xmlns:example="http://site.example/ws/spec" \n' ' xmlns:xs="http://www.w3.org/2001/XMLSchema" \n' ' elementFormDefault="qualified" attributeFormDefault="unqualified">\n' ' <xs:element name="simpleElement" type="xs:string"/>\n' '</xs:schema>') xml_element = etree.fromstring(xml) code_string = xsd2py.generate_code_from_xsd(xml_element) schema, new_symbols = self._generated_symbols(code_string) assert_not_none(schema) assert_length(1, new_symbols) assert_equals(['simpleElement'], list(schema.elements)) simple_element = schema.elements['simpleElement'] assert_isinstance(simple_element._type, xsd.String)
def test_can_generate_extension_imported(self): xml = utils.open_document('tests/assets/generation/extension_imported.xsd') code = xsd2py.generate_code_from_xsd(xml, cwd='tests/assets/generation') schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas) base = symbols['Base'] base.Field1 try: base.Field2 except AttributeError: pass else: self.fail('Unexpected base.Field2') ct = symbols['ComplexType'] ct.Field1 ct.Field2
def test_can_generate_code_with_xsd_refs_to_simple_elements(self): raise SkipTest('References to simple elements not yet implemented') xml = ('<xs:schema targetNamespace="http://site.example/ws/spec" \n' ' xmlns:example="http://site.example/ws/spec" \n' ' xmlns:xs="http://www.w3.org/2001/XMLSchema" \n' ' elementFormDefault="qualified">\n' ' <xs:element name="name" type="xs:string" />\n' ' <xs:element name="job">\n' ' <xs:complexType>\n' ' <xs:sequence>\n' ' <xs:element ref="example:name" />\n' ' </xs:sequence>\n' ' </xs:complexType>\n' ' </xs:element>\n' '</xs:schema>') xml_element = etree.fromstring(xml) code_string = xsd2py.generate_code_from_xsd(xml_element) schema, new_symbols = generated_symbols(code_string) assert_not_none(schema) # somehow we need to be able to have a schema with multiple possible # root elements assert_length(3, new_symbols) assert_contains('Name', new_symbols.keys()) assert_contains('Job', new_symbols.keys()) assert_equals(set(['name', 'job']), list(schema.elements)) Job = new_symbols['Job'] Name = new_symbols['Name'] name_ref = Job.name # not sure if these assertions are correct but they should give you # the idea assert_isinstance(name_ref, xsd.Ref) assert_equals(name_ref._type, Name) job = Job() # Should not raise job.name = u'Foo'
def test_can_generate_code_with_xsd_refs_to_simple_elements(self): raise SkipTest('References to simple elements not yet implemented') xml = ('<xs:schema targetNamespace="http://site.example/ws/spec" \n' ' xmlns:example="http://site.example/ws/spec" \n' ' xmlns:xs="http://www.w3.org/2001/XMLSchema" \n' ' elementFormDefault="qualified">\n' ' <xs:element name="name" type="xs:string" />\n' ' <xs:element name="job">\n' ' <xs:complexType>\n' ' <xs:sequence>\n' ' <xs:element ref="example:name" />\n' ' </xs:sequence>\n' ' </xs:complexType>\n' ' </xs:element>\n' '</xs:schema>') xml_element = etree.fromstring(xml) code_string = xsd2py.generate_code_from_xsd(xml_element) schema, new_symbols = self._generated_symbols(code_string) assert_not_none(schema) # somehow we need to be able to have a schema with multiple possible # root elements assert_length(3, new_symbols) assert_contains('Name', new_symbols.keys()) assert_contains('Job', new_symbols.keys()) assert_equals(set(['name', 'job']), list(schema.elements)) Job = new_symbols['Job'] Name = new_symbols['Name'] name_ref = Job.name # not sure if these assertions are correct but they should give you # the idea assert_isinstance(name_ref, xsd.Ref) assert_equals(name_ref._type, Name) job = Job() # Should not raise job.name = u'Foo'
def test_can_generate_extension(self): xml = utils.open_document('tests/assets/generation/extension.xsd') code = xsd2py.generate_code_from_xsd(xml) schemas, symbols = generated_symbols(code)
def test_schema_xsd_restriction(self): xmlelement = etree.fromstring(XSD_RESTRICTION) code = generate_code_from_xsd(xmlelement) if six.PY3: code = code.decode() compile(code, 'restriction', 'exec')
def test_code_generation_from_xsd_with_ordering_and_elements(self): xmlelement = etree.fromstring(XSD_with_ordering_and_elements) code = b'from soapfish import soap, xsd\n' + generate_code_from_xsd( xmlelement) self._exec(code, {})
def test_code_generation_from_xsd_with_ordering(self): xmlelement = etree.fromstring(XSD_with_ordering) # Add mandatory imports to test the generated code code = b'from soapfish import soap, xsd\n' + generate_code_from_xsd( xmlelement) self._exec(code, {})
def setUp(self): xsd_str = utils.open_document('tests/assets/generation/attrgroup_usage.xsd') self.code = xsd2py.generate_code_from_xsd(xsd_str)
def setUp(self): xsd_str = utils.open_document('tests/assets/generation/attribute_usage.xsd') code = xsd2py.generate_code_from_xsd(xsd_str) self.schemas, self.symbols = testutil.generated_symbols(code) self.SampleType = self.symbols['SampleType']
def test_schema_xsd_restriction(self): xml = utils.open_document('tests/assets/generation/restriction.xsd') code = xsd2py.generate_code_from_xsd(xml).decode() self.assertIn('RestrictedString', code) self.assertIn("pattern=r'[a-z]+'", code)
def setUp(self): xsd_str = utils.open_document( 'tests/assets/generation/attrgroup_usage.xsd') self.code = xsd2py.generate_code_from_xsd(xsd_str)
def test_code_generation_from_xsd(self): xml = utils.open_document('tests/assets/generation/default.xsd') # Add mandatory imports to test the generated code code = xsd2py.generate_code_from_xsd(xml) self._exec(code, {})
def setUp(self): xsd_str = utils.open_document( 'tests/assets/generation/attribute_usage.xsd') code = xsd2py.generate_code_from_xsd(xsd_str) self.schemas, self.symbols = testutil.generated_symbols(code) self.SampleType = self.symbols['SampleType']
def test_code_generation_from_xsd_with_ordering_and_elements(self): xmlelement = etree.fromstring(XSD_with_ordering_and_elements) code = b'from soapfish import soap, xsd\n' + generate_code_from_xsd(xmlelement) self._exec(code, {})
def test_code_generation_from_xsd(self): xmlelement = etree.fromstring(XSD) # Add mandatory imports to test the generated code code = b'from soapfish import soap, xsd\n' + generate_code_from_xsd(xmlelement) self._exec(code, {})
def test_can_generate_extension_of_type_with_special_chars(self): xml = utils.open_document('tests/assets/generation/extension_with_special_chars.xsd') code = xsd2py.generate_code_from_xsd(xml) schemas, symbols = generated_symbols(code) assert_true('BaseType_with_special_chars_123' in symbols) assert_equals('BaseType_with_special_chars_123', symbols['ComplexType'].__base__.__name__)
def test_create_method_list_param(self): xml = utils.open_document('tests/assets/generation/list_param.xsd') code = xsd2py.generate_code_from_xsd(xml).decode() self.assertIn('def create(cls, Items):', code) self.assertIn('instance.Items = Items', code) self.assertNotIn('Itemss', code)