5
5
import ydb
6
6
import ydb_dbapi
7
7
8
- INSERT_YQL = """
9
- DELETE FROM table;
10
- INSERT INTO table (id, val) VALUES
11
- (1, 1),
12
- (2, 2),
13
- (3, 3),
14
- (4, 4)
15
- """
16
-
17
8
18
9
@pytest .fixture
19
10
async def session (
20
11
session_pool : ydb .aio .QuerySessionPool ,
21
12
) -> AsyncGenerator [ydb .aio .QuerySession ]:
22
- await session_pool .execute_with_retries (INSERT_YQL )
13
+ for name in ["table" , "table1" , "table2" ]:
14
+ await session_pool .execute_with_retries (
15
+ f"""
16
+ DELETE FROM { name } ;
17
+ INSERT INTO { name } (id, val) VALUES
18
+ (0, 0),
19
+ (1, 1),
20
+ (2, 2),
21
+ (3, 3)
22
+ """
23
+ )
23
24
24
25
session = await session_pool .acquire ()
25
26
yield session
@@ -30,13 +31,27 @@ async def session(
30
31
def session_sync (
31
32
session_pool_sync : ydb .QuerySessionPool ,
32
33
) -> Generator [ydb .QuerySession ]:
33
- session_pool_sync .execute_with_retries (INSERT_YQL )
34
+ for name in ["table" , "table1" , "table2" ]:
35
+ session_pool_sync .execute_with_retries (
36
+ f"""
37
+ DELETE FROM { name } ;
38
+ INSERT INTO { name } (id, val) VALUES
39
+ (0, 0),
40
+ (1, 1),
41
+ (2, 2),
42
+ (3, 3)
43
+ """
44
+ )
34
45
35
46
session = session_pool_sync .acquire ()
36
47
yield session
37
48
session_pool_sync .release (session )
38
49
39
50
51
+ RESULT_SET_LENGTH = 4
52
+ RESULT_SET_COUNT = 3
53
+
54
+
40
55
class TestAsyncCursor :
41
56
@pytest .mark .asyncio
42
57
async def test_cursor_fetch_one (
@@ -48,10 +63,10 @@ async def test_cursor_fetch_one(
48
63
"""
49
64
await cursor .execute (query = yql_text )
50
65
51
- for i in range (4 ):
66
+ for i in range (RESULT_SET_LENGTH ):
52
67
res = await cursor .fetchone ()
53
68
assert res is not None
54
- assert res [0 ] == i + 1
69
+ assert res [0 ] == i
55
70
56
71
assert await cursor .fetchone () is None
57
72
@@ -68,20 +83,20 @@ async def test_cursor_fetch_many(
68
83
res = await cursor .fetchmany ()
69
84
assert res is not None
70
85
assert len (res ) == 1
71
- assert res [0 ][0 ] == 1
86
+ assert res [0 ][0 ] == 0
72
87
73
88
res = await cursor .fetchmany (size = 2 )
74
89
assert res is not None
75
90
assert len (res ) == 2
76
- assert res [0 ][0 ] == 2
77
- assert res [1 ][0 ] == 3
91
+ assert res [0 ][0 ] == 1
92
+ assert res [1 ][0 ] == 2
78
93
79
94
res = await cursor .fetchmany (size = 2 )
80
95
assert res is not None
81
96
assert len (res ) == 1
82
- assert res [0 ][0 ] == 4
97
+ assert res [0 ][0 ] == 3
83
98
84
- assert await cursor .fetchmany (size = 2 ) is None
99
+ assert await cursor .fetchmany (size = 2 ) == []
85
100
86
101
@pytest .mark .asyncio
87
102
async def test_cursor_fetch_all (
@@ -93,15 +108,15 @@ async def test_cursor_fetch_all(
93
108
"""
94
109
await cursor .execute (query = yql_text )
95
110
96
- assert cursor .rowcount == 4
111
+ assert cursor .rowcount == RESULT_SET_LENGTH
97
112
98
113
res = await cursor .fetchall ()
99
114
assert res is not None
100
- assert len (res ) == 4
101
- for i in range (4 ):
102
- assert res [i ][0 ] == i + 1
115
+ assert len (res ) == RESULT_SET_LENGTH
116
+ for i in range (RESULT_SET_LENGTH ):
117
+ assert res [i ][0 ] == i
103
118
104
- assert await cursor .fetchall () is None
119
+ assert await cursor .fetchall () == []
105
120
106
121
@pytest .mark .asyncio
107
122
async def test_cursor_next_set (
@@ -127,11 +142,77 @@ async def test_cursor_next_set(
127
142
nextset = await cursor .nextset ()
128
143
assert nextset
129
144
130
- assert await cursor .fetchall () is None
145
+ assert await cursor .fetchall () == []
131
146
132
147
nextset = await cursor .nextset ()
133
148
assert not nextset
134
149
150
+ @pytest .mark .asyncio
151
+ async def test_cursor_fetch_one_autoscroll (
152
+ self , session : ydb .aio .QuerySession
153
+ ) -> None :
154
+ async with ydb_dbapi .AsyncCursor (
155
+ session = session , auto_scroll_result_sets = True
156
+ ) as cursor :
157
+ yql_text = """
158
+ SELECT id, val FROM table;
159
+ SELECT id, val FROM table1;
160
+ SELECT id, val FROM table2;
161
+ """
162
+ await cursor .execute (query = yql_text )
163
+
164
+ for i in range (RESULT_SET_LENGTH * RESULT_SET_COUNT ):
165
+ res = await cursor .fetchone ()
166
+ assert res is not None
167
+ assert res [0 ] == i % RESULT_SET_LENGTH
168
+
169
+ assert await cursor .fetchone () is None
170
+ assert not await cursor .nextset ()
171
+
172
+ @pytest .mark .asyncio
173
+ async def test_cursor_fetch_many_autoscroll (
174
+ self , session : ydb .aio .QuerySession
175
+ ) -> None :
176
+ async with ydb_dbapi .AsyncCursor (
177
+ session = session , auto_scroll_result_sets = True
178
+ ) as cursor :
179
+ yql_text = """
180
+ SELECT id, val FROM table;
181
+ SELECT id, val FROM table1;
182
+ SELECT id, val FROM table2;
183
+ """
184
+ await cursor .execute (query = yql_text )
185
+
186
+ halfsize = (RESULT_SET_LENGTH * RESULT_SET_COUNT ) // 2
187
+ for _ in range (2 ):
188
+ res = await cursor .fetchmany (size = halfsize )
189
+ assert res is not None
190
+ assert len (res ) == halfsize
191
+
192
+ assert await cursor .fetchmany (2 ) == []
193
+ assert not await cursor .nextset ()
194
+
195
+ @pytest .mark .asyncio
196
+ async def test_cursor_fetch_all_autoscroll (
197
+ self , session : ydb .aio .QuerySession
198
+ ) -> None :
199
+ async with ydb_dbapi .AsyncCursor (
200
+ session = session , auto_scroll_result_sets = True
201
+ ) as cursor :
202
+ yql_text = """
203
+ SELECT id, val FROM table;
204
+ SELECT id, val FROM table1;
205
+ SELECT id, val FROM table2;
206
+ """
207
+ await cursor .execute (query = yql_text )
208
+
209
+ res = await cursor .fetchall ()
210
+
211
+ assert len (res ) == RESULT_SET_COUNT * RESULT_SET_LENGTH
212
+
213
+ assert await cursor .fetchall () == []
214
+ assert not await cursor .nextset ()
215
+
135
216
136
217
# The same test class as above but for Cursor
137
218
@@ -147,7 +228,7 @@ def test_cursor_fetch_one(self, session_sync: ydb.QuerySession) -> None:
147
228
for i in range (4 ):
148
229
res = cursor .fetchone ()
149
230
assert res is not None
150
- assert res [0 ] == i + 1
231
+ assert res [0 ] == i
151
232
152
233
assert cursor .fetchone () is None
153
234
@@ -161,20 +242,20 @@ def test_cursor_fetch_many(self, session_sync: ydb.QuerySession) -> None:
161
242
res = cursor .fetchmany ()
162
243
assert res is not None
163
244
assert len (res ) == 1
164
- assert res [0 ][0 ] == 1
245
+ assert res [0 ][0 ] == 0
165
246
166
247
res = cursor .fetchmany (size = 2 )
167
248
assert res is not None
168
249
assert len (res ) == 2
169
- assert res [0 ][0 ] == 2
170
- assert res [1 ][0 ] == 3
250
+ assert res [0 ][0 ] == 1
251
+ assert res [1 ][0 ] == 2
171
252
172
253
res = cursor .fetchmany (size = 2 )
173
254
assert res is not None
174
255
assert len (res ) == 1
175
- assert res [0 ][0 ] == 4
256
+ assert res [0 ][0 ] == 3
176
257
177
- assert cursor .fetchmany (size = 2 ) is None
258
+ assert cursor .fetchmany (size = 2 ) == []
178
259
179
260
def test_cursor_fetch_all (self , session_sync : ydb .QuerySession ) -> None :
180
261
with ydb_dbapi .Cursor (session = session_sync ) as cursor :
@@ -189,9 +270,9 @@ def test_cursor_fetch_all(self, session_sync: ydb.QuerySession) -> None:
189
270
assert res is not None
190
271
assert len (res ) == 4
191
272
for i in range (4 ):
192
- assert res [i ][0 ] == i + 1
273
+ assert res [i ][0 ] == i
193
274
194
- assert cursor .fetchall () is None
275
+ assert cursor .fetchall () == []
195
276
196
277
def test_cursor_next_set (self , session_sync : ydb .QuerySession ) -> None :
197
278
with ydb_dbapi .Cursor (session = session_sync ) as cursor :
@@ -214,22 +295,70 @@ def test_cursor_next_set(self, session_sync: ydb.QuerySession) -> None:
214
295
nextset = cursor .nextset ()
215
296
assert nextset
216
297
217
- assert cursor .fetchall () is None
298
+ assert cursor .fetchall () == []
218
299
219
300
nextset = cursor .nextset ()
220
301
assert not nextset
221
302
222
- def test_cursor_autoscroll (self , session_sync : ydb .QuerySession ) -> None :
303
+ def test_cursor_fetch_one_autoscroll (
304
+ self , session_sync : ydb .QuerySession
305
+ ) -> None :
223
306
with ydb_dbapi .Cursor (
224
307
session = session_sync , auto_scroll_result_sets = True
225
308
) as cursor :
226
- yql_text = "SELECT 1 as val; SELECT 2 as val; SELECT 3 as val;"
309
+ yql_text = """
310
+ SELECT id, val FROM table;
311
+ SELECT id, val FROM table1;
312
+ SELECT id, val FROM table2;
313
+ """
227
314
cursor .execute (query = yql_text )
228
315
229
- for i in range (3 ):
316
+ for i in range (RESULT_SET_LENGTH * RESULT_SET_COUNT ):
230
317
res = cursor .fetchone ()
231
318
assert res is not None
232
- assert res [0 ] == i + 1
319
+ assert res [0 ] == i % RESULT_SET_LENGTH
233
320
234
321
assert cursor .fetchone () is None
235
322
assert not cursor .nextset ()
323
+
324
+ def test_cursor_fetch_many_autoscroll (
325
+ self , session_sync : ydb .QuerySession
326
+ ) -> None :
327
+ with ydb_dbapi .Cursor (
328
+ session = session_sync , auto_scroll_result_sets = True
329
+ ) as cursor :
330
+ yql_text = """
331
+ SELECT id, val FROM table;
332
+ SELECT id, val FROM table1;
333
+ SELECT id, val FROM table2;
334
+ """
335
+ cursor .execute (query = yql_text )
336
+
337
+ halfsize = (RESULT_SET_LENGTH * RESULT_SET_COUNT ) // 2
338
+ for _ in range (2 ):
339
+ res = cursor .fetchmany (size = halfsize )
340
+ assert res is not None
341
+ assert len (res ) == halfsize
342
+
343
+ assert cursor .fetchmany (2 ) == []
344
+ assert not cursor .nextset ()
345
+
346
+ def test_cursor_fetch_all_autoscroll (
347
+ self , session_sync : ydb .QuerySession
348
+ ) -> None :
349
+ with ydb_dbapi .Cursor (
350
+ session = session_sync , auto_scroll_result_sets = True
351
+ ) as cursor :
352
+ yql_text = """
353
+ SELECT id, val FROM table;
354
+ SELECT id, val FROM table1;
355
+ SELECT id, val FROM table2;
356
+ """
357
+ cursor .execute (query = yql_text )
358
+
359
+ res = cursor .fetchall ()
360
+
361
+ assert len (res ) == RESULT_SET_COUNT * RESULT_SET_LENGTH
362
+
363
+ assert cursor .fetchall () == []
364
+ assert not cursor .nextset ()
0 commit comments