-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
242 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
from unittest.case import TestCase | ||
|
||
from tests import fixtures_dir | ||
from tests.fixtures.books import BookForm | ||
from tests.fixtures.books import Books | ||
from xsdata.formats.dataclass.parsers.handlers import EventsHandler | ||
from xsdata.formats.dataclass.parsers.handlers import LxmlIterHandler | ||
from xsdata.formats.dataclass.parsers.handlers import LxmlSaxHandler | ||
from xsdata.formats.dataclass.parsers.xml import XmlParser | ||
|
||
books = Books( | ||
book=[ | ||
BookForm( | ||
id="bk001", | ||
author="Hightower, Kim", | ||
title="The First Book", | ||
genre="Fiction", | ||
price=44.95, | ||
pub_date="2000-10-01", | ||
review="An amazing story of nothing.", | ||
), | ||
BookForm( | ||
id="bk002", | ||
author="Nagata, Suanne", | ||
title="Becoming Somebody", | ||
genre="Biography", | ||
price=33.95, | ||
pub_date="2001-01-10", | ||
review="A masterpiece of the fine art of gossiping.", | ||
), | ||
] | ||
) | ||
|
||
|
||
class LxmlIterHandlerTests(TestCase): | ||
def setUp(self) -> None: | ||
self.parser = XmlParser(handler=LxmlIterHandler) | ||
|
||
def test_process(self): | ||
path = fixtures_dir.joinpath("books/books.xml") | ||
self.assertEqual(books, self.parser.from_path(path, Books)) | ||
self.assertEqual({"brk": "urn:books"}, self.parser.namespaces.ns_map) | ||
|
||
def test_process_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.namespaces.ns_map) | ||
|
||
def test_process_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.namespaces.ns_map) | ||
|
||
|
||
class LxmlSaxHandlerTests(TestCase): | ||
def setUp(self): | ||
self.parser = XmlParser(handler=LxmlSaxHandler) | ||
|
||
def test_process(self): | ||
path = fixtures_dir.joinpath("books/books.xml") | ||
self.assertEqual(books, self.parser.from_path(path, Books)) | ||
self.assertEqual({"brk": "urn:books"}, self.parser.namespaces.ns_map) | ||
|
||
def test_close_with_no_objects_returns_none(self): | ||
handler = LxmlSaxHandler( | ||
clazz=Books, parser=self.parser, config=self.parser.config | ||
) | ||
self.assertIsNone(handler.close()) | ||
|
||
|
||
class EventsHandlerTests(TestCase): | ||
def test_process(self): | ||
events = [ | ||
("start-ns", "brk", "urn:books"), | ||
("start", "{urn:books}books", {}, {"brk": "urn:books"}), | ||
("start", "book", {"id": "bk001"}, {"brk": "urn:books"}), | ||
("start", "author", {}, {"brk": "urn:books"}), | ||
("end", "author", "Hightower, Kim", "\n "), | ||
("start", "title", {}, {"brk": "urn:books"}), | ||
("end", "title", "The First Book", "\n "), | ||
("start", "genre", {}, {"brk": "urn:books"}), | ||
("end", "genre", "Fiction", "\n "), | ||
("start", "price", {}, {"brk": "urn:books"}), | ||
("end", "price", "44.95", "\n "), | ||
("start", "pub_date", {}, {"brk": "urn:books"}), | ||
("end", "pub_date", "2000-10-01", "\n "), | ||
("start", "review", {}, {"brk": "urn:books"}), | ||
("end", "review", "An amazing story of nothing.", "\n "), | ||
("end", "book", "\n ", "\n "), | ||
("start", "book", {"id": "bk002"}, {"brk": "urn:books"}), | ||
("start", "author", {}, {"brk": "urn:books"}), | ||
("end", "author", "Nagata, Suanne", "\n "), | ||
("start", "title", {}, {"brk": "urn:books"}), | ||
("end", "title", "Becoming Somebody", "\n "), | ||
("start", "genre", {}, {"brk": "urn:books"}), | ||
("end", "genre", "Biography", "\n "), | ||
("start", "price", {}, {"brk": "urn:books"}), | ||
("end", "price", "33.95", "\n "), | ||
("start", "pub_date", {}, {"brk": "urn:books"}), | ||
("end", "pub_date", "2001-01-10", "\n "), | ||
("start", "review", {}, {"brk": "urn:books"}), | ||
("end", "review", "A masterpiece of the fine art of gossiping.", "\n "), | ||
("end", "book", "\n ", "\n"), | ||
("end", "{urn:books}books", "\n ", None), | ||
] | ||
|
||
parser = XmlParser(handler=EventsHandler) | ||
self.assertEqual(books, parser.parse(events, Books)) | ||
self.assertEqual({}, parser.namespaces.ns_map) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.