def test_element_children(self): sequence_one = Sequence.create(elements=[Element.create(), Element.create()]) sequence_two = Sequence.create( max_occurs=2, elements=[Element.create(), Element.create()] ) restriction = Restriction.create( enumerations=[Enumeration.create(value=x) for x in "abc"], sequence=sequence_two, ) complex_type = ComplexType.create( attributes=[Attribute.create(), Attribute.create()], sequence=sequence_one, simple_content=SimpleContent.create(restriction=Restriction.create()), complex_content=ComplexContent.create(restriction=restriction,), ) children = self.builder.element_children(complex_type) 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(self): self.assertEqual({}, Restriction.create().get_restrictions()) obj = Restriction.create( min_exclusive=MinExclusive.create(value=1), min_inclusive=MinInclusive.create(value=2), min_length=MinLength.create(value=3), max_exclusive=MaxExclusive.create(value=4), max_inclusive=MaxInclusive.create(value=5), max_length=MaxLength.create(value=6), total_digits=TotalDigits.create(value=7), fraction_digits=FractionDigits.create(value=8), length=Length.create(value=9), white_space=WhiteSpace.create(value="collapse"), patterns=[ Pattern.create(value="[0-9]"), Pattern.create(value="[A-Z]") ], enumerations=[Enumeration.create(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_property_real_type(self): obj = Union.create() obj.member_types = "thug life" self.assertEqual(obj.member_types, obj.real_type) obj = Union.create(simple_types=[ SimpleType.create(restriction=Restriction.create(base="foo")), SimpleType.create(restriction=Restriction.create(base="bar")), ]) self.assertEqual("foo bar", obj.real_type)
def test_get_restrictions_with_nested_simple_type(self): obj = Restriction.create( min_length=MinLength.create(value=2), simple_type=SimpleType.create(restriction=Restriction.create( max_length=MaxLength.create(value=10), min_length=MinLength.create(value=5), )), ) expected = {"max_length": 10, "min_length": 2} self.assertEqual(expected, obj.get_restrictions())
def test_property_real_type(self): obj = Restriction.create(base="foo") self.assertEqual(obj.base, obj.real_type) obj.enumerations.append(Enumeration.create()) self.assertIsNone(obj.real_type) obj = Restriction.create(simple_type=SimpleType.create( restriction=Restriction.create(base="bar"))) self.assertEqual("bar", obj.real_type)
def test_get_restrictions(self): obj = SimpleType.create() self.assertEqual({}, obj.get_restrictions()) expected = dict(length=2) obj.restriction = Restriction.create(length=Length.create(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.create() complex_type = ComplexType.create() enumeration = SimpleType.create( restriction=Restriction.create(enumerations=[Enumeration.create(value="a")]) ) element = Element.create( alternatives=[ Alternative.create(complex_type=complex_type, id="a"), Alternative.create(simple_type=simple_type, id="b"), Alternative.create(simple_type=enumeration, id="c"), ] ) result = self.builder.build_inner_classes(element) 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), mock.call(enumeration)] )
def test_children_extensions(self): complex_type = ComplexType.create( attributes=[Attribute.create(index=i) for i in range(2)], simple_content=SimpleContent.create( restriction=Restriction.create(base="bk:b", index=4) ), complex_content=ComplexContent.create( extension=Extension.create(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_real_type(self): obj = SimpleType.create() self.assertIsNone(obj.real_type) obj.union = Union.create(member_types="thug") self.assertEqual("thug", obj.real_type) obj.list = List.create(item_type="foo") self.assertEqual("foo", obj.real_type) obj.restriction = Restriction.create(base="bar") self.assertEqual("bar", obj.real_type) obj = SimpleType.create(restriction=Restriction.create()) obj.restriction.enumerations.append(Enumeration.create()) self.assertIsNone(obj.real_type)
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_build_inner_class_when_union_has_anonymous_enumeration( self, mock_has_anonymous_class, mock_has_anonymous_enumeration, mock_build_class): inner_class = ClassFactory.create() mock_build_class.return_value = inner_class mock_has_anonymous_class.return_value = False mock_has_anonymous_enumeration.return_value = False union = Union.create(name="foo", type="xs:int") simple_type_a = SimpleType.create(restriction=Restriction.create( enumerations=[Enumeration.create(value="a")])) simple_type_b = SimpleType.create(restriction=Restriction.create( enumerations=[Enumeration.create(value="b")])) union.simple_types = [simple_type_a, simple_type_b] self.assertEqual(inner_class, self.builder.build_inner_class(union)) self.assertEqual([simple_type_a], union.simple_types) mock_build_class.assert_called_once_with(simple_type_b)
def test_property_is_enumeration(self): obj = SimpleType.create() self.assertFalse(obj.is_enumeration) obj.restriction = Restriction.create() self.assertFalse(obj.is_enumeration) obj.restriction.enumerations.append(Enumeration.create()) self.assertTrue(obj.is_enumeration)
def test_get_restrictions(self): first = Restriction.create( min_exclusive=MinExclusive.create(value=1), min_inclusive=MinInclusive.create(value=2), ) second = Restriction.create( min_length=MinLength.create(value=3), max_exclusive=MaxExclusive.create(value=4), ) obj = Union.create(simple_types=[ SimpleType.create(restriction=first), SimpleType.create(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): obj = Attribute.create() 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.create(restriction=Restriction.create( length=Length.create(value=1))) expected.update(dict(length=1)) self.assertEqual(expected, obj.get_restrictions())
def test_build_inner_classes_with_enumeration(self, mock_build_class): inner = ClassFactory.enumeration(2) mock_build_class.return_value = inner enumeration = SimpleType.create( restriction=Restriction.create(enumerations=[Enumeration.create(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_class_when_is_anonymous_enumeration( self, mock_has_anonymous_class, mock_has_anonymous_enumeration, mock_build_class): inner_class = ClassFactory.create() mock_build_class.return_value = inner_class mock_has_anonymous_class.return_value = False mock_has_anonymous_enumeration.return_value = False simple_type = SimpleType.create(restriction=Restriction.create()) simple_type.restriction.enumerations.append(Enumeration.create()) self.assertEqual(inner_class, self.builder.build_inner_class(simple_type)) self.assertEqual("value", simple_type.name) self.assertIsNone(simple_type.restriction)
def test_property_real_type(self): obj = Attribute.create() self.assertIsNone(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.create() self.assertIsNone(obj.real_type) obj.simple_type.restriction = Restriction.create(base="thug") self.assertEqual(obj.simple_type.restriction.base, obj.real_type)
def test_has_anonymous_enumeration(self): for clazz in [Element, Attribute]: obj = clazz.create() self.assertFalse(self.builder.has_anonymous_enumeration(obj)) obj = clazz.create(simple_type=SimpleType.create()) self.assertFalse(self.builder.has_anonymous_enumeration(obj)) obj.simple_type.restriction = Restriction.create() self.assertFalse(self.builder.has_anonymous_enumeration(obj)) obj.simple_type.restriction.enumerations.append( Enumeration.create()) self.assertTrue(self.builder.has_anonymous_enumeration(obj)) obj.type = "foo" self.assertFalse(self.builder.has_anonymous_enumeration(obj))
def test_get_restrictions(self): obj = Element.create(min_occurs=1, max_occurs=1) expected = {"min_occurs": 1, "max_occurs": 1} self.assertEqual(expected, obj.get_restrictions()) obj.simple_type = SimpleType.create( restriction=Restriction.create(length=Length.create(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_property_real_type(self): obj = Element.create() self.assertIsNone(obj.real_type) # Inner classes depend on the this to be None obj.complex_type = ComplexType.create() self.assertIsNone(obj.real_type) restriction = Restriction.create(base="xs:int") obj.simple_type = SimpleType.create(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.create(type="foo")) obj.alternatives.append(Alternative.create(type="bar")) obj.alternatives.append(Alternative.create(type="thug")) self.assertEqual("bar foo thug", obj.real_type)
def test_property_extends(self): obj = Restriction.create(base="foo") self.assertEqual("foo", obj.extends)
def test_property_real_name(self): obj = Restriction.create() self.assertEqual("value", obj.real_name)
def test_property_real_type(self): obj = Restriction.create(base="foo") self.assertEqual(obj.base, obj.real_type)