add tests on import-template query (#32469)

This commit is contained in:
Nicolas Roche 2019-04-18 18:41:19 +02:00
parent aefff59310
commit 6eb6a6c525
2 changed files with 75 additions and 6 deletions

View File

@ -29,12 +29,12 @@ class Command(BaseCommand):
def handle(self, *args, **kwargs):
app_name = settings.PROJECT_NAME
basepath = kwargs.get('basepath', '/var/lib/%s/skeletons')
basepath = kwargs.get('basepath') or '/var/lib/%s/skeletons' % app_name
template = '%s/%s.json' % (basepath, kwargs.get('template_name'))
if not os.path.exists(template):
self.stderr.write(self.style.WARNING('Unknown template (%r)' % template))
return
if 'import_site' in get_commands():
call_command('import_site', template)
if not os.path.exists(template):
self.stderr.write(self.style.WARNING(
'Unknown template (%r)' % template))
else:
call_command('import_site', template)

View File

@ -0,0 +1,69 @@
import pytest
import mock
from django.conf import settings
from django.core.management.base import CommandError
from django.test import override_settings
from hobo.agent.common.management.commands.import_template import Command
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
def test_import_template_default(get_commands, call_command, os_path_exists):
"""do import a template from default path"""
expected = [mock.call('import_site', '/var/lib/bobo/skeletons/import_me.json')]
get_commands.return_value = 'import_site'
call_command.return_value = mock.MagicMock()
os_path_exists.return_value = True
# simulate:
# $ XXX-manage import-template import_me
command = Command()
with override_settings(PROJECT_NAME='bobo'):
command.handle(template_name='import_me')
# assert 'import_site' command is run
assert call_command.mock_calls == expected
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
def test_import_template_basepath(get_commands, call_command, os_path_exists):
"""do import a template having basepath parameter provided"""
expected = [mock.call('import_site', '/tmp/import_me.json')]
get_commands.return_value = 'import_site'
call_command.return_value = mock.MagicMock()
os_path_exists.return_value = True
# simulate:
# $ XXX-manage import-template import_me --basepath /tmp
command = Command()
with override_settings(PROJECT_NAME='bobo'):
command.handle(template_name='import_me', basepath='/tmp')
# assert 'import_site' command is run
assert call_command.mock_calls == expected
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
def test_import_template_warning(get_commands, call_command, os_path_exists):
"""returns if template is not found"""
get_commands.return_value = 'import_site'
call_command.return_value = mock.MagicMock()
os_path_exists.return_value = False
# simulate:
# $ XXX-manage import-template import_me
command = Command()
with override_settings(PROJECT_NAME='bobo'):
command.handle(template_name='import_me')
# assert 'import_site' command is not run
assert call_command.mock_calls == []