|
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 ( |
|
14
|
|
|
process_json, |
|
15
|
|
|
read_user_dict, |
|
16
|
|
|
) |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def test_process_json_invalid_json(): |
|
20
|
|
|
with pytest.raises(click.UsageError) as exc_info: |
|
21
|
|
|
process_json('nope]') |
|
22
|
|
|
|
|
23
|
|
|
assert str(exc_info.value) == 'Unable to decode to JSON.' |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def test_process_json_non_dict(): |
|
27
|
|
|
with pytest.raises(click.UsageError) as exc_info: |
|
28
|
|
|
process_json('[1, 2]') |
|
29
|
|
|
|
|
30
|
|
|
assert str(exc_info.value) == 'Requires JSON dict.' |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
def test_process_json_valid_json(): |
|
34
|
|
|
user_value = '{"name": "foobar", "bla": ["a", 1, "b", false]}' |
|
35
|
|
|
|
|
36
|
|
|
assert process_json(user_value) == { |
|
37
|
|
|
'name': 'foobar', |
|
38
|
|
|
'bla': ['a', 1, 'b', False], |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def test_process_json_deep_dict(): |
|
43
|
|
|
user_value = '''{ |
|
44
|
|
|
"key": "value", |
|
45
|
|
|
"integer_key": 37, |
|
46
|
|
|
"dict_key": { |
|
47
|
|
|
"deep_key": "deep_value", |
|
48
|
|
|
"deep_integer": 42, |
|
49
|
|
|
"deep_list": [ |
|
50
|
|
|
"deep value 1", |
|
51
|
|
|
"deep value 2", |
|
52
|
|
|
"deep value 3" |
|
53
|
|
|
] |
|
54
|
|
|
}, |
|
55
|
|
|
"list_key": [ |
|
56
|
|
|
"value 1", |
|
57
|
|
|
"value 2", |
|
58
|
|
|
"value 3" |
|
59
|
|
|
] |
|
60
|
|
|
}''' |
|
61
|
|
|
|
|
62
|
|
|
assert process_json(user_value) == { |
|
63
|
|
|
"key": "value", |
|
64
|
|
|
"integer_key": 37, |
|
65
|
|
|
"dict_key": { |
|
66
|
|
|
"deep_key": "deep_value", |
|
67
|
|
|
"deep_integer": 42, |
|
68
|
|
|
"deep_list": [ |
|
69
|
|
|
"deep value 1", |
|
70
|
|
|
"deep value 2", |
|
71
|
|
|
"deep value 3", |
|
72
|
|
|
] |
|
73
|
|
|
}, |
|
74
|
|
|
"list_key": [ |
|
75
|
|
|
"value 1", |
|
76
|
|
|
"value 2", |
|
77
|
|
|
"value 3", |
|
78
|
|
|
] |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def test_should_raise_type_error(mocker): |
|
83
|
|
|
prompt = mocker.patch('click.prompt') |
|
84
|
|
|
|
|
85
|
|
|
with pytest.raises(TypeError): |
|
86
|
|
|
read_user_dict('name', 'russell') |
|
87
|
|
|
|
|
88
|
|
|
assert not prompt.called |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
def test_should_call_prompt_with_process_json(mocker): |
|
92
|
|
|
"""Test to make sure that process_jon is actually being used |
|
93
|
|
|
to generate a processor for the user input.""" |
|
94
|
|
|
|
|
95
|
|
|
mock_prompt = mocker.patch( |
|
96
|
|
|
'cookiecutter.prompt.click.prompt', |
|
97
|
|
|
autospec=True, |
|
98
|
|
|
) |
|
99
|
|
|
|
|
100
|
|
|
read_user_dict('name', {'project_slug': 'pytest-plugin'}) |
|
101
|
|
|
|
|
102
|
|
|
assert mock_prompt.call_args == mocker.call( |
|
103
|
|
|
'name', |
|
104
|
|
|
type=click.STRING, |
|
105
|
|
|
default='default', |
|
106
|
|
|
value_proc=process_json, |
|
107
|
|
|
) |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
def test_read_user_dict_default_value(mocker): |
|
111
|
|
|
"""Test to make sure that read_user_dict returns the default value for a |
|
112
|
|
|
dict variable rather than the display value. |
|
113
|
|
|
""" |
|
114
|
|
|
mock_prompt = mocker.patch( |
|
115
|
|
|
'cookiecutter.prompt.click.prompt', |
|
116
|
|
|
autospec=True, |
|
117
|
|
|
return_value='default', |
|
118
|
|
|
) |
|
119
|
|
|
|
|
120
|
|
|
val = read_user_dict('name', {'project_slug': 'pytest-plugin'}) |
|
121
|
|
|
|
|
122
|
|
|
assert mock_prompt.call_args == mocker.call( |
|
123
|
|
|
'name', |
|
124
|
|
|
type=click.STRING, |
|
125
|
|
|
default='default', |
|
126
|
|
|
value_proc=process_json, |
|
127
|
|
|
) |
|
128
|
|
|
|
|
129
|
|
|
assert val == {'project_slug': 'pytest-plugin'} |
|
130
|
|
|
|