From efe74fff25e1e85402ecb73d80eea7625246f4ea Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Tue, 6 Oct 2020 00:13:09 +0200 Subject: [PATCH] Refs #32047 -- Added test for using call_command() with constant required options. --- .../commands/required_constant_option.py | 20 ++++++++++++++ tests/user_commands/tests.py | 26 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/user_commands/management/commands/required_constant_option.py diff --git a/tests/user_commands/management/commands/required_constant_option.py b/tests/user_commands/management/commands/required_constant_option.py new file mode 100644 index 0000000000..121bfcc28c --- /dev/null +++ b/tests/user_commands/management/commands/required_constant_option.py @@ -0,0 +1,20 @@ +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument( + '--append_const', + action='append_const', + const=42, + required=True, + ) + parser.add_argument('--const', action='store_const', const=31, required=True) + parser.add_argument('--count', action='count', required=True) + parser.add_argument('--flag_false', action='store_false', required=True) + parser.add_argument('--flag_true', action='store_true', required=True) + + def handle(self, *args, **options): + for option, value in options.items(): + if value is not None: + self.stdout.write('%s=%s' % (option, value)) diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py index fe61a23ccd..e4aeca2600 100644 --- a/tests/user_commands/tests.py +++ b/tests/user_commands/tests.py @@ -275,6 +275,32 @@ class CommandTests(SimpleTestCase): ) self.assertIn(expected_output, out.getvalue()) + def test_required_const_options(self): + args = { + 'append_const': [42], + 'const': 31, + 'count': 1, + 'flag_false': False, + 'flag_true': True, + } + expected_output = '\n'.join( + '%s=%s' % (arg, value) for arg, value in args.items() + ) + out = StringIO() + management.call_command( + 'required_constant_option', + '--append_const', + '--const', + '--count', + '--flag_false', + '--flag_true', + stdout=out, + ) + self.assertIn(expected_output, out.getvalue()) + out.truncate(0) + management.call_command('required_constant_option', **{**args, 'stdout': out}) + self.assertIn(expected_output, out.getvalue()) + def test_subparser(self): out = StringIO() management.call_command('subparser', 'foo', 12, stdout=out)