From c2b5b02966db43a7b276a38641365dd05cbc8660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Fri, 10 Mar 2023 20:17:53 +0100 Subject: [PATCH] add test that will cause error #226 --- admin_tests/admin.py | 6 ++++++ admin_tests/models.py | 19 +++++++++++++++++++ admin_tests/test_admin.py | 19 +++++++++++++++++++ runtests_urls.py | 4 +++- settings.py | 16 +++++++++++++++- 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 admin_tests/admin.py create mode 100644 admin_tests/models.py create mode 100644 admin_tests/test_admin.py diff --git a/admin_tests/admin.py b/admin_tests/admin.py new file mode 100644 index 000000000..dc42133aa --- /dev/null +++ b/admin_tests/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from .models import TestModel + +@admin.register(TestModel) +class TestModelAdmin(admin.ModelAdmin): + list_display = ('name', 'owner') diff --git a/admin_tests/models.py b/admin_tests/models.py new file mode 100644 index 000000000..903bdf6eb --- /dev/null +++ b/admin_tests/models.py @@ -0,0 +1,19 @@ +from django.conf import settings +from django.db.models import Q, UniqueConstraint, Model, CharField, ForeignKey, SET_NULL +from django.db.models.functions import Lower + + +class TestModel(Model): + name = CharField(max_length=20) + owner = ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, + on_delete=SET_NULL) + + class Meta: + ordering = ('name',) + constraints = [ + UniqueConstraint( + Lower("name").desc(), + condition=Q(owner=None), + name="unique_name", + ) + ] diff --git a/admin_tests/test_admin.py b/admin_tests/test_admin.py new file mode 100644 index 000000000..a4cdf1f0e --- /dev/null +++ b/admin_tests/test_admin.py @@ -0,0 +1,19 @@ +from django.test import TestCase +from django.contrib.auth.models import User +from .models import TestModel +from django.test import Client + + +class AdminTestCase(TestCase): + def setUp(self): + self.client = Client() + self.user = User.objects.create(username='admin', is_staff=True, is_superuser=True) + + def test_save_test_model(self): + """ + Model 'TestModel' has UniqueConstraint which caused problems when saving TestModelAdmin in Django >= 4.1 + """ + self.client.force_login(self.user) + response = self.client.post('/admin/tests/test/add/', {'name': 'test', 'public': True}) + self.assertEqual(response.status_code, 302) + self.assertEqual(Test.objects.count(), 1) diff --git a/runtests_urls.py b/runtests_urls.py index b11ff8c6f..4ca4fd1ff 100644 --- a/runtests_urls.py +++ b/runtests_urls.py @@ -1,6 +1,7 @@ import debug_toolbar -from django.urls import re_path, include +from django.urls import path, re_path, include from django.http import HttpResponse +from django.contrib import admin def empty_page(request): @@ -10,4 +11,5 @@ def empty_page(request): urlpatterns = [ re_path(r'^$', empty_page), re_path(r'^__debug__/', include(debug_toolbar.urls)), + path('admin/', admin.site.urls), ] diff --git a/settings.py b/settings.py index 0c1a89ff7..db729a83c 100644 --- a/settings.py +++ b/settings.py @@ -90,9 +90,13 @@ INSTALLED_APPS = [ 'cachalot', + 'cachalot.admin_tests', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.postgres', # Enables the unaccent lookup. + 'django.contrib.sessions', + 'django.contrib.admin', + 'django.contrib.messages', ] MIGRATION_MODULES = { @@ -104,6 +108,12 @@ 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + } }, { 'BACKEND': 'django.template.backends.jinja2.Jinja2', @@ -116,7 +126,11 @@ } ] -MIDDLEWARE = [] +MIDDLEWARE = [ + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', +] PASSWORD_HASHERS = ['django.contrib.auth.hashers.MD5PasswordHasher'] SECRET_KEY = 'it’s not important in tests but we have to set it'