1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
test_load |
5
|
|
|
----------- |
6
|
|
|
""" |
7
|
|
|
|
8
|
|
|
import json |
9
|
|
|
import os |
10
|
|
|
import pytest |
11
|
|
|
|
12
|
|
|
from cookiecutter import replay |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
@pytest.fixture |
16
|
|
|
def template_name(): |
17
|
|
|
"""Fixture to return a valid template_name.""" |
18
|
|
|
return 'cookiedozer_load' |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
@pytest.fixture |
22
|
|
|
def replay_file(replay_test_dir, template_name): |
23
|
|
|
"""Fixture to return a actual file name of the dump.""" |
24
|
|
|
file_name = '{}.json'.format(template_name) |
25
|
|
|
return os.path.join(replay_test_dir, file_name) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def test_type_error_if_no_template_name(replay_test_dir): |
29
|
|
|
"""Test that replay.load raises if the template_name is not a valid str.""" |
30
|
|
|
with pytest.raises(TypeError): |
31
|
|
|
replay.load(replay_test_dir, None) |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
def test_value_error_if_key_missing_in_context(mocker, replay_test_dir): |
35
|
|
|
"""Test that replay.load raises if the loaded context does not contain |
36
|
|
|
'cookiecutter'. |
37
|
|
|
""" |
38
|
|
|
with pytest.raises(ValueError): |
39
|
|
|
replay.load(replay_test_dir, 'invalid_replay') |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def test_io_error_if_no_replay_file(mocker, replay_test_dir): |
43
|
|
|
"""Test that replay.load raises if it cannot find a replay file.""" |
44
|
|
|
with pytest.raises(IOError): |
45
|
|
|
replay.load(replay_test_dir, 'no_replay') |
46
|
|
|
|
47
|
|
|
|
48
|
|
View Code Duplication |
def test_run_json_load(mocker, mock_user_config, template_name, |
|
|
|
|
49
|
|
|
context, replay_test_dir, replay_file): |
50
|
|
|
"""Test that replay.load runs json.load under the hood and that the context |
51
|
|
|
is correctly loaded from the file in replay_dir. |
52
|
|
|
""" |
53
|
|
|
spy_get_replay_file = mocker.spy(replay, 'get_file_name') |
54
|
|
|
|
55
|
|
|
mock_json_load = mocker.patch('json.load', side_effect=json.load) |
56
|
|
|
|
57
|
|
|
loaded_context = replay.load(replay_test_dir, template_name) |
58
|
|
|
|
59
|
|
|
assert not mock_user_config.called |
60
|
|
|
spy_get_replay_file.assert_called_once_with(replay_test_dir, template_name) |
61
|
|
|
|
62
|
|
|
assert mock_json_load.call_count == 1 |
63
|
|
|
(infile_handler,), kwargs = mock_json_load.call_args |
64
|
|
|
assert infile_handler.name == replay_file |
65
|
|
|
assert loaded_context == context |
66
|
|
|
|