Django

Code

Changeset 2310

Show
Ignore:
Timestamp:
02/16/06 08:07:23 (3 years ago)
Author:
russellm
Message:

magic-removal: Fixes #1343 -- Modified model validator to only validate the model being installed. Includes unit test framework changes to allow validation tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/core/management.py

    r2296 r2310  
    525525    # First, try validating the models. 
    526526    s = StringIO() 
    527     num_errors = get_validation_errors(s
     527    num_errors = get_validation_errors(s, app
    528528    if num_errors: 
    529529        sys.stderr.write("Error: %s couldn't be installed, because there were errors in your model:\n" % app_name) 
     
    560560    # First, try validating the models. 
    561561    s = StringIO() 
    562     num_errors = get_validation_errors(s
     562    num_errors = get_validation_errors(s, app
    563563    if num_errors: 
    564564        sys.stderr.write("Error: %s couldn't be installed, because there were errors in your model:\n" % app_name) 
     
    820820        self.outfile.write("%s.%s: %s\n" % (opts.app_label, opts.module_name, error)) 
    821821 
    822 def get_validation_errors(outfile): 
    823     "Validates all installed models. Writes errors, if any, to outfile. Returns number of errors." 
     822def get_validation_errors(outfile, app=None): 
     823    """ 
     824    Validates all models that are part of the specified app. If no app name is provided,  
     825    validates all models of all installed apps. Writes errors, if any, to outfile.  
     826    Returns number of errors. 
     827    """ 
    824828    from django.db import models 
    825829    from django.db.models.fields.related import RelatedObject 
    826830 
    827831    e = ModelErrorCollection(outfile) 
    828     for cls in models.get_models(): 
     832    for cls in models.get_models(app): 
    829833        opts = cls._meta 
    830834 
     
    865869            if f.rel: 
    866870                rel_opts = f.rel.to._meta 
     871                if f.rel.to not in models.get_models(): 
     872                     e.add(opts, "'%s' field: relates to uninstalled model %s" % (f.name, rel_opts.object_name)) 
     873                     
    867874                rel_name = RelatedObject(f.rel.to, cls, f).OLD_get_accessor_name() 
    868875                if rel_name in [r.name for r in rel_opts.fields]: 
     
    880887            if f.rel: 
    881888                rel_opts = f.rel.to._meta 
     889                if f.rel.to not in models.get_models(): 
     890                    e.add(opts, "'%s' field: has m2m relation with uninstalled model %s" % (f.name, rel_opts.object_name)) 
     891 
    882892                rel_name = RelatedObject(f.rel.to, cls, f).OLD_get_accessor_name() 
    883893                if rel_name in [r.name for r in rel_opts.fields]: 
  • django/branches/magic-removal/tests/runtests.py

    r2279 r2310  
    8383        # Determine which models we're going to test. 
    8484        test_models = get_test_models() 
     85        if 'othertests' in self.which_tests: 
     86            self.which_tests.remove('othertests') 
     87            run_othertests = True 
     88            if not self.which_tests: 
     89                test_models = [] 
     90        else: 
     91            run_othertests = not self.which_tests 
     92             
    8593        if self.which_tests: 
    8694            # Only run the specified tests. 
     
    139147                log_error(model_name, "Error while importing", ''.join(traceback.format_exception(*sys.exc_info())[1:])) 
    140148                continue 
    141             self.output(1, "%s model: Installing" % model_name) 
    142             management.install(mod) 
    143  
    144             # Run the API tests. 
    145             p = doctest.DocTestParser() 
    146             test_namespace = dict([(m._meta.object_name, m) \ 
    147                                     for m in django.db.models.get_models(mod)]) 
    148             dtest = p.get_doctest(mod.API_TESTS, test_namespace, model_name, None, None) 
    149             # Manually set verbose=False, because "-v" command-line parameter 
    150             # has side effects on doctest TestRunner class. 
    151             runner = DjangoDoctestRunner(verbosity_level=verbosity_level, verbose=False) 
    152             self.output(1, "%s model: Running tests" % model_name) 
    153             runner.run(dtest, clear_globs=True, out=sys.stdout.write) 
    154  
    155         if not self.which_tests: 
     149                 
     150            if not getattr(mod, 'error_log', None): 
     151                # Model is not marked as an invalid model 
     152                self.output(1, "%s model: Installing" % model_name) 
     153                management.install(mod) 
     154 
     155                # Run the API tests. 
     156                p = doctest.DocTestParser() 
     157                test_namespace = dict([(m._meta.object_name, m) \ 
     158                                        for m in django.db.models.get_models(mod)]) 
     159                dtest = p.get_doctest(mod.API_TESTS, test_namespace, model_name, None, None) 
     160                # Manually set verbose=False, because "-v" command-line parameter 
     161                # has side effects on doctest TestRunner class. 
     162                runner = DjangoDoctestRunner(verbosity_level=verbosity_level, verbose=False) 
     163                self.output(1, "%s model: Running tests" % model_name) 
     164                runner.run(dtest, clear_globs=True, out=sys.stdout.write) 
     165            else:  
     166                # Check that model known to be invalid is invalid for the right reasons. 
     167                self.output(1, "%s model: Validating" % model_name) 
     168             
     169                from cStringIO import StringIO 
     170                s = StringIO() 
     171                count = management.get_validation_errors(s, mod) 
     172                s.seek(0) 
     173                error_log = s.read() 
     174                expected = len(mod.error_log.split('\n')) - 1 
     175                if error_log != mod.error_log: 
     176                    log_error(model_name, 
     177                        "Validator found %d validation errors, %d expected" % (count, expected), 
     178                        "Expected errors:\n%s\n\nActual errors:\n%s" % (mod.error_log, error_log)) 
     179 
     180 
     181        if run_othertests: 
    156182            # Run the non-model tests in the other tests dir 
    157183            self.output(1, "Running other tests")