def test_merge_attributes(self): target = ClassFactory.create( attrs=[ AttrFactory.element(name="a", index=10), AttrFactory.element(name="b", index=1), AttrFactory.element(name="c", index=2), AttrFactory.attribute(name="id", index=0), ] ) source = target.clone() target.attrs[0].restrictions.min_occurs = 2 target.attrs[0].restrictions.max_occurs = 3 source.attrs[1].restrictions.min_occurs = 3 source.attrs[1].restrictions.max_occurs = 4 source.attrs[3].restrictions.min_occurs = 3 source.attrs[3].restrictions.max_occurs = 4 source.attrs.append(AttrFactory.enumeration(name="d", index=4)) ClassUtils.merge_attributes(target, source) names = ["id", "b", "c", "d", "a"] min_occurs = [0, 0, 0, None, 0] max_occurs = [4, 4, 1, None, 3] self.assertEqual(names, [x.name for x in target.attrs]) self.assertEqual(min_occurs, [x.restrictions.min_occurs for x in target.attrs]) self.assertEqual(max_occurs, [x.restrictions.max_occurs for x in target.attrs])
def test_process(self): one = AttrFactory.attribute(fixed=True) one_clone = one.clone() restrictions = Restrictions(min_occurs=10, max_occurs=15) two = AttrFactory.element(restrictions=restrictions, fixed=True) two_clone = two.clone() two_clone.restrictions.min_occurs = 5 two_clone.restrictions.max_occurs = 5 two_clone_two = two.clone() two_clone_two.restrictions.min_occurs = 4 two_clone_two.restrictions.max_occurs = 4 three = AttrFactory.element() four = AttrFactory.enumeration() four_clone = four.clone() five = AttrFactory.element() five_clone = five.clone() five_clone_two = five.clone() target = ClassFactory.create( attrs=[ one, one_clone, two, two_clone, two_clone_two, three, four, four_clone, five, five_clone, five_clone_two, ] ) winners = [one, two, three, four, five] self.processor.process(target) self.assertEqual(winners, target.attrs) self.assertTrue(one.fixed) self.assertIsNone(one.restrictions.min_occurs) self.assertIsNone(one.restrictions.max_occurs) self.assertFalse(two.fixed) self.assertEqual(4, two.restrictions.min_occurs) self.assertEqual(24, two.restrictions.max_occurs) self.assertIsNone(three.restrictions.min_occurs) self.assertIsNone(three.restrictions.max_occurs) self.assertIsNone(four.restrictions.min_occurs) self.assertIsNone(four.restrictions.max_occurs) self.assertEqual(0, five.restrictions.min_occurs) self.assertEqual(3, five.restrictions.max_occurs)
def test_field_metadata_namespace(self): attr = AttrFactory.element(namespace="foo") expected = {"name": "attr_B", "namespace": "foo", "type": "Element"} actual = self.filters.field_metadata(attr, None, ["cls"]) self.assertEqual(expected, actual) actual = self.filters.field_metadata(attr, "foo", ["cls"]) self.assertNotIn("namespace", actual) attr = AttrFactory.attribute(namespace="foo") expected = {"name": "attr_C", "namespace": "foo", "type": "Attribute"} actual = self.filters.field_metadata(attr, None, ["cls"]) self.assertEqual(expected, actual) actual = self.filters.field_metadata(attr, "foo", ["cls"]) self.assertIn("namespace", actual)
def test_property_is_attribute(self): self.assertTrue(AttrFactory.attribute().is_attribute) self.assertTrue(AttrFactory.any_attribute().is_attribute) self.assertFalse(AttrFactory.element().is_attribute)
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)