예제 #1
0
 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))
예제 #2
0
 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()
예제 #3
0
 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)
예제 #4
0
 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))
예제 #5
0
 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))
예제 #6
0
 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))
예제 #7
0
    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
예제 #8
0
 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))
예제 #9
0
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()
예제 #10
0
 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))
예제 #11
0
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()
예제 #12
0
    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)
예제 #13
0
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)
예제 #14
0
def model_class_name(factory_class):
    """Return model class name for factory class.
    """
    return class_name(model_class(factory_class))
예제 #15
0
def admin_class_name(clasz):
    """
    Format admin class name
    """
    return class_name(clasz) + "Admin"
예제 #16
0
    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)
예제 #17
0

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)
예제 #18
0
def default_name(cls, number):
    """Return a default name for given class.
    """
    return "{}_{}".format(class_name(cls), number)
예제 #19
0
def default_name(cls, number):
    """Return a default name for given class.
    """
    return "{}_{}".format(class_name(cls), number)
예제 #20
0
def model_class_name(factory_class):
    """Return model class name for factory class.
    """
    return class_name(model_class(factory_class))