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_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_schema_xsd_include(self): path = 'tests/assets/include/include.wsdl' xml = utils.open_document(path) code = wsdl2py.generate_code_from_wsdl(xml, 'server', cwd=os.path.dirname(path)) if six.PY3: code = code.decode() assert_contains('Schema1_Element', code)
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_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_cannot_omit_required_attribute(self): instance = self.SampleType(value='someValue', type='someType') element = etree.Element('sample') with assert_raises(ValueError) as context: instance.render(element, instance) # the exception raised should mention the field is required assert_contains('required', str(context.caught_exception))
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_import_same_namespace(self): path = 'tests/assets/same_namespace/same_namespace.wsdl' xml = utils.open_document(path) code = wsdl2py.generate_code_from_wsdl(xml, 'server', cwd=os.path.dirname(path)) if six.PY3: code = code.decode() assert_contains('Schema1_Element', code) assert_contains('Schema2_Element', code)
def test_relative_paths(self): path = 'tests/assets/relative/relative.wsdl' xml = utils.open_document(path) code = wsdl2py.generate_code_from_wsdl(xml, 'server', cwd=os.path.dirname(path)) if six.PY3: code = code.decode() assert_contains('Schema2_Element', code) assert_contains('Schema3_Element', code) assert_equals(1, code.count('Schema3_Element'))
def test_can_handle_xsd_element_as_return_value_from_handler(self): def handler(request, input_): return input_ dispatcher = SOAPDispatcher(echo_service(handler)) soap_message = ( '<ns1:echoRequest xmlns:ns1="http://soap.example/echo/types">' '<value>hello</value>' '</ns1:echoRequest>' ) request_message = self._wrap_with_soap_envelope(soap_message) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) body_text = response.http_content if not isinstance(body_text, six.string_types): body_text = body_text.decode('utf-8') assert_contains('<value>hello</value>', body_text)
def assert_is_soap_fault(self, response, fault_code=None, partial_fault_string=None): assert_equals(500, response.http_status_code) assert_equals('text/xml', response.http_headers['Content-Type']) fault_document = etree.fromstring(response.http_content) soap_envelope = fault_document.getroottree() namespaces = {'s': 'http://schemas.xmlsoap.org/soap/envelope/'} fault_nodes = soap_envelope.xpath('/s:Envelope/s:Body/s:Fault', namespaces=namespaces) assert_length(1, fault_nodes, message='expected exactly one SOAP fault') children = list(fault_nodes[0]) assert_length(2, children) xml_fault_code, fault_string = children if fault_code is None: fault_code = 'Client' assert_equals(fault_code, xml_fault_code.text) if partial_fault_string: assert_contains(partial_fault_string, fault_string.text)
def test_can_handle_xsd_element_as_return_value_from_handler(self): def handler(request, input_): return input_ dispatcher = SOAPDispatcher(echo_service(handler)) soap_message = ( '<ns1:echoRequest xmlns:ns1="http://soap.example/echo/types">' '<value>hello</value>' '</ns1:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) body_text = response.http_content if not isinstance(body_text, six.string_types): body_text = body_text.decode('utf-8') assert_contains('<value>hello</value>', body_text)
def test_can_propagate_custom_output_header(self): handler, handler_state = echo_handler() def _handler(request, _input): resp = handler(request, _input) resp.soap_header = EchoOutputHeader(OutputVersion='42') return resp dispatcher = SOAPDispatcher(echo_service(_handler, output_header=EchoOutputHeader)) soap_header = '<tns:InputVersion>42</tns:InputVersion>' soap_message = ( '<tns:echoRequest xmlns:tns="http://soap.example/echo/types">' '<value>foobar</value>' '</tns:echoRequest>' ) request_message = self._wrap_with_soap_envelope(soap_message, header=soap_header) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) self.assert_is_successful_response(response, handler_state) assert_contains(b'<ns0:OutputVersion>42</ns0:OutputVersion>', response.http_content)
def test_can_propagate_custom_output_header(self): handler, handler_state = echo_handler() def _handler(request, _input): resp = handler(request, _input) resp.soap_header = EchoOutputHeader(OutputVersion='42') return resp dispatcher = SOAPDispatcher( echo_service(_handler, output_header=EchoOutputHeader)) soap_header = '<tns:InputVersion>42</tns:InputVersion>' soap_message = ( '<tns:echoRequest xmlns:tns="http://soap.example/echo/types">' '<value>foobar</value>' '</tns:echoRequest>') request_message = self._wrap_with_soap_envelope(soap_message, header=soap_header) request = SOAPRequest(dict(SOAPACTION='echo', REQUEST_METHOD='POST'), request_message) response = dispatcher.dispatch(request) self.assert_is_successful_response(response, handler_state) assert_contains(b'<ns0:OutputVersion>42</ns0:OutputVersion>', response.http_content)
def assert_fail(self, value, actual_iterable, message=None): return assert_raises(AssertionError, lambda: assert_contains(value, actual_iterable, message=message))
def test_empty_header(self): xml = self.SOAP.Envelope.response('Get', Place(), self.SOAP.Header()) assert_contains(b'<ns0:Header/>', xml)
def test_get_error_response(self): response = get_error_response(Code.SERVER, u'some error', actor='me') xml = self._xml_strip(response) assert_contains(b'<ns0:Code><ns0:Value>ns0:Receiver</ns0:Value></ns0:Code>', xml) assert_contains(b'<ns0:Role>me</ns0:Role>', xml)
def assert_fail(self, value, actual_iterable, message=None): return assert_raises( AssertionError, lambda: assert_contains(value, actual_iterable, message=message))
def test_passes_if_iterable_contains_value(self): assert_contains(True, ['foo', True]) assert_contains(True, ('foo', True))
def test_get_error_response(self): response = get_error_response(Code.SERVER, u'some error', actor='me') xml = self._xml_strip(response) assert_contains(b'<faultcode>Server</faultcode>', xml) assert_contains(b'<faultactor>me</faultactor>', xml)