Created
June 27, 2012 08:49
-
-
Save harobed/3002546 to your computer and use it in GitHub Desktop.
How to fill dict attribute with Factory-boy ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserFactory(factory.Factory): | |
FACTORY_FOR = User | |
first_name = factory.InfiniteIterator([ 'Peggie', 'Gabe', 'King', 'Fredy', 'Ole', 'Vernice' ]) | |
last_name = factory.InfiniteIterator([ 'Leannon', 'Barton', 'Sauer', 'Walker', 'Strosin']) | |
username = factory.LazyAttribute( | |
lambda o: '%s.%[email protected]' % (o.first_name.lower(), o.last_name.lower()) | |
) | |
# How can I feed data in dict, metadata isn't another Entities (MongoDB context) | |
metadata = { | |
'a': factory.InfiniteIterator(['foobar', 'foo']) | |
'b': factory.InfiniteIterator(['foobar', 'foo']) | |
} |
rbarrois
commented
Jun 27, 2012
class MetadataFactory(factory.Factory):
FACTORY_FOR = dict
a = factory.InfiniteIterator(['foobar', 'foo'])
b = factory.InfiniteIterator(['foobar', 'foo'])
@classmethod
def _prepare(cls, create, *args, **kwargs):
# HACK: force 'build' mode
return super(MetadataFactory, cls)._prepare(False, *args, **kwargs)
class UserFactory(factory.Factory):
metadata = factory.SubFactory(MetadataFactory)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment