def test_next_node(self): ele = Element("foo") node = PrimitiveNode(position=0, var=XmlText(name="foo", qname=QName("foo"))) with self.assertRaises(XmlContextError): node.next_node(ele, 10, XmlContext())
def test_queue(self, mock_emit_event, mock_next_node): var = XmlText(name="foo", qname=QName("foo")) primitive_node = PrimitiveNode(position=1, var=var) mock_next_node.return_value = primitive_node element = Element("{urn:books}books") config = ParserConfig() root_queue_item = RootNode( position=0, meta=self.parser.context.build(Books), config=config, ) objects = [] queue = [] queue.append(root_queue_item) self.parser.queue(element, queue, objects) self.assertEqual(2, len(queue)) self.assertEqual(root_queue_item, queue[0]) self.assertEqual(primitive_node, queue[1]) mock_emit_event.assert_called_once_with(EventType.START, element.tag, item=root_queue_item, element=element)
def test_parse_element(self, mock_parse_value): mock_parse_value.return_value = 13 var = XmlText(name="foo", qname=QName("foo"), default=100) node = PrimitiveNode(position=0, var=var) ele = Element("foo", nsmap={"foo": "bar"}) ele.text = "13" self.assertEqual((QName("foo"), 13), node.parse_element(ele, [])) mock_parse_value.assert_called_once_with(var.types, ele.text, var.default, ele.nsmap, var.is_tokens)
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))
def test_dequeue(self, mock_parse_element, mock_emit_event): element = Element("author", nsmap={"prefix": "uri"}) element.text = "foobar" objects = [] queue = [] var = XmlText(name="foo", qname=QName("foo")) queue.append(PrimitiveNode(position=0, var=var)) result = self.parser.dequeue(element, queue, objects) self.assertEqual("result", result) self.assertEqual(0, len(queue)) self.assertEqual(("q", result), objects[-1]) mock_parse_element.assert_called_once_with(element, objects) mock_emit_event.assert_called_once_with(EventType.END, element.tag, obj=result, element=element)
def test_next_node_when_given_qname_matches_primitive_var(self): ele = Element("a") ctx = XmlContext() cfg = ParserConfig() var = XmlText(name="a", qname=QName("a"), types=[int], default=100) meta = XmlMeta( name="foo", clazz=None, qname=QName("foo"), source_qname=QName("foo"), nillable=False, vars=[var], ) node = ElementNode(position=0, meta=meta, config=cfg) actual = node.next_node(ele, 10, ctx) self.assertIsInstance(actual, PrimitiveNode) self.assertEqual(10, actual.position) self.assertEqual(var, actual.var)
def test_property_is_tokens(self): var = XmlText(name="foo", qname=QName("foo")) self.assertFalse(var.is_tokens) var = XmlText(name="foo", qname=QName("foo"), default=list) self.assertTrue(var.is_tokens)
def test_property_is_text(self): var = XmlText(name="foo", qname=QName("foo")) self.assertIsInstance(var, XmlVar) self.assertTrue(var.is_text)