Ticket #4371: Bug4371_take2.diff

File Bug4371_take2.diff, 3.4 KB (added by Keith Bussell, 17 years ago)

Combined patch for fix and test

  • django/core/management/commands/loaddata.py

     
    5858                else:
    5959                    formats = []
    6060
    61             if verbosity > 0:
    62                 if formats:
     61            if formats:
     62                if verbosity > 0:
    6363                    print "Loading '%s' fixtures..." % fixture_name
    64                 else:
    65                     print "Skipping fixture '%s': %s is not a known serialization format" % (fixture_name, format)
     64            else:
     65                sys.stderr.write(
     66                    self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." %
     67                         (fixture_name, format)))
     68                transaction.rollback()
     69                transaction.leave_transaction_management()
     70                return
    6671
    6772            for fixture_dir in app_fixtures + list(settings.FIXTURE_DIRS) + ['']:
    6873                if verbosity > 1:
     
    109114                        if verbosity > 1:
    110115                            print "No %s fixture '%s' in %s." % \
    111116                                (format, fixture_name, humanize(fixture_dir))
    112 
     117               
    113118        if count[0] > 0:
    114119            sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
    115120            if sequence_sql:
     
    117122                    print "Resetting sequences"
    118123                for line in sequence_sql:
    119124                    cursor.execute(line)
     125        elif count[1] > 0:
     126            # If we found a fixture file to load, but no objects were loaded, assume error.
     127            sys.stderr.write(
     128                self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
     129                    (fixture_name)))
     130            transaction.rollback()
     131            transaction.leave_transaction_management()
     132            return
    120133
    121134        transaction.commit()
    122135        transaction.leave_transaction_management()
  • tests/regressiontests/fixtures_regress/models.py

     
    4949>>> Stuff.objects.all()
    5050[<Stuff: None is owned by None>]
    5151
     52###############################################
     53# Test for ticket #4371 -- fixture loading fails silently in testcases
     54# Validate that error conditions are caught correctly
     55
     56# redirect stderr for the next few tests...
     57>>> import sys
     58>>> savestderr = sys.stderr
     59>>> sys.stderr = sys.stdout
     60
     61# Loading data of an unknown format should fail
     62>>> management.call_command('loaddata', 'bad_fixture1.unkn', verbosity=0)
     63Problem installing fixture 'bad_fixture1': unkn is not a known serialization format.
     64
     65# Loading a fixture file with invalid data using explicit filename
     66>>> management.call_command('loaddata', 'bad_fixture2.xml', verbosity=0)
     67No fixture data found for 'bad_fixture2'. (File format may be invalid.)
     68
     69# Loading a fixture file with invalid data without file extension
     70>>> management.call_command('loaddata', 'bad_fixture2', verbosity=0)
     71No fixture data found for 'bad_fixture2'. (File format may be invalid.)
     72
     73>>> sys.stderr = savestderr
    5274"""}
Back to Top