Projet

Général

Profil

0001-add-tests-on-import-template-query-32469.patch

Nicolas Roche, 18 avril 2019 18:51

Télécharger (3,3 ko)

Voir les différences:

Subject: [PATCH] add tests on import-template query (#32469)

 .../management/commands/import_template.py    |  5 +-
 tests/test_import_template.py                 | 48 +++++++++++++++++++
 2 files changed, 50 insertions(+), 3 deletions(-)
 create mode 100644 tests/test_import_template.py
hobo/agent/common/management/commands/import_template.py
29 29

  
30 30
    def handle(self, *args, **kwargs):
31 31
        app_name = settings.PROJECT_NAME
32
        basepath = kwargs.get('basepath', '/var/lib/%s/skeletons')
32
        basepath = kwargs.get('basepath') or '/var/lib/%s/skeletons' % app_name
33 33
        template = '%s/%s.json' % (basepath, kwargs.get('template_name'))
34 34

  
35 35
        if not os.path.exists(template):
36
            self.stderr.write(self.style.WARNING('Unknown template (%r)' % template))
37
            return
36
            raise CommandError('Unknown template (%r)' % template)
38 37

  
39 38
        if 'import_site' in get_commands():
40 39
            call_command('import_site', template)
tests/test_import_template.py
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
-