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
|
from typing import Iterator
from unittest import TestCase
from xsdata.models.xsd import (
Enumeration,
FractionDigits,
Length,
MaxExclusive,
MaxInclusive,
MaxLength,
MinExclusive,
MinInclusive,
MinLength,
Pattern,
Restriction,
SimpleType,
TotalDigits,
WhiteSpace,
)
class RestrictionTests(TestCase):
def test_property_attr_types(self):
obj = Restriction()
self.assertEqual([], list(obj.attr_types))
obj = Restriction(base="foo")
self.assertEqual([obj.base], list(obj.attr_types))
obj.enumerations.append(Enumeration())
self.assertEqual([], list(obj.attr_types))
obj = Restriction(simple_type=SimpleType(restriction=Restriction(base="bar")))
self.assertEqual(["bar"], list(obj.attr_types))
def test_property_real_name(self):
obj = Restriction()
self.assertEqual("value", obj.real_name)
def test_property_bases(self):
obj = Restriction()
self.assertEqual([], list(obj.bases))
obj.base = "foo"
self.assertIsInstance(obj.bases, Iterator)
self.assertEqual(["foo"], list(obj.bases))
def test_get_restrictions(self):
self.assertEqual({}, Restriction().get_restrictions())
obj = Restriction(
min_exclusive=MinExclusive(value=1),
min_inclusive=MinInclusive(value=2),
min_length=MinLength(value=3),
max_exclusive=MaxExclusive(value=4),
max_inclusive=MaxInclusive(value=5),
max_length=MaxLength(value=6),
total_digits=TotalDigits(value=7),
fraction_digits=FractionDigits(value=8),
length=Length(value=9),
white_space=WhiteSpace(value="collapse"),
patterns=[Pattern(value="[0-9]"), Pattern(value="[A-Z]")],
enumerations=[Enumeration(value="str")],
)
expected = {
"fraction_digits": 8,
"length": 9,
"max_exclusive": 4,
"max_inclusive": 5,
"max_length": 6,
"min_exclusive": 1,
"min_inclusive": 2,
"min_length": 3,
"pattern": "[0-9]|[A-Z]",
"total_digits": 7,
"white_space": "collapse",
}
self.assertEqual(expected, obj.get_restrictions())
def test_get_restrictions_with_nested_simple_type(self):
obj = Restriction(
min_length=MinLength(value=2),
simple_type=SimpleType(
restriction=Restriction(
max_length=MaxLength(value=10), min_length=MinLength(value=5)
)
),
)
expected = {"max_length": 10, "min_length": 2}
self.assertEqual(expected, obj.get_restrictions())
|