mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #26170 -- Made ModelAdmin views run transactions on the correct database.
Thanks juntatalor for the initial patch.
This commit is contained in:
49
tests/auth_tests/test_admin_multidb.py
Normal file
49
tests/auth_tests/test_admin_multidb.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from django.conf.urls import url
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import connections
|
||||
from django.test import TestCase, mock, override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
class Router(object):
|
||||
target_db = None
|
||||
|
||||
def db_for_read(self, model, **hints):
|
||||
return self.target_db
|
||||
|
||||
db_for_write = db_for_read
|
||||
|
||||
site = admin.AdminSite(name='test_adminsite')
|
||||
site.register(User, admin_class=UserAdmin)
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^admin/', site.urls),
|
||||
]
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF=__name__, DATABASE_ROUTERS=['%s.Router' % __name__])
|
||||
class MultiDatabaseTests(TestCase):
|
||||
multi_db = True
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.superusers = {}
|
||||
for db in connections:
|
||||
Router.target_db = db
|
||||
cls.superusers[db] = User.objects.create_superuser(
|
||||
username='admin', password='something', email='test@test.org',
|
||||
)
|
||||
|
||||
@mock.patch('django.contrib.auth.admin.transaction')
|
||||
def test_add_view(self, mock):
|
||||
for db in connections:
|
||||
Router.target_db = db
|
||||
self.client.force_login(self.superusers[db])
|
||||
self.client.post(reverse('test_adminsite:auth_user_add'), {
|
||||
'username': 'some_user',
|
||||
'password1': 'helloworld',
|
||||
'password2': 'helloworld',
|
||||
})
|
||||
mock.atomic.assert_called_with(using=db)
|
||||
Reference in New Issue
Block a user