Projet

Général

Profil

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

Nicolas Roche, 19 avril 2019 10:10

Télécharger (4,51 ko)

Voir les différences:

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
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
        if not os.path.exists(template):
36
            self.stderr.write(self.style.WARNING('Unknown template (%r)' % template))
37
            return
38

  
39 35
        if 'import_site' in get_commands():
40
            call_command('import_site', template)
36
            if not os.path.exists(template):
37
                self.stderr.write(self.style.WARNING(
38
                    'Unknown template (%r)' % template))
39
            else:
40
                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
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
12
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
13
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
14
def test_import_template_default(get_commands, call_command, os_path_exists):
15
    """do import a template from default path"""
16
    expected = [mock.call('import_site', '/var/lib/bobo/skeletons/import_me.json')]
17

  
18
    get_commands.return_value = 'import_site'
19
    call_command.return_value = mock.MagicMock()
20
    os_path_exists.return_value = True
21

  
22
    # simulate:
23
    # $ XXX-manage import-template import_me
24
    command = Command()
25
    with override_settings(PROJECT_NAME='bobo'):
26
        command.handle(template_name='import_me')
27

  
28
    # assert 'import_site' command is run
29
    assert call_command.mock_calls == expected
30

  
31
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
32
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
33
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
34
def test_import_template_basepath(get_commands, call_command, os_path_exists):
35
    """do import a template having basepath parameter provided"""
36
    expected = [mock.call('import_site', '/tmp/import_me.json')]
37

  
38
    get_commands.return_value = 'import_site'
39
    call_command.return_value = mock.MagicMock()
40
    os_path_exists.return_value = True
41

  
42
    # simulate:
43
    # $ XXX-manage import-template import_me --basepath /tmp
44
    command = Command()
45
    with override_settings(PROJECT_NAME='bobo'):
46
        command.handle(template_name='import_me', basepath='/tmp')
47

  
48
    # assert 'import_site' command is run
49
    assert call_command.mock_calls == expected
50

  
51

  
52
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
53
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
54
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
55
def test_import_template_warning(get_commands, call_command, os_path_exists):
56
    """returns if template is not found"""
57

  
58
    get_commands.return_value = 'import_site'
59
    call_command.return_value = mock.MagicMock()
60
    os_path_exists.return_value = False
61

  
62
    # simulate:
63
    # $ XXX-manage import-template import_me
64
    command = Command()
65
    with override_settings(PROJECT_NAME='bobo'):
66
        command.handle(template_name='import_me')
67

  
68
    # assert 'import_site' command is not run
69
    assert call_command.mock_calls == []
0
-