jrnl/tests/unit/test_override.py
Jonathan Wren 3c923ae943
Allow combinations of --change-time, --delete, and --edit while correctly counting the number of entries affected (#1669)
* Remove search mode conditional that added explicit tag search behavior
* Fix failing change-time test by using same method signature as base journal class
* Fix user input mock - was not appropriately checking return value
* Clean up controller
  - streamline `run` function in `controller.py`
  - add debug logging
  - fix unnecessary import of Journal class (only needed for typing)
  - standardize summary display across different actions
* Add currently-failing test conditions for count messages when changing time and deleting
* Don't show summary if no entries found and prevent extra line break when no entries found by short-circuiting display method
* Track found entry count and remove incorrect modified stat logic
* Track journal entry deletion consistently
* Remove unneeded exception when editor is empty and fix test that was testing incorrect message
* Correct entry edit modified count test
* Track modification of entries with --change-time
* Preserve existing behavior when editor is empty but make the message more clear
* Reconcile tests with new error message when clearing editor in edit mode
* Add found/modified counts to edit tests
* Add tests for found count with -n equivalent argument
* Test combinations of found/deleted messages when using --delete
* Add tests for counting combinations of action arguments (change-time, edit, delete) and for change-time counts. Some are failing and should be investigated
* Remove extraneous comment in test
* Track added/deleted counts in a register in the Journal class instead of attempting to infer it via controller counting
* Add encrypted to more tests
* Fix merge conflict typo
* Change 'write mode' -> 'append mode' in more places

---------

Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>
2023-03-25 12:32:25 -07:00

111 lines
3.1 KiB
Python

# Copyright © 2012-2023 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
from argparse import Namespace
import pytest
from jrnl.override import _convert_dots_to_list
from jrnl.override import _get_config_node
from jrnl.override import _get_key_and_value_from_pair
from jrnl.override import _recursively_apply
from jrnl.override import apply_overrides
@pytest.fixture()
def minimal_config():
cfg = {
"colors": {"body": "red", "date": "green"},
"default": "/tmp/journal.jrnl",
"editor": "vim",
"journals": {"default": "/tmp/journals/journal.jrnl"},
}
return cfg
def expected_args(overrides):
default_args = {
"contains": None,
"debug": False,
"delete": False,
"edit": False,
"end_date": None,
"today_in_history": False,
"month": None,
"day": None,
"year": None,
"excluded": [],
"export": False,
"filename": None,
"limit": None,
"on_date": None,
"preconfig_cmd": None,
"postconfig_cmd": None,
"short": False,
"starred": False,
"start_date": None,
"strict": False,
"tags": False,
"text": [],
"config_override": [],
}
return Namespace(**{**default_args, **overrides})
def test_apply_override(minimal_config):
overrides = {"config_override": [["editor", "nano"]]}
apply_overrides(expected_args(overrides), minimal_config)
assert minimal_config["editor"] == "nano"
def test_override_dot_notation(minimal_config):
overrides = {"config_override": [["colors.body", "blue"]]}
apply_overrides(expected_args(overrides), minimal_config)
assert minimal_config["colors"] == {"body": "blue", "date": "green"}
def test_multiple_overrides(minimal_config):
overrides = {
"config_override": [
["colors.title", "magenta"],
["editor", "nano"],
["journals.burner", "/tmp/journals/burner.jrnl"],
]
}
actual = apply_overrides(expected_args(overrides), minimal_config)
assert actual["editor"] == "nano"
assert actual["colors"]["title"] == "magenta"
assert "burner" in actual["journals"]
assert actual["journals"]["burner"] == "/tmp/journals/burner.jrnl"
def test_recursively_apply():
cfg = {"colors": {"body": "red", "title": "green"}}
cfg = _recursively_apply(cfg, ["colors", "body"], "blue")
assert cfg["colors"]["body"] == "blue"
def test_get_config_node(minimal_config):
assert len(minimal_config.keys()) == 4
assert _get_config_node(minimal_config, "editor") == "vim"
assert _get_config_node(minimal_config, "display_format") is None
def test_get_kv_from_pair():
pair = {"ab.cde": "fgh"}
k, v = _get_key_and_value_from_pair(pair)
assert k == "ab.cde"
assert v == "fgh"
class TestDotNotationToList:
def test_unpack_dots_to_list(self):
keys = "a.b.c.d.e.f"
keys_list = _convert_dots_to_list(keys)
assert len(keys_list) == 6
def test_sequential_delimiters(self):
k = "g.r..h.v"
k_l = _convert_dots_to_list(k)
assert len(k_l) == 4