2017-01-19 18:16:04 +01:00
|
|
|
from unittest import mock
|
|
|
|
|
2016-09-14 22:06:39 +02:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.auth.models import User
|
2017-01-19 18:16:04 +01:00
|
|
|
from django.test import TestCase, override_settings
|
2018-12-07 23:52:28 +01:00
|
|
|
from django.urls import path, reverse
|
2016-09-14 22:06:39 +02:00
|
|
|
|
|
|
|
from .models import Book
|
|
|
|
|
|
|
|
|
2017-01-19 08:39:46 +01:00
|
|
|
class Router:
|
2016-09-14 22:06:39 +02:00
|
|
|
target_db = None
|
|
|
|
|
|
|
|
def db_for_read(self, model, **hints):
|
|
|
|
return self.target_db
|
|
|
|
|
|
|
|
db_for_write = db_for_read
|
|
|
|
|
2022-04-14 12:54:27 +02:00
|
|
|
def allow_relation(self, obj1, obj2, **hints):
|
|
|
|
return True
|
|
|
|
|
2016-11-12 18:11:23 +01:00
|
|
|
|
2016-09-14 22:06:39 +02:00
|
|
|
site = admin.AdminSite(name="test_adminsite")
|
|
|
|
site.register(Book)
|
|
|
|
|
|
|
|
urlpatterns = [
|
2018-12-07 23:52:28 +01:00
|
|
|
path("admin/", site.urls),
|
2016-09-14 22:06:39 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF=__name__, DATABASE_ROUTERS=["%s.Router" % __name__])
|
|
|
|
class MultiDatabaseTests(TestCase):
|
2018-07-12 06:12:20 +02:00
|
|
|
databases = {"default", "other"}
|
2016-09-14 22:06:39 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.superusers = {}
|
|
|
|
cls.test_book_ids = {}
|
2019-12-20 20:49:56 +01:00
|
|
|
for db in cls.databases:
|
2016-09-14 22:06:39 +02:00
|
|
|
Router.target_db = db
|
|
|
|
cls.superusers[db] = User.objects.create_superuser(
|
|
|
|
username="admin",
|
|
|
|
password="something",
|
|
|
|
email="test@test.org",
|
|
|
|
)
|
|
|
|
b = Book(name="Test Book")
|
|
|
|
b.save(using=db)
|
|
|
|
cls.test_book_ids[db] = b.id
|
|
|
|
|
|
|
|
@mock.patch("django.contrib.admin.options.transaction")
|
|
|
|
def test_add_view(self, mock):
|
2019-12-20 20:49:56 +01:00
|
|
|
for db in self.databases:
|
2017-03-07 22:00:43 +01:00
|
|
|
with self.subTest(db=db):
|
|
|
|
Router.target_db = db
|
|
|
|
self.client.force_login(self.superusers[db])
|
|
|
|
self.client.post(
|
|
|
|
reverse("test_adminsite:admin_views_book_add"),
|
|
|
|
{"name": "Foobar: 5th edition"},
|
|
|
|
)
|
|
|
|
mock.atomic.assert_called_with(using=db)
|
2016-09-14 22:06:39 +02:00
|
|
|
|
|
|
|
@mock.patch("django.contrib.admin.options.transaction")
|
|
|
|
def test_change_view(self, mock):
|
2019-12-20 20:49:56 +01:00
|
|
|
for db in self.databases:
|
2017-03-07 22:00:43 +01:00
|
|
|
with self.subTest(db=db):
|
|
|
|
Router.target_db = db
|
|
|
|
self.client.force_login(self.superusers[db])
|
|
|
|
self.client.post(
|
|
|
|
reverse(
|
|
|
|
"test_adminsite:admin_views_book_change",
|
|
|
|
args=[self.test_book_ids[db]],
|
|
|
|
),
|
|
|
|
{"name": "Test Book 2: Test more"},
|
|
|
|
)
|
|
|
|
mock.atomic.assert_called_with(using=db)
|
2016-09-14 22:06:39 +02:00
|
|
|
|
|
|
|
@mock.patch("django.contrib.admin.options.transaction")
|
|
|
|
def test_delete_view(self, mock):
|
2019-12-20 20:49:56 +01:00
|
|
|
for db in self.databases:
|
2017-03-07 22:00:43 +01:00
|
|
|
with self.subTest(db=db):
|
|
|
|
Router.target_db = db
|
|
|
|
self.client.force_login(self.superusers[db])
|
|
|
|
self.client.post(
|
|
|
|
reverse(
|
|
|
|
"test_adminsite:admin_views_book_delete",
|
|
|
|
args=[self.test_book_ids[db]],
|
|
|
|
),
|
|
|
|
{"post": "yes"},
|
|
|
|
)
|
|
|
|
mock.atomic.assert_called_with(using=db)
|