Author: russellm
Date: 2011-08-23 08:36:40 -0700 (Tue, 23 Aug 2011)
New Revision: 16670

Modified:
   django/trunk/tests/modeltests/invalid_models/tests.py
Log:
Corrected the setup and teardown of the refactored invalid_models test so that 
it guarantees that stdout is restored, and purges all the temporary models from 
the app cache after running the test.

Modified: django/trunk/tests/modeltests/invalid_models/tests.py
===================================================================
--- django/trunk/tests/modeltests/invalid_models/tests.py       2011-08-23 
06:31:03 UTC (rev 16669)
+++ django/trunk/tests/modeltests/invalid_models/tests.py       2011-08-23 
15:36:40 UTC (rev 16670)
@@ -1,3 +1,8 @@
+import copy
+
+from django.core.management.validation import get_validation_errors
+from django.db.models.loading import cache, load_app
+from cStringIO import StringIO
 import sys
 
 from django.utils import unittest
@@ -6,26 +11,36 @@
 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
+
+        # This test adds dummy applications to the app cache. These
+        # need to be removed in order to prevent bad interactions
+        # with the flush operation in other tests.
+        self.old_app_models = copy.deepcopy(cache.app_models)
+        self.old_app_store = copy.deepcopy(cache.app_store)
+
+    def tearDown(self):
+        cache.app_models = self.old_app_models
+        cache.app_store = self.old_app_store
+        cache._get_models_cache = {}
+        sys.stdout = self.old_stdout
+
     def test_invalid_models(self):
-        from django.core.management.validation import get_validation_errors
-        from django.db.models.loading import load_app
-        from cStringIO import StringIO
 
         try:
             module = load_app("modeltests.invalid_models.invalid_models")
         except Exception, e:
             self.fail('Unable to load invalid model module')
 
-        # 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.
-        orig_stdout = sys.stdout
-        s = StringIO()
-        sys.stdout = s
-        count = get_validation_errors(s, module)
-        sys.stdout = orig_stdout
-        s.seek(0)
-        error_log = s.read()
+        count = 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')
 
@@ -33,5 +48,3 @@
         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))
-
-

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to