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]" }, { "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_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_set_effective_choices(self): target = ClassFactory.create() attrs = [ AttrFactory.any(), AttrFactory.any(), # first group AttrFactory.any(restrictions=Restrictions(sequential=True)), AttrFactory.any( restrictions=Restrictions(sequential=True, max_occurs=2)), AttrFactory.any( restrictions=Restrictions(sequential=True, max_occurs=2)), # break attr AttrFactory.any(), # second group AttrFactory.any( restrictions=Restrictions(sequential=True, max_occurs=2)), AttrFactory.any( restrictions=Restrictions(sequential=True, max_occurs=1)), AttrFactory.any( restrictions=Restrictions(sequential=True, max_occurs=2)), ] target.attrs.extend(attrs) self.processor.process(target) self.assertIsNone(attrs[0].restrictions.choice) self.assertIsNone(attrs[1].restrictions.choice) # Part of the group but precedes list siblings self.assertIsNone(attrs[2].restrictions.choice) # Part of the group but both are lists self.assertIsNone(attrs[3].restrictions.choice) self.assertIsNone(attrs[4].restrictions.choice) # break attr self.assertIsNone(attrs[5].restrictions.choice) # Second group, mixed list non list sequential elements self.assertEqual("effective_1", attrs[6].restrictions.choice) self.assertEqual("effective_1", attrs[7].restrictions.choice) self.assertEqual("effective_1", attrs[8].restrictions.choice)
def test_property_is_wild_attr(self): attr = AttrFactory.create() self.assertFalse(attr.is_wildcard) attr = AttrFactory.any() self.assertTrue(attr.is_wildcard)