From 8925aaf61304c22f5ba86dad5d6b15c277b33cfb Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 26 Dec 2013 18:03:17 +0100 Subject: [PATCH] Added basic tests for get_app_config[s]. --- tests/apps/models.py | 3 ++ tests/apps/tests.py | 73 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/tests/apps/models.py b/tests/apps/models.py index c3f764a70b..a7a9b691c3 100644 --- a/tests/apps/models.py +++ b/tests/apps/models.py @@ -1,6 +1,9 @@ +from __future__ import unicode_literals + from django.apps.registry import Apps from django.db import models + # We're testing app registry presence on load, so this is handy. new_apps = Apps() diff --git a/tests/apps/tests.py b/tests/apps/tests.py index a14199d41c..a597bc955b 100644 --- a/tests/apps/tests.py +++ b/tests/apps/tests.py @@ -1,13 +1,33 @@ -from __future__ import absolute_import +from __future__ import absolute_import, unicode_literals from django.apps import apps from django.apps.registry import Apps +from django.conf import settings from django.db import models -from django.test import TestCase +from django.test import TestCase, override_settings 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): def test_singleton_master(self): @@ -33,6 +53,51 @@ class AppsTests(TestCase): # ready regardless of whether populate_models() has run. 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): """ Tests that the models in the models.py file were loaded correctly. @@ -55,10 +120,10 @@ class AppsTests(TestCase): 'app_label': "apps", 'apps': new_apps, } - meta = type("Meta", tuple(), meta_contents) + meta = type(str("Meta"), tuple(), meta_contents) body['Meta'] = meta 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! self.assertEqual( old_models,