Refs #32047 -- Added test for using call_command() with constant required options.

This commit is contained in:
Hasan Ramezani 2020-10-06 00:13:09 +02:00 committed by Mariusz Felisiak
parent 0ef04fdd7a
commit efe74fff25
2 changed files with 46 additions and 0 deletions

View File

@ -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))

View File

@ -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)