Completed
Push — master ( a8119a...52e3ec )
by
unknown
01:06
created

test_value_proc_deep_dict()   A

Complexity

Conditions 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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