1 | # -*- coding: utf-8 -*- |
||
2 | |||
3 | """ |
||
4 | test_dump |
||
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' |
||
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 | @pytest.fixture(autouse=True) |
||
29 | def remove_replay_dump(request, replay_file): |
||
30 | """Remove the replay file created by tests.""" |
||
31 | def fin_remove_replay_file(): |
||
32 | if os.path.exists(replay_file): |
||
33 | os.remove(replay_file) |
||
34 | request.addfinalizer(fin_remove_replay_file) |
||
35 | |||
36 | |||
37 | def test_type_error_if_no_template_name(replay_test_dir, context): |
||
38 | """Test that replay.dump raises if the template_name is not a valid str.""" |
||
39 | with pytest.raises(TypeError): |
||
40 | replay.dump(replay_test_dir, None, context) |
||
41 | |||
42 | |||
43 | def test_type_error_if_not_dict_context(replay_test_dir, template_name): |
||
44 | """Test that replay.dump raises if the context is not of type dict.""" |
||
45 | with pytest.raises(TypeError): |
||
46 | replay.dump(replay_test_dir, template_name, 'not_a_dict') |
||
47 | |||
48 | |||
49 | def test_value_error_if_key_missing_in_context(replay_test_dir, template_name): |
||
50 | """Test that replay.dump raises if the context does not contain a key |
||
51 | named 'cookiecutter'. |
||
52 | """ |
||
53 | with pytest.raises(ValueError): |
||
54 | replay.dump(replay_test_dir, template_name, {'foo': 'bar'}) |
||
55 | |||
56 | |||
57 | @pytest.fixture |
||
58 | def mock_ensure_failure(mocker): |
||
59 | return mocker.patch( |
||
60 | 'cookiecutter.replay.make_sure_path_exists', |
||
61 | return_value=False |
||
62 | ) |
||
63 | |||
64 | |||
65 | @pytest.fixture |
||
66 | def mock_ensure_success(mocker): |
||
67 | return mocker.patch( |
||
68 | 'cookiecutter.replay.make_sure_path_exists', |
||
69 | return_value=True |
||
70 | ) |
||
71 | |||
72 | |||
73 | def test_ioerror_if_replay_dir_creation_fails( |
||
74 | mock_ensure_failure, replay_test_dir): |
||
75 | """Test that replay.dump raises when the replay_dir cannot be created.""" |
||
76 | |||
77 | with pytest.raises(IOError): |
||
78 | replay.dump( |
||
79 | replay_test_dir, |
||
80 | 'foo', {'cookiecutter': {'hello': 'world'}} |
||
81 | ) |
||
82 | |||
83 | mock_ensure_failure.assert_called_once_with(replay_test_dir) |
||
84 | |||
85 | |||
86 | View Code Duplication | def test_run_json_dump(mocker, mock_ensure_success, mock_user_config, |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
87 | template_name, context, replay_test_dir, replay_file): |
||
88 | """Test that replay.dump runs json.dump under the hood and that the context |
||
89 | is correctly written to the expected file in the replay_dir. |
||
90 | """ |
||
91 | spy_get_replay_file = mocker.spy(replay, 'get_file_name') |
||
92 | |||
93 | mock_json_dump = mocker.patch('json.dump', side_effect=json.dump) |
||
94 | |||
95 | replay.dump(replay_test_dir, template_name, context) |
||
96 | |||
97 | assert not mock_user_config.called |
||
98 | mock_ensure_success.assert_called_once_with(replay_test_dir) |
||
99 | spy_get_replay_file.assert_called_once_with(replay_test_dir, template_name) |
||
100 | |||
101 | assert mock_json_dump.call_count == 1 |
||
102 | (dumped_context, outfile_handler), kwargs = mock_json_dump.call_args |
||
103 | assert outfile_handler.name == replay_file |
||
104 | assert dumped_context == context |
||
105 |