def test_client_with_soap_fault(self, mock_most): url = "http://localhost:9999/ws/hello" request = fixtures_dir.joinpath("hello/HelloRQ.xml").read_text() response = fixtures_dir.joinpath( "hello/HelloRS_SoapFault.xml").read_bytes() headers = {"content-type": "text/xml"} mock_most.return_value = response config = Config.from_service(HelloGetHelloAsString) serializer = XmlSerializer(config=SerializerConfig(pretty_print=True)) client = Client(config=config, serializer=serializer) result = client.send( {"body": { "get_hello_as_string": { "arg0": "chris" } }}) self.assertIsInstance(result, HelloGetHelloAsString.output) fault = HelloGetHelloAsStringOutput.Body.Fault( faultcode="S:Server", faultstring="foobar", detail=HelloGetHelloAsStringOutput.Body.Fault.Detail( hello_error=HelloError(message="foobar")), ) self.assertEqual(fault, result.body.fault) mock_most.assert_called_once_with(url, data=request, headers=headers)
def test_client(self, mock_most): url = "http://localhost:9999/ws/hello" request = fixtures_dir.joinpath("hello/HelloRQ.xml").read_text() response = fixtures_dir.joinpath("hello/HelloRS.xml").read_bytes() headers = {"content-type": "text/xml"} mock_most.return_value = response config = Config.from_service(HelloGetHelloAsString) serializer = XmlSerializer(config=SerializerConfig(pretty_print=True)) client = Client(config=config, serializer=serializer) result = client.send( {"body": { "get_hello_as_string": { "arg0": "chris" } }}) self.assertIsInstance(result, HelloGetHelloAsString.output) body = HelloGetHelloAsStringOutput.Body( get_hello_as_string_response=GetHelloAsStringResponse( return_value="Hello chris")) self.assertEqual(body, result.body) mock_most.assert_called_once_with(url, data=request, headers=headers)
def test_wget_with_definitions(self, mock_write_file): wsdl = fixtures_dir.joinpath("hello/hello.wsdl").as_uri() xsd = fixtures_dir.joinpath("hello/hello.xsd").as_uri() self.downloader.wget(wsdl) mock_write_file.assert_has_calls( [ mock.call(xsd, "hello.xsd", urlopen(xsd).read().decode()), mock.call(wsdl, None, urlopen(wsdl).read().decode()), ] )
def test_resolve_source(self): hello_path = fixtures_dir.joinpath("hello") file = hello_path.joinpath("hello.xsd") url = "http://www.xsdata/schema.xsd" self.assertEqual([file.as_uri()], list(resolve_source(str(file)))) self.assertEqual([url], list(resolve_source(url))) self.assertEqual(5, len(list(resolve_source(str(hello_path))))) def_xml_path = fixtures_dir.joinpath("defxmlschema") self.assertEqual(55, len(list(resolve_source(str(def_xml_path)))))
def test_client(self, mock_most): url = "http://www.dneonline.com/calculator.asmx" request = fixtures_dir.joinpath("calculator/AddRQ.xml").read_text() response = fixtures_dir.joinpath("calculator/AddRS.xml").read_bytes() headers = {"content-type": "text/xml", "SOAPAction": "http://tempuri.org/Add"} mock_most.return_value = response config = Config.from_service(CalculatorSoapAdd) serializer = XmlSerializer(config=SerializerConfig(pretty_print=True)) client = Client(config=config, serializer=serializer) result = client.send({"Body": {"Add": {"intA": 1, "intB": 3}}}) self.assertIsInstance(result, CalculatorSoapAddOutput) mock_most.assert_called_once_with(url, data=request, headers=headers)
def test_declaration_disabled(self): self.serializer.config.xml_declaration = False actual = self.serializer.render(books, {None: "urn:books"}) expected = fixtures_dir.joinpath("books/books_default_ns.xml").read_text() xml_declaration, expected = expected.split("\n", 1) self.assertEqual(expected, actual)
def test_parser(self): path = fixtures_dir.joinpath("books/books.json") books = self.parser.from_path(path, Books) self.assertIsInstance(books, Books) self.assertEqual( BookForm( author="Hightower, Kim", title="The First Book", genre="Fiction", price=44.95, pub_date=XmlDate.from_string("2000-10-01"), review="An amazing story of nothing.", id="bk001", ), books.book[0], ) self.assertEqual( BookForm( author="Nagata, Suanne", title="Becoming Somebody", genre="Biography", price=None, pub_date=None, review="A masterpiece of the fine art of gossiping.", id="bk002", ), books.book[1], )
def test_generate_with_configuration_file_and_overriding_args( self, mock_init, _): file_path = Path(tempfile.mktemp()) config = GeneratorConfig() config.output.package = "foo.bar" config.output.structure = OutputStructure.FILENAMES with file_path.open("w") as fp: config.write(fp, config) source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd") result = self.runner.invoke( cli, [ str(source), "--config", str(file_path), "--package", "foo", "--ns-struct", ], ) config = mock_init.call_args[1]["config"] self.assertIsNone(result.exception) self.assertEqual("foo", config.output.package) self.assertEqual(OutputStructure.NAMESPACES, config.output.structure) file_path.unlink()
def test_parse_with_xinclude(self): path = fixtures_dir.joinpath("books/books-xinclude.xml") ns_map = {"brk": "urn:books", "xi": "http://www.w3.org/2001/XInclude"} self.parser.config.process_xinclude = True self.assertEqual(books, self.parser.from_path(path, Books)) self.assertEqual(ns_map, self.parser.ns_map)
def test_parse_with_xinclude_from_memory(self): path = fixtures_dir.joinpath("books/books-xinclude.xml") ns_map = {"brk": "urn:books", "xi": "http://www.w3.org/2001/XInclude"} self.parser.config.process_xinclude = True self.parser.config.base_url = path.as_uri() self.assertEqual(books, self.parser.from_bytes(path.read_bytes(), Books)) self.assertEqual(ns_map, self.parser.ns_map)
def test_pretty_print_false(self): self.serializer.config.pretty_print = False actual = self.serializer.render(books) expected = fixtures_dir.joinpath("books/books_auto_ns.xml").read_text() _, actual = actual.split("\n", 1) _, expected = expected.split("\n", 1) self.assertEqual(expected.replace(" ", "").replace("\n", ""), actual)
def test_wget_with_schema(self, mock_write_file): path = fixtures_dir.joinpath("books/schema.xsd") content = path.read_bytes() uri = path.as_uri() self.downloader.wget(uri) self.downloader.wget(uri) # once mock_write_file.assert_called_once_with(uri, None, content.decode())
def test_generate_with_wsdl_mode(self, mock_init, mock_process_definitions): source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd") result = self.runner.invoke( cli, [str(source), "--package", "foo", "--wsdl"]) self.assertIsNone(result.exception) mock_process_definitions.assert_called_once_with(source.as_uri(), )
def test_generate_with_print_mode(self, mock_init, mock_process): source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd") result = self.runner.invoke( cli, [str(source), "--package", "foo", "--print"]) self.assertIsNone(result.exception) self.assertEqual([source.as_uri()], mock_process.call_args[0][0]) self.assertEqual(logging.ERROR, logger.getEffectiveLevel()) self.assertTrue(mock_init.call_args[1]["print"])
def test_primer_schema_plantuml(): schema = fixtures_dir.joinpath("primer/order.xsd") package = "tests.fixtures.primer" runner = CliRunner() result = runner.invoke( cli, [str(schema), "--package", package, "--output", "plantuml"]) if result.exception: raise result.exception
def test_render_with_default_namespace_prefix(self): actual = self.serializer.render(books, {None: "urn:books"}) expected = fixtures_dir.joinpath( "books/books_default_ns.xml").read_text() xml_declaration, actual = actual.split("\n", 1) _, expected = expected.split("\n", 1) self.assertEqual(expected, actual)
def test_generate_with_docstring_style(self, mock_init, mock_process): source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd") result = self.runner.invoke( cli, [str(source), "--package", "foo", "--docstring-style", "Google"]) config = mock_init.call_args[1]["config"] self.assertIsNone(result.exception) self.assertEqual([source.as_uri()], mock_process.call_args[0][0]) self.assertEqual(DocstringStyle.GOOGLE, config.output.docstring_style)
def test_wsdl_codegen(self): schema = fixtures_dir.joinpath("calculator/services.wsdl") package = "tests.fixtures.calculator" runner = CliRunner() result = runner.invoke(cli, [str(schema), "--package", package]) if result.exception: raise result.exception clazz = load_class(result.output, "CalculatorSoapMultiplyOutput") self.assertEqual("Envelope", clazz.Meta.name)
def test_parser_entry_points(self): path = fixtures_dir.joinpath("books/books.json") books = self.parser.from_string(path.read_text(), Books) self.assertIsInstance(books, Books) books = self.parser.from_bytes(path.read_bytes(), Books) self.assertIsInstance(books, Books) books = self.parser.parse(str(path), Books) self.assertIsInstance(books, Books)
def test_generate_with_default_output(self, mock_init, mock_process): source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd") result = self.runner.invoke(cli, [str(source), "--package", "foo"]) config = mock_init.call_args[1]["config"] self.assertIsNone(result.exception) self.assertFalse(mock_init.call_args[1]["print"]) self.assertEqual("foo", config.output.package) self.assertEqual("dataclasses", config.output.format) self.assertEqual(OutputStructure.FILENAMES, config.output.structure) self.assertEqual([source.as_uri()], mock_process.call_args[0][0])
def test_parse_list_of_objects(self): path = fixtures_dir.joinpath("books/books.json") data = json.loads(path.read_text()) book_list = data["book"] json_string = json.dumps(book_list) books = self.parser.from_string(json_string, List[BookForm]) self.assertIsInstance(books, list) self.assertEqual(2, len(books)) self.assertIsInstance(books[0], BookForm) self.assertIsInstance(books[1], BookForm)
def test_wsdl_codegen(self): schema = fixtures_dir.joinpath("hello/hello.wsdl") package = "tests.fixtures.hello" runner = CliRunner() result = runner.invoke(cli, [str(schema), "--package", package]) if result.exception: raise result.exception clazz = load_class(result.output, "HelloGetHelloAsStringOutput") self.assertEqual("Envelope", clazz.Meta.name)
def test_complete(self): path = fixtures_dir.joinpath("calculator/services.wsdl").resolve() parser = DefinitionsParser() definitions = parser.from_path(path, Definitions) self.assertIsInstance(definitions, Definitions) self.assertEqual(1, len(definitions.services)) self.assertEqual(2, len(definitions.bindings)) self.assertEqual(1, len(definitions.port_types)) self.assertEqual(1, len(definitions.types.schemas)) self.assertEqual(8, len(definitions.messages))
def test_parse_with_unknown_class(self): path = fixtures_dir.joinpath("books/books.json") books = self.parser.from_path(path) self.assertIsInstance(books, Books) self.assertEqual(2, len(books.book)) json_array = JsonSerializer().render(books.book) book_list = self.parser.from_string(json_array) self.assertIsInstance(book_list, list) self.assertEqual(2, len(book_list)) self.assertIsInstance(book_list[0], BookForm) self.assertIsInstance(book_list[1], BookForm)
def test_parser_with_unknown_class(self): path = fixtures_dir.joinpath("books/books.json") books = self.parser.from_path(path) self.assertIsInstance(books, Books) self.assertEqual(2, len(books.book)) with self.assertRaises(ParserError) as cm: self.parser.from_string('{"please": 1, "dont": 1, "exists": 2}') self.assertEqual( "No class found matching the document keys(['please', 'dont', 'exists'])", str(cm.exception), )
def test_primer_schema(): schema = fixtures_dir.joinpath("primer/order.xsd") package = "tests.fixtures.primer" runner = CliRunner() result = runner.invoke( cli, [str(schema), "--package", package, "--docstring-style", "NumPy"]) if result.exception: raise result.exception clazz = load_class(result.output, "PurchaseOrder") assert "purchaseOrder" == clazz.Meta.name validate_bindings(schema, clazz)
def test_resolve_source(self): file = fixtures_dir.joinpath("defxmlschema/chapter03.xsd") url = "http://www.xsdata/schema.xsd" self.assertEqual([file.as_uri()], list(resolve_source(str(file), wsdl=False))) self.assertEqual([url], list(resolve_source(url, wsdl=False))) self.assertEqual( [x.as_uri() for x in fixtures_dir.glob("*.xsd")], list(resolve_source(str(fixtures_dir), wsdl=False)), ) with self.assertRaises(CodeGenerationError) as cm: list(resolve_source(str(fixtures_dir), wsdl=True)) self.assertEqual("WSDL mode doesn't support scanning directories.", str(cm.exception))
def test_json_documents(): filepath = fixtures_dir.joinpath("series") package = "tests.fixtures.series" runner = CliRunner() result = runner.invoke(cli, [str(filepath), "--package", package]) if result.exception: raise result.exception clazz = load_class(result.output, "Series") parser = JsonParser() serializer = JsonSerializer(indent=4) for i in range(1, 3): ori = filepath.joinpath(f"show{i}.json").read_text() obj = parser.from_string(ori, clazz) actual = serializer.render(obj) assert filter_none(json.loads(ori)) == filter_none(json.loads(actual))
def test_parser(self): parser = TreeParser() path = fixtures_dir.joinpath("books/bk001.xml") actual = parser.from_path(path) expected = AnyElement( qname="book", text="", children=[ AnyElement(qname="author", text="Hightower, Kim"), AnyElement(qname="title", text="The First Book"), AnyElement(qname="genre", text="Fiction"), AnyElement(qname="price", text="44.95"), AnyElement(qname="pub_date", text="2000-10-01"), AnyElement(qname="review", text="An amazing story of nothing."), ], attributes={"id": "bk001"}, ) self.assertEqual(expected, actual)
def test_generate_with_configuration_file(self, mock_init, mock_process): file_path = Path(tempfile.mktemp()) config = GeneratorConfig() config.output.package = "foo.bar" config.output.structure = OutputStructure.NAMESPACES with file_path.open("w") as fp: config.write(fp, config) source = fixtures_dir.joinpath("defxmlschema/chapter03.xsd") result = self.runner.invoke( cli, [str(source), "--config", str(file_path)], catch_exceptions=False) config = mock_init.call_args[1]["config"] self.assertIsNone(result.exception) self.assertFalse(mock_init.call_args[1]["print"]) self.assertEqual("foo.bar", config.output.package) self.assertEqual("dataclasses", config.output.format) self.assertEqual(OutputStructure.NAMESPACES, config.output.structure) self.assertEqual([source.as_uri()], mock_process.call_args[0][0]) file_path.unlink()