@@ -46,6 +46,98 @@ final class AsyncPostgresConnectionTests: XCTestCase {
4646 }
4747 }
4848
49+ func testSelect10kRowsWithMetadata( ) async throws {
50+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
51+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
52+ let eventLoop = eventLoopGroup. next ( )
53+
54+ let start = 1
55+ let end = 10000
56+
57+ try await withTestConnection ( on: eventLoop) { connection in
58+ let ( result, metadata) = try await connection. query (
59+ " SELECT generate_series( \( start) , \( end) ); " ,
60+ logger: . psqlTest
61+ ) { rows in
62+ var counter = 0
63+ for try await row in rows {
64+ let element = try row. decode ( Int . self)
65+ XCTAssertEqual ( element, counter + 1 )
66+ counter += 1
67+ }
68+ return counter
69+ }
70+
71+ XCTAssertEqual ( metadata. command, " SELECT " )
72+ XCTAssertEqual ( metadata. oid, nil )
73+ XCTAssertEqual ( metadata. rows, end)
74+
75+ XCTAssertEqual ( result, end)
76+ }
77+ }
78+
79+ func testSelectRowsWithMetadataNotConsumedAtAll( ) async throws {
80+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
81+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
82+ let eventLoop = eventLoopGroup. next ( )
83+
84+ let start = 1
85+ let end = 10000
86+
87+ try await withTestConnection ( on: eventLoop) { connection in
88+ let ( _, metadata) = try await connection. query (
89+ " SELECT generate_series( \( start) , \( end) ); " ,
90+ logger: . psqlTest
91+ ) { _ in }
92+
93+ XCTAssertEqual ( metadata. command, " SELECT " )
94+ XCTAssertEqual ( metadata. oid, nil )
95+ XCTAssertEqual ( metadata. rows, end)
96+ }
97+ }
98+
99+ func testSelectRowsWithMetadataNotFullyConsumed( ) async throws {
100+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
101+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
102+ let eventLoop = eventLoopGroup. next ( )
103+
104+ try await withTestConnection ( on: eventLoop) { connection in
105+ do {
106+ _ = try await connection. query (
107+ " SELECT generate_series(1, 10000); " ,
108+ logger: . psqlTest
109+ ) { rows in
110+ for try await _ in rows { break }
111+ }
112+ // This path is also fine
113+ } catch is CancellationError {
114+ // Expected
115+ } catch {
116+ XCTFail ( " Expected 'CancellationError', got: \( String ( reflecting: error) ) " )
117+ }
118+ }
119+ }
120+
121+ func testExecuteRowsWithMetadata( ) async throws {
122+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
123+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
124+ let eventLoop = eventLoopGroup. next ( )
125+
126+ let start = 1
127+ let end = 10000
128+
129+ try await withTestConnection ( on: eventLoop) { connection in
130+ let metadata = try await connection. execute (
131+ " SELECT generate_series( \( start) , \( end) ); " ,
132+ logger: . psqlTest
133+ )
134+
135+ XCTAssertEqual ( metadata. command, " SELECT " )
136+ XCTAssertEqual ( metadata. oid, nil )
137+ XCTAssertEqual ( metadata. rows, end)
138+ }
139+ }
140+
49141 func testSelectActiveConnection( ) async throws {
50142 let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
51143 defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
@@ -207,7 +299,7 @@ final class AsyncPostgresConnectionTests: XCTestCase {
207299
208300 try await withTestConnection ( on: eventLoop) { connection in
209301 // Max binds limit is UInt16.max which is 65535 which is 3 * 5 * 17 * 257
210- // Max columns limit is 1664 , so we will only make 5 * 257 columns which is less
302+ // Max columns limit appears to be ~1600 , so we will only make 5 * 257 columns which is less
211303 // Then we will insert 3 * 17 rows
212304 // In the insertion, there will be a total of 3 * 17 * 5 * 257 == UInt16.max bindings
213305 // If the test is successful, it means Postgres supports UInt16.max bindings
@@ -241,13 +333,8 @@ final class AsyncPostgresConnectionTests: XCTestCase {
241333 unsafeSQL: " INSERT INTO table1 VALUES \( insertionValues) " ,
242334 binds: binds
243335 )
244- try await connection. query ( insertionQuery, logger: . psqlTest)
245-
246- let countQuery = PostgresQuery ( unsafeSQL: " SELECT COUNT(*) FROM table1 " )
247- let countRows = try await connection. query ( countQuery, logger: . psqlTest)
248- var countIterator = countRows. makeAsyncIterator ( )
249- let insertedRowsCount = try await countIterator. next ( ) ? . decode ( Int . self, context: . default)
250- XCTAssertEqual ( rowsCount, insertedRowsCount)
336+ let metadata = try await connection. execute ( insertionQuery, logger: . psqlTest)
337+ XCTAssertEqual ( metadata. rows, rowsCount)
251338
252339 let dropQuery = PostgresQuery ( unsafeSQL: " DROP TABLE table1 " )
253340 try await connection. query ( dropQuery, logger: . psqlTest)
0 commit comments