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_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_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_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_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_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 = 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_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_property_is_attribute(self): obj = Enumeration.create() self.assertTrue(obj)
def test_get_restrictions(self): obj = Enumeration.create() self.assertEqual({}, obj.get_restrictions())
def test_property_default(self): obj = Enumeration.create(value="foo") self.assertEqual("foo", obj.default)
def test_property_real_type(self): obj = Enumeration.create() self.assertIsNone(obj.real_type)
def test_property_real_name(self): obj = Enumeration.create(value="foo") self.assertEqual("foo", obj.real_name)