def test_property_extensions(self): obj = Restriction() self.assertEqual([], list(obj.extensions)) obj.base = "foo" self.assertIsInstance(obj.extensions, Iterator) self.assertEqual(["foo"], list(obj.extensions))
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_element_children(self): sequence_one = Sequence(elements=[Element(), Element()]) sequence_two = Sequence(max_occurs=2, elements=[Element(), Element()]) restriction = Restriction( enumerations=[Enumeration(value=x) for x in "abc"], sequence=sequence_two, ) complex_type = ComplexType( attributes=[Attribute(), Attribute()], sequence=sequence_one, simple_content=SimpleContent(restriction=Restriction()), complex_content=ComplexContent(restriction=restriction,), ) restrictions = Restrictions.from_element(complex_type) children = self.builder.element_children(complex_type, restrictions) expected = [ (sequence_two.elements[0], Restrictions.from_element(sequence_two)), (sequence_two.elements[1], Restrictions.from_element(sequence_two)), (restriction.enumerations[0], Restrictions.from_element(restriction)), (restriction.enumerations[1], Restrictions.from_element(restriction)), (restriction.enumerations[2], Restrictions.from_element(restriction)), (sequence_one.elements[0], Restrictions.from_element(sequence_one)), (sequence_one.elements[1], Restrictions.from_element(sequence_one)), (complex_type.attributes[0], Restrictions.from_element(complex_type)), (complex_type.attributes[1], Restrictions.from_element(complex_type)), ] self.assertIsInstance(children, GeneratorType) self.assertEqual(expected, list(children))
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())
def test_property_real_type(self): obj = Restriction(base="foo") self.assertEqual(obj.base, obj.real_type) obj.enumerations.append(Enumeration()) self.assertIsNone(obj.real_type) obj = Restriction(simple_type=SimpleType(restriction=Restriction( base="bar"))) self.assertEqual("bar", obj.real_type)
def test_property_real_type(self): obj = Union() obj.member_types = "thug life" self.assertEqual(obj.member_types, obj.real_type) obj = Union(simple_types=[ SimpleType(restriction=Restriction(base="foo")), SimpleType(restriction=Restriction(base="bar")), ]) self.assertEqual("foo bar", obj.real_type)
def test_property_attr_types(self): obj = Union() obj.member_types = "thug life" self.assertEqual(["thug", "life"], list(obj.attr_types)) obj = Union(simple_types=[ SimpleType(restriction=Restriction(base="foo")), SimpleType(restriction=Restriction(base="bar")), ]) self.assertEqual(["foo", "bar"], list(obj.attr_types))
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_get_restrictions(self): obj = SimpleType() self.assertEqual({}, obj.get_restrictions()) expected = dict(length=2) obj.restriction = Restriction(length=Length(value=2)) self.assertEqual(expected, obj.get_restrictions())
def test_build_inner_classes(self, mock_build_class): inner_classes = ClassFactory.list(2) mock_build_class.side_effect = inner_classes simple_type = SimpleType() complex_type = ComplexType() enumeration = SimpleType(restriction=Restriction( enumerations=[Enumeration(value="a")])) element = Element(alternatives=[ Alternative(complex_type=complex_type, id="a"), Alternative(simple_type=simple_type, id="b"), Alternative(simple_type=enumeration, id="c"), ]) result = SchemaMapper.build_inner_classes(element, "module", "target_ns") self.assertIsInstance(result, Iterator) self.assertEqual(inner_classes, list(result)) self.assertEqual("a", complex_type.name) self.assertEqual("c", enumeration.name) mock_build_class.assert_has_calls([ mock.call(complex_type, Tag.ALTERNATIVE, "module", "target_ns"), mock.call(enumeration, Tag.ALTERNATIVE, "module", "target_ns"), ])
def test_property_attr_types(self): obj = SimpleType() self.assertEqual([], list(obj.attr_types)) obj.union = Union(member_types="thug") self.assertEqual(["thug"], list(obj.attr_types)) obj.list = List(item_type="foo") self.assertEqual(["foo"], list(obj.attr_types)) obj.restriction = Restriction(base="bar") self.assertEqual(["bar"], list(obj.attr_types)) obj = SimpleType(restriction=Restriction()) obj.restriction.enumerations.append(Enumeration()) self.assertEqual([], list(obj.attr_types))
def test_property_real_type(self): obj = SimpleType() self.assertIsNone(obj.real_type) obj.union = Union(member_types="thug") self.assertEqual("thug", obj.real_type) obj.list = List(item_type="foo") self.assertEqual("foo", obj.real_type) obj.restriction = Restriction(base="bar") self.assertEqual("bar", obj.real_type) obj = SimpleType(restriction=Restriction()) obj.restriction.enumerations.append(Enumeration()) self.assertIsNone(obj.real_type)
def test_get_restrictions(self): first = Restriction(min_exclusive=MinExclusive(value=1), min_inclusive=MinInclusive(value=2)) second = Restriction(min_length=MinLength(value=3), max_exclusive=MaxExclusive(value=4)) obj = Union(simple_types=[ SimpleType(restriction=first), SimpleType(restriction=second), ]) expected = { "max_exclusive": 4, "min_exclusive": 1, "min_inclusive": 2, "min_length": 3, } self.assertEqual(expected, obj.get_restrictions())
def test_get_restrictions(self): first = Restriction(min_exclusive=MinExclusive(value="1"), min_inclusive=MinInclusive(value="2")) second = Restriction(min_length=MinLength(value="3"), max_exclusive=MaxExclusive(value="4")) obj = Union(simple_types=[ SimpleType(restriction=first), SimpleType(restriction=second), ]) expected = { "max_exclusive": "4", "min_exclusive": "1", "min_inclusive": "2", "min_length": "3", } self.assertEqual(expected, obj.get_restrictions())
def test_property_is_enumeration(self): obj = SimpleType() self.assertFalse(obj.is_enumeration) obj.restriction = Restriction() self.assertFalse(obj.is_enumeration) obj.restriction.enumerations.append(Enumeration()) self.assertTrue(obj.is_enumeration)
def test_end_restriction(self): restriction = Restriction() not_restriction = Element() element = etree.Element("uno") self.parser.end_restriction(not_restriction, element) self.parser.end_restriction(restriction, element) default_open_content = DefaultOpenContent() self.parser.default_open_content = default_open_content self.parser.end_restriction(restriction, element) self.assertIs(default_open_content, restriction.open_content) open_content = OpenContent() restriction.open_content = open_content self.parser.end_restriction(restriction, element) self.assertIs(open_content, restriction.open_content)
def test_get_restrictions(self): obj = Attribute() self.assertEqual({}, obj.get_restrictions()) obj.use = UseType.REQUIRED expected = {"max_occurs": 1, "min_occurs": 1, "required": True} self.assertEqual(expected, obj.get_restrictions()) obj.simple_type = SimpleType(restriction=Restriction(length=Length(value=1))) expected.update(dict(length=1)) self.assertEqual(expected, obj.get_restrictions())
def test_end_restriction(self): restriction = Restriction() not_restriction = Element() self.parser.end_restriction(not_restriction) self.parser.end_restriction(restriction) default_open_content = DefaultOpenContent() self.parser.default_open_content = default_open_content self.parser.end_restriction(restriction) self.assertIs(default_open_content, restriction.open_content) open_content = OpenContent() restriction.open_content = open_content self.parser.end_restriction(restriction) self.assertIs(open_content, restriction.open_content) obj = ComplexType() self.parser.end_open_content(obj) self.assertIsNone(obj.open_content)
def test_build_inner_classes_with_enumeration(self, mock_build_class): inner = ClassFactory.enumeration(2) mock_build_class.return_value = inner enumeration = SimpleType( restriction=Restriction(enumerations=[Enumeration(value="a")]) ) result = self.builder.build_inner_classes(enumeration) self.assertIsInstance(result, Iterator) self.assertEqual([inner], list(result)) self.assertIsNone(enumeration.name)
def test_build_inner_classes_with_enumeration(self, mock_build_class): inner = ClassFactory.enumeration(2) mock_build_class.return_value = inner enumeration = SimpleType(restriction=Restriction( enumerations=[Enumeration(value="a")])) result = SchemaMapper.build_inner_classes(enumeration, "module", "target_ns") self.assertIsInstance(result, Iterator) self.assertEqual([inner], list(result)) self.assertIsNone(enumeration.name) mock_build_class.assert_called_once_with(enumeration, Tag.SIMPLE_TYPE, "module", "target_ns")
def test_property_real_type(self): obj = Attribute() self.assertEqual("", obj.real_type) obj.ref = "foo" self.assertEqual(obj.ref, obj.real_type) obj.type = "bar" self.assertEqual(obj.type, obj.real_type) obj.simple_type = SimpleType() self.assertEqual("", obj.real_type) obj.simple_type.restriction = Restriction(base="thug") self.assertEqual(obj.simple_type.restriction.base, obj.real_type)
def test_property_attr_types(self): obj = Attribute() self.assertEqual([], list(obj.attr_types)) obj.ref = "foo" self.assertEqual([obj.ref], list(obj.attr_types)) obj.type = "bar" self.assertEqual([obj.type], list(obj.attr_types)) obj.simple_type = SimpleType() self.assertEqual([], list(obj.attr_types)) obj.simple_type.restriction = Restriction(base="thug") self.assertEqual([obj.simple_type.restriction.base], list(obj.attr_types))
def test_get_restrictions(self): obj = Attribute() self.assertEqual({}, obj.get_restrictions()) obj.use = UseType.REQUIRED expected = {"required": True} self.assertEqual(expected, obj.get_restrictions()) obj.use = UseType.PROHIBITED expected = {"prohibited": True} self.assertEqual(expected, obj.get_restrictions()) obj.simple_type = SimpleType(restriction=Restriction(length=Length( value=1))) expected["length"] = 1 self.assertEqual(expected, obj.get_restrictions())
def test_get_restrictions(self): obj = Element(min_occurs=1, max_occurs=1) expected = {"min_occurs": 1, "max_occurs": 1} self.assertEqual(expected, obj.get_restrictions()) obj.simple_type = SimpleType(restriction=Restriction(length=Length( value=9))) expected.update({"length": 9}) self.assertEqual(expected, obj.get_restrictions()) obj.nillable = False self.assertEqual(expected, obj.get_restrictions()) obj.nillable = True expected.update({"nillable": True}) self.assertEqual(expected, obj.get_restrictions())
def test_get_restrictions(self): obj = Element(min_occurs=1, max_occurs=1) expected = {"min_occurs": 1, "max_occurs": 1} self.assertEqual(expected, obj.get_restrictions()) obj.simple_type = SimpleType(restriction=Restriction(length=Length(value=9))) expected["length"] = 9 self.assertEqual(expected, obj.get_restrictions()) obj.nillable = False self.assertEqual(expected, obj.get_restrictions()) obj.nillable = True expected["nillable"] = True self.assertEqual(expected, obj.get_restrictions()) obj.type = "NMTOKENS" expected["tokens"] = True self.assertEqual(expected, obj.get_restrictions())
def test_children_extensions(self): complex_type = ComplexType( attributes=[Attribute(index=i) for i in range(2)], simple_content=SimpleContent(restriction=Restriction(base="bk:b", index=4)), complex_content=ComplexContent(extension=Extension(base="bk:ext", index=7)), ) item = ClassFactory.create() children = self.builder.children_extensions(complex_type, item) expected = list( map( ExtensionFactory.create, [ AttrTypeFactory.create(name="bk:b", index=4), AttrTypeFactory.create(name="bk:ext", index=7), ], ) ) self.assertIsInstance(children, GeneratorType) self.assertEqual(expected, list(children))
def test_property_attr_types(self): obj = Element() self.assertEqual([], list(obj.attr_types)) # Inner classes depend on the this to be None obj.complex_type = ComplexType() self.assertEqual([], list(obj.attr_types)) restriction = Restriction(base="xs:int") obj.simple_type = SimpleType(restriction=restriction) self.assertEqual([restriction.base], list(obj.attr_types)) obj.ref = "foo" self.assertEqual([obj.ref], list(obj.attr_types)) obj.type = "bar" self.assertEqual([obj.type], list(obj.attr_types)) obj.alternatives.append(Alternative(type="foo")) obj.alternatives.append(Alternative(type="bar")) obj.alternatives.append(Alternative(type="thug")) self.assertEqual(["bar", "foo", "bar", "thug"], list(obj.attr_types))
def test_children_extensions(self): complex_type = ComplexType( attributes=[Attribute() for _ in range(2)], simple_content=SimpleContent(restriction=Restriction(base="bk:b")), complex_content=ComplexContent(extension=Extension(base="bk:c")), ) complex_type.simple_content.restriction.index = 4 complex_type.complex_content.extension.index = 7 item = ClassFactory.create(ns_map={"bk": "book"}) children = SchemaMapper.children_extensions(complex_type, item) expected = list( map( ExtensionFactory.create, [ AttrTypeFactory.create(qname=build_qname("book", "b")), AttrTypeFactory.create(qname=build_qname("book", "c")), ], )) self.assertIsInstance(children, GeneratorType) self.assertEqual(expected, list(children))
def test_property_real_type(self): obj = Element() self.assertEqual("", obj.real_type) # Inner classes depend on the this to be None obj.complex_type = ComplexType() self.assertEqual("", obj.real_type) restriction = Restriction(base="xs:int") obj.simple_type = SimpleType(restriction=restriction) self.assertEqual(restriction.base, obj.real_type) obj.ref = "foo" self.assertEqual(obj.ref, obj.real_type) obj.type = "bar" self.assertEqual(obj.type, obj.real_type) obj.alternatives.append(Alternative(type="foo")) obj.alternatives.append(Alternative(type="bar")) obj.alternatives.append(Alternative(type="thug")) self.assertEqual("bar foo thug", obj.real_type)
def test_property_real_name(self): obj = Restriction() self.assertEqual("value", obj.real_name)