summaryrefslogtreecommitdiff
path: root/tests/invalid_models_tests/tests.py
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-12-18 17:56:11 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2013-12-22 11:39:17 +0100
commitf25fa9d8590c7759c1ec7ffecf1d67be81237674 (patch)
treec0a4a40a5371ad8e0d1c1af8c04295e874170041 /tests/invalid_models_tests/tests.py
parent439b364e1e81371bd87488beed35ce051be8aaaa (diff)
Deprecated load_app().
Adjusted several tests that used it to add apps to the app cache and then attempted to remove them by manipulating attributes directly. Also renamed invalid_models to invalid_models_tests to avoid clashing application labels between the outer and the inner invalid_models applications.
Diffstat (limited to 'tests/invalid_models_tests/tests.py')
-rw-r--r--tests/invalid_models_tests/tests.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/invalid_models_tests/tests.py b/tests/invalid_models_tests/tests.py
new file mode 100644
index 0000000000..4e0cef546b
--- /dev/null
+++ b/tests/invalid_models_tests/tests.py
@@ -0,0 +1,46 @@
+import sys
+import unittest
+
+from django.core.apps import app_cache
+from django.core.management.validation import get_validation_errors
+from django.test.utils import override_settings
+from django.utils.six import StringIO
+
+
+class InvalidModelTestCase(unittest.TestCase):
+ """Import an appliation with invalid models and test the exceptions."""
+
+ def setUp(self):
+ # Make sure sys.stdout is not a tty so that we get errors without
+ # coloring attached (makes matching the results easier). We restore
+ # sys.stderr afterwards.
+ self.old_stdout = sys.stdout
+ self.stdout = StringIO()
+ sys.stdout = self.stdout
+
+ def tearDown(self):
+ sys.stdout = self.old_stdout
+
+ # Technically, this isn't an override -- TEST_SWAPPED_MODEL must be
+ # set to *something* in order for the test to work. However, it's
+ # easier to set this up as an override than to require every developer
+ # to specify a value in their test settings.
+ @override_settings(
+ TEST_SWAPPED_MODEL='invalid_models.ReplacementModel',
+ TEST_SWAPPED_MODEL_BAD_VALUE='not-a-model',
+ TEST_SWAPPED_MODEL_BAD_MODEL='not_an_app.Target',
+ )
+ def test_invalid_models(self):
+ with app_cache._with_app("invalid_models_tests.invalid_models"):
+ module = app_cache.get_app_config("invalid_models").models_module
+ get_validation_errors(self.stdout, module)
+
+ self.stdout.seek(0)
+ error_log = self.stdout.read()
+ actual = error_log.split('\n')
+ expected = module.model_errors.split('\n')
+
+ unexpected = [err for err in actual if err not in expected]
+ missing = [err for err in expected if err not in actual]
+ self.assertFalse(unexpected, "Unexpected Errors: " + '\n'.join(unexpected))
+ self.assertFalse(missing, "Missing Errors: " + '\n'.join(missing))