Skip to content

Commit

Permalink
add test for overriding a django param default
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Jan 4, 2024
1 parent 1e2109b commit c13110a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
18 changes: 15 additions & 3 deletions django_typer/tests/test_app/management/commands/dj_params4.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.core.management import CommandError

from typing import Annotated
from typer import Option
from django_typer import TyperCommand, callback, command, types
from django_typer.tests.utils import log_django_parameters

Expand All @@ -8,8 +9,19 @@ class Command(TyperCommand):
help = "Test that django parameters work as expected"

@command(epilog="epilog")
def handle(self, throw: bool = False, verbosity: types.Verbosity = 1):
def handle(
self,
throw: bool = False,
verbosity: types.Verbosity = 1,
traceback: Annotated[
bool,
Option(
help=("Raise on CommandError exceptions"),
rich_help_panel=types.COMMON_PANEL,
),
] = True # this should change the default!
):
assert self.__class__ == Command
log_django_parameters(self, verbosity=verbosity)
log_django_parameters(self, verbosity=verbosity, traceback=traceback)
if throw:
raise CommandError("Test Exception")
13 changes: 10 additions & 3 deletions django_typer/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,16 @@ def test_skip_checks(self):
def test_traceback(self):
for cmd, args in self.commands:
result = run_command(cmd, *args, "--throw")
self.assertFalse("Traceback" in result)
result_tb = run_command(cmd, "--traceback", *args, "--throw")
self.assertTrue("Traceback" in result_tb)
if cmd != 'dj_params4':
self.assertFalse("Traceback" in result)
else:
self.assertTrue("Traceback" in result)
if cmd != 'dj_params4':
result_tb = run_command(cmd, "--traceback", *args, "--throw")
self.assertTrue("Traceback" in result_tb)
else:
result_tb = run_command(cmd, "--no-traceback", *args, "--throw")
self.assertFalse("Traceback" in result_tb)

def test_verbosity(self):
run_command("dj_params3", "cmd1")
Expand Down
18 changes: 9 additions & 9 deletions django_typer/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from typer import Option

_COMMON_PANEL = "Django"
COMMON_PANEL = "Django"


def print_version(context, _, value):
Expand All @@ -28,7 +28,7 @@ def print_version(context, _, value):
help="Show program's version number and exit.",
callback=print_version,
is_eager=True,
rich_help_panel=_COMMON_PANEL,
rich_help_panel=COMMON_PANEL,
),
]

Expand All @@ -42,7 +42,7 @@ def print_version(context, _, value):
show_choices=True,
min=0,
max=3,
rich_help_panel=_COMMON_PANEL,
rich_help_panel=COMMON_PANEL,
),
]

Expand All @@ -54,7 +54,7 @@ def print_version(context, _, value):
'"myproject.settings.main". If this isn\'t provided, the '
"DJANGO_SETTINGS_MODULE environment variable will be used."
),
rich_help_panel=_COMMON_PANEL,
rich_help_panel=COMMON_PANEL,
),
]

Expand All @@ -65,7 +65,7 @@ def print_version(context, _, value):
"A directory to add to the Python path, e.g. "
'"/home/djangoprojects/myproject".'
),
rich_help_panel=_COMMON_PANEL,
rich_help_panel=COMMON_PANEL,
),
]

Expand All @@ -74,7 +74,7 @@ def print_version(context, _, value):
Option(
"--traceback",
help=("Raise on CommandError exceptions"),
rich_help_panel=_COMMON_PANEL,
rich_help_panel=COMMON_PANEL,
),
]

Expand All @@ -83,7 +83,7 @@ def print_version(context, _, value):
Option(
"--no-color",
help=("Don't colorize the command output."),
rich_help_panel=_COMMON_PANEL,
rich_help_panel=COMMON_PANEL,
),
]

Expand All @@ -92,13 +92,13 @@ def print_version(context, _, value):
Option(
"--force-color",
help=("Force colorization of the command output."),
rich_help_panel=_COMMON_PANEL,
rich_help_panel=COMMON_PANEL,
),
]

SkipChecks = Annotated[
bool,
Option(
"--skip-checks", help=("Skip system checks."), rich_help_panel=_COMMON_PANEL
"--skip-checks", help=("Skip system checks."), rich_help_panel=COMMON_PANEL
),
]

0 comments on commit c13110a

Please sign in to comment.