diff --git a/mayan/apps/common/tests/mixins.py b/mayan/apps/common/tests/mixins.py index a729e7d280..26c76d0278 100644 --- a/mayan/apps/common/tests/mixins.py +++ b/mayan/apps/common/tests/mixins.py @@ -11,8 +11,8 @@ from django.conf import settings from django.conf.urls import url from django.contrib.contenttypes.models import ContentType from django.core import management -from django.db import connection -from django.db import models +from django.db import connection. models +from django.db.models.signals import post_save, pre_save from django.http import HttpResponse from django.template import Context, Template from django.test.utils import ContextList @@ -176,12 +176,35 @@ class RandomPrimaryKeyModelMonkeyPatchMixin(object): if instance.pk: return self.method_save_original(instance, *args, **kwargs) else: + # Set meta.auto_created to True to have the original save_base + # not send the pre_save signal which would normally send + # the instance without a primary key. Since we assign a random + # primary key any pre_save signal handler that relies on an + # empty primary key will fail. + # The meta.auto_created and manual pre_save sending emulates + # the original behavior. Since meta.auto_created also disables + # the post_save signal we must also send it ourselves. + # This hack work with Django 1.11 .save_base() but can break + # in future versions if that method is updated. + pre_save.send( + sender=instance.__class__, instance=instance, raw=False, + update_fields=None, + ) + instance._meta.auto_created = True instance.pk = RandomPrimaryKeyModelMonkeyPatchMixin.get_unique_primary_key( model=instance._meta.model ) instance.id = instance.pk - return instance.save_base(force_insert=True) + result = instance.save_base(force_insert=True) + instance._meta.auto_created = False + + post_save.send( + sender=instance.__class__, instance=instance, created=True, + update_fields=None, raw=False + ) + + return result setattr(models.Model, 'save', method_save_new) super(RandomPrimaryKeyModelMonkeyPatchMixin, self).setUp()