@@ -14,6 +14,10 @@ import Testing
1414
1515private final class MockMSSQLPluginDriver : PluginDatabaseDriver , @unchecked Sendable {
1616 private var schema : String ?
17+ var cancelQueryCallCount = 0
18+ var applyQueryTimeoutValues : [ Int ] = [ ]
19+ var executedQueries : [ String ] = [ ]
20+ var shouldFailExecute = true
1721
1822 init ( initialSchema: String ? ) {
1923 schema = initialSchema
@@ -29,12 +33,25 @@ private final class MockMSSQLPluginDriver: PluginDatabaseDriver, @unchecked Send
2933 func connect( ) async throws { }
3034 func disconnect( ) { }
3135
36+ func cancelQuery( ) throws {
37+ cancelQueryCallCount += 1
38+ }
39+
40+ func applyQueryTimeout( _ seconds: Int ) async throws {
41+ applyQueryTimeoutValues. append ( seconds)
42+ executedQueries. append ( " SET LOCK_TIMEOUT \( seconds * 1_000 ) " )
43+ }
44+
3245 func execute( query: String ) async throws -> PluginQueryResult {
33- throw NSError (
34- domain: " MockMSSQLPluginDriver " ,
35- code: - 1 ,
36- userInfo: [ NSLocalizedDescriptionKey: " Not connected " ]
37- )
46+ executedQueries. append ( query)
47+ if shouldFailExecute {
48+ throw NSError (
49+ domain: " MockMSSQLPluginDriver " ,
50+ code: - 1 ,
51+ userInfo: [ NSLocalizedDescriptionKey: " Not connected " ]
52+ )
53+ }
54+ return PluginQueryResult ( columns: [ ] , columnTypeNames: [ ] , rows: [ ] , rowsAffected: 0 , executionTime: 0 )
3855 }
3956
4057 func fetchTables( schema: String ? ) async throws -> [ PluginTableInfo ] { [ ] }
@@ -64,10 +81,16 @@ struct MSSQLDriverTests {
6481 }
6582
6683 private func makeAdapter( mssqlSchema: String ? = nil ) -> PluginDriverAdapter {
84+ let ( adapter, _) = makeAdapterWithMock ( mssqlSchema: mssqlSchema)
85+ return adapter
86+ }
87+
88+ private func makeAdapterWithMock( mssqlSchema: String ? = nil ) -> ( PluginDriverAdapter , MockMSSQLPluginDriver ) {
6789 let conn = makeConnection ( mssqlSchema: mssqlSchema)
6890 let effectiveSchema : String ? = if let s = mssqlSchema, !s. isEmpty { s } else { " dbo " }
69- let pluginDriver = MockMSSQLPluginDriver ( initialSchema: effectiveSchema)
70- return PluginDriverAdapter ( connection: conn, pluginDriver: pluginDriver)
91+ let mock = MockMSSQLPluginDriver ( initialSchema: effectiveSchema)
92+ let adapter = PluginDriverAdapter ( connection: conn, pluginDriver: mock)
93+ return ( adapter, mock)
7194 }
7295
7396 // MARK: - Initialization Tests
@@ -147,4 +170,49 @@ struct MSSQLDriverTests {
147170 _ = try await adapter. execute ( query: " SELECT 1 " )
148171 }
149172 }
173+
174+ // MARK: - cancelQuery Tests
175+
176+ @Test ( " cancelQuery delegates to plugin driver " )
177+ func cancelQueryDelegatesToPlugin( ) throws {
178+ let ( adapter, mock) = makeAdapterWithMock ( )
179+ try adapter. cancelQuery ( )
180+ #expect( mock. cancelQueryCallCount == 1 )
181+ }
182+
183+ @Test ( " cancelQuery can be called multiple times " )
184+ func cancelQueryMultipleCalls( ) throws {
185+ let ( adapter, mock) = makeAdapterWithMock ( )
186+ try adapter. cancelQuery ( )
187+ try adapter. cancelQuery ( )
188+ try adapter. cancelQuery ( )
189+ #expect( mock. cancelQueryCallCount == 3 )
190+ }
191+
192+ // MARK: - applyQueryTimeout Tests
193+
194+ @Test ( " applyQueryTimeout delegates to plugin driver with correct value " )
195+ func applyQueryTimeoutDelegates( ) async throws {
196+ let ( adapter, mock) = makeAdapterWithMock ( )
197+ mock. shouldFailExecute = false
198+ try await adapter. applyQueryTimeout ( 30 )
199+ #expect( mock. applyQueryTimeoutValues == [ 30 ] )
200+ }
201+
202+ @Test ( " applyQueryTimeout with zero is handled by plugin " )
203+ func applyQueryTimeoutZero( ) async throws {
204+ let ( adapter, mock) = makeAdapterWithMock ( )
205+ mock. shouldFailExecute = false
206+ try await adapter. applyQueryTimeout ( 0 )
207+ #expect( mock. applyQueryTimeoutValues == [ 0 ] )
208+ }
209+
210+ @Test ( " applyQueryTimeout with different values records each call " )
211+ func applyQueryTimeoutMultipleCalls( ) async throws {
212+ let ( adapter, mock) = makeAdapterWithMock ( )
213+ mock. shouldFailExecute = false
214+ try await adapter. applyQueryTimeout ( 10 )
215+ try await adapter. applyQueryTimeout ( 60 )
216+ #expect( mock. applyQueryTimeoutValues == [ 10 , 60 ] )
217+ }
150218}
0 commit comments