11using System . Data . Common ;
22
33using Microsoft . Extensions . DependencyInjection ;
4+ using Microsoft . Extensions . Logging ;
45
56namespace FluentCommand . SqlServer . Tests ;
67
@@ -23,6 +24,8 @@ public void WhenConnectionOpened_ConnectionInterceptorCalledOnce()
2324
2425 interceptor . ConnectionOpenedCount . Should ( ) . Be ( 1 ) ;
2526 interceptor . ConnectionOpenedAsyncCount . Should ( ) . Be ( 0 ) ;
27+ interceptor . ConnectionClosingCount . Should ( ) . Be ( 1 ) ;
28+ interceptor . ConnectionClosingAsyncCount . Should ( ) . Be ( 0 ) ;
2629 }
2730
2831 [ Fact ]
@@ -34,10 +37,12 @@ public async Task WhenConnectionOpenedAsync_ConnectionInterceptorCalledOnce()
3437 await using var session = new DataSession ( config . CreateConnection ( ) , interceptors : [ interceptor ] ) ;
3538
3639 await session . EnsureConnectionAsync ( TestCancellation ) ;
37- session . ReleaseConnection ( ) ;
40+ await session . ReleaseConnectionAsync ( ) ;
3841
3942 interceptor . ConnectionOpenedAsyncCount . Should ( ) . Be ( 1 ) ;
4043 interceptor . ConnectionOpenedCount . Should ( ) . Be ( 0 ) ;
44+ interceptor . ConnectionClosingAsyncCount . Should ( ) . Be ( 1 ) ;
45+ interceptor . ConnectionClosingCount . Should ( ) . Be ( 0 ) ;
4146 }
4247
4348 [ Fact ]
@@ -54,6 +59,7 @@ public void WhenEnsureConnectionCalledTwice_ConnectionInterceptorCalledOnce()
5459 session . ReleaseConnection ( ) ;
5560
5661 interceptor . ConnectionOpenedCount . Should ( ) . Be ( 1 ) ;
62+ interceptor . ConnectionClosingCount . Should ( ) . Be ( 1 ) ;
5763 }
5864
5965 [ Fact ]
@@ -144,11 +150,27 @@ public void WhenCreatedWithNoInterceptors_SessionExposesEmptyList()
144150 session . Interceptors . Should ( ) . BeEmpty ( ) ;
145151 }
146152
153+ [ Fact ]
154+ public void WhenSqlPrintExecuted_PrintMessageIsLogged ( )
155+ {
156+ var config = Services . GetRequiredService < IDataConfiguration > ( ) ;
157+ var logger = new TrackingLogger < MessageInterceptor > ( ) ;
158+ var interceptor = new MessageInterceptor ( logger ) ;
159+
160+ using var session = new DataSession ( config . CreateConnection ( ) , interceptors : [ interceptor ] ) ;
161+
162+ session . Sql ( "PRINT 'fluent command print message'; SELECT 1" ) . QueryValue < int > ( ) . Should ( ) . Be ( 1 ) ;
163+
164+ logger . Messages . Should ( ) . Contain ( m => m . Contains ( "fluent command print message" , StringComparison . Ordinal ) ) ;
165+ }
166+
147167
148168 private sealed class TrackingConnectionInterceptor : IDataConnectionInterceptor
149169 {
150170 public int ConnectionOpenedCount { get ; private set ; }
151171 public int ConnectionOpenedAsyncCount { get ; private set ; }
172+ public int ConnectionClosingCount { get ; private set ; }
173+ public int ConnectionClosingAsyncCount { get ; private set ; }
152174
153175 public void ConnectionOpened ( DbConnection connection , IDataSession session )
154176 => ConnectionOpenedCount ++ ;
@@ -158,6 +180,15 @@ public Task ConnectionOpenedAsync(DbConnection connection, IDataSession session,
158180 ConnectionOpenedAsyncCount ++ ;
159181 return Task . CompletedTask ;
160182 }
183+
184+ public void ConnectionClosing ( DbConnection connection , IDataSession session )
185+ => ConnectionClosingCount ++ ;
186+
187+ public Task ConnectionClosingAsync ( DbConnection connection , IDataSession session , CancellationToken cancellationToken = default )
188+ {
189+ ConnectionClosingAsyncCount ++ ;
190+ return Task . CompletedTask ;
191+ }
161192 }
162193
163194 private sealed class TrackingCommandInterceptor : IDataCommandInterceptor
@@ -174,4 +205,25 @@ public Task CommandExecutingAsync(DbCommand command, IDataSession session, Cance
174205 return Task . CompletedTask ;
175206 }
176207 }
208+
209+ private sealed class TrackingLogger < T > : ILogger < T >
210+ {
211+ public List < string > Messages { get ; } = [ ] ;
212+
213+ public IDisposable ? BeginScope < TState > ( TState state )
214+ where TState : notnull
215+ => null ;
216+
217+ public bool IsEnabled ( LogLevel logLevel ) => true ;
218+
219+ public void Log < TState > (
220+ LogLevel logLevel ,
221+ EventId eventId ,
222+ TState state ,
223+ Exception ? exception ,
224+ Func < TState , Exception ? , string > formatter )
225+ {
226+ Messages . Add ( formatter ( state , exception ) ) ;
227+ }
228+ }
177229}
0 commit comments