forked from balisujohn/localwriter
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_venv_image.py
More file actions
58 lines (46 loc) · 2.11 KB
/
Copy pathtest_venv_image.py
File metadata and controls
58 lines (46 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# WriterAgent - tests for run_venv_python_script image handling
from __future__ import annotations
from unittest.mock import MagicMock, patch
from plugin.calc.python.venv import RunVenvPythonScript
from plugin.scripting.payload_codec import PAYLOAD_IMAGE
_IMAGE_PAYLOAD = {
"__wa_payload__": PAYLOAD_IMAGE,
"format": "svg",
"data": b"<svg xmlns='http://www.w3.org/2000/svg'></svg>",
}
def test_calc_image_result_inserts_on_sheet():
tool = RunVenvPythonScript()
ctx = MagicMock()
ctx.doc_type = "calc"
ctx.ctx = MagicMock()
with (
patch("plugin.calc.python.venv.run_code_in_user_venv", return_value={"status": "ok", "result": _IMAGE_PAYLOAD}),
patch("plugin.calc.python.venv.write_image_payload_to_temp", return_value="/tmp/plot.svg"),
patch(
"plugin.framework.queue_executor.execute_on_main_thread",
side_effect=lambda fn, *args, **kwargs: fn(*args, **kwargs),
) as main_thread,
patch("plugin.calc.python.image_egress.insert_image_result_on_sheet") as insert,
):
out = tool.execute(ctx, code="import matplotlib.pyplot as plt\nplt.plot([1])")
assert out["status"] == "ok"
assert out["image_inserted"] is True
assert out["image_path"] == "/tmp/plot.svg"
assert "active sheet" in out["message"]
assert main_thread.call_count == 1
insert.assert_called_once_with(ctx.ctx, _IMAGE_PAYLOAD)
def test_writer_image_result_returns_path_only():
tool = RunVenvPythonScript()
ctx = MagicMock()
ctx.doc_type = "writer"
ctx.ctx = MagicMock()
with (
patch("plugin.calc.python.venv.run_code_in_user_venv", return_value={"status": "ok", "result": _IMAGE_PAYLOAD}),
patch("plugin.calc.python.venv.write_image_payload_to_temp", return_value="/tmp/plot.svg"),
patch("plugin.calc.python.image_egress.insert_image_result_on_sheet") as insert,
):
out = tool.execute(ctx, code="import matplotlib.pyplot as plt\nplt.plot([1])")
assert out["status"] == "ok"
assert out.get("image_inserted") is None
assert out["image_path"] == "/tmp/plot.svg"
insert.assert_not_called()