forked from balisujohn/localwriter
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_executor.py
More file actions
164 lines (135 loc) · 5.71 KB
/
Copy pathtest_executor.py
File metadata and controls
164 lines (135 loc) · 5.71 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2024 John Balis
# Copyright (c) 2026 KeithCu (modifications and relicensing)
#
# Based on code from the LibrePythonista project (Apache 2.0)
# Source: https://github.com/Amourspirit/python-libre-pythonista-ext/blob/main/tests/test_code/test_code_ast.py
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
import unittest.mock
from unittest.mock import MagicMock
from plugin.calc.python.executor import PythonExecutor, ExecutePythonScript
def test_arithmetic():
executor = PythonExecutor("test_url")
assert executor.execute_with_return("1 + 1") == 2
assert executor.execute_with_return("x = 10; x * 2") == 20
def test_assignment():
executor = PythonExecutor("test_url")
# Last line is an assignment
assert executor.execute_with_return("y = 100") == 100
# Verify it's in the state
assert executor.executor.state["y"] == 100
def test_no_persistence_across_executor_instances():
e1 = PythonExecutor("test_url")
e1.execute_with_return("z = 5")
e2 = PythonExecutor("test_url")
from plugin.framework.errors import WriterAgentException
with pytest.raises(WriterAgentException):
e2.execute_with_return("z + 10")
def test_fn_definition():
executor = PythonExecutor("test_url")
code = """
def add_five(n):
return n + 5
add_five(10)
"""
assert executor.execute_with_return(code) == 15
def test_class_definition():
executor = PythonExecutor("test_url")
code = """
class Counter:
def __init__(self):
self.count = 0
def inc(self):
self.count += 1
return self.count
c = Counter()
c.inc()
c.inc()
"""
assert executor.execute_with_return(code) == 2
def test_syntax_error():
executor = PythonExecutor("test_url")
from plugin.framework.errors import WriterAgentException
with pytest.raises(WriterAgentException) as excinfo:
executor.execute_with_return("if x")
assert excinfo.value.code == "PYTHON_EXECUTION_ERROR"
def test_tool_integration():
ctx = MagicMock()
ctx.doc.getURL.return_value = "file:///test.ods"
# Mocking the bridge and manipulator
# Note: These are instantiated inside execute() so we'll need to mock them if we want to verify calls
# but for a basic integration test we can just ensure they don't crash.
tool = ExecutePythonScript()
# First call
res1 = tool.execute(ctx, code="val = 42")
assert res1["status"] == "ok"
assert res1["result"] == 42
# Second call: fresh environment (no val from first call)
from plugin.framework.errors import WriterAgentException
with pytest.raises(WriterAgentException):
tool.execute(ctx, code="val + 8")
def test_formatting():
"""format_result stringifies custom objects; class must live in executor env (same snippet)."""
executor = PythonExecutor("test")
code = """
class MyObj:
def __str__(self): return "CUSTOM_OBJ"
MyObj()
"""
res = executor.execute_with_return(code)
assert "CUSTOM_OBJ" in res
def test_native_helpers():
# This requires more complex mocking since bridge/manipulator are used
executor = PythonExecutor("test")
bridge = MagicMock()
manipulator = MagicMock()
inspector = MagicMock()
manipulator.safe_get_cell_value.return_value = "HELLO"
inspector.read_range.return_value = [[{"value": 1}, {"value": 2}]]
executor.inject_helpers(bridge, manipulator, inspector)
# Test reading cell
assert executor.execute_with_return("lp('A1')") == "HELLO"
# Test reading range
assert executor.execute_with_return("Sheet('A1:B1')") == [[1, 2]]
# Test writing
executor.execute_with_return("set_range('C1', 123)")
manipulator.write_formula_range.assert_called_with('C1', 123)
def test_target_range_tool():
from plugin.calc.python.executor import ExecutePythonScript
tool = ExecutePythonScript()
ctx = MagicMock()
ctx.doc.getURL.return_value = "file:///test.ods"
# We will need to patch CellManipulator to verify the write
with unittest.mock.patch('plugin.calc.python.executor.CellManipulator') as MockManip:
inst = MockManip.return_value
inst.write_formula_range.return_value = "Success"
res = tool.execute(ctx, code="10 + 20", target_range="D1")
assert res["result"] == 30
assert res["write_status"] == "Success"
inst.write_formula_range.assert_called_with("D1", 30)
def test_data_range_injection():
from plugin.calc.python.executor import ExecutePythonScript
tool = ExecutePythonScript()
ctx = MagicMock()
ctx.doc.getURL.return_value = "file:///test.ods"
with unittest.mock.patch('plugin.calc.python.executor.CellInspector') as MockInspector:
inst = MockInspector.return_value
inst.read_range.return_value = [[{"value": 10}, {"value": 20}, {"value": 30}]]
# Test passing data_range which is a row (gets flattened to flat list)
code = "result = 0\nfor x in data:\n result += x\nresult"
res = tool.execute(ctx, code=code, data_range="A1:C1")
assert res["result"] == 60
inst.read_range.assert_called_with("A1:C1")