示例#1
0
    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],
        )
示例#2
0
    def test_parser(self):
        parser = JsonParser()
        books = parser.from_string(json.dumps(self.data), Books)

        self.assertIsInstance(books, Books)

        self.assertEqual(
            BookForm(
                author="Hightower, Kim",
                title="The First Book",
                genre="Fiction",
                price=44.95,
                pub_date="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],
        )
示例#3
0
    def test_to_xml(self):
        self.assertEqual(None, to_xml(None))
        self.assertEqual("1", to_xml(1))
        self.assertEqual("1.5", to_xml(1.5))
        self.assertEqual("true", to_xml(True))
        self.assertEqual("false", to_xml(False))
        self.assertEqual("optional", to_xml(UseType.OPTIONAL))
        self.assertEqual("INF", to_xml(float("inf")))
        self.assertEqual("INF", to_xml(float("+inf")))
        self.assertEqual("-INF", to_xml(float("-inf")))
        self.assertEqual("NaN", to_xml(float("nan")))
        self.assertEqual("INF", to_xml(Decimal("inf")))
        self.assertEqual("INF", to_xml(Decimal("+inf")))
        self.assertEqual("-INF", to_xml(Decimal("-inf")))
        self.assertEqual("8.77683E-8", to_xml(Decimal("8.77683E-8")))
        self.assertEqual("8.77683E-08", to_xml(float("8.77683E-8")))
        self.assertEqual("a", to_xml(QName("a")))
        self.assertEqual("a", to_xml(QName("a")))
        self.assertEqual("{a}b", to_xml(QName("a", "b")))
        self.assertEqual("1 2", to_xml([1, 2]))
        self.assertEqual("INF optional",
                         to_xml([float("inf"), UseType.OPTIONAL]))

        namespaces = Namespaces()
        namespaces.add("a", "aa")
        self.assertEqual("aa:b", to_xml(QName("a", "b"), namespaces))
        self.assertEqual("b", to_xml(QName("b"), namespaces))

        with self.assertRaises(ConverterError):
            to_xml(BookForm())
示例#4
0
    def setUp(self):
        super().setUp()
        self.books = Books(book=[
            BookForm(
                id="bk001",
                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.",
            ),
            BookForm(
                id="bk002",
                author="Nagata, Suanne",
                title="Becoming Somebody",
                genre="Biography",
                review="A masterpiece of the fine art of gossiping.",
            ),
        ])

        self.expected = {
            "book": [
                {
                    "author": "Hightower, Kim",
                    "genre": "Fiction",
                    "id": "bk001",
                    "lang": "en",
                    "price": 44.95,
                    "pub_date": "2000-10-01",
                    "review": "An amazing story of nothing.",
                    "title": "The First Book",
                },
                {
                    "author": "Nagata, Suanne",
                    "genre": "Biography",
                    "id": "bk002",
                    "lang": "en",
                    "review": "A masterpiece of the fine art of gossiping.",
                    "title": "Becoming Somebody",
                },
            ]
        }
示例#5
0
 def setUp(self):
     super(XmlSerializerTests, self).setUp()
     self.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",
             review="A masterpiece of the fine art of gossiping.",
         ),
     ])
示例#6
0
    def test_write_object_with_derived_element(self):
        book = BookForm(id="123")
        obj = DerivedElement(qname="item", value=book)

        result = self.serializer.write_object(obj)
        expected = [
            (XmlWriterEvent.START, "item"),
            (XmlWriterEvent.ATTR, "id", "123"),
            (XmlWriterEvent.ATTR, "lang", "en"),
            (XmlWriterEvent.ATTR, QNames.XSI_TYPE, "{urn:books}BookForm"),
            (XmlWriterEvent.END, "item"),
        ]
        self.assertIsInstance(result, Generator)
        self.assertEqual(expected, list(result))
示例#7
0
    def test_write_mixed_content(self):
        var = XmlVar(wildcard=True, qname="a", name="a", mixed=True)
        book = BookForm(id="123")
        ebook = DerivedElement("ebook", BookForm(id="123"))
        value = ["text", AnyElement(qname="br"), book, ebook, "tail"]
        result = self.serializer.write_value(value, var, "xsdata")
        expected = [
            (XmlWriterEvent.DATA, "text"),
            (XmlWriterEvent.START, "br"),
            (XmlWriterEvent.DATA, None),
            (XmlWriterEvent.END, "br"),
            (XmlWriterEvent.START, "{xsdata}BookForm"),
            (XmlWriterEvent.ATTR, "id", "123"),
            (XmlWriterEvent.ATTR, "lang", "en"),
            (XmlWriterEvent.END, "{xsdata}BookForm"),
            (XmlWriterEvent.START, "ebook"),
            (XmlWriterEvent.ATTR, "id", "123"),
            (XmlWriterEvent.ATTR, "lang", "en"),
            (XmlWriterEvent.END, "ebook"),
            (XmlWriterEvent.DATA, "tail"),
        ]

        self.assertIsInstance(result, Generator)
        self.assertEqual(expected, list(result))
示例#8
0
    def test_write_xsi_type(self):
        var = XmlVar(
            element=True, qname="a", name="a", dataclass=True, types=[BookForm]
        )
        value = BookForm(id="123")
        expected = [
            (XmlWriterEvent.START, "a"),
            (XmlWriterEvent.ATTR, "id", "123"),
            (XmlWriterEvent.ATTR, "lang", "en"),
            (XmlWriterEvent.END, "a"),
        ]

        result = self.serializer.write_value(value, var, "xsdata")
        self.assertIsInstance(result, Generator)
        self.assertEqual(expected, list(result))
示例#9
0
 def test_write_dataclass(self):
     book = BookForm(id="123", title="Misterioso: A Crime Novel", price=19.5)
     result = self.serializer.write_object(book)
     expected = [
         (XmlWriterEvent.START, "BookForm"),
         (XmlWriterEvent.ATTR, "id", "123"),
         (XmlWriterEvent.ATTR, "lang", "en"),
         (XmlWriterEvent.START, "title"),
         (XmlWriterEvent.DATA, "Misterioso: A Crime Novel"),
         (XmlWriterEvent.END, "title"),
         (XmlWriterEvent.START, "price"),
         (XmlWriterEvent.DATA, 19.5),
         (XmlWriterEvent.END, "price"),
         (XmlWriterEvent.END, "BookForm"),
     ]
     self.assertIsInstance(result, Generator)
     self.assertEqual(expected, list(result))
示例#10
0
def make_books(how_many: int):
    return Books(book=[
        BookForm(
            author="Arne Dahl",
            title="Misterioso: A Crime Novel",
            genre="Thrillers & Suspense",
            price=15.95,
            pub_date=XmlDate(1999, 10, 5),
            review=(
                "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
                "Integer at erat sagittis, accumsan mauris eu, egestas "
                "quam. Nam tristique felis justo, vel iaculis ipsum cursus "
                "at. Praesent varius enim ac nunc interdum placerat. "
                "Integer porttitor, nibh at viverra vehicula, leo dui "
                "suscipit nisi, ornare laoreet eros neque nec mi. Proin."),
            id="9788831796781",
        ) for _ in range(how_many)
    ])
示例#11
0
    def test_write_any_type_with_derived_element_dataclass(self):
        var = XmlVar(wildcard=True, qname="a", name="a")
        value = DerivedElement(qname="a",
                               value=BookForm(title="def"),
                               substituted=True)
        expected = [
            (XmlWriterEvent.START, "a"),
            (XmlWriterEvent.ATTR, "lang", "en"),
            (XmlWriterEvent.ATTR, QNames.XSI_TYPE,
             QName("{urn:books}BookForm")),
            (XmlWriterEvent.START, "title"),
            (XmlWriterEvent.DATA, "def"),
            (XmlWriterEvent.END, "title"),
            (XmlWriterEvent.END, "a"),
        ]

        result = self.serializer.write_value(value, var, "xsdata")
        self.assertIsInstance(result, Generator)
        self.assertEqual(expected, list(result))
示例#12
0
    def test_to_xml(self):
        self.assertEqual(None, to_xml(None))
        self.assertEqual("1", to_xml(1))
        self.assertEqual("1.5", to_xml(1.5))
        self.assertEqual("true", to_xml(True))
        self.assertEqual("false", to_xml(False))
        self.assertEqual("optional", to_xml(UseType.OPTIONAL))
        self.assertEqual("INF", to_xml(float("inf")))
        self.assertEqual("INF", to_xml(float("+inf")))
        self.assertEqual("-INF", to_xml(float("-inf")))
        self.assertEqual("NaN", to_xml(float("nan")))
        self.assertEqual("INF", to_xml(Decimal("inf")))
        self.assertEqual("INF", to_xml(Decimal("+inf")))
        self.assertEqual("-INF", to_xml(Decimal("-inf")))
        self.assertEqual("8.77683E-8", to_xml(Decimal("8.77683E-8")))
        self.assertEqual("8.77683E-08", to_xml(float("8.77683E-8")))
        self.assertEqual(QName("a"), to_xml(QName("a")))

        with self.assertRaises(ConverterError):
            to_xml(BookForm())
示例#13
0
 def test_write_dataclass_can_overwrite_params(self):
     book = BookForm(id="123", title="Misterioso: A Crime Novel", price=19.5)
     result = self.serializer.write_dataclass(
         book, "xsdata", "book", True, "foo:book"
     )
     expected = [
         (XmlWriterEvent.START, "book"),
         (XmlWriterEvent.ATTR, "id", "123"),
         (XmlWriterEvent.ATTR, "lang", "en"),
         (XmlWriterEvent.ATTR, QNames.XSI_TYPE, "foo:book"),
         (XmlWriterEvent.ATTR, QNames.XSI_NIL, "true"),
         (XmlWriterEvent.START, "title"),
         (XmlWriterEvent.DATA, "Misterioso: A Crime Novel"),
         (XmlWriterEvent.END, "title"),
         (XmlWriterEvent.START, "price"),
         (XmlWriterEvent.DATA, 19.5),
         (XmlWriterEvent.END, "price"),
         (XmlWriterEvent.END, "book"),
     ]
     self.assertIsInstance(result, Generator)
     self.assertEqual(expected, list(result))
示例#14
0
 def test_parse_empty_document(self):
     self.assertEqual(BookForm(), self.parser.from_string("{}", BookForm))
     self.assertEqual([], self.parser.from_string("[]", List[BookForm]))
示例#15
0
from tests.fixtures.books import Books, BookForm
from xsdata.models.datatype import XmlDate

books = Books(book=[
    BookForm(
        id="bk001",
        author="Hightower, Kim",
        title="The First Book",
        genre="Fiction",
        price=44.95,
        pub_date=XmlDate(2000, 10, 1),
        review="An amazing story of nothing.",
    ),
    BookForm(
        id="bk002",
        author="Nagata, Suanne",
        title="Becoming Somebody",
        genre="Biography",
        price=33.95,
        pub_date=XmlDate(2001, 1, 10),
        review="A masterpiece of the fine art of gossiping.",
    ),
])
events = [('start-ns', 'brk', 'urn:books'),
          ('start', '{urn:books}books', {}, {
              'brk': 'urn:books'
          }),
          ('start', 'book', {
              'id': 'bk001',
              'lang': 'en'
          }, {