Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Corrected the setup and teardown of the refactored invalid_models tes…

…t so that it guarantees that stdout is restored, and purges all the temporary models from the app cache after running the test.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16670 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8b87a94357c1e52c9d5aed8c684e1a4f1dcc1c89 1 parent be4acff
Russell Keith-Magee authored

Showing 1 changed file with 28 additions and 15 deletions. Show diff stats Hide diff stats

  1. 43  tests/modeltests/invalid_models/tests.py
43  tests/modeltests/invalid_models/tests.py
... ...
@@ -1,3 +1,8 @@
  1
+import copy
  2
+
  3
+from django.core.management.validation import get_validation_errors
  4
+from django.db.models.loading import cache, load_app
  5
+from cStringIO import StringIO
1 6
 import sys
2 7
 
3 8
 from django.utils import unittest
@@ -6,26 +11,36 @@
6 11
 class InvalidModelTestCase(unittest.TestCase):
7 12
     """Import an appliation with invalid models and test the exceptions."""
8 13
 
  14
+    def setUp(self):
  15
+        # Make sure sys.stdout is not a tty so that we get errors without
  16
+        # coloring attached (makes matching the results easier). We restore
  17
+        # sys.stderr afterwards.
  18
+        self.old_stdout = sys.stdout
  19
+        self.stdout = StringIO()
  20
+        sys.stdout = self.stdout
  21
+
  22
+        # This test adds dummy applications to the app cache. These
  23
+        # need to be removed in order to prevent bad interactions
  24
+        # with the flush operation in other tests.
  25
+        self.old_app_models = copy.deepcopy(cache.app_models)
  26
+        self.old_app_store = copy.deepcopy(cache.app_store)
  27
+
  28
+    def tearDown(self):
  29
+        cache.app_models = self.old_app_models
  30
+        cache.app_store = self.old_app_store
  31
+        cache._get_models_cache = {}
  32
+        sys.stdout = self.old_stdout
  33
+
9 34
     def test_invalid_models(self):
10  
-        from django.core.management.validation import get_validation_errors
11  
-        from django.db.models.loading import load_app
12  
-        from cStringIO import StringIO
13 35
 
14 36
         try:
15 37
             module = load_app("modeltests.invalid_models.invalid_models")
16 38
         except Exception, e:
17 39
             self.fail('Unable to load invalid model module')
18 40
 
19  
-        # Make sure sys.stdout is not a tty so that we get errors without
20  
-        # coloring attached (makes matching the results easier). We restore
21  
-        # sys.stderr afterwards.
22  
-        orig_stdout = sys.stdout
23  
-        s = StringIO()
24  
-        sys.stdout = s
25  
-        count = get_validation_errors(s, module)
26  
-        sys.stdout = orig_stdout
27  
-        s.seek(0)
28  
-        error_log = s.read()
  41
+        count = get_validation_errors(self.stdout, module)
  42
+        self.stdout.seek(0)
  43
+        error_log = self.stdout.read()
29 44
         actual = error_log.split('\n')
30 45
         expected = module.model_errors.split('\n')
31 46
 
@@ -33,5 +48,3 @@ def test_invalid_models(self):
33 48
         missing = [err for err in expected if err not in actual]
34 49
         self.assertFalse(unexpected, "Unexpected Errors: " + '\n'.join(unexpected))
35 50
         self.assertFalse(missing, "Missing Errors: " + '\n'.join(missing))
36  
-
37  
-

0 notes on commit 8b87a94

Please sign in to comment.
Something went wrong with that request. Please try again.