refactor override unittest

This commit is contained in:
Suhas 2021-01-21 22:47:29 -05:00
parent c4b3d51181
commit 9e2b7dd0c1

View file

@ -1,3 +1,4 @@
from typing import Any
import pytest import pytest
import pytest_mock import pytest_mock
import mock import mock
@ -5,27 +6,49 @@ import mock
import yaml import yaml
from jrnl.args import parse_args from jrnl.args import parse_args
from jrnl.jrnl import run from jrnl.jrnl import run
from jrnl import install from jrnl import install, editor
@pytest.fixture() @pytest.fixture()
def minimal_config(): def minimal_config():
with open("features/data/configs/editor.yaml", "r") as cfg_file: with open("features/data/configs/editor.yaml", "r") as cfg_file:
yield yaml.load(cfg_file.read()) cfg = yaml.load(cfg_file.read())
yield cfg
@pytest.fixture()
def expected_override(minimal_config):
exp_out_cfg = minimal_config.copy()
exp_out_cfg['editor'] = 'nano'
exp_out_cfg['journal'] = 'features/journals/simple.journal'
yield exp_out_cfg
from jrnl import jrnl from jrnl import jrnl
@mock.patch.object(jrnl, "write_mode")
@mock.patch('sys.stdin.isatty')
@mock.patch.object(install, "load_or_install_jrnl") @mock.patch.object(install, "load_or_install_jrnl")
@mock.patch('subprocess.call')
def test_override_configured_editor( def test_override_configured_editor(
mock_load_or_install, mock_write_mode, minimal_config mock_subprocess_call,
mock_load_or_install,
mock_isatty,
minimal_config,
expected_override,
capsys
): ):
mock_load_or_install.return_value = minimal_config mock_load_or_install.return_value = minimal_config
mock_isatty.return_value = True
cli_args = ["--override", '{"editor": "nano"}'] cli_args = ["--override", '{"editor": "nano"}']
parser = parse_args(cli_args) parser = parse_args(cli_args)
assert parser.config_override.__len__() == 1 assert parser.config_override.__len__() == 1
with mock.patch("subprocess.call"): def mock_editor_launch(editor):
res = run(parser) print("%s launched! Success!"%editor)
assert res == None with mock.patch.object(jrnl,'_write_in_editor', side_effect=mock_editor_launch(parser.config_override['editor']), return_value = 'note_contents') as mock_write_in_editor:
run(parser)
mock_write_in_editor.assert_called_once_with(expected_override)