Added basic tests for get_app_config[s].

This commit is contained in:
Aymeric Augustin 2013-12-26 18:03:17 +01:00
parent aef019de61
commit 8925aaf613
2 changed files with 72 additions and 4 deletions

View File

@ -1,6 +1,9 @@
from __future__ import unicode_literals
from django.apps.registry import Apps from django.apps.registry import Apps
from django.db import models from django.db import models
# We're testing app registry presence on load, so this is handy. # We're testing app registry presence on load, so this is handy.
new_apps = Apps() new_apps = Apps()

View File

@ -1,13 +1,33 @@
from __future__ import absolute_import from __future__ import absolute_import, unicode_literals
from django.apps import apps from django.apps import apps
from django.apps.registry import Apps from django.apps.registry import Apps
from django.conf import settings
from django.db import models from django.db import models
from django.test import TestCase from django.test import TestCase, override_settings
from .models import TotallyNormal, SoAlternative, new_apps from .models import TotallyNormal, SoAlternative, new_apps
# Small list with a variety of cases for tests that iterate on installed apps.
# Intentionally not in alphabetical order to check if the order is preserved.
SOME_INSTALLED_APPS = [
'apps.apps.MyAdmin',
'apps.apps.MyAuth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
SOME_INSTALLED_APPS_NAMES = [
'django.contrib.admin',
'django.contrib.auth',
] + SOME_INSTALLED_APPS[2:]
SOME_INSTALLED_APPS_WTH_MODELS_NAMES = SOME_INSTALLED_APPS_NAMES[:4]
class AppsTests(TestCase): class AppsTests(TestCase):
def test_singleton_master(self): def test_singleton_master(self):
@ -33,6 +53,51 @@ class AppsTests(TestCase):
# ready regardless of whether populate_models() has run. # ready regardless of whether populate_models() has run.
self.assertTrue(apps.ready) self.assertTrue(apps.ready)
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
def test_get_app_configs(self):
"""
Tests get_app_configs().
"""
app_configs = apps.get_app_configs()
self.assertListEqual(
[app_config.name for app_config in app_configs],
SOME_INSTALLED_APPS_NAMES)
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
def test_get_app_configs_with_models(self):
"""
Tests get_app_configs(only_with_models_module=True).
"""
app_configs = apps.get_app_configs(only_with_models_module=True)
self.assertListEqual(
[app_config.name for app_config in app_configs],
SOME_INSTALLED_APPS_WTH_MODELS_NAMES)
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
def test_get_app_config(self):
"""
Tests get_app_config().
"""
app_config = apps.get_app_config('admin')
self.assertEqual(app_config.name, 'django.contrib.admin')
app_config = apps.get_app_config('staticfiles')
self.assertEqual(app_config.name, 'django.contrib.staticfiles')
with self.assertRaises(LookupError):
apps.get_app_config('webdesign')
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
def test_get_app_config_with_models(self):
"""
Tests get_app_config(only_with_models_module=True).
"""
app_config = apps.get_app_config('admin', only_with_models_module=True)
self.assertEqual(app_config.name, 'django.contrib.admin')
with self.assertRaises(LookupError):
apps.get_app_config('staticfiles', only_with_models_module=True)
def test_models_py(self): def test_models_py(self):
""" """
Tests that the models in the models.py file were loaded correctly. Tests that the models in the models.py file were loaded correctly.
@ -55,10 +120,10 @@ class AppsTests(TestCase):
'app_label': "apps", 'app_label': "apps",
'apps': new_apps, 'apps': new_apps,
} }
meta = type("Meta", tuple(), meta_contents) meta = type(str("Meta"), tuple(), meta_contents)
body['Meta'] = meta body['Meta'] = meta
body['__module__'] = TotallyNormal.__module__ body['__module__'] = TotallyNormal.__module__
temp_model = type("SouthPonies", (models.Model,), body) temp_model = type(str("SouthPonies"), (models.Model,), body)
# Make sure it appeared in the right place! # Make sure it appeared in the right place!
self.assertEqual( self.assertEqual(
old_models, old_models,