class UserFactory(Factory): username = generators.UUIDGenerator(max_length=30) first_name = generators.LoremWordGenerator(1) last_name = generators.LoremWordGenerator(1) password = generators.StaticGenerator(UNUSABLE_PASSWORD) is_active = generators.StaticGenerator(True) # don't generate admin users is_staff = generators.StaticGenerator(False) is_superuser = generators.StaticGenerator(False) date_joined = generators.DateTimeGenerator(max_date=timezone.now()) last_login = generators.DateTimeGenerator(max_date=timezone.now())
def process_m2m(self, instance, field): # check django's version number to determine how intermediary models # are checked if they are auto created or not. from django import VERSION auto_created_through_model = False through = field.rel.through if VERSION < (1, 2): if through: if isinstance(through, basestring): bits = through.split('.') if len(bits) < 2: bits = [instance._meta.app_label] + bits through = models.get_model(*bits) else: auto_created_through_model = True else: auto_created_through_model = through._meta.auto_created if auto_created_through_model: return self.process_field(instance, field) # if m2m relation has intermediary model: # * only generate relation if 'generate_m2m' is given # * first generate intermediary model and assign a newly created # related model to the foreignkey if field.name in self.generate_m2m: # get fk to related model on intermediary model related_fks = [fk for fk in through._meta.fields if isinstance(fk, related.ForeignKey) and \ fk.rel.to is field.rel.to] self_fks = [fk for fk in through._meta.fields if isinstance(fk, related.ForeignKey) and \ fk.rel.to is self.model] assert len(related_fks) == 1 assert len(self_fks) == 1 related_fk = related_fks[0] self_fk = self_fks[0] min_count, max_count = self.generate_m2m[field.name] related_mockup = get_mockup(field.rel.to) related_generator = generators.InstanceGenerator(related_mockup) through_mockup = get_mockup(through) # update the through mockup factory params = { self_fk.name: generators.StaticGenerator(instance), related_fk.name: generators.InstanceGenerator(related_mockup), } through_mockup.update_fieldname_generator(**params) generator = generators.MultipleInstanceGenerator(through_mockup, min_count=min_count, max_count=max_count) generator.generate()
def __init__(self, *args, **kwargs): ''' By default the password is set to an unusable value, this makes it impossible to login with the generated users. If you want to use for example ``mockups.create_one('auth.User')`` in your unittests to have a user instance which you can use to login with the testing client you can provide a ``username`` and a ``password`` argument. Then you can do something like:: mockups.create_one('auth.User', username='******', password='******'foo', password='******') ''' self.username = kwargs.pop('username', None) self.password = kwargs.pop('password', None) super(UserMockup, self).__init__(*args, **kwargs) if self.username: self.update_fieldname_generator( username=generators.StaticGenerator(self.username))
class GF(Factory): intfield = generators.StaticGenerator(1) chars = generators.ChoiceGenerator(char_values) nullbool = generators.NullBooleanGenerator shortchars = generators.CallableGenerator(lambda: u'ab')
class GF(Factory): name = generators.StaticGenerator('bar')
class SimpleFactory(Factory): name = generators.StaticGenerator('foo')