Beispiel #1
0
    def test_property_is_any_type(self):
        var = XmlElement(name="foo", qname=QName("foo"))
        self.assertFalse(var.is_any_type)

        var = XmlElement(name="foo", qname=QName("foo"), types=[int, object])
        self.assertFalse(var.is_any_type)

        var = XmlElement(name="foo", qname=QName("foo"), types=[object])
        self.assertTrue(var.is_any_type)
Beispiel #2
0
    def test_next_node_when_given_qname_matches_dataclass_var(
            self, mock_ctx_fetch, mock_element_xsi_type):
        ele = Element("a")
        ctx = XmlContext()
        cfg = ParserConfig()
        var = XmlElement(name="a",
                         qname=QName("a"),
                         types=[Foo],
                         dataclass=True)
        meta = XmlMeta(
            name="foo",
            clazz=None,
            qname=QName("foo"),
            source_qname=QName("foo"),
            nillable=False,
            vars=[var],
        )
        xsi_type = QName("foo")
        namespace = meta.qname.namespace
        mock_ctx_fetch.return_value = replace(meta)
        mock_element_xsi_type.return_value = xsi_type
        node = ElementNode(position=0, meta=meta, config=cfg)

        actual = node.next_node(ele, 10, ctx)
        self.assertIsInstance(actual, ElementNode)
        self.assertEqual(10, actual.position)
        self.assertIs(mock_ctx_fetch.return_value, actual.meta)
        mock_ctx_fetch.assert_called_once_with(var.clazz, namespace, xsi_type)
Beispiel #3
0
    def test_get_value(self):
        data = dict(foo="bar", bar="foo")

        foo_field = XmlText(name="foo", qname=QName("foo"), types=[str])
        bar_field = XmlElement(name="bar",
                               qname=QName("bar"),
                               types=[str],
                               default=list)

        self.assertEqual("bar", JsonParser.get_value(data, foo_field))
        self.assertEqual(["foo"], JsonParser.get_value(data, bar_field))
        self.assertIsNone(JsonParser.get_value({}, bar_field))
Beispiel #4
0
    def test_property_element_form(self):
        meta = XmlMeta(
            name="foo",
            clazz=BookForm,
            qname=QName("foo"),
            source_qname=QName("foo"),
            nillable=False,
        )
        self.assertEqual(FormType.UNQUALIFIED, meta.element_form)

        meta = replace(meta, qname=QName("bar", "foo"))
        self.assertEqual(FormType.QUALIFIED, meta.element_form)

        meta.vars.append(XmlElement(name="a", qname=QName("a")))
        self.assertEqual(FormType.UNQUALIFIED, meta.element_form)

        meta.vars.append(
            XmlElement(name="b", qname=QName("b"), namespaces=["bar"]))
        self.assertEqual(FormType.UNQUALIFIED, meta.element_form)

        meta.vars.pop(0)
        self.assertEqual(FormType.QUALIFIED, meta.element_form)
Beispiel #5
0
    def test_get_type_hints_with_dataclass_list(self):
        result = list(self.ctx.get_type_hints(Books, None))

        expected = XmlElement(
            name="book",
            qname=QName("book"),
            types=[BookForm],
            dataclass=True,
            default=list,
        )

        self.assertTrue(expected.is_list)
        self.assertEqual(1, len(result))
        self.assertEqual(expected, result[0])
        self.assertTrue(result[0].dataclass)
        self.assertEqual(BookForm, result[0].clazz)
Beispiel #6
0
    def test_build_build_vars(self, mock_get_type_hints):
        var = XmlElement(name="foo", qname=QName("foo", "bar"), types=[int])
        mock_get_type_hints.return_value = [var]

        result = self.ctx.build(ItemsType, None)
        expected = XmlMeta(
            name="ItemsType",
            clazz=ItemsType,
            qname=QName("ItemsType"),
            source_qname=QName("ItemsType"),
            nillable=False,
            vars=[var],
        )

        self.assertEqual(expected, result)
        mock_get_type_hints.assert_called_once_with(ItemsType, None)
Beispiel #7
0
    def test_get_type_hints(self):
        result = self.ctx.get_type_hints(BookForm, None)
        self.assertIsInstance(result, Iterator)

        expected = [
            XmlElement(
                name="author",
                qname=QName("author"),
                types=[str],
            ),
            XmlElement(name="title", qname=QName("title"), types=[str]),
            XmlElement(name="genre", qname=QName("genre"), types=[str]),
            XmlElement(
                name="price",
                qname=QName("price"),
                types=[float],
            ),
            XmlElement(
                name="pub_date",
                qname=QName("pub_date"),
                types=[str],
            ),
            XmlElement(
                name="review",
                qname=QName("review"),
                types=[str],
            ),
            XmlAttribute(name="id", qname=QName("id"), types=[str]),
            XmlAttribute(
                name="lang",
                qname=QName("lang"),
                types=[str],
                init=False,
                default="en",
            ),
        ]

        result = list(result)
        self.assertEqual(expected, result)
        for var in result:
            self.assertFalse(var.dataclass)
            self.assertIsNone(var.clazz)
Beispiel #8
0
    def test_property_is_element(self):
        var = XmlElement(name="foo", qname=QName("foo"))

        self.assertIsInstance(var, XmlVar)
        self.assertTrue(var.is_element)