11import pytest
22from django .core .management import call_command
33from django .core .management .base import CommandError
4+ from ellar .common .constants import MODULE_METADATA
5+ from ellar .reflect import reflect
6+
7+ from ellar_django .module import DjangoModule , _default_blacklisted_commands
8+
9+ HELP_OUTPUT = """Usage: django [OPTIONS] COMMAND [ARGS]...
10+
11+ Ellar Django Commands
12+
13+ Options:
14+ -v, --version Show the version and exit.
15+ --help Show this message and exit.
16+
17+ Commands:
18+ changepassword [django] Change a user's password for...
19+ check [django] Checks the entire Django project for...
20+ clearsessions [django] Can be run as a cronjob or directly...
21+ collectstatic [django] Collect static files in a single...
22+ compilemessages [django] Compiles .po files to .mo files for...
23+ createcachetable [django] Creates the tables needed to use the...
24+ createsuperuser [django] Used to create a superuser.
25+ dbshell [django] Runs the command-line client for...
26+ diffsettings [django] Displays differences between the...
27+ dumpdata [django] Output the contents of the database...
28+ findstatic [django] Finds the absolute paths for the...
29+ flush [django] Removes ALL DATA from the database,...
30+ inspectdb [django] Introspects the database tables in...
31+ loaddata [django] Installs the named fixture(s) in the...
32+ makemessages [django] Runs over the entire source tree of...
33+ makemigrations [django] Creates new migration(s) for apps.
34+ migrate [django] Updates database schema.
35+ optimizemigration [django] Optimizes the operations for the...
36+ remove_stale_contenttypes [django]
37+ sendtestemail [django] Sends a test email to the email...
38+ shell [django] Runs a Python interactive interpreter.
39+ showmigrations [django] Shows all available migrations for...
40+ sqlflush [django] Returns a list of the SQL statements...
41+ sqlmigrate [django] Prints the SQL statements for the...
42+ sqlsequencereset [django] Prints the SQL statements for...
43+ squashmigrations [django] Squashes an existing set of...
44+ test [django] Discover and run tests in the...
45+ testserver [django] Runs a development server with data...
46+ """
447
548
649def test_command_succeeds () -> None :
@@ -10,3 +53,77 @@ def test_command_succeeds() -> None:
1053def test_nonexistent_command_fails () -> None :
1154 with pytest .raises (CommandError , match = "Unknown command" ):
1255 call_command ("nonexistent_command" )
56+
57+
58+ def test_command_help (cli_runner ):
59+ with reflect .context ():
60+ DjangoModule .setup (
61+ settings_module = "example_app.wsgi_django.settings"
62+ ).apply_configuration ()
63+ django_command_group = reflect .get_metadata (
64+ MODULE_METADATA .COMMANDS , DjangoModule
65+ )[0 ]
66+
67+ res = cli_runner .invoke (django_command_group , ["--help" ])
68+ assert res .exit_code == 0
69+ assert res .stdout == HELP_OUTPUT
70+
71+
72+ def test_check_help (cli_runner ):
73+ with reflect .context ():
74+ DjangoModule .setup (
75+ settings_module = "example_app.wsgi_django.settings"
76+ ).apply_configuration ()
77+ django_command_group = reflect .get_metadata (
78+ MODULE_METADATA .COMMANDS , DjangoModule
79+ )[0 ]
80+
81+ res = cli_runner .invoke (django_command_group , ["check" , "--help" ])
82+ assert res .exit_code == 0
83+ assert (
84+ "usage: manage.py check [-h] [--tag TAGS] [--list-tags] [--deploy]"
85+ in res .stdout
86+ )
87+
88+
89+ @pytest .mark .parametrize ("command_name" , list (_default_blacklisted_commands ))
90+ def test_default_blacklist_command (cli_runner , command_name ):
91+ with reflect .context ():
92+ DjangoModule .setup (
93+ settings_module = "example_app.wsgi_django.settings"
94+ ).apply_configuration ()
95+ django_command_group = reflect .get_metadata (
96+ MODULE_METADATA .COMMANDS , DjangoModule
97+ )[0 ]
98+
99+ res = cli_runner .invoke (django_command_group , [command_name ])
100+ assert res .exit_code == 2
101+ assert f"Error: No such command '{ command_name } '" in res .stdout
102+
103+
104+ def test_django_version (cli_runner ):
105+ with reflect .context ():
106+ DjangoModule .setup (
107+ settings_module = "example_app.wsgi_django.settings"
108+ ).apply_configuration ()
109+ django_command_group = reflect .get_metadata (
110+ MODULE_METADATA .COMMANDS , DjangoModule
111+ )[0 ]
112+
113+ res = cli_runner .invoke (django_command_group , ["-v" ])
114+ assert res .exit_code == 0
115+ assert "Django Version: " in res .stdout
116+
117+
118+ def test_collect_static_version (cli_runner ):
119+ with reflect .context ():
120+ DjangoModule .setup (
121+ settings_module = "example_app.wsgi_django.settings"
122+ ).apply_configuration ()
123+ django_command_group = reflect .get_metadata (
124+ MODULE_METADATA .COMMANDS , DjangoModule
125+ )[0 ]
126+
127+ res = cli_runner .invoke (django_command_group , ["check" , "db_models" ])
128+ assert res .exit_code == 0
129+ assert res .stdout == "System check identified no issues (0 silenced).\n "
0 commit comments