From 6eb6a6c525adad8031bbae9d06b1eec9b1f5d6ed Mon Sep 17 00:00:00 2001 From: Nicolas ROCHE Date: Thu, 18 Apr 2019 18:41:19 +0200 Subject: [PATCH] add tests on import-template query (#32469) --- .../management/commands/import_template.py | 12 ++-- tests/test_import_template.py | 69 +++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 tests/test_import_template.py diff --git a/hobo/agent/common/management/commands/import_template.py b/hobo/agent/common/management/commands/import_template.py index f80a545..d1d3bf2 100644 --- a/hobo/agent/common/management/commands/import_template.py +++ b/hobo/agent/common/management/commands/import_template.py @@ -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) diff --git a/tests/test_import_template.py b/tests/test_import_template.py new file mode 100644 index 0000000..5c4f665 --- /dev/null +++ b/tests/test_import_template.py @@ -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 == []