1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #13636 -- Migrated fixtures tests to use unittests, eliminating another set of expensive flush calls. Thanks to Eric Holscher for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13319 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee
2010-06-05 05:26:04 +00:00
parent 3e1e04d6d0
commit 5da6aeba17
5 changed files with 314 additions and 255 deletions

View File

@@ -213,6 +213,8 @@ class BaseCommand(object):
sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
sys.exit(1)
try:
self.stdout = options.get('stdout', sys.stdout)
self.stderr = options.get('stderr', sys.stderr)
if self.requires_model_validation:
self.validate()
output = self.handle(*args, **options)
@@ -223,12 +225,12 @@ class BaseCommand(object):
from django.db import connections, DEFAULT_DB_ALIAS
connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
if connection.ops.start_transaction_sql():
print self.style.SQL_KEYWORD(connection.ops.start_transaction_sql())
print output
self.stdout.write(self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()))
self.stdout.write(output)
if self.output_transaction:
print self.style.SQL_KEYWORD(connection.ops.end_transaction_sql())
self.stdout.write(self.style.SQL_KEYWORD("COMMIT;") + '\n')
except CommandError, e:
sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
self.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
sys.exit(1)
def validate(self, app=None, display_num_errors=False):
@@ -250,7 +252,7 @@ class BaseCommand(object):
error_text = s.read()
raise CommandError("One or more models did not validate:\n%s" % error_text)
if display_num_errors:
print "%s error%s found" % (num_errors, num_errors != 1 and 's' or '')
self.stdout.write("%s error%s found\n" % (num_errors, num_errors != 1 and 's' or ''))
def handle(self, *args, **options):
"""

View File

@@ -112,10 +112,10 @@ class Command(BaseCommand):
if formats:
if verbosity > 1:
print "Loading '%s' fixtures..." % fixture_name
self.stdout.write("Loading '%s' fixtures...\n" % fixture_name)
else:
sys.stderr.write(
self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." %
self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format.\n" %
(fixture_name, format)))
transaction.rollback(using=using)
transaction.leave_transaction_management(using=using)
@@ -128,7 +128,7 @@ class Command(BaseCommand):
for fixture_dir in fixture_dirs:
if verbosity > 1:
print "Checking %s for fixtures..." % humanize(fixture_dir)
self.stdout.write("Checking %s for fixtures...\n" % humanize(fixture_dir))
label_found = False
for combo in product([using, None], formats, compression_formats):
@@ -141,16 +141,16 @@ class Command(BaseCommand):
)
if verbosity > 1:
print "Trying %s for %s fixture '%s'..." % \
(humanize(fixture_dir), file_name, fixture_name)
self.stdout.write("Trying %s for %s fixture '%s'...\n" % \
(humanize(fixture_dir), file_name, fixture_name))
full_path = os.path.join(fixture_dir, file_name)
open_method = compression_types[compression_format]
try:
fixture = open_method(full_path, 'r')
if label_found:
fixture.close()
print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
(fixture_name, humanize(fixture_dir)))
self.stderr.write(self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting.\n" %
(fixture_name, humanize(fixture_dir))))
transaction.rollback(using=using)
transaction.leave_transaction_management(using=using)
return
@@ -158,8 +158,8 @@ class Command(BaseCommand):
fixture_count += 1
objects_in_fixture = 0
if verbosity > 0:
print "Installing %s fixture '%s' from %s." % \
(format, fixture_name, humanize(fixture_dir))
self.stdout.write("Installing %s fixture '%s' from %s.\n" % \
(format, fixture_name, humanize(fixture_dir)))
try:
objects = serializers.deserialize(format, fixture, using=using)
for obj in objects:
@@ -190,7 +190,7 @@ class Command(BaseCommand):
# error was encountered during fixture loading.
if objects_in_fixture == 0:
sys.stderr.write(
self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)\n" %
(fixture_name)))
transaction.rollback(using=using)
transaction.leave_transaction_management(using=using)
@@ -198,8 +198,8 @@ class Command(BaseCommand):
except Exception, e:
if verbosity > 1:
print "No %s fixture '%s' in %s." % \
(format, fixture_name, humanize(fixture_dir))
self.stdout.write("No %s fixture '%s' in %s.\n" % \
(format, fixture_name, humanize(fixture_dir)))
# If we found even one object in a fixture, we need to reset the
# database sequences.
@@ -207,7 +207,7 @@ class Command(BaseCommand):
sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
if sequence_sql:
if verbosity > 1:
print "Resetting sequences"
self.stdout.write("Resetting sequences\n")
for line in sequence_sql:
cursor.execute(line)
@@ -217,10 +217,10 @@ class Command(BaseCommand):
if object_count == 0:
if verbosity > 0:
print "No fixtures found."
self.stdout.write("No fixtures found.\n")
else:
if verbosity > 0:
print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)
self.stdout.write("Installed %d object(s) from %d fixture(s)\n" % (object_count, fixture_count))
# Close the DB connection. This is required as a workaround for an
# edge case in MySQL: if the same connection is used to