2011-08-23 23:36:40 +08:00
|
|
|
import copy
|
2011-10-14 02:04:12 +08:00
|
|
|
import sys
|
|
|
|
from cStringIO import StringIO
|
2011-08-23 23:36:40 +08:00
|
|
|
|
|
|
|
from django.core.management.validation import get_validation_errors
|
|
|
|
from django.db.models.loading import cache, load_app
|
2011-08-23 12:25:07 +08:00
|
|
|
|
|
|
|
from django.utils import unittest
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidModelTestCase(unittest.TestCase):
|
|
|
|
"""Import an appliation with invalid models and test the exceptions."""
|
|
|
|
|
2011-08-23 23:36:40 +08:00
|
|
|
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
|
|
|
|
|
2011-08-23 12:25:07 +08:00
|
|
|
def test_invalid_models(self):
|
|
|
|
|
|
|
|
try:
|
|
|
|
module = load_app("modeltests.invalid_models.invalid_models")
|
|
|
|
except Exception, e:
|
|
|
|
self.fail('Unable to load invalid model module')
|
|
|
|
|
2011-08-23 23:36:40 +08:00
|
|
|
count = get_validation_errors(self.stdout, module)
|
|
|
|
self.stdout.seek(0)
|
|
|
|
error_log = self.stdout.read()
|
2011-08-23 12:25:07 +08:00
|
|
|
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))
|