97 lines
3.1 KiB
Python
97 lines
3.1 KiB
Python
import io
|
|
import sys
|
|
import unittest
|
|
from contextlib import redirect_stdout
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
|
if str(PROJECT_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
from journal.cli import main as cli_main
|
|
|
|
|
|
class CliFragmentsHybridTests(unittest.TestCase):
|
|
def test_fragments_list_calls_sidecar(self):
|
|
with (
|
|
patch.object(sys, "argv", ["journal", "fragments", "list"]),
|
|
patch("journal.cli.main.BACKEND_MODE", "csharp-hybrid"),
|
|
patch(
|
|
"journal.cli.main.list_fragments",
|
|
return_value=[{"Id": "1", "Type": "!NOTE", "Description": "desc", "Tags": []}],
|
|
) as mock_call,
|
|
redirect_stdout(io.StringIO()) as stdout,
|
|
):
|
|
cli_main.main()
|
|
|
|
mock_call.assert_called_once_with()
|
|
self.assertIn("!NOTE", stdout.getvalue())
|
|
|
|
def test_fragments_create_calls_sidecar(self):
|
|
with (
|
|
patch.object(
|
|
sys,
|
|
"argv",
|
|
[
|
|
"journal",
|
|
"fragments",
|
|
"create",
|
|
"--type",
|
|
"!TRIGGER",
|
|
"--description",
|
|
"flashback",
|
|
"--tag",
|
|
"stress",
|
|
],
|
|
),
|
|
patch("journal.cli.main.BACKEND_MODE", "csharp-hybrid"),
|
|
patch("journal.cli.main.create_fragment", return_value={"Id": "1"}) as mock_call,
|
|
redirect_stdout(io.StringIO()) as stdout,
|
|
):
|
|
cli_main.main()
|
|
|
|
mock_call.assert_called_once_with(
|
|
type_="!TRIGGER",
|
|
description="flashback",
|
|
time=None,
|
|
tags=["stress"],
|
|
)
|
|
self.assertIn("Fragment created.", stdout.getvalue())
|
|
|
|
def test_fragments_search_calls_sidecar_with_command_fields(self):
|
|
with (
|
|
patch.object(
|
|
sys,
|
|
"argv",
|
|
["journal", "fragments", "search", "--type", "!NOTE", "--tag", "daily"],
|
|
),
|
|
patch("journal.cli.main.BACKEND_MODE", "csharp-hybrid"),
|
|
patch("journal.cli.main.search_fragments", return_value=[]) as mock_call,
|
|
redirect_stdout(io.StringIO()) as stdout,
|
|
):
|
|
cli_main.main()
|
|
|
|
mock_call.assert_called_once_with(
|
|
type_="!NOTE",
|
|
tag="daily",
|
|
)
|
|
self.assertIn("No fragments found.", stdout.getvalue())
|
|
|
|
def test_fragments_command_requires_hybrid(self):
|
|
with (
|
|
patch.object(sys, "argv", ["journal", "fragments", "list"]),
|
|
patch("journal.cli.main.BACKEND_MODE", "python"),
|
|
patch("journal.cli.main.list_fragments") as mock_call,
|
|
redirect_stdout(io.StringIO()) as stdout,
|
|
):
|
|
cli_main.main()
|
|
|
|
mock_call.assert_not_called()
|
|
self.assertIn("requires JOURNAL_BACKEND_MODE=csharp-hybrid", stdout.getvalue())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|