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)
Exemple #2
0
    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)
Exemple #3
0
 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)
Exemple #4
0
    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_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)
Exemple #6
0
    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)
Exemple #8
0
    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'
Exemple #9
0
 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)
Exemple #10
0
 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)
Exemple #11
0
 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__)
Exemple #12
0
 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))
Exemple #13
0
 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)
Exemple #14
0
 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)
Exemple #15
0
 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))
Exemple #17
0
    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)
Exemple #18
0
    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)
Exemple #19
0
    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)
Exemple #20
0
    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)
Exemple #21
0
    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)
Exemple #22
0
    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 = ('<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)
Exemple #24
0
    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
Exemple #25
0
    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'
Exemple #28
0
    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'
Exemple #29
0
 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)
Exemple #30
0
 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')
Exemple #31
0
 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, {})
Exemple #32
0
 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']
Exemple #35
0
 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)
Exemple #36
0
 def setUp(self):
     xsd_str = utils.open_document(
         'tests/assets/generation/attrgroup_usage.xsd')
     self.code = xsd2py.generate_code_from_xsd(xsd_str)
Exemple #37
0
 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, {})
Exemple #38
0
 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']
Exemple #39
0
 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, {})
Exemple #40
0
 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)
Exemple #41
0
 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 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__)
Exemple #44
0
 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)
 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')