@@ -47,7 +47,7 @@ final class AsyncPostgresConnectionTests: XCTestCase {
4747 }
4848 }
4949
50- func testSelect10kRowsAndConsume ( ) async throws {
50+ func testSelect10kRowsWithMetadata ( ) async throws {
5151 let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
5252 defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
5353 let eventLoop = eventLoopGroup. next ( )
@@ -56,24 +56,28 @@ final class AsyncPostgresConnectionTests: XCTestCase {
5656 let end = 10000
5757
5858 try await withTestConnection ( on: eventLoop) { connection in
59- let rows = try await connection. query ( " SELECT generate_series( \( start) , \( end) ); " , logger: . psqlTest)
60-
61- let counter = ManagedAtomic ( 0 )
62- let metadata = try await rows. consume { row in
63- let element = try row. decode ( Int . self)
64- let newCounter = counter. wrappingIncrementThenLoad ( ordering: . relaxed)
65- XCTAssertEqual ( element, newCounter)
59+ let ( result, metadata) = try await connection. query (
60+ " SELECT generate_series( \( start) , \( end) ); " ,
61+ logger: . psqlTest
62+ ) { rows in
63+ var counter = 0
64+ for try await row in rows {
65+ let element = try row. decode ( Int . self)
66+ XCTAssertEqual ( element, counter + 1 )
67+ counter += 1
68+ }
69+ return counter
6670 }
6771
6872 XCTAssertEqual ( metadata. command, " SELECT " )
6973 XCTAssertEqual ( metadata. oid, nil )
70- XCTAssertEqual ( metadata. rows, 10000 )
74+ XCTAssertEqual ( metadata. rows, end )
7175
72- XCTAssertEqual ( counter . load ( ordering : . relaxed ) , end)
76+ XCTAssertEqual ( result , end)
7377 }
7478 }
7579
76- func testSelect10kRowsAndCollect ( ) async throws {
80+ func testSelectRowsWithMetadataNotConsumedAtAll ( ) async throws {
7781 let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
7882 defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
7983 let eventLoop = eventLoopGroup. next ( )
@@ -82,20 +86,56 @@ final class AsyncPostgresConnectionTests: XCTestCase {
8286 let end = 10000
8387
8488 try await withTestConnection ( on: eventLoop) { connection in
85- let rows = try await connection. query ( " SELECT generate_series( \( start) , \( end) ); " , logger: . psqlTest)
86- let ( metadata, elements) = try await rows. collectWithMetadata ( )
87- var counter = 0
88- for row in elements {
89- let element = try row. decode ( Int . self)
90- XCTAssertEqual ( element, counter + 1 )
91- counter += 1
92- }
89+ let ( _, metadata) = try await connection. query (
90+ " SELECT generate_series( \( start) , \( end) ); " ,
91+ logger: . psqlTest
92+ ) { _ in }
9393
9494 XCTAssertEqual ( metadata. command, " SELECT " )
9595 XCTAssertEqual ( metadata. oid, nil )
96- XCTAssertEqual ( metadata. rows, 10000 )
96+ XCTAssertEqual ( metadata. rows, end)
97+ }
98+ }
9799
98- XCTAssertEqual ( counter, end)
100+ func testSelectRowsWithMetadataNotFullyConsumed( ) async throws {
101+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
102+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
103+ let eventLoop = eventLoopGroup. next ( )
104+
105+ try await withTestConnection ( on: eventLoop) { connection in
106+ do {
107+ _ = try await connection. query (
108+ " SELECT generate_series(1, 10000); " ,
109+ logger: . psqlTest
110+ ) { rows in
111+ for try await _ in rows { break }
112+ }
113+ XCTFail ( " Expected a failure " )
114+ } catch is CancellationError {
115+ // Expected
116+ } catch {
117+ XCTFail ( " Expected 'CancellationError', got: \( String ( reflecting: error) ) " )
118+ }
119+ }
120+ }
121+
122+ func testExecuteRowsWithMetadata( ) async throws {
123+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
124+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
125+ let eventLoop = eventLoopGroup. next ( )
126+
127+ let start = 1
128+ let end = 10000
129+
130+ try await withTestConnection ( on: eventLoop) { connection in
131+ let metadata = try await connection. execute (
132+ " SELECT generate_series( \( start) , \( end) ); " ,
133+ logger: . psqlTest
134+ )
135+
136+ XCTAssertEqual ( metadata. command, " SELECT " )
137+ XCTAssertEqual ( metadata. oid, nil )
138+ XCTAssertEqual ( metadata. rows, end)
99139 }
100140 }
101141
@@ -294,8 +334,7 @@ final class AsyncPostgresConnectionTests: XCTestCase {
294334 unsafeSQL: " INSERT INTO table1 VALUES \( insertionValues) " ,
295335 binds: binds
296336 )
297- let result = try await connection. query ( insertionQuery, logger: . psqlTest)
298- let metadata = try await result. collectWithMetadata ( ) . metadata
337+ let metadata = try await connection. execute ( insertionQuery, logger: . psqlTest)
299338 XCTAssertEqual ( metadata. rows, rowsCount)
300339
301340 let dropQuery = PostgresQuery ( unsafeSQL: " DROP TABLE table1 " )
0 commit comments