From 888c86dcf30defe533451bcefc6e09a6e181389f Mon Sep 17 00:00:00 2001 From: Jorge Bastida Date: Sat, 18 May 2013 18:04:45 +0200 Subject: Fixed #20445 -- Raised original exception after command error --- tests/admin_scripts/tests.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'tests/admin_scripts') diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index c8986770f3..f0ef0fb293 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -1305,13 +1305,15 @@ class CommandTypes(AdminScriptTestCase): sys.stderr = err = StringIO() try: command.execute = lambda args: args # This will trigger TypeError - with self.assertRaises(SystemExit): + + # If the Exception is not CommandError it should always + # raise the original exception. + with self.assertRaises(TypeError): command.run_from_argv(['', '']) - err_message = err.getvalue() - # Exceptions other than CommandError automatically output the traceback - self.assertIn("Traceback", err_message) - self.assertIn("TypeError", err_message) + # If the Exception is CommandError and --traceback is not present + # this command should raise a SystemExit and don't print any + # traceback to the stderr. command.execute = raise_command_error err.truncate(0) with self.assertRaises(SystemExit): @@ -1320,12 +1322,12 @@ class CommandTypes(AdminScriptTestCase): self.assertNotIn("Traceback", err_message) self.assertIn("CommandError", err_message) + # If the Exception is CommandError and --traceback is present + # this command should raise the original CommandError as if it + # were not a CommandError. err.truncate(0) - with self.assertRaises(SystemExit): + with self.assertRaises(CommandError): command.run_from_argv(['', '', '--traceback']) - err_message = err.getvalue() - self.assertIn("Traceback (most recent call last)", err_message) - self.assertIn("CommandError", err_message) finally: sys.stderr = old_stderr -- cgit v1.3 From 59235816bd063cb8b24acfc92ae1b5cd09a1f7ba Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 29 May 2013 13:47:49 -0400 Subject: Fixed #20509 - Proper parsing for dumpdata --pks option. Thanks weipin for the report and Baptiste Mispelon for the patch. --- django/core/management/commands/dumpdata.py | 7 ++++--- tests/admin_scripts/tests.py | 19 +++++++++++++++++++ tests/fixtures/tests.py | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) (limited to 'tests/admin_scripts') diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py index b1e06e4255..c5eb1b9a9e 100644 --- a/django/core/management/commands/dumpdata.py +++ b/django/core/management/commands/dumpdata.py @@ -21,8 +21,9 @@ class Command(BaseCommand): help='Use natural keys if they are available.'), make_option('-a', '--all', action='store_true', dest='use_base_manager', default=False, help="Use Django's base manager to dump all models stored in the database, including those that would otherwise be filtered or modified by a custom manager."), - make_option('--pks', dest='primary_keys', action='append', default=[], - help="Only dump objects with given primary keys. Accepts a comma seperated list of keys. This option will only work when you specify one model."), + make_option('--pks', dest='primary_keys', help="Only dump objects with " + "given primary keys. Accepts a comma seperated list of keys. " + "This option will only work when you specify one model."), ) help = ("Output the contents of the database as a fixture of the given " "format (using each model's default manager unless --all is " @@ -44,7 +45,7 @@ class Command(BaseCommand): if pks: primary_keys = pks.split(',') else: - primary_keys = False + primary_keys = [] excluded_apps = set() excluded_models = set() diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index f0ef0fb293..9e6b1f557b 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -1682,3 +1682,22 @@ class DiffSettings(AdminScriptTestCase): out, err = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "### STATIC_URL = None") + +class Dumpdata(AdminScriptTestCase): + """Tests for dumpdata management command.""" + + def setUp(self): + self.write_settings('settings.py') + + def tearDown(self): + self.remove_settings('settings.py') + + def test_pks_parsing(self): + """Regression for #20509 + + Test would raise an exception rather than printing an error message. + """ + args = ['dumpdata', '--pks=1'] + out, err = self.run_manage(args) + self.assertOutput(err, "You can only use --pks option with one model") + self.assertNoOutput(out) diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index 4bf60e988a..1b4d6eeeef 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -27,7 +27,7 @@ class TestCaseFixtureLoadingTests(TestCase): class DumpDataAssertMixin(object): def _dumpdata_assert(self, args, output, format='json', natural_keys=False, - use_base_manager=False, exclude_list=[], primary_keys=[]): + use_base_manager=False, exclude_list=[], primary_keys=''): new_io = six.StringIO() management.call_command('dumpdata', *args, **{'format': format, 'stdout': new_io, -- cgit v1.3