Skip to content

Commit c295d27

Browse files
committed
Add tests and documentation for management commands
1 parent 10cf234 commit c295d27

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

docs/commands.rst

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,38 @@ Management Commands
77
render_process_graph
88
--------------------
99

10-
Render graphs for all processes as SVG files to the working directory.
10+
Render process graph to file::
11+
12+
usage: manage.py render_process_graph [-h] [-f {svg,pdf,png}] [-d DIRECTORY]
13+
[-c] [--version] [-v {0,1,2,3}]
14+
[--settings SETTINGS]
15+
[--pythonpath PYTHONPATH] [--traceback]
16+
[--no-color]
17+
[model [model ...]]
18+
19+
Render process graph to file.
20+
21+
positional arguments:
22+
model
23+
24+
optional arguments:
25+
-h, --help show this help message and exit
26+
-f {svg,pdf,png}, --format {svg,pdf,png}
27+
Output file format. Default: svg
28+
-d DIRECTORY, --directory DIRECTORY
29+
Output directory. Default is current working
30+
directory.
31+
-c, --cleanup Remove dot-files after rendering.
32+
--version show program's version number and exit
33+
-v {0,1,2,3}, --verbosity {0,1,2,3}
34+
Verbosity level; 0=minimal output, 1=normal output,
35+
2=verbose output, 3=very verbose output
36+
--settings SETTINGS The Python path to a settings module, e.g.
37+
"myproject.settings.main". If this isn't provided, the
38+
DJANGO_SETTINGS_MODULE environment variable will be
39+
used.
40+
--pythonpath PYTHONPATH
41+
A directory to add to the Python path, e.g.
42+
"/home/djangoprojects/myproject".
43+
--traceback Raise on CommandError exceptions
44+
--no-color Don't colorize the command output.

galahad/management/commands/render_process_graph.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
from django.apps import apps
22
from django.core.management import BaseCommand
33

4+
from galahad import utils
45
from galahad.models import Process
56

67

78
class Command(BaseCommand):
8-
help = "Render graphs for all processes as SVG files to the working directory"
9+
help = "Render process graph to file."
910

1011
def add_arguments(self, parser):
12+
parser.add_argument(
13+
'model',
14+
nargs='*',
15+
type=str,
16+
)
1117
parser.add_argument(
1218
'-f', '--format',
1319
dest='format', type=str,
@@ -28,12 +34,18 @@ def add_arguments(self, parser):
2834
)
2935

3036
def handle(self, *args, **options):
37+
models = options['model']
3138
verbosity = options['verbosity']
3239
file_format = options['format']
3340
cleanup = options['cleanup']
3441
directory = options.get('directory', None)
3542

36-
for model in apps.get_models():
43+
models = [
44+
apps.get_model(s)
45+
for s in models
46+
] or utils.get_processes()
47+
48+
for model in models:
3749
if issubclass(model, Process) and model != Process:
3850
opt = model._meta
3951
if verbosity > 0:
@@ -47,3 +59,5 @@ def handle(self, *args, **options):
4759
graph.render(filename=filename, directory=directory, cleanup=cleanup)
4860
if verbosity > 0:
4961
self.stdout.write("Done!", self.style.SUCCESS)
62+
else:
63+
self.stderr.write("%r is not a Process subclass" % model, self.style.WARNING)

tests/commands/__init__.py

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
import tempfile
3+
from pathlib import Path
4+
5+
from django.core.management import call_command
6+
7+
8+
def test_call_no_args():
9+
tmp_dir = Path(tempfile.mkdtemp())
10+
call_command('render_process_graph', '-d', tmp_dir)
11+
assert os.path.exists(str(tmp_dir / 'testapp_simpleprocess.svg'))
12+
assert os.path.exists(str(tmp_dir / 'testapp_simpleprocess'))
13+
14+
15+
def test_call_cleanup():
16+
tmp_dir = Path(tempfile.mkdtemp())
17+
call_command('render_process_graph', '-d', tmp_dir, '-c')
18+
assert os.path.exists(str(tmp_dir / 'testapp_simpleprocess.svg'))
19+
assert not os.path.exists(str(tmp_dir / 'testapp_simpleprocess'))
20+
21+
22+
def test_call_format_pdf():
23+
tmp_dir = Path(tempfile.mkdtemp())
24+
call_command('render_process_graph', '-d', tmp_dir, '-f', 'pdf')
25+
assert os.path.exists(str(tmp_dir / 'testapp_simpleprocess.pdf'))
26+
27+
28+
def test_call_format_png():
29+
tmp_dir = Path(tempfile.mkdtemp())
30+
call_command('render_process_graph', '-d', tmp_dir, '-f', 'png')
31+
assert os.path.exists(str(tmp_dir / 'testapp_simpleprocess.png'))
32+
33+
34+
def test_call_explicit_processes():
35+
tmp_dir = Path(tempfile.mkdtemp())
36+
call_command('render_process_graph', '-d', tmp_dir, 'testapp.loopprocess', 'testapp.splitjoinprocess')
37+
assert not os.path.exists(str(tmp_dir / 'testapp_simpleprocess.svg'))
38+
assert os.path.exists(str(tmp_dir / 'testapp_loopprocess.svg'))
39+
assert os.path.exists(str(tmp_dir / 'testapp_splitjoinprocess.svg'))
40+
41+
42+
def test_call_explicit_processes_invalid():
43+
tmp_dir = Path(tempfile.mkdtemp())
44+
call_command('render_process_graph', '-d', tmp_dir, 'auth.user', 'testapp.splitjoinprocess')
45+
assert not os.path.exists(str(tmp_dir / 'testapp_simpleprocess.svg'))
46+
assert not os.path.exists(str(tmp_dir / 'auth_user.svg'))
47+
assert os.path.exists(str(tmp_dir / 'testapp_splitjoinprocess.svg'))

0 commit comments

Comments
 (0)