1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
from typing import Iterable, Optional
import pytest
from django.core.management import call_command
@pytest.fixture
def command_run():
"""Allows management command run.
Example::
def test_this(command_run, capsys):
result = command_run('my_command', args=['one'], options={'two': 'three'})
out, err = capsys.readouterr()
.. warning:: Django < 1.10 will always return `None`, no matter what command returns.
:param command_name: Command name to run.
:param args: Required arguments to pass to a command.
:param options: Optional arguments to pass to a command.
:returns: Command output.
"""
def command_run_(command_name: str, args: Iterable[str] = None, options: dict = None) -> Optional[str]:
args = args or []
options = options or {}
return call_command(command_name, *args, **options)
return command_run_
@pytest.fixture
def command_makemigrations(conf_app_name):
"""Allows to run makemigrations command.
.. note:: This command can be useful to generate
migrations for your application (without a project creation).
Example::
def test_makemigrations(command_makemigrations):
command_makemigrations()
:param app: Application name to run 'makemigrations' for.
* By default, a name from 'conf_app_name' fixture is used.
* If empty string, command is run for any application.
:param args: Additional arguments to pass to the command.
"""
def command_migration_(*, app: str = None, args: Iterable[str] = None) -> Optional[str]:
args = args or []
if app is None:
app = conf_app_name
if app:
args.append(app)
return call_command('makemigrations', *args)
return command_migration_
|