mirror of
https://github.com/Hopiu/django-cachalot.git
synced 2026-03-16 21:30:23 +00:00
* Update utils.py verify get_meta isn't none before requesting db_table * Add passenv to tox.ini * Fix test_explain in sqlite * add test that will cause error #226 * try to get around problem with PASSWORD in GitHub actions testing * fix tests broken not counting with other applications permissions --------- Co-authored-by: hho6643 <63743025+hho6643@users.noreply.github.com> Co-authored-by: Andrew Chen Wang <60190294+Andrew-Chen-Wang@users.noreply.github.com>
This commit is contained in:
parent
866b662273
commit
03f675c96f
10 changed files with 118 additions and 5 deletions
0
cachalot/admin_tests/__init__.py
Normal file
0
cachalot/admin_tests/__init__.py
Normal file
6
cachalot/admin_tests/admin.py
Normal file
6
cachalot/admin_tests/admin.py
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
from django.contrib import admin
|
||||||
|
from .models import TestModel
|
||||||
|
|
||||||
|
@admin.register(TestModel)
|
||||||
|
class TestModelAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('name', 'owner')
|
||||||
52
cachalot/admin_tests/migrations/0001_initial.py
Normal file
52
cachalot/admin_tests/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
# Generated by Django 4.1.7 on 2023-03-10 19:33
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
import django.db.models.functions.text
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="TestModel",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.AutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("name", models.CharField(max_length=20)),
|
||||||
|
(
|
||||||
|
"owner",
|
||||||
|
models.ForeignKey(
|
||||||
|
blank=True,
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.SET_NULL,
|
||||||
|
to=settings.AUTH_USER_MODEL,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
"ordering": ("name",),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
migrations.AddConstraint(
|
||||||
|
model_name="testmodel",
|
||||||
|
constraint=models.UniqueConstraint(
|
||||||
|
fields=["name"],
|
||||||
|
condition=models.Q(owner=None),
|
||||||
|
name="unique_name",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
||||||
0
cachalot/admin_tests/migrations/__init__.py
Normal file
0
cachalot/admin_tests/migrations/__init__.py
Normal file
18
cachalot/admin_tests/models.py
Normal file
18
cachalot/admin_tests/models.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db.models import Q, UniqueConstraint, Model, CharField, ForeignKey, SET_NULL
|
||||||
|
|
||||||
|
|
||||||
|
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(
|
||||||
|
fields=["name"],
|
||||||
|
condition=Q(owner=None),
|
||||||
|
name="unique_name",
|
||||||
|
)
|
||||||
|
]
|
||||||
19
cachalot/admin_tests/test_admin.py
Normal file
19
cachalot/admin_tests/test_admin.py
Normal file
|
|
@ -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/admin_tests/testmodel/add/', {'name': 'test', 'public': True})
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertEqual(TestModel.objects.count(), 1)
|
||||||
|
|
@ -52,12 +52,13 @@ class ReadTestCase(TestUtilsMixin, TransactionTestCase):
|
||||||
self.group__permissions = list(Permission.objects.all()[:3])
|
self.group__permissions = list(Permission.objects.all()[:3])
|
||||||
self.group.permissions.add(*self.group__permissions)
|
self.group.permissions.add(*self.group__permissions)
|
||||||
self.user = User.objects.create_user('user')
|
self.user = User.objects.create_user('user')
|
||||||
self.user__permissions = list(Permission.objects.all()[3:6])
|
self.user__permissions = list(Permission.objects.filter(content_type__app_label='auth')[3:6])
|
||||||
self.user.groups.add(self.group)
|
self.user.groups.add(self.group)
|
||||||
self.user.user_permissions.add(*self.user__permissions)
|
self.user.user_permissions.add(*self.user__permissions)
|
||||||
self.admin = User.objects.create_superuser('admin', 'admin@test.me',
|
self.admin = User.objects.create_superuser('admin', 'admin@test.me',
|
||||||
'password')
|
'password')
|
||||||
self.t1__permission = (Permission.objects.order_by('?')
|
self.t1__permission = (Permission.objects
|
||||||
|
.order_by('?')
|
||||||
.select_related('content_type')[0])
|
.select_related('content_type')[0])
|
||||||
self.t1 = Test.objects.create(
|
self.t1 = Test.objects.create(
|
||||||
name='test1', owner=self.user,
|
name='test1', owner=self.user,
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,6 @@ if __name__ == '__main__':
|
||||||
django.setup()
|
django.setup()
|
||||||
from django.test.runner import DiscoverRunner
|
from django.test.runner import DiscoverRunner
|
||||||
test_runner = DiscoverRunner(verbosity=2, interactive=False)
|
test_runner = DiscoverRunner(verbosity=2, interactive=False)
|
||||||
failures = test_runner.run_tests(['cachalot.tests'])
|
failures = test_runner.run_tests(['cachalot.tests', 'cachalot.admin_tests'])
|
||||||
if failures:
|
if failures:
|
||||||
sys.exit(failures)
|
sys.exit(failures)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import debug_toolbar
|
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.http import HttpResponse
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
|
||||||
def empty_page(request):
|
def empty_page(request):
|
||||||
|
|
@ -10,4 +11,5 @@ def empty_page(request):
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
re_path(r'^$', empty_page),
|
re_path(r'^$', empty_page),
|
||||||
re_path(r'^__debug__/', include(debug_toolbar.urls)),
|
re_path(r'^__debug__/', include(debug_toolbar.urls)),
|
||||||
|
path('admin/', admin.site.urls),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
17
settings.py
17
settings.py
|
|
@ -12,6 +12,7 @@ DATABASES = {
|
||||||
'ENGINE': 'django.db.backends.postgresql',
|
'ENGINE': 'django.db.backends.postgresql',
|
||||||
'NAME': 'cachalot',
|
'NAME': 'cachalot',
|
||||||
'USER': 'cachalot',
|
'USER': 'cachalot',
|
||||||
|
'PASSWORD': 'password',
|
||||||
'HOST': '127.0.0.1',
|
'HOST': '127.0.0.1',
|
||||||
},
|
},
|
||||||
'mysql': {
|
'mysql': {
|
||||||
|
|
@ -90,9 +91,13 @@ elif DEFAULT_CACHE_ALIAS == 'pylibmc':
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
'cachalot',
|
'cachalot',
|
||||||
|
'cachalot.admin_tests',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
'django.contrib.postgres', # Enables the unaccent lookup.
|
'django.contrib.postgres', # Enables the unaccent lookup.
|
||||||
|
'django.contrib.sessions',
|
||||||
|
'django.contrib.admin',
|
||||||
|
'django.contrib.messages',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIGRATION_MODULES = {
|
MIGRATION_MODULES = {
|
||||||
|
|
@ -104,6 +109,12 @@ TEMPLATES = [
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
'DIRS': [],
|
'DIRS': [],
|
||||||
'APP_DIRS': True,
|
'APP_DIRS': True,
|
||||||
|
'OPTIONS': {
|
||||||
|
'context_processors': [
|
||||||
|
'django.contrib.auth.context_processors.auth',
|
||||||
|
'django.contrib.messages.context_processors.messages',
|
||||||
|
],
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.jinja2.Jinja2',
|
'BACKEND': 'django.template.backends.jinja2.Jinja2',
|
||||||
|
|
@ -116,7 +127,11 @@ TEMPLATES = [
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = []
|
MIDDLEWARE = [
|
||||||
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
|
]
|
||||||
PASSWORD_HASHERS = ['django.contrib.auth.hashers.MD5PasswordHasher']
|
PASSWORD_HASHERS = ['django.contrib.auth.hashers.MD5PasswordHasher']
|
||||||
SECRET_KEY = 'it’s not important in tests but we have to set it'
|
SECRET_KEY = 'it’s not important in tests but we have to set it'
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue