tests.py 2.9 KB

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