1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
import sys
from unittest import mock
from xsdata.codegen.mappers.dict import DictMapper
from xsdata.codegen.models import Restrictions
from xsdata.codegen.utils import ClassUtils
from xsdata.models.enums import DataType, Tag
from xsdata.utils.testing import (
AttrFactory,
AttrTypeFactory,
ClassFactory,
FactoryTestCase,
)
class DictMapperTests(FactoryTestCase):
@mock.patch.object(ClassUtils, "flatten")
@mock.patch.object(DictMapper, "build_class")
def test_map(self, mock_build_class, mock_flatten):
data = {"value": 1}
root_class = ClassFactory.create()
flat_classes = ClassFactory.list(5)
iter_flat_classes = iter(flat_classes)
mock_build_class.return_value = root_class
mock_flatten.return_value = iter_flat_classes
actual = DictMapper.map(data, "root", "tests")
self.assertEqual(flat_classes, actual)
mock_build_class.assert_called_once_with(data, "root")
mock_flatten.assert_called_once_with(root_class, "tests/root")
def test_build_class(self):
data = {"a": 1, "b": True}
actual = DictMapper.build_class(data, "root")
expected = ClassFactory.create(
tag=Tag.ELEMENT,
qname="root",
location="",
module=None,
ns_map={},
attrs=[
AttrFactory.native(
DataType.SHORT,
tag=Tag.ELEMENT,
name="a",
index=0,
),
AttrFactory.native(
DataType.BOOLEAN,
tag=Tag.ELEMENT,
name="b",
index=1,
),
],
)
self.assertEqual(expected, actual)
def test_build_class_attribute_from_list(self):
target = ClassFactory.create()
data = [1, True, 1.1]
DictMapper.build_class_attribute(target, "a", data)
expected = AttrFactory.create(
name="a",
tag=Tag.ELEMENT,
types=[
AttrTypeFactory.native(DataType.SHORT, tag=Tag.ELEMENT),
AttrTypeFactory.native(DataType.BOOLEAN, tag=Tag.ELEMENT),
AttrTypeFactory.native(DataType.FLOAT, tag=Tag.ELEMENT),
],
)
restrictions = Restrictions(min_occurs=1, max_occurs=sys.maxsize)
self.assertEqual(expected, target.attrs[0])
self.assertEqual(restrictions, target.attrs[0].restrictions)
def test_build_class_attribute_from_empty_list(self):
target = ClassFactory.create()
data = []
DictMapper.build_class_attribute(target, "a", data)
expected = AttrFactory.create(
name="a",
tag=Tag.ELEMENT,
types=[
AttrTypeFactory.native(DataType.ANY_SIMPLE_TYPE, tag=Tag.ELEMENT),
],
)
restrictions = Restrictions(min_occurs=1, max_occurs=sys.maxsize)
self.assertEqual(expected, target.attrs[0])
self.assertEqual(restrictions, target.attrs[0].restrictions)
def test_build_class_attribute_from_dict(self):
target = ClassFactory.create()
data = {"sub1": 1, "sub2": "value"}
DictMapper.build_class_attribute(target, "a", data)
expected = AttrFactory.create(
name="a",
tag=Tag.ELEMENT,
types=[AttrTypeFactory.create(qname="a", forward=True)],
)
expected_inner = ClassFactory.create(
qname="a",
tag=Tag.ELEMENT,
location="",
module=None,
ns_map={},
attrs=[
AttrFactory.native(DataType.SHORT, name="sub1"),
AttrFactory.native(DataType.STRING, name="sub2"),
],
)
self.assertEqual(expected, target.attrs[0])
self.assertEqual(expected_inner, target.inner[0])
self.assertEqual(1, len(target.inner))
|