File: test_load.py

package info (click to toggle)
cookiecutter 2.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,780 kB
  • sloc: python: 6,097; makefile: 113; sh: 8
file content (60 lines) | stat: -rw-r--r-- 1,882 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""test_load."""

import json
import os

import pytest

from cookiecutter import replay


@pytest.fixture
def template_name():
    """Fixture to return a valid template_name."""
    return 'cookiedozer_load'


@pytest.fixture
def replay_file(replay_test_dir, template_name):
    """Fixture to return a actual file name of the dump."""
    file_name = f'{template_name}.json'
    return os.path.join(replay_test_dir, file_name)


def test_type_error_if_no_template_name(replay_test_dir):
    """Test that replay.load raises if the template_name is not a valid str."""
    with pytest.raises(TypeError):
        replay.load(replay_test_dir, None)


def test_value_error_if_key_missing_in_context(mocker, replay_test_dir):
    """Test that replay.load raises if the loaded context does not contain \
    'cookiecutter'."""
    with pytest.raises(ValueError):
        replay.load(replay_test_dir, 'invalid_replay')


def test_io_error_if_no_replay_file(mocker, replay_test_dir):
    """Test that replay.load raises if it cannot find a replay file."""
    with pytest.raises(IOError):
        replay.load(replay_test_dir, 'no_replay')


def test_run_json_load(
    mocker, mock_user_config, template_name, context, replay_test_dir, replay_file
):
    """Test that replay.load runs json.load under the hood and that the context \
    is correctly loaded from the file in replay_dir."""
    spy_get_replay_file = mocker.spy(replay, 'get_file_name')

    mock_json_load = mocker.patch('json.load', side_effect=json.load)

    loaded_context = replay.load(replay_test_dir, template_name)

    assert not mock_user_config.called
    spy_get_replay_file.assert_called_once_with(replay_test_dir, template_name)

    assert mock_json_load.call_count == 1
    (infile_handler,), kwargs = mock_json_load.call_args
    assert infile_handler.name == replay_file
    assert loaded_context == context