summaryrefslogtreecommitdiff
path: root/tests/app_loading
diff options
context:
space:
mode:
authorCarl Meyer <carl@oddbird.net>2014-01-25 22:50:40 -0700
committerCarl Meyer <carl@oddbird.net>2014-01-25 22:50:40 -0700
commitca95f8e4359325567fa441eef8f18cb710850eeb (patch)
tree1f1ae7e470570a5c634d564a577740de3f2c77f1 /tests/app_loading
parent8bc3780b67cc37dec04d622833dfa3a26c38fa84 (diff)
downloaddjango-ca95f8e4359325567fa441eef8f18cb710850eeb.tar.gz
Moved sys.path-extending decorator to django.test.utils and used throughout test suite.
Thanks Aymeric for the suggestion.
Diffstat (limited to 'tests/app_loading')
-rw-r--r--tests/app_loading/tests.py44
1 files changed, 21 insertions, 23 deletions
diff --git a/tests/app_loading/tests.py b/tests/app_loading/tests.py
index 63f47fa5fa..71f1a49b63 100644
--- a/tests/app_loading/tests.py
+++ b/tests/app_loading/tests.py
@@ -1,10 +1,10 @@
from __future__ import unicode_literals
import os
-import sys
from django.apps import apps
from django.test import TestCase
+from django.test.utils import extend_sys_path
from django.utils._os import upath
from django.utils import six
@@ -12,56 +12,54 @@ from django.utils import six
class EggLoadingTest(TestCase):
def setUp(self):
- self.old_path = sys.path[:]
self.egg_dir = '%s/eggs' % os.path.dirname(upath(__file__))
def tearDown(self):
apps.clear_cache()
- sys.path = self.old_path
def test_egg1(self):
"""Models module can be loaded from an app in an egg"""
egg_name = '%s/modelapp.egg' % self.egg_dir
- sys.path.append(egg_name)
- with self.settings(INSTALLED_APPS=['app_with_models']):
- models_module = apps.get_app_config('app_with_models').models_module
- self.assertIsNotNone(models_module)
+ with extend_sys_path(egg_name):
+ with self.settings(INSTALLED_APPS=['app_with_models']):
+ models_module = apps.get_app_config('app_with_models').models_module
+ self.assertIsNotNone(models_module)
del apps.all_models['app_with_models']
def test_egg2(self):
"""Loading an app from an egg that has no models returns no models (and no error)"""
egg_name = '%s/nomodelapp.egg' % self.egg_dir
- sys.path.append(egg_name)
- with self.settings(INSTALLED_APPS=['app_no_models']):
- models_module = apps.get_app_config('app_no_models').models_module
- self.assertIsNone(models_module)
+ with extend_sys_path(egg_name):
+ with self.settings(INSTALLED_APPS=['app_no_models']):
+ models_module = apps.get_app_config('app_no_models').models_module
+ self.assertIsNone(models_module)
del apps.all_models['app_no_models']
def test_egg3(self):
"""Models module can be loaded from an app located under an egg's top-level package"""
egg_name = '%s/omelet.egg' % self.egg_dir
- sys.path.append(egg_name)
- with self.settings(INSTALLED_APPS=['omelet.app_with_models']):
- models_module = apps.get_app_config('app_with_models').models_module
- self.assertIsNotNone(models_module)
+ with extend_sys_path(egg_name):
+ with self.settings(INSTALLED_APPS=['omelet.app_with_models']):
+ models_module = apps.get_app_config('app_with_models').models_module
+ self.assertIsNotNone(models_module)
del apps.all_models['app_with_models']
def test_egg4(self):
"""Loading an app with no models from under the top-level egg package generates no error"""
egg_name = '%s/omelet.egg' % self.egg_dir
- sys.path.append(egg_name)
- with self.settings(INSTALLED_APPS=['omelet.app_no_models']):
- models_module = apps.get_app_config('app_no_models').models_module
- self.assertIsNone(models_module)
+ with extend_sys_path(egg_name):
+ with self.settings(INSTALLED_APPS=['omelet.app_no_models']):
+ models_module = apps.get_app_config('app_no_models').models_module
+ self.assertIsNone(models_module)
del apps.all_models['app_no_models']
def test_egg5(self):
"""Loading an app from an egg that has an import error in its models module raises that error"""
egg_name = '%s/brokenapp.egg' % self.egg_dir
- sys.path.append(egg_name)
- with six.assertRaisesRegex(self, ImportError, 'modelz'):
- with self.settings(INSTALLED_APPS=['broken_app']):
- pass
+ with extend_sys_path(egg_name):
+ with six.assertRaisesRegex(self, ImportError, 'modelz'):
+ with self.settings(INSTALLED_APPS=['broken_app']):
+ pass
class GetModelsTest(TestCase):