parts/django/tests/regressiontests/app_loading/tests.py
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 import copy
       
     2 import os
       
     3 import sys
       
     4 import time
       
     5 from unittest import TestCase
       
     6 
       
     7 from django.conf import Settings
       
     8 from django.db.models.loading import cache, load_app
       
     9 
       
    10 
       
    11 class InstalledAppsGlobbingTest(TestCase):
       
    12     def setUp(self):
       
    13         self.OLD_SYS_PATH = sys.path[:]
       
    14         sys.path.append(os.path.dirname(os.path.abspath(__file__)))
       
    15         self.OLD_TZ = os.environ.get("TZ")
       
    16 
       
    17     def test_globbing(self):
       
    18         settings = Settings('test_settings')
       
    19         self.assertEquals(settings.INSTALLED_APPS, ['parent.app', 'parent.app1', 'parent.app_2'])
       
    20 
       
    21     def tearDown(self):
       
    22         sys.path = self.OLD_SYS_PATH
       
    23         if hasattr(time, "tzset") and self.OLD_TZ:
       
    24             os.environ["TZ"] = self.OLD_TZ
       
    25             time.tzset()
       
    26 
       
    27 
       
    28 class EggLoadingTest(TestCase):
       
    29 
       
    30     def setUp(self):
       
    31         self.old_path = sys.path[:]
       
    32         self.egg_dir = '%s/eggs' % os.path.dirname(__file__)
       
    33 
       
    34         # This test adds dummy applications to the app cache. These
       
    35         # need to be removed in order to prevent bad interactions
       
    36         # with the flush operation in other tests.
       
    37         self.old_app_models = copy.deepcopy(cache.app_models)
       
    38         self.old_app_store = copy.deepcopy(cache.app_store)
       
    39 
       
    40     def tearDown(self):
       
    41         sys.path = self.old_path
       
    42         cache.app_models = self.old_app_models
       
    43         cache.app_store = self.old_app_store
       
    44 
       
    45     def test_egg1(self):
       
    46         """Models module can be loaded from an app in an egg"""
       
    47         egg_name = '%s/modelapp.egg' % self.egg_dir
       
    48         sys.path.append(egg_name)
       
    49         models = load_app('app_with_models')
       
    50         self.assertFalse(models is None)
       
    51 
       
    52     def test_egg2(self):
       
    53         """Loading an app from an egg that has no models returns no models (and no error)"""
       
    54         egg_name = '%s/nomodelapp.egg' % self.egg_dir
       
    55         sys.path.append(egg_name)
       
    56         models = load_app('app_no_models')
       
    57         self.assertTrue(models is None)
       
    58 
       
    59     def test_egg3(self):
       
    60         """Models module can be loaded from an app located under an egg's top-level package"""
       
    61         egg_name = '%s/omelet.egg' % self.egg_dir
       
    62         sys.path.append(egg_name)
       
    63         models = load_app('omelet.app_with_models')
       
    64         self.assertFalse(models is None)
       
    65 
       
    66     def test_egg4(self):
       
    67         """Loading an app with no models from under the top-level egg package generates no error"""
       
    68         egg_name = '%s/omelet.egg' % self.egg_dir
       
    69         sys.path.append(egg_name)
       
    70         models = load_app('omelet.app_no_models')
       
    71         self.assertTrue(models is None)
       
    72 
       
    73     def test_egg5(self):
       
    74         """Loading an app from an egg that has an import error in its models module raises that error"""
       
    75         egg_name = '%s/brokenapp.egg' % self.egg_dir
       
    76         sys.path.append(egg_name)
       
    77         self.assertRaises(ImportError, load_app, 'broken_app')
       
    78         try:
       
    79             load_app('broken_app')
       
    80         except ImportError, e:
       
    81             # Make sure the message is indicating the actual
       
    82             # problem in the broken app.
       
    83             self.assertTrue("modelz" in e.args[0])