def test_sanitize_attribute_sequence(self): def len_sequential(target: Class): return len([ attr for attr in target.attrs if attr.restrictions.sequential ]) restrictions = Restrictions(max_occurs=2, sequential=True) target = ClassFactory.create(attrs=[ AttrFactory.create(restrictions=restrictions.clone()), AttrFactory.create(restrictions=restrictions.clone()), ]) attrs_clone = [attr.clone() for attr in target.attrs] self.sanitizer.process_attribute_sequence(target, target.attrs[0]) self.assertEqual(2, len_sequential(target)) target.attrs[0].restrictions.sequential = False self.sanitizer.process_attribute_sequence(target, target.attrs[0]) self.assertEqual(1, len_sequential(target)) self.sanitizer.process_attribute_sequence(target, target.attrs[1]) self.assertEqual(0, len_sequential(target)) target.attrs = attrs_clone target.attrs[1].restrictions.sequential = False self.sanitizer.process_attribute_sequence(target, target.attrs[0]) self.assertEqual(0, len_sequential(target)) target.attrs[0].restrictions.sequential = True target.attrs[0].restrictions.max_occurs = 0 target.attrs[1].restrictions.sequential = True self.sanitizer.process_attribute_sequence(target, target.attrs[0]) self.assertEqual(1, len_sequential(target))
def element_children( cls, obj: ElementBase, parent_restrictions: Restrictions ) -> Iterator[Tuple[ElementBase, Restrictions]]: """Recursively find and return all child elements that are qualified to be class attributes, with all their restrictions.""" for child in obj.children(): if child.is_attribute: yield child, parent_restrictions else: restrictions = parent_restrictions.clone() restrictions.merge(Restrictions.from_element(child)) yield from cls.element_children(child, restrictions)
def test_clone(self): restrictions = Restrictions(max_occurs=2) clone = restrictions.clone() self.assertEqual(clone, restrictions) self.assertIsNot(clone, restrictions)
def test_process_attribute_restrictions(self): required = Restrictions(min_occurs=1, max_occurs=1) attr = AttrFactory.attribute(restrictions=required.clone()) self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.min_occurs) self.assertIsNone(attr.restrictions.max_occurs) tokens = Restrictions(required=True, tokens=True, min_occurs=1, max_occurs=1) attr = AttrFactory.element(restrictions=tokens.clone()) self.sanitizer.process_attribute_restrictions(attr) self.assertFalse(attr.restrictions.required) self.assertIsNone(attr.restrictions.min_occurs) self.assertIsNone(attr.restrictions.max_occurs) attr = AttrFactory.element(restrictions=tokens.clone()) attr.restrictions.max_occurs = 2 self.sanitizer.process_attribute_restrictions(attr) self.assertFalse(attr.restrictions.required) self.assertIsNotNone(attr.restrictions.min_occurs) self.assertIsNotNone(attr.restrictions.max_occurs) multiple = Restrictions(min_occurs=0, max_occurs=2) attr = AttrFactory.create(tag=Tag.EXTENSION, restrictions=multiple) self.sanitizer.process_attribute_restrictions(attr) self.assertTrue(attr.restrictions.required) self.assertIsNone(attr.restrictions.min_occurs) self.assertIsNone(attr.restrictions.max_occurs) multiple = Restrictions(max_occurs=2, required=True) attr = AttrFactory.element(restrictions=multiple, fixed=True) self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.required) self.assertEqual(0, attr.restrictions.min_occurs) self.assertFalse(attr.fixed) attr = AttrFactory.element(restrictions=required.clone()) self.sanitizer.process_attribute_restrictions(attr) self.assertTrue(attr.restrictions.required) self.assertIsNone(attr.restrictions.min_occurs) self.assertIsNone(attr.restrictions.max_occurs) restrictions = Restrictions(required=True, min_occurs=0, max_occurs=1) attr = AttrFactory.element(restrictions=restrictions, default="A", fixed=True) self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.required) self.assertIsNone(attr.restrictions.min_occurs) self.assertIsNone(attr.restrictions.max_occurs) self.assertIsNone(attr.default) self.assertFalse(attr.fixed) attr = AttrFactory.element(restrictions=required.clone(), default="A") self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.required) attr = AttrFactory.element(restrictions=required.clone(), fixed=True) self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.required) attr = AttrFactory.element(restrictions=required.clone()) attr.restrictions.nillable = True self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.required)