Ticket #7835: 7835.test_extra_apps.diff

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

Path with suggested new API (installed_apps and extra_apps)

  • django/django/test/testcases.py

     
    88from django.core.management import call_command
    99from django.core.urlresolvers import clear_url_caches
    1010from django.db import transaction
     11from django.db.models.loading import load_app
    1112from django.http import QueryDict
    1213from django.test import _doctest as doctest
    1314from django.test.client import Client
     
    173174    def _pre_setup(self):
    174175        """Performs any pre-test setup. This includes:
    175176
     177            * If the Test Case class has a 'installed_apps' member, replace the
     178              INSTALLED_APPS with it.
     179            * If the Test Case class has a 'extra_apps' member, append it to
     180              INSTALLED_APPS.
    176181            * Flushing the database.
    177182            * If the Test Case class has a 'fixtures' member, installing the
    178183              named fixtures.
     
    180185              ROOT_URLCONF with it.
    181186            * Clearing the mail test outbox.
    182187        """
     188        if hasattr(self, 'installed_apps'):
     189            self._old_installed_apps = settings.INSTALLED_APPS
     190            settings.INSTALLED_APPS = list(self.installed_apps)
     191        if hasattr(self, 'extra_apps'):
     192            if hasattr(self, '_old_installed_apps'):
     193                # extra_apps and installed_apps can coexist
     194                settings.INSTALLED_APPS += list(self.extra_apps)
     195            else:
     196                self._old_installed_apps = settings.INSTALLED_APPS
     197                settings.INSTALLED_APPS += list(self.extra_apps)
     198        if hasattr(self, '_old_installed_apps'):
     199            for app_label in settings.INSTALLED_APPS:
     200                load_app(app_label)
     201            # Create the new tables in db
     202            call_command('syncdb', verbosity=0, interactive=False)
    183203        call_command('flush', verbosity=0, interactive=False)
    184204        if hasattr(self, 'fixtures'):
    185205            # We have to use this slightly awkward syntax due to the fact
     
    220240        """ Performs any post-test things. This includes:
    221241
    222242            * Putting back the original ROOT_URLCONF if it was changed.
     243            * Putting back the original INSTALLED_APPS if it was changed.
    223244        """
    224245        if hasattr(self, '_old_root_urlconf'):
    225246            settings.ROOT_URLCONF = self._old_root_urlconf
     
    224245        if hasattr(self, '_old_root_urlconf'):
    225246            settings.ROOT_URLCONF = self._old_root_urlconf
    226247            clear_url_caches()
     248        if hasattr(self, '_old_installed_apps'):
     249            settings.INSTALLED_APPS = self._old_installed_apps
     250            #TODO: test apps should be unloaded from the app cache, and the extra tables destroyed.
    227251
    228252    def assertRedirects(self, response, expected_url, status_code=302,
    229253                        target_status_code=200, host=None):
Back to Top