Django

Code

Changeset 7595

Show
Ignore:
Timestamp:
06/08/08 03:21:18 (3 months ago)
Author:
russellm
Message:

Fixed #4371 -- Improved error checking when loading fixtures. Code now catches explicitly named fixture formats that are not supported (e.g, YAML fixtures if you don't have PyYAML installed), and fixtures that are empty (which can happen due to XML tag errors). Thanks to John Shaffer for the suggestion, and Keith Bussell for his work on the fix.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r7586 r7595  
    7979    btoll@bestweb.net 
    8080    Jonathan Buchanan <jonathan.buchanan@gmail.com> 
     81    Keith Bussell <kbussell@gmail.com> 
    8182    Juan Manuel Caicedo <juan.manuel.caicedo@gmail.com> 
    8283    Trevor Caira <trevor@caira.com> 
  • django/trunk/django/core/management/commands/loaddata.py

    r7294 r7595  
    3333        fixture_count = 0 
    3434        object_count = 0 
     35        objects_per_fixture = [] 
    3536        models = set() 
    3637 
     
    6162                    formats = [] 
    6263 
    63             if verbosity >= 2
    64                 if formats
     64            if formats
     65                if verbosity > 1
    6566                    print "Loading '%s' fixtures..." % fixture_name 
    66                 else: 
    67                     print "Skipping fixture '%s': %s is not a known serialization format" % (fixture_name, format) 
     67            else: 
     68                sys.stderr.write( 
     69                    self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." % 
     70                        (fixture_name, format))) 
     71                transaction.rollback() 
     72                transaction.leave_transaction_management() 
     73                return 
    6874 
    6975            if os.path.isabs(fixture_name): 
     
    94100                        else: 
    95101                            fixture_count += 1 
     102                            objects_per_fixture.append(0) 
    96103                            if verbosity > 0: 
    97104                                print "Installing %s fixture '%s' from %s." % \ 
     
    101108                                for obj in objects: 
    102109                                    object_count += 1 
     110                                    objects_per_fixture[-1] += 1 
    103111                                    models.add(obj.object.__class__) 
    104112                                    obj.save() 
     
    118126                            fixture.close() 
    119127                    except: 
    120                         if verbosity >= 2
     128                        if verbosity > 1
    121129                            print "No %s fixture '%s' in %s." % \ 
    122130                                (format, fixture_name, humanize(fixture_dir)) 
    123131 
     132 
     133        # If any of the fixtures we loaded contain 0 objects, assume that an  
     134        # error was encountered during fixture loading. 
     135        if 0 in objects_per_fixture: 
     136            sys.stderr.write( 
     137                self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" % 
     138                    (fixture_name))) 
     139            transaction.rollback() 
     140            transaction.leave_transaction_management() 
     141            return 
     142             
     143        # If we found even one object in a fixture, we need to reset the  
     144        # database sequences. 
    124145        if object_count > 0: 
    125146            sequence_sql = connection.ops.sequence_reset_sql(self.style, models) 
     
    129150                for line in sequence_sql: 
    130151                    cursor.execute(line) 
    131  
     152             
    132153        transaction.commit() 
    133154        transaction.leave_transaction_management() 
    134155 
    135156        if object_count == 0: 
    136             if verbosity >= 2
     157            if verbosity > 1
    137158                print "No fixtures found." 
    138159        else: 
  • django/trunk/tests/regressiontests/fixtures_regress/models.py

    r7145 r7595  
    72721 
    7373 
     74############################################### 
     75# Test for ticket #4371 -- fixture loading fails silently in testcases 
     76# Validate that error conditions are caught correctly 
     77 
     78# redirect stderr for the next few tests... 
     79>>> import sys 
     80>>> savestderr = sys.stderr 
     81>>> sys.stderr = sys.stdout 
     82 
     83# Loading data of an unknown format should fail 
     84>>> management.call_command('loaddata', 'bad_fixture1.unkn', verbosity=0) 
     85Problem installing fixture 'bad_fixture1': unkn is not a known serialization format. 
     86 
     87# Loading a fixture file with invalid data using explicit filename 
     88>>> management.call_command('loaddata', 'bad_fixture2.xml', verbosity=0) 
     89No fixture data found for 'bad_fixture2'. (File format may be invalid.) 
     90 
     91# Loading a fixture file with invalid data without file extension 
     92>>> management.call_command('loaddata', 'bad_fixture2', verbosity=0) 
     93No fixture data found for 'bad_fixture2'. (File format may be invalid.) 
     94 
     95>>> sys.stderr = savestderr 
     96 
    7497"""}