PageRenderTime 34ms CodeModel.GetById 19ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/invalid_models/tests.py

https://code.google.com/p/mango-py/
Python | 50 lines | 32 code | 11 blank | 7 comment | 10 complexity | e764f1e5b63299821aa65411493063bc MD5 | raw file
 1import copy
 2
 3from django.core.management.validation import get_validation_errors
 4from django.db.models.loading import cache, load_app
 5from cStringIO import StringIO
 6import sys
 7
 8from django.utils import unittest
 9
10
11class InvalidModelTestCase(unittest.TestCase):
12    """Import an appliation with invalid models and test the exceptions."""
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
34    def test_invalid_models(self):
35
36        try:
37            module = load_app("modeltests.invalid_models.invalid_models")
38        except Exception, e:
39            self.fail('Unable to load invalid model module')
40
41        count = get_validation_errors(self.stdout, module)
42        self.stdout.seek(0)
43        error_log = self.stdout.read()
44        actual = error_log.split('\n')
45        expected = module.model_errors.split('\n')
46
47        unexpected = [err for err in actual if err not in expected]
48        missing = [err for err in expected if err not in actual]
49        self.assertFalse(unexpected, "Unexpected Errors: " + '\n'.join(unexpected))
50        self.assertFalse(missing, "Missing Errors: " + '\n'.join(missing))