@@ -139,6 +139,45 @@ async def test_executemany_basic(self):
139139 ('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 )
140140 ])
141141
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+
142181 async def test_executemany_bad_input (self ):
143182 with self .assertRaisesRegex (
144183 exceptions .DataError ,
@@ -288,11 +327,13 @@ async def test_executemany_client_server_failure_conflict(self):
288327
289328 async def test_executemany_prepare (self ):
290329 stmt = await self .con .prepare ('''
291- INSERT INTO exmany VALUES($1, $2)
330+ INSERT INTO exmany VALUES($1, $2) RETURNING a, b
292331 ''' )
293332 result = await stmt .executemany ([
294333 ('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 )
295334 ])
335+ # While the query contains a "RETURNING" clause, by default
336+ # `executemany` does not return anything
296337 self .assertIsNone (result )
297338 result = await self .con .fetch ('''
298339 SELECT * FROM exmany
@@ -308,3 +349,13 @@ async def test_executemany_prepare(self):
308349 self .assertEqual (result , [
309350 ('a' , 1 ), ('b' , 2 ), ('c' , 3 ), ('d' , 4 )
310351 ])
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