Completed
Pull Request — master (#694)
by Eric
01:25
created

test_get_config_with_default_value()   B

Complexity

Conditions 7

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
c 1
b 0
f 1
dl 0
loc 15
rs 7.3333
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
'''
5
test_context_config
6
---------------
7
8
Tests for context related config functions
9
'''
10
11
import pytest
12
13
from cookiecutter.config import get_from_context, set_to_context, \
14
    get_from_cookiecutter_context, set_to_cookiecutter_context
15
16
17
@pytest.fixture
18
def get_context():
19
    """
20
    helper method to get a context object
21
    """
22
    basic = {
23
        'string': 'value1',
24
        'list': ['value1', 'value2'],
25
        'dict': {
26
            'string': 'value1',
27
            'deep': {
28
                'path': {
29
                    'list': ['value1', 'value2'],
30
                }
31
            }
32
        }
33
    }
34
35
    nested_list = [[1, 2], [3, 4]]
36
37
    return {
38
        'basic': basic,
39
        'nested_list': nested_list
40
    }
41
42
43
def test_get_config():
44
    """
45
    get the config value of a first level key
46
    """
47
    context = get_context()['basic']
48
49
    assert context['string'] == get_from_context(context, 'string')
50
    assert context['list'] == get_from_context(context, 'list')
51
    assert context['dict'] == get_from_context(context, 'dict')
52
53
54
def test_get_config_dot_notation():
55
    """
56
    get a value using dot notation
57
    """
58
    context = get_context()['basic']
59
60
    assert context['dict']['string'] == get_from_context(
61
        context, 'dict.string')
62
    assert context['dict']['deep']['path']['list'] == get_from_context(
63
        context, 'dict.deep.path.list')
64
    assert context['list'][0] == get_from_context(context, 'list.0')
65
66
67
def test_get_config_dot_notation_for_nested_lists():
68
    """
69
    get a value using dot notation in nested lists
70
    """
71
    context = get_context()['nested_list']
72
73
    assert context[1][0] == get_from_context(context, '1.0')
74
75
76
def test_get_config_with_non_existing_key():
77
    """
78
    returns None if the given key does not exist
79
    """
80
    context = get_context()['basic']
81
82
    assert get_from_context(context, 'not_existing') is None
83
    assert get_from_context(context, 'dict.not_existing') is None
84
85
86
def test_get_config_with_default_value():
87
    """
88
    a default value can be specified to be returned in case the key does not
89
    exist
90
    """
91
    context = get_context()['basic']
92
    expected = 'otherstring'
93
    key = 'not_existing'
94
95
    assert expected == get_from_context(context, key, expected)
96
    assert [] == get_from_context(context, key, [])
97
    assert {} == get_from_context(context, key, {})
98
    assert get_from_context(context, key, True)
99
    assert not get_from_context(context, key, False)
100
    assert key not in context
101
102
103
def test_get_config_create_missing_key():
104
    """
105
    a missing key can be initialized
106
    """
107
    context = get_context()['basic']
108
    value = 'value2'
109
    key = 'string2'
110
111
    assert get_from_context(context, 'dict.missing', update=True) is None
112
    assert 'missing' in context['dict']
113
    assert value == get_from_context(context, key, value, True)
114
    assert key in context
115
116
117
def test_set_config():
118
    """
119
    set a configuration key/value pair
120
    """
121
    context = {}
122
    expected = {'key': 'value'}
123
    set_to_context(context, 'key', 'value')
124
125
    assert expected == context
126
127
128
def test_set_config_dot_notation():
129
    """
130
    set a value using dot notation
131
    """
132
    context = {}
133
    expected = {
134
        'path': {
135
            'to': {
136
                'deep': {
137
                    'key': 'value'
138
                }
139
            }
140
        }
141
    }
142
    set_to_context(context, 'path.to.deep.key', 'value')
143
144
    assert expected == context
145
146
147
def test_get_cookiecutter_shorthand(mocker):
148
    mock = mocker.patch('cookiecutter.config.get_from_context')
149
    context = get_context()['basic']
150
151
    get_from_cookiecutter_context(context, 'key')
152
153
    mock.assert_called_once_with(context, 'cookiecutter.key', None, False)
154
155
156
def test_set_cookiecutter_shorthand(mocker):
157
    mock = mocker.patch('cookiecutter.config.set_to_context')
158
    context = get_context()['basic']
159
160
    set_to_cookiecutter_context(context, 'key', 'value')
161
162
    mock.assert_called_once_with(context, 'cookiecutter.key', 'value')
163