@@ -143,7 +143,7 @@ public void SingleByExistingIdTest()
143143 }
144144
145145 [ Test ]
146- public async Task SingleByExistingIdAsyncTest ( )
146+ public async Task SingleByExistingIdAsyncTest1 ( )
147147 {
148148 await RunWithinSessionAsync ( async ( s ) => {
149149 var existingId = ( int ) existingKeys [ customerType ] [ 0 ] . Value . GetValue ( 0 , out var _ ) ;
@@ -164,6 +164,28 @@ await RunWithinSessionAsync(async (s) => {
164164 } ) ;
165165 }
166166
167+ [ Test ]
168+ public async Task SingleByExistingIdAsyncTest2 ( )
169+ {
170+ await RunWithinSessionAsync ( async ( s ) => {
171+ var existingId = ( int ) existingKeys [ customerType ] [ 0 ] . Value . GetValue ( 0 , out var _ ) ;
172+ var detector = new QueryExecutionDetector ( ) ;
173+ using ( detector . Attach ( s ) ) {
174+ // Entity is not in cache yet
175+ var existingEntity = await s . Query . SingleAsync < Customer > ( existingId ) ;
176+ }
177+ Assert . That ( detector . DbCommandsDetected , Is . True ) ;
178+ detector . Reset ( ) ;
179+
180+ using ( detector . Attach ( s ) ) {
181+ // now it is in cache
182+ var existingEntity = await s . Query . SingleAsync < Customer > ( existingId ) ;
183+ }
184+ Assert . That ( detector . DbCommandsDetected , Is . False ) ;
185+ detector . Reset ( ) ;
186+ } ) ;
187+ }
188+
167189 [ Test ]
168190 public void SingleByInexistentIdTest ( )
169191 {
@@ -186,7 +208,7 @@ public void SingleByInexistentIdTest()
186208 }
187209
188210 [ Test ]
189- public async Task SingleByInexistentIdAsyncTest ( )
211+ public async Task SingleByInexistentIdAsyncTest1 ( )
190212 {
191213 await RunWithinSessionAsync ( async ( s ) => {
192214 var inexistentId = 9999 ;
@@ -207,6 +229,28 @@ await RunWithinSessionAsync(async (s) => {
207229 } ) ;
208230 }
209231
232+ [ Test ]
233+ public async Task SingleByInexistentIdAsyncTest2 ( )
234+ {
235+ await RunWithinSessionAsync ( async ( s ) => {
236+ var inexistentId = 9999 ;
237+
238+ var detector = new QueryExecutionDetector ( ) ;
239+ using ( detector . Attach ( s ) ) {
240+ _ = Assert . ThrowsAsync < KeyNotFoundException > ( async ( ) => await s . Query . SingleAsync < Customer > ( inexistentId ) ) ;
241+ }
242+ Assert . That ( detector . DbCommandsDetected , Is . True ) ;
243+ detector . Reset ( ) ;
244+
245+ using ( detector . Attach ( s ) ) {
246+ _ = Assert . ThrowsAsync < KeyNotFoundException > ( async ( ) => await s . Query . SingleAsync < Customer > ( inexistentId ) ) ;
247+ }
248+ Assert . That ( detector . DbCommandsDetected , Is . False ) ;
249+ detector . Reset ( ) ;
250+ await Task . CompletedTask ;
251+ } ) ;
252+ }
253+
210254 [ Test ]
211255 public void SingleOrDefaultByExistingKeyTest ( )
212256 {
@@ -327,7 +371,7 @@ public void SingleOrDefaultByExistingIdTest()
327371 }
328372
329373 [ Test ]
330- public async Task SingleOrDefaultByExistingIdAsyncTest ( )
374+ public async Task SingleOrDefaultByExistingIdAsyncTest1 ( )
331375 {
332376 await RunWithinSessionAsync ( async ( s ) => {
333377 var existingId = ( int ) existingKeys [ customerType ] [ 0 ] . Value . GetValue ( 0 , out var _ ) ;
@@ -348,6 +392,28 @@ await RunWithinSessionAsync(async (s) => {
348392 } ) ;
349393 }
350394
395+ [ Test ]
396+ public async Task SingleOrDefaultByExistingIdAsyncTest2 ( )
397+ {
398+ await RunWithinSessionAsync ( async ( s ) => {
399+ var existingId = ( int ) existingKeys [ customerType ] [ 0 ] . Value . GetValue ( 0 , out var _ ) ;
400+ var detector = new QueryExecutionDetector ( ) ;
401+ using ( detector . Attach ( s ) ) {
402+ // Entity is not in cache yet
403+ var existingEntity = await s . Query . SingleOrDefaultAsync < Customer > ( existingId ) ;
404+ }
405+ Assert . That ( detector . DbCommandsDetected , Is . True ) ;
406+ detector . Reset ( ) ;
407+
408+ using ( detector . Attach ( s ) ) {
409+ // now it is in cache
410+ var existingEntity = await s . Query . SingleOrDefaultAsync < Customer > ( existingId ) ;
411+ }
412+ Assert . That ( detector . DbCommandsDetected , Is . False ) ;
413+ detector . Reset ( ) ;
414+ } ) ;
415+ }
416+
351417 [ Test ]
352418 public void SingleOrDefaultByInexistentIdTest ( )
353419 {
@@ -372,7 +438,7 @@ public void SingleOrDefaultByInexistentIdTest()
372438 }
373439
374440 [ Test ]
375- public async Task SingleOrDefaultByInexistentIdAsyncTest ( )
441+ public async Task SingleOrDefaultByInexistentIdAsyncTest1 ( )
376442 {
377443 await RunWithinSessionAsync ( async ( s ) => {
378444 var inexistentId = 9999 ;
@@ -395,6 +461,29 @@ await RunWithinSessionAsync(async (s) => {
395461 } ) ;
396462 }
397463
464+ [ Test ]
465+ public async Task SingleOrDefaultByInexistentIdAsyncTest2 ( )
466+ {
467+ await RunWithinSessionAsync ( async ( s ) => {
468+ var inexistentId = 9999 ;
469+
470+ var detector = new QueryExecutionDetector ( ) ;
471+ using ( detector . Attach ( s ) ) {
472+ var shouldBeNull = await s . Query . SingleOrDefaultAsync < Customer > ( inexistentId ) ;
473+ Assert . That ( shouldBeNull , Is . Null ) ;
474+ }
475+ Assert . That ( detector . DbCommandsDetected , Is . True ) ;
476+ detector . Reset ( ) ;
477+
478+ using ( detector . Attach ( s ) ) {
479+ var shouldBeNull = await s . Query . SingleOrDefaultAsync < Customer > ( inexistentId ) ;
480+ Assert . That ( shouldBeNull , Is . Null ) ;
481+ }
482+ Assert . That ( detector . DbCommandsDetected , Is . False ) ;
483+ detector . Reset ( ) ;
484+ await Task . CompletedTask ;
485+ } ) ;
486+ }
398487
399488 private void RunWithinSession ( Action < Session > testAction )
400489 {
0 commit comments