Changed get_validation_errors to use an app config.

This commit is contained in:
Aymeric Augustin 2013-12-29 21:46:26 +01:00
parent 856aaaf2b1
commit e5bcd1d455
4 changed files with 13 additions and 13 deletions

View File

@ -196,21 +196,21 @@ class CustomUserModelValidationTestCase(TestCase):
def test_required_fields_is_list(self):
"REQUIRED_FIELDS should be a list."
new_io = StringIO()
get_validation_errors(new_io, apps.get_app_config('auth').models_module)
get_validation_errors(new_io, apps.get_app_config('auth'))
self.assertIn("The REQUIRED_FIELDS must be a list or tuple.", new_io.getvalue())
@override_settings(AUTH_USER_MODEL='auth.CustomUserBadRequiredFields')
def test_username_not_in_required_fields(self):
"USERNAME_FIELD should not appear in REQUIRED_FIELDS."
new_io = StringIO()
get_validation_errors(new_io, apps.get_app_config('auth').models_module)
get_validation_errors(new_io, apps.get_app_config('auth'))
self.assertIn("The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.", new_io.getvalue())
@override_settings(AUTH_USER_MODEL='auth.CustomUserNonUniqueUsername')
def test_username_non_unique(self):
"A non-unique USERNAME_FIELD should raise a model validation error."
new_io = StringIO()
get_validation_errors(new_io, apps.get_app_config('auth').models_module)
get_validation_errors(new_io, apps.get_app_config('auth'))
self.assertIn("The USERNAME_FIELD must be unique. Add unique=True to the field parameters.", new_io.getvalue())

View File

@ -141,8 +141,8 @@ class BaseCommand(object):
performed prior to executing the command. Default value is
``True``. To validate an individual application's models
rather than all applications' models, call
``self.validate(app)`` from ``handle()``, where ``app`` is the
application's Python module.
``self.validate(app_config)`` from ``handle()``, where ``app_config``
is the application's configuration provided by the app registry.
``leave_locale_alone``
A boolean indicating whether the locale set in settings should be
@ -304,16 +304,16 @@ class BaseCommand(object):
if saved_locale is not None:
translation.activate(saved_locale)
def validate(self, app=None, display_num_errors=False):
def validate(self, app_config=None, display_num_errors=False):
"""
Validates the given app, raising CommandError for any errors.
If app is None, then this will validate all installed apps.
If app_config is None, then this will validate all installed apps.
"""
from django.core.management.validation import get_validation_errors
s = StringIO()
num_errors = get_validation_errors(s, app)
num_errors = get_validation_errors(s, app_config)
if num_errors:
s.seek(0)
error_text = s.read()

View File

@ -20,7 +20,7 @@ class ModelErrorCollection:
self.outfile.write(self.style.ERROR(force_str("%s: %s\n" % (context, error))))
def get_validation_errors(outfile, app=None):
def get_validation_errors(outfile, app_config=None):
"""
Validates all models that are part of the specified app. If no app name is provided,
validates all models of all installed apps. Writes errors, if any, to outfile.
@ -32,7 +32,7 @@ def get_validation_errors(outfile, app=None):
e = ModelErrorCollection(outfile)
for cls in apps.get_models(app, include_swapped=True):
for cls in (app_config or apps).get_models(include_swapped=True):
opts = cls._meta
# Check swappable attribute.

View File

@ -32,13 +32,13 @@ class InvalidModelTestCase(unittest.TestCase):
TEST_SWAPPED_MODEL_BAD_MODEL='not_an_app.Target',
)
def test_invalid_models(self):
module = apps.get_app_config("invalid_models").models_module
get_validation_errors(self.stdout, module)
app_config = apps.get_app_config("invalid_models")
get_validation_errors(self.stdout, app_config)
self.stdout.seek(0)
error_log = self.stdout.read()
actual = error_log.split('\n')
expected = module.model_errors.split('\n')
expected = app_config.models_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]