File: test_admin_multidb.py

package info (click to toggle)
python-django 1%3A1.11.29-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,428 kB
  • sloc: python: 220,776; javascript: 13,523; makefile: 209; xml: 201; sh: 64
file content (50 lines) | stat: -rw-r--r-- 1,496 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)