Skip to content

Commit efe74ff

Browse files
hramezanifelixxm
authored andcommitted
Refs #32047 -- Added test for using call_command() with constant required options.
1 parent 0ef04fd commit efe74ff

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django.core.management.base import BaseCommand
2+
3+
4+
class Command(BaseCommand):
5+
def add_arguments(self, parser):
6+
parser.add_argument(
7+
'--append_const',
8+
action='append_const',
9+
const=42,
10+
required=True,
11+
)
12+
parser.add_argument('--const', action='store_const', const=31, required=True)
13+
parser.add_argument('--count', action='count', required=True)
14+
parser.add_argument('--flag_false', action='store_false', required=True)
15+
parser.add_argument('--flag_true', action='store_true', required=True)
16+
17+
def handle(self, *args, **options):
18+
for option, value in options.items():
19+
if value is not None:
20+
self.stdout.write('%s=%s' % (option, value))

tests/user_commands/tests.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,32 @@ def test_mutually_exclusive_group_required_const_options(self):
275275
)
276276
self.assertIn(expected_output, out.getvalue())
277277

278+
def test_required_const_options(self):
279+
args = {
280+
'append_const': [42],
281+
'const': 31,
282+
'count': 1,
283+
'flag_false': False,
284+
'flag_true': True,
285+
}
286+
expected_output = '\n'.join(
287+
'%s=%s' % (arg, value) for arg, value in args.items()
288+
)
289+
out = StringIO()
290+
management.call_command(
291+
'required_constant_option',
292+
'--append_const',
293+
'--const',
294+
'--count',
295+
'--flag_false',
296+
'--flag_true',
297+
stdout=out,
298+
)
299+
self.assertIn(expected_output, out.getvalue())
300+
out.truncate(0)
301+
management.call_command('required_constant_option', **{**args, 'stdout': out})
302+
self.assertIn(expected_output, out.getvalue())
303+
278304
def test_subparser(self):
279305
out = StringIO()
280306
management.call_command('subparser', 'foo', 12, stdout=out)

0 commit comments

Comments
 (0)