Repository navigation
Expand file tree
/
Copy pathtest_function.py
More file actions
326 lines (249 loc) · 10.8 KB
/
Copy pathtest_function.py
File metadata and controls
326 lines (249 loc) · 10.8 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""Generic tests for TableFunctionGenerator behavior."""
from __future__ import annotations
import pyarrow as pa
import pytest
import structlog
from tests.utils import make_schema
from vgi.function import Arguments, Invocation
from vgi.table_function import (
CardinalityInfo,
Output,
OutputGenerator,
TableFunctionGenerator,
)
from vgi.testing import FunctionTestClientError, TableFunctionTestClient
class TestTableFunctionGeneratorProtocol:
"""Tests for the TableFunctionGenerator protocol."""
def test_empty_process_generator(self) -> None:
"""Default process() should produce no output."""
class EmptyFunction(TableFunctionGenerator):
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("x", pa.int64())])
with TableFunctionTestClient(EmptyFunction) as client:
outputs = list(client.table_function())
# StopIteration from empty generator means no outputs
assert len(outputs) == 0
def test_single_batch_output(self) -> None:
"""Function yielding single batch should work."""
class SingleBatchFunction(TableFunctionGenerator):
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("x", pa.int64())])
def process(self) -> OutputGenerator:
yield Output(
pa.RecordBatch.from_pydict(
{"x": [1, 2, 3]}, schema=self.output_schema
)
)
with TableFunctionTestClient(SingleBatchFunction) as client:
outputs = list(client.table_function())
assert len(outputs) == 1
assert outputs[0].column("x").to_pylist() == [1, 2, 3]
def test_multiple_batch_output(self) -> None:
"""Function yielding multiple batches should work."""
class MultiBatchFunction(TableFunctionGenerator):
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("n", pa.int64())])
def process(self) -> OutputGenerator:
for i in range(3):
yield Output(
pa.RecordBatch.from_pydict(
{"n": [i]}, schema=self.output_schema
)
)
with TableFunctionTestClient(MultiBatchFunction) as client:
outputs = list(client.table_function())
assert len(outputs) == 3
for i, batch in enumerate(outputs):
assert batch.column("n").to_pylist() == [i]
class TestTableFunctionGeneratorLifecycle:
"""Tests for the setup/teardown lifecycle."""
def test_setup_called_before_process(self) -> None:
"""setup() should be called before process() runs."""
call_order: list[str] = []
class LifecycleFunction(TableFunctionGenerator):
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("x", pa.int64())])
def setup(self) -> None:
call_order.append("setup")
def process(self) -> OutputGenerator:
call_order.append("process")
yield Output(
pa.RecordBatch.from_pydict({"x": [1]}, schema=self.output_schema)
)
def teardown(self) -> None:
call_order.append("teardown")
with TableFunctionTestClient(LifecycleFunction) as client:
list(client.table_function())
assert call_order == ["setup", "process", "teardown"]
def test_teardown_called_on_exception(self) -> None:
"""teardown() should be called even if process() raises."""
teardown_called = False
class ExceptionFunction(TableFunctionGenerator):
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("x", pa.int64())])
def process(self) -> OutputGenerator:
raise ValueError("test error")
yield # Make it a generator
def teardown(self) -> None:
nonlocal teardown_called
teardown_called = True
with (
TableFunctionTestClient(ExceptionFunction) as client,
pytest.raises(FunctionTestClientError),
):
list(client.table_function())
assert teardown_called
class TestTableFunctionGeneratorSchemaValidation:
"""Tests for output schema validation."""
def test_valid_schema_passes(self) -> None:
"""Output matching schema should pass validation."""
class ValidSchemaFunction(TableFunctionGenerator):
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("x", pa.int64())])
def process(self) -> OutputGenerator:
yield Output(
pa.RecordBatch.from_pydict({"x": [1]}, schema=self.output_schema)
)
with TableFunctionTestClient(ValidSchemaFunction) as client:
outputs = list(client.table_function())
assert len(outputs) == 1
def test_invalid_schema_raises(self) -> None:
"""Output with wrong schema should raise SchemaValidationError."""
class InvalidSchemaFunction(TableFunctionGenerator):
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("x", pa.int64())])
def process(self) -> OutputGenerator:
# Return batch with wrong column name
wrong_schema = make_schema([pa.field("y", pa.int64())])
wrong_batch = pa.RecordBatch.from_pydict(
{"y": [1]}, schema=wrong_schema
)
yield Output(wrong_batch)
with (
TableFunctionTestClient(InvalidSchemaFunction) as client,
pytest.raises(FunctionTestClientError) as exc_info,
):
list(client.table_function())
# The exception message should mention schema mismatch
assert "schema" in str(exc_info.value).lower()
class TestTableFunctionGeneratorCardinality:
"""Tests for cardinality hints."""
def test_default_cardinality_is_none(self) -> None:
"""Default cardinality() should return None."""
class NoCardinalityFunction(TableFunctionGenerator):
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("x", pa.int64())])
invocation = Invocation(
function_name="test",
arguments=Arguments(),
in_out_function_input_schema=None,
correlation_id="test",
invocation_id=b"test",
)
func = NoCardinalityFunction(
invocation=invocation,
logger=structlog.get_logger(),
)
assert func.cardinality() is None
def test_custom_cardinality(self) -> None:
"""Custom cardinality() should be respected."""
class CardinalityFunction(TableFunctionGenerator):
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("x", pa.int64())])
def cardinality(self) -> CardinalityInfo:
return CardinalityInfo(estimate=100, max=1000)
invocation = Invocation(
function_name="test",
arguments=Arguments(),
in_out_function_input_schema=None,
correlation_id="test",
invocation_id=b"test",
)
func = CardinalityFunction(
invocation=invocation,
logger=structlog.get_logger(),
)
cardinality = func.cardinality()
assert cardinality is not None
assert cardinality.estimate == 100
assert cardinality.max == 1000
class TestTableFunctionGeneratorArguments:
"""Tests for argument handling."""
def test_positional_arguments(self) -> None:
"""Function should receive positional arguments."""
from vgi.arguments import Arg
class ArgFunction(TableFunctionGenerator):
count: int = Arg[int](0, doc="Count") # type: ignore[assignment]
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("n", pa.int64())])
def process(self) -> OutputGenerator:
yield Output(
pa.RecordBatch.from_pydict(
{"n": list(range(self.count))}, schema=self.output_schema
)
)
with TableFunctionTestClient(ArgFunction) as client:
outputs = list(
client.table_function(arguments=Arguments(positional=(pa.scalar(5),)))
)
assert len(outputs) == 1
assert outputs[0].column("n").to_pylist() == [0, 1, 2, 3, 4]
def test_named_arguments(self) -> None:
"""Function should receive named arguments."""
from vgi.arguments import Arg
class NamedArgFunction(TableFunctionGenerator):
value: int = Arg[int]("value", doc="Value") # type: ignore[assignment]
@property
def output_schema(self) -> pa.Schema:
return make_schema([pa.field("result", pa.int64())])
def process(self) -> OutputGenerator:
yield Output(
pa.RecordBatch.from_pydict(
{"result": [self.value * 2]}, schema=self.output_schema
)
)
with TableFunctionTestClient(NamedArgFunction) as client:
outputs = list(
client.table_function(
arguments=Arguments(named={"value": pa.scalar(21)})
)
)
assert len(outputs) == 1
assert outputs[0].column("result").to_pylist() == [42]
class TestTableFunctionGeneratorEmptyBatch:
"""Tests for empty batch handling."""
def test_empty_output_batch_property(self) -> None:
"""empty_output_batch should have correct schema and zero rows."""
class TestFunction(TableFunctionGenerator):
@property
def output_schema(self) -> pa.Schema:
return make_schema(
[
pa.field("a", pa.int64()),
pa.field("b", pa.string()),
]
)
invocation = Invocation(
function_name="test",
arguments=Arguments(),
in_out_function_input_schema=None,
correlation_id="test",
invocation_id=b"test",
)
func = TestFunction(
invocation=invocation,
logger=structlog.get_logger(),
)
empty = func.empty_output_batch
assert empty.num_rows == 0
assert empty.schema == func.output_schema