tests: test commands

This commit is contained in:
cătălin 2023-04-03 18:40:11 +02:00
commit 570c29d9f1
Signed by: catalin
GPG key ID: 686088EF78EE4083
16 changed files with 235 additions and 52 deletions

View file

@ -0,0 +1,63 @@
from pathlib import Path
import pendulum
import pytest as pytest
from halig import utils
from halig.commands.notebooks import NotebooksCommand
from halig.settings import Settings
@pytest.fixture()
def notes(notebooks_path: Path):
personal = (notebooks_path / "Personal")
work = (notebooks_path / "Work")
personal.mkdir()
work.mkdir()
personal_todos = (personal / "todos.age")
personal_todos.touch()
work_todos = (work / "todos.age")
work_todos.touch()
dailies = work / "Dailies"
dailies.mkdir()
dt = pendulum.now()
for day_offset in range(10):
dt = dt.subtract(days=day_offset)
(dailies / f"{dt.date()}.age").touch()
@pytest.fixture
def notebooks_command(settings: Settings):
return NotebooksCommand(max_depth=float("inf"), settings=settings)
@pytest.fixture()
def current_note(notes, settings, encryptor) -> Path:
note_path = settings.notebooks_root_path / f"{utils.now().date()}.age"
note_path.touch()
data = encryptor.encrypt("foo".encode())
with note_path.open("wb") as f:
f.write(data)
return note_path
@pytest.fixture
def current_daily(notes, settings, encryptor) -> Path:
note_path = settings.notebooks_root_path / "Work" / "Dailies" / f"{utils.now().date()}.age"
data = encryptor.encrypt("foo".encode())
with note_path.open("wb") as f:
f.write(data)
return note_path
@pytest.fixture
def mock_edit(mocker):
def edit(callargs: list):
with open(callargs[1], "wb") as f:
f.write("edited".encode())
mocker.patch('halig.commands.edit.subprocess.call', side_effect=edit)

View file

@ -0,0 +1,30 @@
import pytest
from halig.commands.edit import EditCommand
from halig.settings import Settings
def test_edit_raises_invalid_age_file(notes, settings: Settings):
note_path = settings.notebooks_root_path / "foo.txt"
note_path.touch()
with pytest.raises(ValueError, match="is not a valid AGE file"):
EditCommand(note_path, settings=settings, )
def test_edit_current_note(mock_edit, current_note, settings: Settings, encryptor):
edit_command = EditCommand(note_path=settings.notebooks_root_path, settings=settings)
assert edit_command.note_path == current_note
edit_command.run()
with current_note.open("rb") as f:
contents = encryptor.decrypt(f.read()).decode()
assert contents == "edited"
def test_edit_current_daily(mock_edit, current_daily, settings, encryptor):
current_daily.unlink()
edit_command = EditCommand(note_path=current_daily, settings=settings)
assert edit_command.note_path == current_daily
edit_command.run()
with current_daily.open("rb") as f:
contents = encryptor.decrypt(f.read()).decode()
assert contents == "edited"

View file

@ -0,0 +1,42 @@
from halig.commands.notebooks import NotebooksCommand
def test_build_tree_max_depth_0(notes, notebooks_command: NotebooksCommand):
notebooks_command.max_depth = 0
tree = notebooks_command.build_tree(notebooks_command.settings.notebooks_root_path)
assert not tree.children
def test_build_tree_max_depth_1(notes, notebooks_command: NotebooksCommand):
notebooks_command.max_depth = 1
tree = notebooks_command.build_tree(notebooks_command.settings.notebooks_root_path)
personal = tree.children[0]
work = tree.children[1]
assert personal.label == "Personal"
assert work.label == "Work"
assert not personal.children
assert not work.children
def test_build_tree_max_depth_2(notes, notebooks_command: NotebooksCommand):
notebooks_command.max_depth = 2
tree = notebooks_command.build_tree(notebooks_command.settings.notebooks_root_path)
personal = tree.children[0]
work = tree.children[1]
assert personal.label == "Personal"
assert work.label == "Work"
assert len(work.children) == 2
assert len(personal.children) == 1
def test_build_tree_max_depth_inf(notes, notebooks_command: NotebooksCommand):
tree = notebooks_command.build_tree(notebooks_command.settings.notebooks_root_path)
personal = tree.children[0]
work = tree.children[1]
assert personal.label == "Personal"
assert work.label == "Work"
assert len(work.children) == 2
assert len(personal.children) == 1
assert work.children[0].label == "Dailies"
assert len(work.children[0].children) == 10

View file

@ -0,0 +1,30 @@
from pathlib import Path
import pytest
from halig.commands.show import ShowCommand
from halig.settings import Settings
def test_show_raises_note_path_does_not_exist(notes, settings: Settings):
with pytest.raises(ValueError, match="does not exist"):
ShowCommand(Path('foo'), settings=settings, )
def test_show_raises_note_path_is_not_age_valid(notes, settings: Settings):
note_path = settings.notebooks_root_path / "foo.txt"
note_path.touch()
with pytest.raises(ValueError, match="is not a valid AGE file"):
ShowCommand(note_path, settings=settings, )
def test_show_current_note(current_note, settings):
show_command = ShowCommand(note_path=settings.notebooks_root_path, settings=settings)
assert show_command.note_path == current_note
assert show_command.decrypt() == "foo"
def test_show_current_daily(current_daily, settings: Settings):
show_command = ShowCommand(note_path=current_daily, settings=settings)
assert show_command.note_path == current_daily
assert show_command.decrypt() == "foo"

View file

@ -41,6 +41,7 @@ def ssh_recipient(halig_ssh_public_key: str) -> Recipient:
@pytest.fixture()
def halig_path(fs, halig_ssh_public_key, halig_ssh_private_key) -> Path:
fs.add_real_paths(["/etc/localtime"])
ssh_path = Path("~/.ssh").expanduser()
ssh_path.mkdir(parents=True)