def test_attribute_metadata_name(self): attr = AttrFactory.element(local_name="foo", name="bar") actual = attribute_metadata(attr, None) self.assertEqual("foo", actual["name"]) attr = AttrFactory.element(local_name="foo", name="Foo") self.assertNotIn("name", attribute_metadata(attr, None)) attr = AttrFactory.create(tag=Tag.ANY, local_name="foo", name="bar") self.assertNotIn("name", attribute_metadata(attr, None))
def test_field_metadata_name(self): attr = AttrFactory.element(name="bar") attr.local_name = "foo" self.assertEqual("foo", self.filters.field_metadata(attr, None, [])["name"]) attr = AttrFactory.element(name="Foo") attr.local_name = "foo" self.assertNotIn("name", self.filters.field_metadata(attr, None, [])) attr = AttrFactory.create(tag=Tag.ANY, name="bar") attr.local_name = "foo" self.assertNotIn("name", self.filters.field_metadata(attr, None, []))
def test_class_docstring(self): target = ClassFactory.create(attrs=[ AttrFactory.element(help="help"), AttrFactory.element(help="Foo\nBar"), AttrFactory.element(), ]) expected = '''""" :ivar attr_b: help :ivar attr_c: Foo Bar :ivar attr_d: """''' self.assertEqual(expected, class_docstring(target))
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_property_is_factory(self): self.assertTrue(AttrFactory.any_attribute().is_factory) element = AttrFactory.element() self.assertFalse(element.is_factory) element.restrictions.max_occurs = 2 self.assertTrue(element.is_factory)
def test_property_is_enumeration(self): obj = ClassFactory.enumeration(2) self.assertTrue(obj.is_enumeration) obj.attrs.append(AttrFactory.element()) self.assertFalse(obj.is_enumeration) obj.attrs.clear() self.assertFalse(obj.is_enumeration)
def test_field_choices(self): attr = AttrFactory.create( choices=[ AttrFactory.element( namespace="foo", types=[type_float], restrictions=Restrictions(max_exclusive="10"), ), AttrFactory.element(namespace="bar"), AttrFactory.any(namespace="##other"), AttrFactory.element(name="bar", default="aa"), AttrFactory.element(name="tok", restrictions=Restrictions(tokens=True)), ] ) actual = self.filters.field_choices(attr, "foo", ["a", "b"]) expected = [ {"name": "attr_B", "type": "Type[float]", "max_exclusive": 10.0}, {"name": "attr_C", "namespace": "bar", "type": "Type[str]"}, { "name": "attr_D", "namespace": "##other", "wildcard": True, "type": "Type[object]", }, {"default": '"aa"', "name": "bar", "type": "Type[str]"}, { "default_factory": "list", "name": "tok", "tokens": True, "type": "Type[List[str]]", }, ] self.assertEqual(expected, actual) self.filters.docstring_style = DocstringStyle.ACCESSIBLE attr.choices[0].help = "help" actual = self.filters.field_choices(attr, None, []) self.assertEqual(attr.choices[0].help, actual[0]["doc"]) self.assertNotIn("doc", actual[1])
def test_field_metadata_namespace(self): attr = AttrFactory.element(namespace="foo") expected = {"name": "attr_B", "namespace": "foo", "type": "Element"} self.assertEqual(expected, self.filters.field_metadata(attr, None, [])) self.assertNotIn("namespace", self.filters.field_metadata(attr, "foo", [])) attr = AttrFactory.attribute(namespace="foo") expected = {"name": "attr_C", "namespace": "foo", "type": "Attribute"} self.assertEqual(expected, self.filters.field_metadata(attr, None, [])) self.assertIn("namespace", self.filters.field_metadata(attr, "foo", []))
def test_process(self): target = ClassFactory.elements(2) self.processor.process(target) self.assertEqual(2, len(target.attrs)) self.assertEqual(0, len(target.inner)) target = ClassFactory.enumeration(2) self.processor.process(target) self.assertEqual(2, len(target.attrs)) self.assertEqual(0, len(target.inner)) enumerations = list(target.attrs) target.attrs.append(AttrFactory.element()) self.processor.process(target) self.assertEqual(1, len(target.attrs)) self.assertEqual(1, len(target.inner)) self.assertEqual(enumerations, target.inner[0].attrs) target.attrs.append(AttrFactory.enumeration()) self.processor.process(target) self.assertEqual(1, len(target.attrs)) self.assertEqual(1, len(target.inner)) self.assertEqual(3, len(target.inner[0].attrs)) target.attrs.append(AttrFactory.element()) self.processor.process(target) self.assertEqual(2, len(target.attrs)) self.assertEqual(1, len(target.inner)) self.assertEqual(3, len(target.inner[0].attrs)) target.attrs.append(AttrFactory.enumeration()) with self.assertRaises(AnalyzerValueError) as cm: self.processor.process(target) self.assertEqual("Mixed enumeration with more than one normal field.", str(cm.exception))
def test_process(self, mock_create_substitutions, mock_process_attribute): def init_substitutions(): self.processor.substitutions = {} mock_create_substitutions.side_effect = init_substitutions target = ClassFactory.create( attrs=[AttrFactory.enumeration(), AttrFactory.any(), AttrFactory.element(),] ) self.processor.process(target) self.processor.process(ClassFactory.create()) mock_process_attribute.assert_called_once_with(target, target.attrs[2]) mock_create_substitutions.assert_called_once()
def test__eq__(self): attr = AttrFactory.element() clone = attr.clone() self.assertIsNot(attr, clone) self.assertEqual(attr, clone) attr.default = "foo" self.assertEqual(attr, clone) attr.restrictions.length = 10 self.assertEqual(attr, clone) attr.index = -1 self.assertEqual(attr, clone) attr.namespace = __file__ self.assertNotEqual(attr, clone)
def test_compare_attributes(self): source = ClassFactory.elements(2) self.assertEqual(2, ClassUtils.compare_attributes(source, source)) target = ClassFactory.create() self.assertEqual(0, ClassUtils.compare_attributes(source, target)) target.attrs = [attr.clone() for attr in source.attrs] self.assertEqual(2, ClassUtils.compare_attributes(source, target)) source.attrs.append(AttrFactory.element()) self.assertEqual(1, ClassUtils.compare_attributes(source, target)) source.attrs = AttrFactory.list(3) self.assertEqual(0, ClassUtils.compare_attributes(source, target)) self.assertEqual(0, ClassUtils.INCLUDES_NONE) self.assertEqual(1, ClassUtils.INCLUDES_SOME) self.assertEqual(2, ClassUtils.INCLUDES_ALL)
def test_attribute_metadata(self): attr = AttrFactory.element() expected = {"name": "attr_B", "type": "Element"} self.assertEqual(expected, attribute_metadata(attr, None)) self.assertEqual(expected, attribute_metadata(attr, "foo"))
def test_property_is_group(self): self.assertTrue(AttrFactory.group().is_group) self.assertTrue(AttrFactory.attribute_group().is_group) self.assertFalse(AttrFactory.element().is_group)
def test_property_is_enumeration(self): self.assertTrue(AttrFactory.enumeration().is_enumeration) self.assertFalse(AttrFactory.element().is_enumeration)
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_field_metadata(self): attr = AttrFactory.element() expected = {"name": "attr_B", "type": "Element"} self.assertEqual(expected, self.filters.field_metadata(attr, None, [])) self.assertEqual(expected, self.filters.field_metadata(attr, "foo", []))
def test_field_metadata_mixed(self): attr = AttrFactory.element(mixed=True) expected = {"mixed": True, "name": "attr_B", "type": "Element"} self.assertEqual(expected, self.filters.field_metadata(attr, "foo", []))