From 9bacbe8efbab13fd7b3acb03d7eb87962ebd47e2 Mon Sep 17 00:00:00 2001 From: Garulf <535299+Garulf@users.noreply.github.com> Date: Mon, 19 Feb 2024 06:15:50 -0500 Subject: [PATCH] Update tests --- tests/test_api.py | 38 +++++++++++++++++++++--------------- tests/test_id_generation.py | 7 +++++++ tests/test_jsonrpc.py | 30 ---------------------------- tests/test_jsonrpc_client.py | 17 ++++++++++++++++ tests/test_jsonrpc_server.py | 23 ++++++++++++++++++++++ 5 files changed, 69 insertions(+), 46 deletions(-) create mode 100644 tests/test_id_generation.py delete mode 100644 tests/test_jsonrpc.py create mode 100644 tests/test_jsonrpc_client.py create mode 100644 tests/test_jsonrpc_server.py diff --git a/tests/test_api.py b/tests/test_api.py index 3e2afe9..7b45a0a 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,61 +1,67 @@ from pyflowlauncher import api -def test_send_action(): - assert api._send_action("Test", "Test") == {"method": "Flow.Launcher.Test", "parameters": ("Test",)} +def check_output(func, method, parameters): + req = func(*parameters) + assert req["method"] == method + assert req["parameters"] == parameters + + +def test_get_namespace(): + assert api._get_namespace("Test") == "Flow.Launcher.Test" def test_change_query(): - assert api.change_query("Test", False) == {"method": "Flow.Launcher.ChangeQuery", "parameters": ("Test", False)} + check_output(api.change_query, "Flow.Launcher.ChangeQuery", ["Test", False]) def test_shell_run(): - assert api.shell_run("Test", "Test") == {"method": "Flow.Launcher.ShellRun", "parameters": ("Test", "Test")} + check_output(api.shell_run, "Flow.Launcher.ShellRun", ["Test", "Test"]) def test_close_app(): - assert api.close_app() == {"method": "Flow.Launcher.CloseApp", "parameters": ()} + check_output(api.close_app, "Flow.Launcher.CloseApp", []) def test_hide_app(): - assert api.hide_app() == {"method": "Flow.Launcher.HideApp", "parameters": ()} + check_output(api.hide_app, "Flow.Launcher.HideApp", []) def test_show_app(): - assert api.show_app() == {"method": "Flow.Launcher.ShowApp", "parameters": ()} + check_output(api.show_app, "Flow.Launcher.ShowApp", []) def test_show_msg(): - assert api.show_msg("Test", "Test", "Test") == {"method": "Flow.Launcher.ShowMsg", "parameters": ("Test", "Test", "Test")} + check_output(api.show_msg, "Flow.Launcher.ShowMsg", ["Test", "Test", "Test"]) def test_open_setting_dialog(): - assert api.open_setting_dialog() == {"method": "Flow.Launcher.OpenSettingDialog", "parameters": ()} + check_output(api.open_setting_dialog, "Flow.Launcher.OpenSettingDialog", []) def test_start_loading_bar(): - assert api.start_loading_bar() == {"method": "Flow.Launcher.StartLoadingBar", "parameters": ()} + check_output(api.start_loading_bar, "Flow.Launcher.StartLoadingBar", []) def test_stop_loading_bar(): - assert api.stop_loading_bar() == {"method": "Flow.Launcher.StopLoadingBar", "parameters": ()} + check_output(api.stop_loading_bar, "Flow.Launcher.StopLoadingBar", []) def test_reload_plugins(): - assert api.reload_plugins() == {"method": "Flow.Launcher.ReloadPlugins", "parameters": ()} + check_output(api.reload_plugins, "Flow.Launcher.ReloadPlugins", []) def test_copy_to_clipboard(): - assert api.copy_to_clipboard("Test", False, True) == {"method": "Flow.Launcher.CopyToClipboard", "parameters": ("Test", False, True)} + check_output(api.copy_to_clipboard, "Flow.Launcher.CopyToClipboard", ["Test", False, True]) def test_open_directory(): - assert api.open_directory("Test", "Test") == {"method": "Flow.Launcher.OpenDirectory", "parameters": ("Test", "Test")} + check_output(api.open_directory, "Flow.Launcher.OpenDirectory", ["Test", "Test"]) def test_open_url(): - assert api.open_url("Test", False) == {"method": "Flow.Launcher.OpenUrl", "parameters": ("Test", False)} + check_output(api.open_url, "Flow.Launcher.OpenUrl", ["Test", False]) def test_open_uri(): - assert api.open_uri("Test") == {"method": "Flow.Launcher.OpenAppUri", "parameters": ("Test",)} + check_output(api.open_uri, "Flow.Launcher.OpenUri", ["Test"]) diff --git a/tests/test_id_generation.py b/tests/test_id_generation.py new file mode 100644 index 0000000..cb98d4a --- /dev/null +++ b/tests/test_id_generation.py @@ -0,0 +1,7 @@ +from pyflowlauncher.jsonrpc import id_generation + + +def test_increment_int(): + id_gen = id_generation.incremental_int(1) + assert next(id_gen) == 1 + assert next(id_gen) == 2 diff --git a/tests/test_jsonrpc.py b/tests/test_jsonrpc.py deleted file mode 100644 index f9e183b..0000000 --- a/tests/test_jsonrpc.py +++ /dev/null @@ -1,30 +0,0 @@ -import sys -import pytest - -from pyflowlauncher.jsonrpc import JsonRPCClient - - -@pytest.fixture -def capture_stdout(monkeypatch): - buffer = {"stdout": "", "write_calls": 0} - - def fake_writes(s): - buffer["stdout"] += s - buffer["write_calls"] += 1 - - monkeypatch.setattr(sys.stdout, "write", fake_writes) - return buffer - - -def test_send(capture_stdout): - jsonrpc = JsonRPCClient() - jsonrpc.send({"method": "Test", "parameters": []}) - - assert capture_stdout["stdout"] == '{"method": "Test", "parameters": []}' - - -def test_recieve(monkeypatch): - jsonrpc = JsonRPCClient() - - monkeypatch.setattr(sys, "argv", ["test.py", '{"method": "Test", "parameters": []}']) - assert jsonrpc.recieve() == {"method": "Test", "parameters": []} diff --git a/tests/test_jsonrpc_client.py b/tests/test_jsonrpc_client.py new file mode 100644 index 0000000..9f72f4c --- /dev/null +++ b/tests/test_jsonrpc_client.py @@ -0,0 +1,17 @@ +from pyflowlauncher.jsonrpc import client + + +def test_create_request(): + assert client.create_request("Test", ["Test", False], id=1) == { + "method": "Test", "parameters": ["Test", False], "id": 1, "jsonrpc": "2.0"} + + +def test_request_from_string(): + assert client.request_from_string( + "Test", ["Test", False], id=1) == '{"jsonrpc": "2.0", "method": "Test", "parameters": ["Test", false], "id": 1}' + + +def test_send_request(capsys): + client.send_request(client.create_request("Test", ["Test", False], id=1)) + captured = capsys.readouterr() + assert captured.out == '{"jsonrpc": "2.0", "method": "Test", "parameters": ["Test", false], "id": 1}\n' diff --git a/tests/test_jsonrpc_server.py b/tests/test_jsonrpc_server.py new file mode 100644 index 0000000..06b6840 --- /dev/null +++ b/tests/test_jsonrpc_server.py @@ -0,0 +1,23 @@ +from pyflowlauncher.jsonrpc import server + + +def test_parse_request(): + assert server.parse_request('{"jsonrpc": "2.0", "method": "Test", "params": ["Test", false], "id": 1}') == { + "jsonrpc": "2.0", + "method": "Test", + "params": ["Test", False], + "id": 1, + } + + +def test_create_response(): + assert server.create_response("Test", 1) == { + "jsonrpc": "2.0", + "result": "Test", + "id": 1, + "SettingsChange": None, + } + + +def test_response(): + assert server.response("Test", 1) == '{"jsonrpc": "2.0", "result": "Test", "id": 1, "SettingsChange": null}'