Code

Ticket #7835: 7835.test_extra_apps.diff

File 7835.test_extra_apps.diff, 3.0 KB (added by julien, 5 years ago)

Path with suggested new API (installed_apps and extra_apps)

Line 
1Index: django/django/test/testcases.py
2===================================================================
3--- django/django/test/testcases.py     (revision 9534)
4+++ django/django/test/testcases.py     (working copy)
5@@ -8,6 +8,7 @@
6 from django.core.management import call_command
7 from django.core.urlresolvers import clear_url_caches
8 from django.db import transaction
9+from django.db.models.loading import load_app
10 from django.http import QueryDict
11 from django.test import _doctest as doctest
12 from django.test.client import Client
13@@ -173,6 +174,10 @@
14     def _pre_setup(self):
15         """Performs any pre-test setup. This includes:
16 
17+            * If the Test Case class has a 'installed_apps' member, replace the
18+              INSTALLED_APPS with it.
19+            * If the Test Case class has a 'extra_apps' member, append it to
20+              INSTALLED_APPS.
21             * Flushing the database.
22             * If the Test Case class has a 'fixtures' member, installing the
23               named fixtures.
24@@ -180,6 +185,21 @@
25               ROOT_URLCONF with it.
26             * Clearing the mail test outbox.
27         """
28+        if hasattr(self, 'installed_apps'):
29+            self._old_installed_apps = settings.INSTALLED_APPS
30+            settings.INSTALLED_APPS = list(self.installed_apps)
31+        if hasattr(self, 'extra_apps'):
32+            if hasattr(self, '_old_installed_apps'):
33+                # extra_apps and installed_apps can coexist
34+                settings.INSTALLED_APPS += list(self.extra_apps)
35+            else:
36+                self._old_installed_apps = settings.INSTALLED_APPS
37+                settings.INSTALLED_APPS += list(self.extra_apps)
38+        if hasattr(self, '_old_installed_apps'):
39+            for app_label in settings.INSTALLED_APPS:
40+                load_app(app_label)
41+            # Create the new tables in db
42+            call_command('syncdb', verbosity=0, interactive=False)
43         call_command('flush', verbosity=0, interactive=False)
44         if hasattr(self, 'fixtures'):
45             # We have to use this slightly awkward syntax due to the fact
46@@ -220,6 +240,7 @@
47         """ Performs any post-test things. This includes:
48 
49             * Putting back the original ROOT_URLCONF if it was changed.
50+            * Putting back the original INSTALLED_APPS if it was changed.
51         """
52         if hasattr(self, '_old_root_urlconf'):
53             settings.ROOT_URLCONF = self._old_root_urlconf
54@@ -224,6 +245,9 @@
55         if hasattr(self, '_old_root_urlconf'):
56             settings.ROOT_URLCONF = self._old_root_urlconf
57             clear_url_caches()
58+        if hasattr(self, '_old_installed_apps'):
59+            settings.INSTALLED_APPS = self._old_installed_apps
60+            #TODO: test apps should be unloaded from the app cache, and the extra tables destroyed.
61 
62     def assertRedirects(self, response, expected_url, status_code=302,
63                         target_status_code=200, host=None):