merge in develop

This commit is contained in:
Jonathan Wren 2022-06-04 19:28:11 -07:00
commit 15b86cae97
30 changed files with 827 additions and 222 deletions

View file

@ -1,25 +0,0 @@
import argparse
from unittest import mock
import pytest
import jrnl
from jrnl.jrnl import _display_search_results
# fmt: off
# see: https://github.com/psf/black/issues/664
@pytest.mark.parametrize("export_format", [ "pretty", "short","markdown"])
#fmt: on
@mock.patch.object(argparse, "Namespace", return_value={"export": "markdown", "filename": "irrele.vant"})
def test_export_format(mock_args, export_format):
test_journal = jrnl.Journal.Journal
mock_args.export = export_format
#fmt: off
# see: https://github.com/psf/black/issues/664
with mock.patch("builtins.print") as mock_spy_print, \
mock.patch('jrnl.Journal.Journal.pprint') as mock_pprint:
_display_search_results(mock_args, test_journal)
mock_spy_print.assert_called_once_with(mock_pprint())
#fmt: on

48
tests/unit/test_jrnl.py Normal file
View file

@ -0,0 +1,48 @@
from unittest import mock
import pytest
import random
import string
import jrnl
from jrnl.jrnl import _display_search_results
from jrnl.args import parse_args
@pytest.fixture
def random_string():
return "".join(random.choices(string.ascii_uppercase + string.digits, k=25))
@pytest.mark.parametrize("export_format", ["pretty", "short"])
@mock.patch("builtins.print")
@mock.patch("jrnl.Journal.Journal.pprint")
def test_display_search_results_pretty_short(mock_pprint, mock_print, export_format):
mock_args = parse_args(["--format", export_format])
test_journal = mock.Mock(wraps=jrnl.Journal.Journal)
_display_search_results(mock_args, test_journal)
mock_print.assert_called_once_with(mock_pprint.return_value)
@pytest.mark.parametrize(
"export_format", ["markdown", "json", "xml", "yaml", "fancy", "dates"]
)
@mock.patch("jrnl.plugins.get_exporter")
@mock.patch("builtins.print")
def test_display_search_results_builtin_plugins(
mock_print, mock_exporter, export_format, random_string
):
test_filename = random_string
mock_args = parse_args(["--format", export_format, "--file", test_filename])
test_journal = mock.Mock(wraps=jrnl.Journal.Journal)
mock_export = mock.Mock()
mock_exporter.return_value.export = mock_export
_display_search_results(mock_args, test_journal)
mock_exporter.assert_called_once_with(export_format)
mock_export.assert_called_once_with(test_journal, test_filename)
mock_print.assert_called_once_with(mock_export.return_value)

View file

@ -17,6 +17,7 @@ def expected_args(**kwargs):
"contains": None,
"debug": False,
"delete": False,
"change_time": None,
"edit": False,
"end_date": None,
"today_in_history": False,
@ -58,6 +59,13 @@ def test_delete_alone():
assert cli_as_dict("--delete") == expected_args(delete=True)
def test_change_time_alone():
assert cli_as_dict("--change-time") == expected_args(change_time="now")
assert cli_as_dict("--change-time yesterday") == expected_args(
change_time="yesterday"
)
def test_diagnostic_alone():
from jrnl.commands import preconfig_diagnostic

103
tests/unit/test_path.py Normal file
View file

@ -0,0 +1,103 @@
import pytest
import random
import string
from os import getenv
from unittest.mock import patch
from jrnl.path import home_dir
from jrnl.path import expand_path
from jrnl.path import absolute_path
@pytest.fixture
def home_dir_str(monkeypatch):
username = "username"
monkeypatch.setenv("USERPROFILE", username) # for windows
monkeypatch.setenv("HOME", username) # for *nix
return username
@pytest.fixture
def random_test_var(monkeypatch):
name = f"JRNL_TEST_{''.join(random.sample(string.ascii_uppercase, 10))}"
val = "".join(random.sample(string.ascii_lowercase, 25))
monkeypatch.setenv(name, val)
return (name, val)
def test_home_dir(home_dir_str):
assert home_dir() == home_dir_str
@pytest.mark.on_posix
@pytest.mark.parametrize(
"path",
["~"],
)
def test_expand_path_actually_expands_mac_linux(path):
# makes sure that path isn't being returns as-is
assert expand_path(path) != path
@pytest.mark.on_win
@pytest.mark.parametrize(
"path",
["~", "%USERPROFILE%"],
)
def test_expand_path_actually_expands_windows(path):
# makes sure that path isn't being returns as-is
assert expand_path(path) != path
@pytest.mark.on_posix
@pytest.mark.parametrize(
"paths",
[
["~", "HOME"],
],
)
def test_expand_path_expands_into_correct_value_mac_linux(paths):
input_path, expected_path = paths[0], paths[1]
assert expand_path(input_path) == getenv(expected_path)
@pytest.mark.on_win
@pytest.mark.parametrize(
"paths",
[
["~", "USERPROFILE"],
["%USERPROFILE%", "USERPROFILE"],
],
)
def test_expand_path_expands_into_correct_value_windows(paths):
input_path, expected_path = paths[0], paths[1]
assert expand_path(input_path) == getenv(expected_path)
@pytest.mark.on_posix
@pytest.mark.parametrize("_", range(25))
def test_expand_path_expands_into_random_env_value_mac_linux(_, random_test_var):
var_name, var_value = random_test_var[0], random_test_var[1]
assert expand_path(var_name) == var_name
assert expand_path(f"${var_name}") == var_value # mac & linux
assert expand_path(f"${var_name}") == getenv(var_name)
@pytest.mark.on_win
@pytest.mark.parametrize("_", range(25))
def test_expand_path_expands_into_random_env_value_windows(_, random_test_var):
var_name, var_value = random_test_var[0], random_test_var[1]
assert expand_path(var_name) == var_name
assert expand_path(f"%{var_name}%") == var_value # windows
assert expand_path(f"%{var_name}%") == getenv(var_name)
@patch("jrnl.path.expand_path")
@patch("os.path.abspath")
def test_absolute_path(mock_abspath, mock_expand_path):
test_val = "test_value"
assert absolute_path(test_val) == mock_abspath.return_value
mock_expand_path.assert_called_with(test_val)
mock_abspath.assert_called_with(mock_expand_path.return_value)