Repository navigation
Expand file tree
/
Copy pathtest_repeat_inputs_function.py
More file actions
128 lines (106 loc) · 4.8 KB
/
Copy pathtest_repeat_inputs_function.py
File metadata and controls
128 lines (106 loc) · 4.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
"""Tests for the RepeatInputsFunction (explosion)."""
from __future__ import annotations
import pyarrow as pa
from tests.utils import make_schema
from vgi.client import Client
from vgi.function import Arguments
class TestRepeatInputsFunction:
"""Tests for the repeat_inputs function (explosion)."""
def test_repeat_custom_count(
self, example_worker: str, simple_batches: list[pa.RecordBatch]
) -> None:
"""Should respect custom repeat count argument."""
repeat_count = 3
with Client(example_worker) as client:
output_batches = list(
client.table_in_out_function(
function_name="repeat_inputs",
arguments=Arguments(positional=(pa.scalar(repeat_count),)),
input=iter(simple_batches),
)
)
total_input_rows = sum(b.num_rows for b in simple_batches)
total_output_rows = sum(b.num_rows for b in output_batches)
assert total_output_rows == total_input_rows * repeat_count
def test_repeat_single_time(
self, example_worker: str, simple_batches: list[pa.RecordBatch]
) -> None:
"""Repeat count of 1 should act like echo."""
with Client(example_worker) as client:
output_batches = list(
client.table_in_out_function(
function_name="repeat_inputs",
arguments=Arguments(positional=(pa.scalar(1),), named={}),
input=iter(simple_batches),
)
)
total_input_rows = sum(b.num_rows for b in simple_batches)
total_output_rows = sum(b.num_rows for b in output_batches)
assert total_output_rows == total_input_rows
def test_repeat_distributed_many_batches(self, example_worker: str) -> None:
"""Should correctly repeat across many batches with multiple workers."""
schema = make_schema([pa.field("a", pa.int64()), pa.field("b", pa.float64())])
# Create 100 batches, each with 50 rows
num_batches = 100
rows_per_batch = 50
repeat_count = 3
batches = []
for batch_idx in range(num_batches):
start = batch_idx * rows_per_batch
end = (batch_idx + 1) * rows_per_batch
a_values = list(range(start, end))
b_values = [float(v) * 0.5 for v in a_values]
batch = pa.RecordBatch.from_pydict(
{"a": a_values, "b": b_values}, schema=schema
)
batches.append(batch)
with Client(example_worker) as client:
output_batches = list(
client.table_in_out_function(
function_name="repeat_inputs",
arguments=Arguments(positional=(pa.scalar(repeat_count),)),
input=iter(batches),
)
)
total_input_rows = sum(b.num_rows for b in batches)
total_output_rows = sum(b.num_rows for b in output_batches)
assert total_output_rows == total_input_rows * repeat_count
def test_repeat_distributed_preserves_data(self, example_worker: str) -> None:
"""Should preserve data correctly when repeated across workers."""
schema = make_schema(
[pa.field("id", pa.int64()), pa.field("value", pa.string())]
)
# Create batches with distinct values to verify data integrity
batches = [
pa.RecordBatch.from_pydict(
{"id": [1, 2, 3], "value": ["a", "b", "c"]}, schema=schema
),
pa.RecordBatch.from_pydict(
{"id": [4, 5, 6], "value": ["d", "e", "f"]}, schema=schema
),
pa.RecordBatch.from_pydict(
{"id": [7, 8, 9], "value": ["g", "h", "i"]}, schema=schema
),
]
repeat_count = 2
with Client(example_worker) as client:
output_batches = list(
client.table_in_out_function(
function_name="repeat_inputs",
arguments=Arguments(positional=(pa.scalar(repeat_count),)),
input=iter(batches),
)
)
# Combine all outputs into a table
table = pa.Table.from_batches(output_batches)
result = table.to_pydict()
# Each id should appear exactly repeat_count times
for expected_id in range(1, 10):
count = result["id"].count(expected_id)
assert count == repeat_count, f"id {expected_id} appeared {count} times"
# Each value should appear exactly repeat_count times
for expected_value in ["a", "b", "c", "d", "e", "f", "g", "h", "i"]:
count = result["value"].count(expected_value)
assert count == repeat_count, (
f"value {expected_value} appeared {count} times"
)