@@ -139,6 +139,45 @@ async def test_executemany_basic(self):
139
139
('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 )
140
140
])
141
141
142
+ async def test_executemany_returning (self ):
143
+ result = await self .con .executemany ('''
144
+ INSERT INTO exmany VALUES($1, $2) RETURNING a, b
145
+ ''' , [
146
+ ('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 )
147
+ ], return_rows = True )
148
+ self .assertEqual (result , [
149
+ ('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 )
150
+ ])
151
+ result = await self .con .fetch ('''
152
+ SELECT * FROM exmany
153
+ ''' )
154
+ self .assertEqual (result , [
155
+ ('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 )
156
+ ])
157
+
158
+ # Empty set
159
+ await self .con .executemany ('''
160
+ INSERT INTO exmany VALUES($1, $2) RETURNING a, b
161
+ ''' , (), return_rows = True )
162
+ result = await self .con .fetch ('''
163
+ SELECT * FROM exmany
164
+ ''' )
165
+ self .assertEqual (result , [
166
+ ('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 )
167
+ ])
168
+
169
+ # Without "RETURNING"
170
+ result = await self .con .executemany ('''
171
+ INSERT INTO exmany VALUES($1, $2)
172
+ ''' , [('e' , 5 ), ('f' , 6 )], return_rows = True )
173
+ self .assertEqual (result , [])
174
+ result = await self .con .fetch ('''
175
+ SELECT * FROM exmany
176
+ ''' )
177
+ self .assertEqual (result , [
178
+ ('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 ), ('e' , 5 ), ('f' , 6 )
179
+ ])
180
+
142
181
async def test_executemany_bad_input (self ):
143
182
with self .assertRaisesRegex (
144
183
exceptions .DataError ,
@@ -288,11 +327,13 @@ async def test_executemany_client_server_failure_conflict(self):
288
327
289
328
async def test_executemany_prepare (self ):
290
329
stmt = await self .con .prepare ('''
291
- INSERT INTO exmany VALUES($1, $2)
330
+ INSERT INTO exmany VALUES($1, $2) RETURNING a, b
292
331
''' )
293
332
result = await stmt .executemany ([
294
333
('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 )
295
334
])
335
+ # While the query contains a "RETURNING" clause, by default
336
+ # `executemany` does not return anything
296
337
self .assertIsNone (result )
297
338
result = await self .con .fetch ('''
298
339
SELECT * FROM exmany
@@ -308,3 +349,13 @@ async def test_executemany_prepare(self):
308
349
self .assertEqual (result , [
309
350
('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 )
310
351
])
352
+ # Now with `return_rows=True`, we should retrieve the tuples
353
+ # from the "RETURNING" clause.
354
+ result = await stmt .executemany ([('e' , 5 ), ('f' , 6 )], return_rows = True )
355
+ self .assertEqual (result , [('e' , 5 ), ('f' , 6 )])
356
+ result = await self .con .fetch ('''
357
+ SELECT * FROM exmany
358
+ ''' )
359
+ self .assertEqual (result , [
360
+ ('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 ), ('e' , 5 ), ('f' , 6 )
361
+ ])
0 commit comments