def test_can_generate_code_with_xsd_refs_to_elements_with_anoynmous_complex_types(self): # The final test should have an object graph representation of the # schema below. Currently I don't know how to represent multiple # xs:elements in a schema without using ComplexTypes. # Maybe we should have a special type like AnonymousComplexType and # put that directly into schema.elements? xml = utils.open_document('tests/assets/generation/reference_complex.xsd') schema = xsdspec.Schema.parse_xmlelement(etree.fromstring(xml)) code = xsd2py.schema_to_py(schema, ['xs']) schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas) assert_length(3, symbols) assert_contains('Person', symbols.keys()) assert_contains('Job', symbols.keys()) assert_equals(set(['person', 'job']), list(schemas[0].elements)) Job = symbols['Job'] Person = symbols['Person'] person_ref = Job.person assert_isinstance(person_ref, xsd.Ref) assert_equals(person_ref._type, Person) job = Job() person = Person() person.name = 'Foo' job.person = person
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_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_code_with_xsd_refs_to_elements_with_anoynmous_complex_types( self): # The final test should have an object graph representation of the # schema below. Currently I don't know how to represent multiple # xs:elements in a schema without using ComplexTypes. # Maybe we should have a special type like AnonymousComplexType and # put that directly into schema.elements? xml = utils.open_document( 'tests/assets/generation/reference_complex.xsd') schema = xsdspec.Schema.parse_xmlelement(etree.fromstring(xml)) code = xsd2py.schema_to_py(schema, ['xs']) schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas) assert_length(3, symbols) assert_contains('Person', symbols.keys()) assert_contains('Job', symbols.keys()) assert_equals(set(['person', 'job']), list(schemas[0].elements)) Job = symbols['Job'] Person = symbols['Person'] person_ref = Job.person assert_isinstance(person_ref, xsd.Ref) assert_equals(person_ref._type, Person) job = Job() person = Person() person.name = 'Foo' job.person = person
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_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_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_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_can_generate_code_for_two_schemas(self): xml = utils.open_document("tests/assets/generation/multi_schema.wsdl") code = wsdl2py.generate_code_from_wsdl(xml, "client") schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas) assert_length(4, symbols) assert_equals(["A"], list(schemas[0].elements)) assert_equals(["B"], list(schemas[1].elements))
def test_can_generate_code_for_two_schemas(self): xml = utils.open_document('tests/assets/generation/multi_schema.wsdl') code = wsdl2py.generate_code_from_wsdl(xml, 'client') schemas, symbols = generated_symbols(code) self.assertTrue(schemas) self.assertEqual(len([s for s in symbols if s.startswith('Schema_')]), 2) self.assertEqual(['A'], list(schemas[0].elements)) self.assertEqual(['B'], list(schemas[1].elements))
def test_can_generate_code_for_two_schemas(self): xml = utils.open_document('tests/assets/generation/multi_schema.wsdl') code = wsdl2py.generate_code_from_wsdl(xml, 'client') schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas) assert_length(2, [s for s in symbols if s.startswith('Schema_')]) assert_equals(['A'], list(schemas[0].elements)) assert_equals(['B'], list(schemas[1].elements))
def test_can_generate_code_for_simple_wsdl_import(self): path = 'tests/assets/generation/import_simple.wsdl' xml = utils.open_document(path) code = wsdl2py.generate_code_from_wsdl(xml, 'client', cwd=os.path.dirname(path)) schemas, symbols = generated_symbols(code) self.assertTrue(schemas)
def test_can_generate_code_for_looped_wsdl_import(self): path = 'tests/assets/generation/import_looped.wsdl' xml = utils.open_document(path) code = wsdl2py.generate_code_from_wsdl(xml, 'client', cwd=os.path.dirname(path)) schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas)
def test_can_generate_code_for_inheritance(self): xml = utils.open_document('tests/assets/generation/inheritance.wsdl') code = wsdl2py.generate_code_from_wsdl(xml, 'client') schemas, symbols = generated_symbols(code) self.assertTrue(schemas) self.assertEqual(len(symbols), 4) self.assertEqual(['B', 'A'], list(schemas[0].elements)) self.assertIsInstance(schemas[0].elements['B']._type, xsd.String) self.assertIsInstance(schemas[0].elements['A']._type, schemas[0].elements['B']._type.__class__)
def test_can_generate_code_for_multiple_schemas(self): # raise SkipTest('can not generate code for wsdl with multiple schemas') code_string = wsdl2py.generate_code_from_wsdl(WSDL, 'client') schema, new_symbols = generated_symbols(code_string) assert_not_none(schema) assert_length(4, new_symbols) assert_equals(['B', 'A'], list(schema.elements))
def test_can_generate_code_for_inheritance(self): xml = utils.open_document('tests/assets/generation/inheritance.wsdl') code = wsdl2py.generate_code_from_wsdl(xml, 'client') schemas, symbols = generated_symbols(code) assert_is_not_empty(schemas) assert_length(4, symbols) assert_equals(['B', 'A'], list(schemas[0].elements)) assert_isinstance(schemas[0].elements['B']._type, xsd.String) assert_isinstance(schemas[0].elements['A']._type, schemas[0].elements['B']._type.__class__)
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_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_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_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_code_with_xsd_refs_to_elements_with_anoynmous_complex_types( self): raise SkipTest( 'References to elements with anonymous complex types are not yet implemented' ) # The final test should have an object graph representation of the # schema below. Currently I don't know how to represent multiple # xs:elements in a schema without using ComplexTypes. # Maybe we should have a special type like AnonymousComplexType and # put that directly into schema.elements? 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="person">\n' ' <xs:complexType>\n' ' <xs:sequence>\n' ' <xs:element name="name" type="xs:string" />\n' ' </xs:sequence>\n' ' </xs:complexType>\n' ' </xs:element>\n' ' <xs:element name="job">\n' ' <xs:complexType>\n' ' <xs:sequence>\n' ' <xs:element ref="example:person" />\n' ' </xs:sequence>\n' ' </xs:complexType>\n' ' </xs:element>\n' '</xs:schema>') xml_element = etree.fromstring(xml) generated_schema = xsdspec.Schema.parse_xmlelement(xml_element) code_string = xsd2py.schema_to_py(generated_schema, ['xs']) schema, new_symbols = generated_symbols(code_string) assert_not_none(schema) assert_length(3, new_symbols) assert_contains('Person', new_symbols.keys()) assert_contains('Job', new_symbols.keys()) assert_equals(set(['person', 'job']), list(schema.elements)) Job = new_symbols['Job'] Person = new_symbols['Person'] person_ref = Job.person assert_isinstance(person_ref, xsd.Ref) assert_equals(person_ref._type, Person) job = Job() person = Person() person.name = u'Foo' job.person = person
def test_can_generate_code_with_xsd_refs_to_elements_with_anoynmous_complex_types(self): raise SkipTest('References to elements with anonymous complex types are not yet implemented') # The final test should have an object graph representation of the # schema below. Currently I don't know how to represent multiple # xs:elements in a schema without using ComplexTypes. # Maybe we should have a special type like AnonymousComplexType and # put that directly into schema.elements? 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="person">\n' ' <xs:complexType>\n' ' <xs:sequence>\n' ' <xs:element name="name" type="xs:string" />\n' ' </xs:sequence>\n' ' </xs:complexType>\n' ' </xs:element>\n' ' <xs:element name="job">\n' ' <xs:complexType>\n' ' <xs:sequence>\n' ' <xs:element ref="example:person" />\n' ' </xs:sequence>\n' ' </xs:complexType>\n' ' </xs:element>\n' '</xs:schema>') xml_element = etree.fromstring(xml) generated_schema = xsdspec.Schema.parse_xmlelement(xml_element) code_string = xsd2py.schema_to_py(generated_schema, ['xs']) schema, new_symbols = generated_symbols(code_string) assert_not_none(schema) assert_length(3, new_symbols) assert_contains('Person', new_symbols.keys()) assert_contains('Job', new_symbols.keys()) assert_equals(set(['person', 'job']), list(schema.elements)) Job = new_symbols['Job'] Person = new_symbols['Person'] person_ref = Job.person assert_isinstance(person_ref, xsd.Ref) assert_equals(person_ref._type, Person) job = Job() person = Person() person.name = u'Foo' job.person = person
def test_can_generate_remote_tree(self): def _mock(path): if path == 'http://example.org/xsd/simple_element.xsd': filename = 'tests/assets/generation/simple_element.xsd' else: self.fail('Unexpected remote path: %s' % path) with open(filename, 'rb') as f: return f.read() xml = utils.open_document('tests/assets/generation/import_remote.wsdl') with mock.patch('soapfish.xsd2py.open_document') as p: p.side_effect = _mock code = wsdl2py.generate_code_from_wsdl( xml, 'client', cwd='http://example.org/code/') schemas, symbols = generated_symbols(code)
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) 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_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 = 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_code_for_inheritance(self): raise SkipTest('can not generate code for wsdl with type inheritance') xml = '<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' \ ' <wsdl:types>' \ ' <xsd:schema elementFormDefault="qualified" targetNamespace="http://example.org/A">' \ ' <xsd:element name="A" type="B"/>' \ ' <xsd:element name="B" type="xsd:string"/>' \ ' </xsd:schema>' \ ' </wsdl:types>' \ '</wsdl:definitions>' code_string = wsdl2py.generate_code_from_wsdl(xml, 'client') schema, new_symbols = generated_symbols(code_string) assert_not_none(schema) assert_length(4, new_symbols) assert_equals(['B', 'A'], list(schema.elements)) assert_isinstance(schema.elements['B']._type, xsd.String) assert_isinstance(schema.elements['A']._type, schema.elements['B']._type.__class__)
def test_can_generate_code_for_two_schemas(self): # raise SkipTest('can not generate code for wsdl with multiple schemas') xml = '<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:b="http://example.org/B">' \ ' <wsdl:types>' \ ' <xsd:schema elementFormDefault="qualified" targetNamespace="http://example.org/A">' \ ' <xsd:import namespace="http://example.org/B"/>' \ ' <xsd:element name="A" type="b:B"/>' \ ' </xsd:schema>' \ ' <xsd:schema elementFormDefault="qualified" targetNamespace="http://example.org/B">' \ ' <xsd:element name="B" type="xsd:string"/>' \ ' </xsd:schema>' \ ' </wsdl:types>' \ '</wsdl:definitions>' code_string = wsdl2py.generate_code_from_wsdl(xml, 'client') schema, new_symbols = generated_symbols(code_string) assert_not_none(schema) assert_length(4, new_symbols) assert_equals(['B', 'A'], list(schema.elements))
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 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 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_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_can_generate_code_for_hello(self): code_string = wsdl2py.generate_code_from_wsdl(WSDL_CHANGED, 'client') schema, new_symbols = generated_symbols(code_string) assert_not_none(schema)
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__)