summaryrefslogtreecommitdiff
path: root/tests/user_commands
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2020-10-06 00:13:09 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-10-06 08:23:25 +0200
commitefe74fff25e1e85402ecb73d80eea7625246f4ea (patch)
treee2256bed9c4c1b682fffadfba46dd454c77fdca8 /tests/user_commands
parent0ef04fdd7ab75daa59536261bbfc5da4c4e31079 (diff)
downloaddjango-efe74fff25e1e85402ecb73d80eea7625246f4ea.tar.gz
Refs #32047 -- Added test for using call_command() with constant required options.
Diffstat (limited to 'tests/user_commands')
-rw-r--r--tests/user_commands/management/commands/required_constant_option.py20
-rw-r--r--tests/user_commands/tests.py26
2 files changed, 46 insertions, 0 deletions
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)