Completed
Push — master ( bf618f...1f875c )
by Simone
16s queued 12s
created

test_should_not_call_process_json_default_value()   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
"""Test `process_json`, `read_user_dict` functions in `cookiecutter.prompt`."""
2
import click
3
import pytest
4
5
from cookiecutter.prompt import (
6
    process_json,
7
    read_user_dict,
8
)
9
10
11
def test_process_json_invalid_json():
12
    """Test `process_json` for correct error on malformed input."""
13
    with pytest.raises(click.UsageError) as exc_info:
14
        process_json('nope]')
15
16
    assert str(exc_info.value) == 'Unable to decode to JSON.'
17
18
19
def test_process_json_non_dict():
20
    """Test `process_json` for correct error on non-JSON input."""
21
    with pytest.raises(click.UsageError) as exc_info:
22
        process_json('[1, 2]')
23
24
    assert str(exc_info.value) == 'Requires JSON dict.'
25
26
27
def test_process_json_valid_json():
28
    """Test `process_json` for correct output on JSON input.
29
30
    Test for simple dict with list.
31
    """
32
    user_value = '{"name": "foobar", "bla": ["a", 1, "b", false]}'
33
34
    assert process_json(user_value) == {
35
        'name': 'foobar',
36
        'bla': ['a', 1, 'b', False],
37
    }
38
39
40
def test_process_json_deep_dict():
41
    """Test `process_json` for correct output on JSON input.
42
43
    Test for dict in dict case.
44
    """
45
    user_value = '''{
46
        "key": "value",
47
        "integer_key": 37,
48
        "dict_key": {
49
            "deep_key": "deep_value",
50
            "deep_integer": 42,
51
            "deep_list": [
52
                "deep value 1",
53
                "deep value 2",
54
                "deep value 3"
55
            ]
56
        },
57
        "list_key": [
58
            "value 1",
59
            "value 2",
60
            "value 3"
61
        ]
62
    }'''
63
64
    assert process_json(user_value) == {
65
        "key": "value",
66
        "integer_key": 37,
67
        "dict_key": {
68
            "deep_key": "deep_value",
69
            "deep_integer": 42,
70
            "deep_list": ["deep value 1", "deep value 2", "deep value 3"],
71
        },
72
        "list_key": ["value 1", "value 2", "value 3"],
73
    }
74
75
76
def test_should_raise_type_error(mocker):
77
    """Test `default_value` arg verification in `read_user_dict` function."""
78
    prompt = mocker.patch('cookiecutter.prompt.click.prompt')
79
80
    with pytest.raises(TypeError):
81
        read_user_dict('name', 'russell')
82
83
    assert not prompt.called
84
85
86
def test_should_call_prompt_with_process_json(mocker):
87
    """Test to make sure that `process_json` is actually being used.
88
89
    Verifies generation of a processor for the user input.
90
    """
91
    mock_prompt = mocker.patch('cookiecutter.prompt.click.prompt', autospec=True)
92
93
    read_user_dict('name', {'project_slug': 'pytest-plugin'})
94
95
    assert mock_prompt.call_args == mocker.call(
96
        'name', type=click.STRING, default='default', value_proc=process_json,
97
    )
98
99
100
def test_should_not_call_process_json_default_value(mocker, monkeypatch):
101
    """Make sure that `process_json` is not called when using default value."""
102
    mock_process_json = mocker.patch('cookiecutter.prompt.process_json', autospec=True)
103
104
    runner = click.testing.CliRunner()
105
    with runner.isolation(input="\n"):
106
        read_user_dict('name', {'project_slug': 'pytest-plugin'})
107
108
    mock_process_json.assert_not_called()
109
110
111
def test_read_user_dict_default_value(mocker):
112
    """Make sure that `read_user_dict` returns the default value.
113
114
    Verify return of a dict variable rather than the display value.
115
    """
116
    mock_prompt = mocker.patch(
117
        'cookiecutter.prompt.click.prompt', autospec=True, 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', type=click.STRING, default='default', value_proc=process_json,
124
    )
125
126
    assert val == {'project_slug': 'pytest-plugin'}
127