Skip to content

Commit

Permalink
ci: Increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tefra committed Mar 2, 2024
1 parent c87563d commit c4741d3
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
21 changes: 20 additions & 1 deletion tests/codegen/handlers/test_disambiguate_choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def test_process_with_duplicate_simple_types(self):
self.assertEqual("a", target.inner[0].qname)
self.assertEqual("{xs}b", target.inner[1].qname)

self.assertEqual(["a", "{xs}b"], [x.qname for x in compound.types])

def test_process_with_duplicate_any_types(self):
compound = AttrFactory.create(tag=Tag.CHOICE, types=[])
target = ClassFactory.create()
Expand All @@ -91,7 +93,7 @@ def test_process_with_duplicate_any_types(self):
self.assertEqual("{xs}b", target.inner[1].qname)

def test_process_with_duplicate_complex_types(self):
compound = AttrFactory.create(tag=Tag.CHOICE, types=[])
compound = AttrFactory.any()
target = ClassFactory.create()
target.attrs.append(compound)
compound.choices.append(AttrFactory.reference(name="a", qname="myint"))
Expand All @@ -112,6 +114,8 @@ def test_process_with_duplicate_complex_types(self):
self.assertEqual("myint", inner.extensions[0].type.qname)
self.assertEqual("myint", inner.extensions[0].type.qname)

self.assertEqual(DataType.ANY_TYPE, compound.types[0].datatype)

def test_disambiguate_choice_with_unnest_true(self):
target = ClassFactory.create()
attr = AttrFactory.reference(qname="a")
Expand All @@ -137,6 +141,21 @@ def test_disambiguate_choice_with_circular_ref(self):
self.assertTrue(attr.types[0].circular)
self.assertIsNotNone(self.container.find(attr.qname))

def test_find_ambiguous_choices_ignore_wildcards(self):
"""Wildcards are merged."""

attr = AttrFactory.create()
attr.choices.append(AttrFactory.any())
attr.choices.append(AttrFactory.any())
attr.choices.append(
AttrFactory.create(
name="this", types=[AttrTypeFactory.native(DataType.ANY_TYPE)]
)
)

result = self.handler.find_ambiguous_choices(attr)
self.assertEqual(["this"], [x.name for x in result])

def test_is_simple_type(self):
attr = AttrFactory.native(DataType.STRING)
self.assertTrue(self.handler.is_simple_type(attr))
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass
from dataclasses import field
from decimal import Decimal
from typing import Dict, Any
from typing import List
from typing import Optional
Expand Down Expand Up @@ -98,7 +99,7 @@ class ChoiceType:
{"name": "float", "type": float},
{"name": "qname", "type": QName},
{"name": "union", "type": Type["UnionType"], "namespace": "foo"},
{"name": "tokens", "type": List[str], "tokens": True},
{"name": "tokens", "type": List[Decimal], "tokens": True},
{
"wildcard": True,
"type": object,
Expand Down
3 changes: 2 additions & 1 deletion tests/formats/dataclass/models/test_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import uuid
from dataclasses import dataclass, field, fields, make_dataclass
from decimal import Decimal
from typing import Dict, Iterator, List, Tuple, Union, get_type_hints
from unittest import TestCase, mock
from xml.etree.ElementTree import QName
Expand Down Expand Up @@ -376,7 +377,7 @@ def test_build_with_choice_field(self):
index=8,
name="choice",
qname="{bar}tokens",
types=(str,),
types=(Decimal,),
tokens_factory=list,
derived=True,
factory=list,
Expand Down
13 changes: 10 additions & 3 deletions tests/formats/dataclass/parsers/test_dict.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from dataclasses import asdict, make_dataclass
from decimal import Decimal
from typing import List, Optional, Union
from xml.etree.ElementTree import QName

Expand Down Expand Up @@ -232,14 +233,13 @@ def test_bind_simple_type_with_wildcard_var(self):
self.assertEqual(2, actual.wildcard)

def test_bind_simple_type_with_elements_var(self):
data = {"choice": ["1.0", 1, "a", "{a}b"]}
data = {"choice": ["1.0", 1, ["1.2"], "{a}b"]}

actual = self.decoder.bind_dataclass(data, ChoiceType)

self.assertEqual(1.0, actual.choice[0])
self.assertEqual(1, actual.choice[1])
self.assertEqual(QName("a"), actual.choice[2])
self.assertIsInstance(actual.choice[2], QName)
self.assertEqual([Decimal("1.2")], actual.choice[2])
self.assertEqual(QName("{a}b"), actual.choice[3])
self.assertIsInstance(actual.choice[3], QName)

Expand Down Expand Up @@ -309,6 +309,13 @@ def test_bind_derived_value_with_choice_var(self):
str(cm.exception),
)

def test_bind_derived_value_with_simple_type(self):
data = {"choice": [{"qname": "float", "value": 1, "type": None}]}

actual = self.decoder.bind_dataclass(data, ChoiceType)
expected = ChoiceType(choice=[DerivedElement(qname="float", value=1)])
self.assertEqual(expected, actual)

def test_bind_wildcard_dataclass(self):
data = {"a": None, "wildcard": {"x": 1}}
expected = ExtendedType(wildcard=TypeA(x=1))
Expand Down
7 changes: 3 additions & 4 deletions tests/formats/dataclass/test_elements.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import make_dataclass
from decimal import Decimal
from unittest import mock
from unittest.case import TestCase
from xml.etree.ElementTree import QName
Expand Down Expand Up @@ -45,7 +46,7 @@ def test_property_element_types(self):
meta = self.context.build(ChoiceType)
var = meta.choices[0]
self.assertEqual(
{TypeA, TypeB, int, float, QName, UnionType, str}, var.element_types
{TypeA, TypeB, int, float, QName, UnionType, Decimal}, var.element_types
)

def test_find_choice(self):
Expand Down Expand Up @@ -88,9 +89,7 @@ def test_find_value_choice(self):
meta = self.context.build(ChoiceType)
var = meta.choices[0]

self.assertEqual(
var.elements["tokens"], var.find_value_choice(["f", "e"], False)
)
self.assertEqual(var.elements["tokens"], var.find_value_choice(["1.2"], False))
self.assertIsNone(var.find_value_choice([], False))
self.assertEqual(var.elements["qname"], var.find_value_choice("foo", False))
self.assertEqual(var.elements["int"], var.find_value_choice(1, False))
Expand Down
1 change: 1 addition & 0 deletions xsdata/formats/dataclass/parsers/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ def bind_derived_value(self, meta: XmlMeta, var: XmlVar, data: Dict) -> Any:
return self.bind_derived_value(meta, choice, data)

if not isinstance(params, dict):
# Is this scenario still possible???
value = self.bind_text(meta, var, params)
elif xsi_type:
clazz: Optional[Type] = self.context.find_type(xsi_type)
Expand Down
2 changes: 1 addition & 1 deletion xsdata/utils/namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def local_name(qname: str) -> str:
return split_qname(qname)[1]


NCNAME_PUNCTUATION = {"\u00B7", "\u0387", ".", "-", "_"}
NCNAME_PUNCTUATION = {"\u00b7", "\u0387", ".", "-", "_"}


def is_ncname(name: Optional[str]) -> bool:
Expand Down

0 comments on commit c4741d3

Please sign in to comment.