def test_overwrite_attributes(self): class GF(Factory): name = generators.StaticGenerator('bar') class M(SimpleMockup): factory = GF mockups.register(SimpleModel, M) for obj in mockups.create(SimpleModel, 10): self.assertEqual(obj.name, 'bar') obj = mockups.create_one(SimpleModel) self.assertEqual(obj.name, 'bar')
def test_using_registry(self): mockups.register(SimpleModel, SimpleMockup) models = ("mockups_test.SimpleModel:10",) self.command.handle(*models, **self.options) for obj in SimpleModel.objects.all(): self.assertEqual(obj.name, "foo")
def test_create(self): mockups.register(SimpleModel, SimpleMockup) for obj in mockups.create(SimpleModel, 10): self.assertEqual(obj.name, "foo") obj = mockups.create_one(SimpleModel) self.assertEqual(obj.name, "foo")
def test_registration(self): mockups.register(SimpleModel, SimpleMockup) self.assertTrue(SimpleModel in mockups.helpers._registry) self.assertEqual(mockups.helpers._registry[SimpleModel], SimpleMockup)
#-*- coding: utf-8 -*- import mockups from models import UserProfile from mockups import Mockup, Factory import string import random class UserProfileFactory(Factory): pass class UserProfileMockup(Mockup): # don't follow permissions and groups follow_m2m = False # generate_fk= False factory = UserProfileFactory def post_process_instance(self, instance): instance.user.username = ''.join( (random.choice(string.letters + string.digits) for _ in xrange(random.randint(5, 8)))) instance.user.save() mockups.register(UserProfile, UserProfileMockup, fail_silently=True)
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) ) def unique_email(self, model, instance): if User.objects.filter(email=instance.email): raise mockups.InvalidConstraint(('email',)) def prepare_class(self): self.add_constraint(self.unique_email) def post_process_instance(self, instance): # make sure user's last login was not before he joined if instance.last_login < instance.date_joined: instance.last_login = instance.date_joined if self.password: instance.set_password(self.password) instance.save() return instance mockups.register(User, UserMockup, fail_silently=True)
def test_using_registry(self): mockups.register(SimpleModel, SimpleMockup) models = ('mockups_test.SimpleModel:10',) self.command.handle(*models, **self.options) for obj in SimpleModel.objects.all(): self.assertEqual(obj.name, 'foo')
def test_create(self): mockups.register(SimpleModel, SimpleMockup) for obj in mockups.create(SimpleModel, 10): self.assertEqual(obj.name, 'foo') obj = mockups.create_one(SimpleModel) self.assertEqual(obj.name, 'foo')
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)) def unique_email(self, model, instance): if User.objects.filter(email=instance.email): raise mockups.InvalidConstraint(('email', )) def prepare_class(self): self.add_constraint(self.unique_email) def post_process_instance(self, instance): # make sure user's last login was not before he joined if instance.last_login < instance.date_joined: instance.last_login = instance.date_joined if self.password: instance.set_password(self.password) return instance mockups.register(User, UserMockup, fail_silently=True)