def test_update_with_public_ignores_factory_attributes(self): """Ensure that a DeclarationDict ignores FACTORY_ keys.""" d = containers.DeclarationDict() d.update_with_public({ 'one': 1, 'FACTORY_FOR': 2, 'FACTORY_ARG_PARAMETERS': 3, }) self.assertEqual(['one'], list(d)) self.assertEqual([1], list(d.values()))
def test_update_with_public(self): d = containers.DeclarationDict() d.update_with_public({ 'one': 1, '_two': 2, 'three': 3, 'classmethod': classmethod(lambda c: 1), 'staticmethod': staticmethod(lambda: 1), }) self.assertEqual(set(['one', 'three']), set(d)) self.assertEqual(set([1, 3]), set(d.values()))
def __new__(cls, class_name, bases, attrs, extra_attrs=None): """Record attributes as a pattern for later instance construction. This is called when a new Factory subclass is defined; it will collect attribute declaration from the class definition. Args: class_name (str): the name of the class being created bases (list of class): the parents of the class being created attrs (str => obj dict): the attributes as defined in the class definition extra_attrs (str => obj dict): extra attributes that should not be included in the factory defaults, even if public. This argument is only provided by extensions of this metaclass. Returns: A new class """ parent_factories = get_factory_bases(bases) if not parent_factories: # If this isn't a subclass of Factory, don't do anything special. return super(BaseFactoryMetaClass, cls).__new__(cls, class_name, bases, attrs) declarations = containers.DeclarationDict() postgen_declarations = containers.PostGenerationDeclarationDict() # Add parent declarations in reverse order. for base in reversed(parent_factories): # Import parent PostGenerationDeclaration postgen_declarations.update_with_public( getattr(base, CLASS_ATTRIBUTE_POSTGEN_DECLARATIONS, {})) # Import all 'public' attributes (avoid those starting with _) declarations.update_with_public( getattr(base, CLASS_ATTRIBUTE_DECLARATIONS, {})) # Import attributes from the class definition non_postgen_attrs = postgen_declarations.update_with_public(attrs) # Store protected/private attributes in 'non_factory_attrs'. non_factory_attrs = declarations.update_with_public(non_postgen_attrs) # Store the DeclarationDict in the attributes of the newly created class non_factory_attrs[CLASS_ATTRIBUTE_DECLARATIONS] = declarations non_factory_attrs[ CLASS_ATTRIBUTE_POSTGEN_DECLARATIONS] = postgen_declarations # Add extra args if provided. if extra_attrs: non_factory_attrs.update(extra_attrs) return super(BaseFactoryMetaClass, cls).__new__(cls, class_name, bases, non_factory_attrs)
def test_insert(self): one = OrderedDeclarationMock() two = 2 three = OrderedDeclarationMock() four = OrderedDeclarationMock() d = containers.DeclarationDict(dict(one=one, two=two, four=four)) self.assertEqual(set(['two', 'one', 'four']), set(d)) d['three'] = three self.assertEqual(set(['two', 'one', 'three', 'four']), set(d))
def test_replace(self): one = OrderedDeclarationMock() two = 2 three = OrderedDeclarationMock() four = OrderedDeclarationMock() d = containers.DeclarationDict(dict(one=one, two=two, three=three)) self.assertEqual(set(['two', 'one', 'three']), set(d)) d['three'] = four self.assertEqual(set(['two', 'one', 'three']), set(d)) self.assertEqual(set([two, one, four]), set(d.values()))
def test_copy(self): one = OrderedDeclarationMock() two = 2 three = OrderedDeclarationMock() four = OrderedDeclarationMock() d = containers.DeclarationDict(dict(one=one, two=two, three=three)) d2 = d.copy({'five': 5}) self.assertEqual(5, d2['five']) self.assertFalse('five' in d) d.pop('one') self.assertEqual(one, d2['one']) d2['two'] = four self.assertEqual(four, d2['two']) self.assertEqual(two, d['two'])
def test_basics(self): one = OrderedDeclarationMock() two = 2 three = OrderedDeclarationMock() d = containers.DeclarationDict(dict(one=one, two=two, three=three)) self.assertTrue('one' in d) self.assertTrue('two' in d) self.assertTrue('three' in d) self.assertEqual(one, d['one']) self.assertEqual(two, d['two']) self.assertEqual(three, d['three']) self.assertEqual(one, d.pop('one')) self.assertFalse('one' in d) d['one'] = one self.assertTrue('one' in d) self.assertEqual(one, d['one']) self.assertEqual(set(['one', 'two', 'three']), set(d))
def declarations(cls, extra): d = containers.DeclarationDict({'one': 1, 'two': la}) d.update(extra) return d