|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
""" |
|
4
|
|
|
test_read_user_dict |
|
5
|
|
|
------------------- |
|
6
|
|
|
""" |
|
7
|
|
|
|
|
8
|
|
|
from __future__ import unicode_literals |
|
9
|
|
|
|
|
10
|
|
|
import click |
|
11
|
|
|
import pytest |
|
12
|
|
|
|
|
13
|
|
|
from cookiecutter.prompt import read_user_dict |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
def test_use_default_dict(mocker): |
|
17
|
|
|
prompt = mocker.patch('click.prompt') |
|
18
|
|
|
prompt.return_value = 'default' |
|
19
|
|
|
|
|
20
|
|
|
VARIABLE = 'var' |
|
21
|
|
|
DEFAULT = {"key": 1} |
|
22
|
|
|
|
|
23
|
|
|
assert read_user_dict(VARIABLE, DEFAULT) == {'key': 1} |
|
24
|
|
|
|
|
25
|
|
|
click.prompt.assert_called_once_with(VARIABLE, default='default') |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def test_empty_input_dict(mocker): |
|
29
|
|
|
prompt = mocker.patch('click.prompt') |
|
30
|
|
|
prompt.return_value = '{}' |
|
31
|
|
|
|
|
32
|
|
|
VARIABLE = 'var' |
|
33
|
|
|
DEFAULT = {"key": 1} |
|
34
|
|
|
|
|
35
|
|
|
assert read_user_dict(VARIABLE, DEFAULT) == {} |
|
36
|
|
|
|
|
37
|
|
|
click.prompt.assert_called_once_with(VARIABLE, default='default') |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def test_use_empty_default_dict(mocker): |
|
41
|
|
|
prompt = mocker.patch('click.prompt') |
|
42
|
|
|
prompt.return_value = 'default' |
|
43
|
|
|
|
|
44
|
|
|
VARIABLE = 'var' |
|
45
|
|
|
DEFAULT = {} |
|
46
|
|
|
|
|
47
|
|
|
assert read_user_dict(VARIABLE, DEFAULT) == {} |
|
48
|
|
|
|
|
49
|
|
|
click.prompt.assert_called_once_with(VARIABLE, default='default') |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def test_shallow_dict(mocker): |
|
53
|
|
|
prompt = mocker.patch('click.prompt') |
|
54
|
|
|
prompt.return_value = '{"key": 2}' |
|
55
|
|
|
|
|
56
|
|
|
VARIABLE = 'var' |
|
57
|
|
|
DEFAULT = {} |
|
58
|
|
|
|
|
59
|
|
|
assert read_user_dict(VARIABLE, DEFAULT) == {'key': 2} |
|
60
|
|
|
|
|
61
|
|
|
click.prompt.assert_called_once_with(VARIABLE, default='default') |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def test_deep_dict(mocker): |
|
65
|
|
|
prompt = mocker.patch('click.prompt') |
|
66
|
|
|
prompt.return_value = '''{ |
|
67
|
|
|
"key": "value", |
|
68
|
|
|
"integer_key": 37, |
|
69
|
|
|
"dict_key": { |
|
70
|
|
|
"deep_key": "deep_value", |
|
71
|
|
|
"deep_integer": 42, |
|
72
|
|
|
"deep_list": [ |
|
73
|
|
|
"deep value 1", |
|
74
|
|
|
"deep value 2", |
|
75
|
|
|
"deep value 3" |
|
76
|
|
|
] |
|
77
|
|
|
}, |
|
78
|
|
|
"list_key": [ |
|
79
|
|
|
"value 1", |
|
80
|
|
|
"value 2", |
|
81
|
|
|
"value 3" |
|
82
|
|
|
] |
|
83
|
|
|
}''' |
|
84
|
|
|
|
|
85
|
|
|
VARIABLE = 'var' |
|
86
|
|
|
DEFAULT = {} |
|
87
|
|
|
|
|
88
|
|
|
assert read_user_dict(VARIABLE, DEFAULT) == { |
|
89
|
|
|
"key": "value", |
|
90
|
|
|
"integer_key": 37, |
|
91
|
|
|
"dict_key": { |
|
92
|
|
|
"deep_key": "deep_value", |
|
93
|
|
|
"deep_integer": 42, |
|
94
|
|
|
"deep_list": [ |
|
95
|
|
|
"deep value 1", |
|
96
|
|
|
"deep value 2", |
|
97
|
|
|
"deep value 3", |
|
98
|
|
|
] |
|
99
|
|
|
}, |
|
100
|
|
|
"list_key": [ |
|
101
|
|
|
"value 1", |
|
102
|
|
|
"value 2", |
|
103
|
|
|
"value 3", |
|
104
|
|
|
] |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
click.prompt.assert_called_once_with(VARIABLE, default='default') |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
def test_raise_if_value_is_not_dict(): |
|
111
|
|
|
with pytest.raises(TypeError): |
|
112
|
|
|
read_user_dict('foo', 'NOT A LIST') |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
def test_raise_if_value_not_valid_json(mocker): |
|
116
|
|
|
prompt = mocker.patch('click.prompt') |
|
117
|
|
|
prompt.return_value = '{' |
|
118
|
|
|
|
|
119
|
|
|
with pytest.raises(ValueError): |
|
120
|
|
|
read_user_dict('foo', {}) |
|
121
|
|
|
|