|
1 |
import pytest
|
|
2 |
import mock
|
|
3 |
|
|
4 |
from django.conf import settings
|
|
5 |
from django.core.management.base import CommandError
|
|
6 |
from django.test import override_settings
|
|
7 |
|
|
8 |
from hobo.agent.common.management.commands.import_template import Command
|
|
9 |
|
|
10 |
|
|
11 |
@pytest.fixture
|
|
12 |
def template():
|
|
13 |
# build a template file to import
|
|
14 |
basepath = '/tmp'
|
|
15 |
template_name = 'import_me'
|
|
16 |
with open('/%s/%s.json' % (basepath, template_name),'w') as handler:
|
|
17 |
handler.write('some content to import')
|
|
18 |
return template_name
|
|
19 |
|
|
20 |
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
|
|
21 |
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
|
|
22 |
def test_import_template_ok(mocked_get_commands, mocked_call_command, template):
|
|
23 |
"""do import a template"""
|
|
24 |
expected = [mock.call('import_site', '/tmp/import_me.json')]
|
|
25 |
|
|
26 |
mocked_get_commands.return_value = 'import_site'
|
|
27 |
mocked_call_command.return_value = mock.MagicMock()
|
|
28 |
|
|
29 |
# simulate
|
|
30 |
# $ XXX-manage import-template import_me --basepath /tmp
|
|
31 |
command = Command()
|
|
32 |
with override_settings(PROJECT_NAME='bobo'):
|
|
33 |
command.handle(template_name=template, basepath='/tmp')
|
|
34 |
|
|
35 |
# assert 'import_site' command is run
|
|
36 |
assert mocked_call_command.mock_calls == expected
|
|
37 |
|
|
38 |
def test_import_template_default_path():
|
|
39 |
"""check default path where template should be found"""
|
|
40 |
expected = "Unknown template ('/var/lib/bobo/skeletons/import_me.json')"
|
|
41 |
|
|
42 |
# simulate
|
|
43 |
# $ XXX-manage import-template import_me
|
|
44 |
command = Command()
|
|
45 |
with override_settings(PROJECT_NAME='bobo'):
|
|
46 |
with pytest.raises(CommandError) as e_info:
|
|
47 |
command.handle(template_name='import_me')
|
|
48 |
assert str(e_info.value) == expected
|
0 |
|
-
|