Ejemplo n.º 1
0
    def test_queue_node_with_wildcard_element(self, mock_get_var,
                                              mock_create_wildcard_queue_item,
                                              mock_emit_event):
        element = Element("{urn:books}foobar")
        class_queue_item = ClassQueueItem(index=0,
                                          position=0,
                                          meta=self.parser.class_meta(Books))
        mock_get_var.return_value = ClassVar(qname="{urn:books}:parent",
                                             types=[],
                                             name="",
                                             tag=Tag.ANY_ELEMENT)

        self.parser.queue.append(class_queue_item)
        self.parser.queue_node(element)

        self.assertEqual(11, self.parser.index)
        self.assertEqual(2, len(self.parser.queue))
        self.assertEqual(class_queue_item, self.parser.queue[0])
        self.assertEqual("yes", self.parser.queue[1])

        mock_create_wildcard_queue_item.assert_called_once_with(
            "{urn:books}:parent")
        mock_emit_event.assert_called_once_with(EventType.START,
                                                element.tag,
                                                item=class_queue_item,
                                                element=element)
Ejemplo n.º 2
0
    def get_type_hints(self, clazz,
                       parent_ns: Optional[str]) -> Iterator[ClassVar]:
        type_hints = get_type_hints(clazz)

        for var in fields(clazz):
            type_hint = type_hints[var.name]
            types = self.real_types(type_hint)

            tag = Tag.from_metadata_type(var.metadata.get("type"))
            namespace = var.metadata.get("namespace")
            real_namespace = self.resolve_namespace(namespace, tag, parent_ns)
            wild_namespaces = self.wild_namespaces(namespace, tag, parent_ns)

            local_name = var.metadata.get("name") or self.name_generator(
                var.name)
            is_class = next(
                (False for clazz in types if not is_dataclass(clazz)), True)

            yield ClassVar(
                name=var.name,
                qname=QName(real_namespace, local_name),
                wild_ns=wild_namespaces,
                tag=tag,
                init=var.init,
                nillable=var.metadata.get("nillable", False),
                dataclass=is_class,
                sequential=var.metadata.get("sequential", False),
                types=types,
                default=self.default_value(var),
            )
Ejemplo n.º 3
0
    def test_get_type_hints(self):
        result = self.inspect.get_type_hints(BookForm, None)
        self.assertIsInstance(result, Iterator)

        expected = [
            ClassVar(
                name="author",
                qname=QName("author"),
                types=[str],
                tag=Tag.ELEMENT,
            ),
            ClassVar(name="title",
                     qname=QName("title"),
                     types=[str],
                     tag=Tag.ELEMENT),
            ClassVar(name="genre",
                     qname=QName("genre"),
                     types=[str],
                     tag=Tag.ELEMENT),
            ClassVar(
                name="price",
                qname=QName("price"),
                types=[float],
                tag=Tag.ELEMENT,
            ),
            ClassVar(
                name="pub_date",
                qname=QName("pub_date"),
                types=[str],
                tag=Tag.ELEMENT,
            ),
            ClassVar(
                name="review",
                qname=QName("review"),
                types=[str],
                tag=Tag.ELEMENT,
            ),
            ClassVar(name="id",
                     qname=QName("id"),
                     types=[str],
                     tag=Tag.ATTRIBUTE),
            ClassVar(
                name="lang",
                qname=QName("lang"),
                types=[str],
                tag=Tag.ATTRIBUTE,
                init=False,
                default="en",
            ),
        ]

        result = list(result)
        self.assertEqual(expected, result)
        for var in result:
            self.assertFalse(var.dataclass)
            self.assertIsNone(var.clazz)
Ejemplo n.º 4
0
    def test_get_value(self):
        data = dict(foo="bar", bar="foo")

        foo_field = ClassVar(name="foo",
                             qname=QName("foo"),
                             types=[str],
                             tag=Tag.TEXT)
        bar_field = ClassVar(
            name="bar",
            qname=QName("bar"),
            types=[str],
            default=list,
            tag=Tag.ELEMENT,
        )

        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))
Ejemplo n.º 5
0
    def test_create_primitive_queue_item(self):
        class_var = ClassVar(qname="",
                             types=[int, str],
                             name="",
                             tag="",
                             default=1)

        actual = self.parser.create_primitive_queue_item(class_var)
        expected = PrimitiveQueueItem(index=10,
                                      position=5,
                                      default=1,
                                      types=[int, str])
        self.assertEqual(expected, actual)
Ejemplo n.º 6
0
    def test_create_class_queue_item(self, mock_class_meta):
        mock_class_meta.return_value = "yes"
        class_var = ClassVar(
            qname="author",
            types=[BookForm],
            dataclass=True,
            name="",
            tag="",
            default=10,
        )

        qname = QName("{urn}book")
        actual = self.parser.create_class_queue_item(class_var, qname)
        expected = ClassQueueItem(index=10, position=5, default=10, meta="yes")
        self.assertEqual(expected, actual)
        mock_class_meta.assert_called_once_with(BookForm, qname.namespace)
Ejemplo n.º 7
0
    def test_get_type_hints_with_wildcard_element(self):
        result = list(self.inspect.get_type_hints(TextType, None))

        expected = ClassVar(
            name="any_element",
            qname=QName(None, "any_element"),
            types=[object],
            tag=Tag.ANY_ELEMENT,
            init=True,
            nillable=False,
            dataclass=False,
            default=list,
            wild_ns=["##any"],
        )

        self.assertEqual(2, len(result))
        self.assertEqual(expected, result[0])
Ejemplo n.º 8
0
    def test_get_type_hints_with_dataclass_list(self):
        result = list(self.inspect.get_type_hints(Books, None))

        expected = ClassVar(
            name="book",
            qname=QName("book"),
            types=[BookForm],
            dataclass=True,
            default=list,
            tag=Tag.ELEMENT,
        )

        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)
Ejemplo n.º 9
0
    def test_class_meta_build_vars(self, mock_get_type_hints):
        var = ClassVar(name="foo",
                       qname=QName("foo", "bar"),
                       types=[int],
                       tag=Tag.ELEMENT)
        mock_get_type_hints.return_value = [var]

        result = self.inspect.class_meta(ItemsType, None)
        expected = ClassMeta(
            name="ItemsType",
            clazz=ItemsType,
            qname=QName("ItemsType"),
            nillable=False,
            vars={var.qname: var},
        )

        self.assertEqual(expected, result)
        mock_get_type_hints.assert_called_once_with(ItemsType, None)