def verify_clear(self, factory_class, association_name, other_class, contact_attr_name=None, expected_contact_count=1, expected_other_model_count=1, **kwargs): """Verify underlying object state following clear""" instance = self.create_instance(factory_class, **kwargs) contact_attr_name = contact_attr_name or "contact" contact = getattr(instance, contact_attr_name) association = getattr(contact, association_name) association.clear() self.assertEqual(association.count(), 0, "Unexpected association entries") model_class = factory_class.model_class() self.assertEqual(model_class.objects.count(), 0, "unexpected %s instances" % class_name(model_class)) self.assertEqual(models.Contact.objects.count(), expected_contact_count, "unexpected Contact instances") if other_class: self.assertEqual( other_class.objects.count(), expected_other_model_count, "unexpected %s instances" % class_name(other_class))
def verify_named_instances_crud(self, instances, factory_class, get_by_name): """Verify crud operations on created named instances. """ model_class = factory_class.model_class() model_class_name = class_name(model_class) for instance in instances: self.verify_instance(instance) instance.full_clean() instance_count = model_class.objects.count() self.assertEqual( len(instances), instance_count, "Missing %s instances after create" % model_class_name) instance = model_class.objects.get(name=get_by_name) instance.name = 'new name' instance.save() self.assertEqual( instance.version, 2, "%s version mismatch after save" % model_class_name) instance.delete() self.assertEqual( model_class.objects.count() + 1, instance_count, "%s instance mismatch after delete" % model_class_name) model_class.objects.all().delete()
def named_instance(self, name): """Find a named instance. """ try: return self.get(name=name) except self.model.DoesNotExist: logger.warning( 'Failed to retrieve instance of type (%s) named (%s)', class_name(self.model), name) return self.get(name=constants.UNKNOWN)
def verify_other_delete(self, factory_class, attr_name, **kwargs): """Verify delete propagation through other model.""" instance = self.create_instance(factory_class, **kwargs) value = getattr(instance, attr_name) value.delete() model_class = factory_class.model_class() self.assertEqual( model_class.objects.count(), 0, "%s instance mismatch following contact delete" % class_name(model_class))
def verify_contact_delete(self, factory_class, contact_attr_name=None, **kwargs): """Verify contact delete propagation.""" instance = self.create_instance(factory_class, **kwargs) contact_attr_name = contact_attr_name or "contact" contact = getattr(instance, contact_attr_name) contact.delete() model_class = factory_class.model_class() self.assertEqual( model_class.objects.count(), 0, "%s instance mismatch following contact delete" % class_name(model_class))
def verify_create(self, factory_class, **kwargs): """Verify instance creation.""" model_class = factory_class.model_class() model_class_name = class_name(model_class) instance = factory_class(**kwargs) self.verify_instance(instance) instance.full_clean() self.assertEqual( 1, model_class.objects.count(), "Missing %s instances after create" % model_class_name) return instance
def delete_association(association_class, **kwargs): """Remove an association. Requires deletion when using 'through'. """ try: instance = association_class.objects.get(**kwargs) except association_class.DoesNotExist: logger.exception("%s instance with kwargs %s not found", class_name(association_class), kwargs) raise return instance.delete()
def delete_association(association_class, **kwargs): """Remove an association. Requires deletion when using 'through'. """ try: instance = association_class.objects.get(**kwargs) except association_class.DoesNotExist: logger.exception( "%s instance with kwargs %s not found", class_name(association_class), kwargs) raise return instance.delete()
def verify_versioned_model_crud(self, factory_class, **kwargs): """Verify versioned model simple crud operations. """ instance = self.verify_create(factory_class, **kwargs) model_class = factory_class.model_class() model_class_name = class_name(model_class) fetched = model_class.objects.get(pk=instance.id) fetched.save() self.assertEqual( fetched.version, 2, "%s version mismatch after save" % model_class_name) fetched.delete() self.assertEqual( model_class.objects.count(), 0, "%s instance mismatch after delete" % model_class_name)
def many_to_many_field(to_class, db_table=None, **kwargs): """Return a new instance of many to many model field. """ defaults = dict( db_constraint=True) related_name = kwargs.pop('related_name', None) defaults.update(kwargs) if inspect.isclass(to_class): related_name = ( related_name or '{}_set'.format(inflection.camelize(class_name(to_class)))) return models.ManyToManyField( to_class, db_table=db_table, related_name=related_name, **defaults)
def model_class_name(factory_class): """Return model class name for factory class. """ return class_name(model_class(factory_class))
def admin_class_name(clasz): """ Format admin class name """ return class_name(clasz) + "Admin"
user_can_access_owned_objects_only = True user_owned_objects_field = "creation_user" form = forms.UserProfileAdminForm list_display = ("id", "get_username") list_filter = ("user__username", ) ordering = ("id", ) def get_username(self, instance): """return username .""" return instance.user.username get_username.short_description = "username" _named_classes = ( models.ContactType, models.ContactRelationshipType, ) for clasz in _named_classes: admin_site_register(clasz, (NamedModelAdmin, ), named_model_admin_class_attrs(class_name(clasz))) _other_model_classes = (models.Contact, models.UserProfile) _other_admin_classes = (ContactAdmin, UserProfileAdmin) for model_class, admin_class in zip(_other_model_classes, _other_admin_classes): admin.site.register(model_class, admin_class)
class ImageReferenceAdmin(VersionedModelAdmin): """ Image model admin class """ form = forms.ImageReferenceAdminForm list_display = ("id", "image", "url", "version", "update_time", "update_user") list_display_links = ("id", "image", ) limit_qs_to_request_user = True fieldsets = ( ("Image reference", {'fields': _image_reference_fields}),) + _versioned_fields _named_classes = (models.DocumentOrientation, models.ImageFormat, ) for clasz in _named_classes: admin_site_register( clasz, (NamedModelAdmin,), named_model_admin_class_attrs(class_name(clasz))) _other_model_classes = (models.Image, models.ImageReference) _other_admin_classes = (ImageAdmin, ImageReferenceAdmin) for model_class, admin_class in zip(_other_model_classes, _other_admin_classes): admin.site.register(model_class, admin_class)
def default_name(cls, number): """Return a default name for given class. """ return "{}_{}".format(class_name(cls), number)