This repository was archived by the owner on Mar 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathexecution_test.py
More file actions
172 lines (140 loc) · 4.89 KB
/
execution_test.py
File metadata and controls
172 lines (140 loc) · 4.89 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
from datetime import datetime
from typing import AsyncIterator, Iterator
import kaskada as kd
import pandas as pd
import pytest
import asyncio
@pytest.fixture
async def source_int64() -> kd.sources.CsvString:
content = "\n".join(
[
"time,key,m,n",
"1996-12-19T16:39:57,A,5,10",
"1996-12-19T16:39:58,B,24,3",
"1996-12-19T16:39:59,A,17,6",
"1996-12-19T16:40:00,A,,9",
"1996-12-19T16:40:01,A,12,",
"1996-12-19T16:40:02,A,,",
]
)
return await kd.sources.CsvString.create(
content, time_column="time", key_column="key"
)
async def test_iter_pandas(golden, source_int64) -> None:
batches = source_int64.run_iter(row_limit=4, max_batch_size=2)
# 4 rows, max 2 per batch = 2 batches
golden.jsonl(next(batches))
golden.jsonl(next(batches))
with pytest.raises(StopIteration):
next(batches)
async def test_iter_rows(source_int64) -> None:
results: Iterator[dict] = source_int64.run_iter("row", row_limit=2)
assert next(results)["m"] == 5
assert next(results)["m"] == 24
with pytest.raises(StopIteration):
next(results)
@kd.udf("add_one<N: number>(x: N) -> N")
def add_one(x: pd.Series) -> pd.Series:
"""Use Pandas to add one."""
return x + 1
async def test_iter_udf(source_int64) -> None:
results = source_int64.col("m").pipe(add_one).run_iter("row", row_limit=2)
assert next(results)["result"] == 6
assert next(results)["result"] == 25
async def test_iter_pandas_async(golden, source_int64) -> None:
batches: AsyncIterator[pd.DataFrame] = source_int64.run_iter(
row_limit=4, max_batch_size=2
)
# 4 rows, max 2 per batch = 2 batches.
# We could test using `await anext(batches)`, but that wasn't introduced
# until Python 3.10. Since everything else works in 3.8 and 3.9, we just
# call `__anext__` directly.
golden.jsonl(await batches.__anext__())
golden.jsonl(await batches.__anext__())
with pytest.raises(StopAsyncIteration):
await batches.__anext__()
async def test_iter_pandas_async_live(golden, source_int64) -> None:
data2 = "\n".join(
[
"time,key,m,n",
"1996-12-20T16:39:57,A,5,10",
"1996-12-20T16:39:58,B,24,3",
"1996-12-20T16:39:59,A,17,6",
"1996-12-20T16:40:00,C,,9",
"1996-12-20T16:40:01,A,12,",
"1996-12-20T16:40:02,A,,",
]
)
execution = source_int64.run_iter(mode="live")
# Await the first batch.
golden.jsonl(await execution.__anext__())
# Add data and await the second batch.
await source_int64.add_string(data2)
golden.jsonl(await execution.__anext__())
execution.stop()
with pytest.raises(StopAsyncIteration):
print(await execution.__anext__())
async def test_snapshot(golden, source_int64) -> None:
query = source_int64.col("m").sum()
golden.jsonl(query.to_pandas(kd.results.Snapshot()))
golden.jsonl(
query.to_pandas(
kd.results.Snapshot(
changed_since=datetime.fromisoformat("1996-12-19T16:39:59+00:00")
)
)
)
golden.jsonl(
query.to_pandas(
kd.results.Snapshot(at=datetime.fromisoformat("1996-12-20T12:00:00+00:00"))
)
)
async def test_history(golden, source_int64) -> None:
query = source_int64.col("m").sum()
golden.jsonl(query.to_pandas(kd.results.History()))
golden.jsonl(
query.to_pandas(
kd.results.History(
since=datetime.fromisoformat("1996-12-19T16:39:59+00:00")
)
)
)
golden.jsonl(
query.to_pandas(
kd.results.History(
until=datetime.fromisoformat("1996-12-20T12:00:00+00:00")
)
)
)
golden.jsonl(
query.to_pandas(
kd.results.History(
since=datetime.fromisoformat("1996-12-19T16:39:59+00:00"),
until=datetime.fromisoformat("1996-12-20T12:00:00+00:00"),
)
)
)
async def test_iter_pandas_async_live_timeout(golden, source_int64) -> None:
data2 = "\n".join(
[
"time,key,m,n",
"1996-12-20T16:39:57,A,5,10",
"1996-12-20T16:39:58,B,24,3",
"1996-12-20T16:39:59,A,17,6",
"1996-12-20T16:40:00,C,,9",
"1996-12-20T16:40:01,A,12,",
"1996-12-20T16:40:02,A,,",
]
)
execution = source_int64.run_iter(mode="live")
# Await the first batch.
golden.jsonl(await execution.__anext__())
# Add data and await the second batch.
await source_int64.add_string(data2)
golden.jsonl(await execution.__anext__())
with pytest.raises(TimeoutError):
async with asyncio.timeout(1):
await execution.__anext__()
execution.stop()
with pytest.raises(StopAsyncIteration):
print(await execution.__anext__())