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 |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
@pytest.fixture |
17
|
|
|
def get_context(): |
18
|
|
|
""" |
19
|
|
|
helper method to get a context object |
20
|
|
|
""" |
21
|
|
|
basic = { |
22
|
|
|
'string': 'value1', |
23
|
|
|
'list': ['value1', 'value2'], |
24
|
|
|
'dict': { |
25
|
|
|
'string': 'value1', |
26
|
|
|
'deep': { |
27
|
|
|
'path': { |
28
|
|
|
'list': ['value1', 'value2'], |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
nested_list = [[1, 2], [3, 4]] |
35
|
|
|
|
36
|
|
|
return { |
37
|
|
|
'basic': basic, |
38
|
|
|
'nested_list': nested_list |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def test_get_config(): |
43
|
|
|
""" |
44
|
|
|
get the config value of a first level key |
45
|
|
|
""" |
46
|
|
|
context = get_context()['basic'] |
47
|
|
|
|
48
|
|
|
assert context['string'] == get_from_context(context, 'string') |
49
|
|
|
assert context['list'] == get_from_context(context, 'list') |
50
|
|
|
assert context['dict'] == get_from_context(context, 'dict') |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def test_get_config_dot_notation(): |
54
|
|
|
""" |
55
|
|
|
get a value using dot notation |
56
|
|
|
""" |
57
|
|
|
context = get_context()['basic'] |
58
|
|
|
|
59
|
|
|
assert context['dict']['string'] == get_from_context( |
60
|
|
|
context, 'dict.string') |
61
|
|
|
assert context['dict']['deep']['path']['list'] == get_from_context( |
62
|
|
|
context, 'dict.deep.path.list') |
63
|
|
|
assert context['list'][0] == get_from_context(context, 'list.0') |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def test_get_config_dot_notation_for_nested_lists(): |
67
|
|
|
""" |
68
|
|
|
get a value using dot notation in nested lists |
69
|
|
|
""" |
70
|
|
|
context = get_context()['nested_list'] |
71
|
|
|
|
72
|
|
|
assert context[1][0] == get_from_context(context, '1.0') |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_get_config_with_non_existing_key(): |
76
|
|
|
""" |
77
|
|
|
returns None if the given key does not exist |
78
|
|
|
""" |
79
|
|
|
context = get_context()['basic'] |
80
|
|
|
|
81
|
|
|
assert get_from_context(context, 'not_existing') is None |
82
|
|
|
assert get_from_context(context, 'dict.not_existing') is None |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
def test_get_config_with_default_value(): |
86
|
|
|
""" |
87
|
|
|
a default value can be specified to be returned in case the key does not |
88
|
|
|
exist |
89
|
|
|
""" |
90
|
|
|
context = get_context()['basic'] |
91
|
|
|
expected = 'otherstring' |
92
|
|
|
key = 'not_existing' |
93
|
|
|
|
94
|
|
|
assert expected == get_from_context(context, key, expected) |
95
|
|
|
assert [] == get_from_context(context, key, []) |
96
|
|
|
assert {} == get_from_context(context, key, {}) |
97
|
|
|
assert get_from_context(context, key, True) |
98
|
|
|
assert not get_from_context(context, key, False) |
99
|
|
|
assert key not in context |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
def test_get_config_create_missing_key(): |
103
|
|
|
""" |
104
|
|
|
a missing key can be initialized |
105
|
|
|
""" |
106
|
|
|
context = get_context()['basic'] |
107
|
|
|
value = 'value2' |
108
|
|
|
key = 'string2' |
109
|
|
|
|
110
|
|
|
assert get_from_context(context, 'dict.missing', update=True) is None |
111
|
|
|
assert 'missing' in context['dict'] |
112
|
|
|
assert value == get_from_context(context, key, value, True) |
113
|
|
|
assert key in context |
114
|
|
|
|