def resourceSetUp(self, *args, **kwargs): password = '******' user_mockup = Mockup(CachipunUser, generate_fk=True) user = user_mockup.create_one() user.set_password(password) user.save() self.assertTrue(self.client.login( username=user.username, password=password ), "Could not login as %s/%s" % (user.username, password) )
def seed(self, model, constraints=None, follow_fk=None, generate_fk=None, follow_m2m=None, factory=None, model_properties=None, commit=True): """Creates and saves an instance of 'model' in the database. The values generated by Mockup class for the fields may not be acceptable. Custom values for the fields can be provided in model_properties. Parameters: ``model``: A model class which is used to create the test data. ``constraints``: A list of callables. The constraints are used to verify if the created model instance may be used. The callable gets the actual model as first and the instance as second parameter. The instance is not populated yet at this moment. The callable may raise an :exc:`InvalidConstraint` exception to indicate which fields violate the constraint. ``follow_fk``: A boolean value indicating if foreign keys should be set to random, already existing, instances of the related model. ``generate_fk``: A boolean which indicates if related models should also be created with random values. The *follow_fk* parameter will be ignored if *generate_fk* is set to ``True``. ``follow_m2m``: A tuple containing minium and maximum of model instances that are assigned to ``ManyToManyField``. No new instances will be created. Default is (1, 5). You can ignore ``ManyToManyField`` fields by setting this parameter to ``False``. ``generate_m2m``: A tuple containing minimum and maximum number of model instance that are newly created and assigned to the ``ManyToManyField``. Default is ``False`` which disables the generation of new related instances. The value of ``follow_m2m`` will be ignored if this parameter is set. ``factory``: A Factory *instance*, overriding the one defined in the Mockup class. ``model_properties``: A dict containing the custom properties for the ``model`` ``commit``: A boolean which is set to True by default and indicates whether the model should be saved to the database or not. """ #if not isinstance(model, models.Model): # raise InvalidModelError("%s is not a valid Django model." % model) if model_properties is None: model_properties = {} # Creates and randomly populates the data mockup = Mockup(model, constraints=constraints, follow_fk=follow_fk, generate_fk=generate_fk, follow_m2m=follow_m2m, factory=factory) created_model = mockup.create_one(commit=commit) # set the attributes of the model as provided in model_properties for key in model_properties.iterkeys(): setattr(created_model, key, model_properties[key]) if commit: created_model.save() return created_model