|
10 | 10 |
|
11 | 11 | using System.Linq;
|
12 | 12 | using NHibernate.Cfg;
|
| 13 | +using NHibernate.DomainModel.Northwind.Entities; |
13 | 14 | using NHibernate.Linq;
|
14 | 15 | using NUnit.Framework;
|
15 | 16 |
|
@@ -281,5 +282,140 @@ public async Task CanBeCombinedWithFetchAsync()
|
281 | 282 | Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(5), "Unexpected cache put count");
|
282 | 283 | Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count");
|
283 | 284 | }
|
| 285 | + |
| 286 | + [Test] |
| 287 | + public async Task FetchIsCachableAsync() |
| 288 | + { |
| 289 | + Sfi.Statistics.Clear(); |
| 290 | + await (Sfi.EvictQueriesAsync()); |
| 291 | + |
| 292 | + Order order; |
| 293 | + |
| 294 | + using (var s = Sfi.OpenSession()) |
| 295 | + using (var t = s.BeginTransaction()) |
| 296 | + { |
| 297 | + order = (await (s.Query<Order>() |
| 298 | + .WithOptions(o => o.SetCacheable(true)) |
| 299 | + .Fetch(x => x.Customer) |
| 300 | + .FetchMany(x => x.OrderLines) |
| 301 | + .ThenFetch(x => x.Product) |
| 302 | + .ThenFetchMany(x => x.OrderLines) |
| 303 | + .Where(x => x.OrderId == 10248) |
| 304 | + .ToListAsync())) |
| 305 | + .First(); |
| 306 | + |
| 307 | + await (t.CommitAsync()); |
| 308 | + } |
| 309 | + |
| 310 | + AssertFetchedOrder(order); |
| 311 | + |
| 312 | + Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(1), "Unexpected execution count"); |
| 313 | + Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1), "Unexpected cache put count"); |
| 314 | + Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(1), "Unexpected cache miss count"); |
| 315 | + |
| 316 | + Sfi.Statistics.Clear(); |
| 317 | + |
| 318 | + using (var s = Sfi.OpenSession()) |
| 319 | + using (var t = s.BeginTransaction()) |
| 320 | + { |
| 321 | + order = (await (s.Query<Order>() |
| 322 | + .WithOptions(o => o.SetCacheable(true)) |
| 323 | + .Fetch(x => x.Customer) |
| 324 | + .FetchMany(x => x.OrderLines) |
| 325 | + .ThenFetch(x => x.Product) |
| 326 | + .ThenFetchMany(x => x.OrderLines) |
| 327 | + .Where(x => x.OrderId == 10248) |
| 328 | + .ToListAsync())) |
| 329 | + .First(); |
| 330 | + await (t.CommitAsync()); |
| 331 | + } |
| 332 | + |
| 333 | + AssertFetchedOrder(order); |
| 334 | + |
| 335 | + Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0), "Unexpected execution count"); |
| 336 | + Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(0), "Unexpected cache put count"); |
| 337 | + Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count"); |
| 338 | + Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1), "Unexpected cache hit count"); |
| 339 | + |
| 340 | + } |
| 341 | + |
| 342 | + [Test] |
| 343 | + public async Task FutureFetchIsCachableAsync() |
| 344 | + { |
| 345 | + Sfi.Statistics.Clear(); |
| 346 | + await (Sfi.EvictQueriesAsync()); |
| 347 | + var multiQueries = Sfi.ConnectionProvider.Driver.SupportsMultipleQueries; |
| 348 | + |
| 349 | + Order order; |
| 350 | + |
| 351 | + using (var s = Sfi.OpenSession()) |
| 352 | + using (var t = s.BeginTransaction()) |
| 353 | + { |
| 354 | + s.Query<Order>() |
| 355 | + .WithOptions(o => o.SetCacheable(true)) |
| 356 | + .Fetch(x => x.Customer) |
| 357 | + .Where(x => x.OrderId == 10248) |
| 358 | + .ToFuture(); |
| 359 | + |
| 360 | + order = s.Query<Order>() |
| 361 | + .WithOptions(o => o.SetCacheable(true)) |
| 362 | + .FetchMany(x => x.OrderLines) |
| 363 | + .ThenFetch(x => x.Product) |
| 364 | + .ThenFetchMany(x => x.OrderLines) |
| 365 | + .Where(x => x.OrderId == 10248) |
| 366 | + .ToFuture() |
| 367 | + .ToList() |
| 368 | + .First(); |
| 369 | + |
| 370 | + await (t.CommitAsync()); |
| 371 | + } |
| 372 | + |
| 373 | + AssertFetchedOrder(order); |
| 374 | + |
| 375 | + Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(multiQueries ? 1 : 2), "Unexpected execution count"); |
| 376 | + Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(2), "Unexpected cache put count"); |
| 377 | + Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(2), "Unexpected cache miss count"); |
| 378 | + |
| 379 | + Sfi.Statistics.Clear(); |
| 380 | + |
| 381 | + using (var s = Sfi.OpenSession()) |
| 382 | + using (var t = s.BeginTransaction()) |
| 383 | + { |
| 384 | + s.Query<Order>() |
| 385 | + .WithOptions(o => o.SetCacheable(true)) |
| 386 | + .Fetch(x => x.Customer) |
| 387 | + .Where(x => x.OrderId == 10248) |
| 388 | + .ToFuture(); |
| 389 | + |
| 390 | + order = s.Query<Order>() |
| 391 | + .WithOptions(o => o.SetCacheable(true)) |
| 392 | + .FetchMany(x => x.OrderLines) |
| 393 | + .ThenFetch(x => x.Product) |
| 394 | + .ThenFetchMany(x => x.OrderLines) |
| 395 | + .Where(x => x.OrderId == 10248) |
| 396 | + .ToFuture() |
| 397 | + .ToList() |
| 398 | + .First(); |
| 399 | + |
| 400 | + await (t.CommitAsync()); |
| 401 | + } |
| 402 | + |
| 403 | + AssertFetchedOrder(order); |
| 404 | + |
| 405 | + Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(0), "Unexpected execution count"); |
| 406 | + Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(0), "Unexpected cache put count"); |
| 407 | + Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0), "Unexpected cache miss count"); |
| 408 | + Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(2), "Unexpected cache hit count"); |
| 409 | + } |
| 410 | + |
| 411 | + private static void AssertFetchedOrder(Order order) |
| 412 | + { |
| 413 | + Assert.That(NHibernateUtil.IsInitialized(order.Customer), Is.True, "Expected the fetched Customer to be initialized"); |
| 414 | + Assert.That(NHibernateUtil.IsInitialized(order.OrderLines), Is.True, "Expected the fetched OrderLines to be initialized"); |
| 415 | + Assert.That(order.OrderLines, Has.Count.EqualTo(3), "Expected the fetched OrderLines to have 3 items"); |
| 416 | + var orderLine = order.OrderLines.First(); |
| 417 | + Assert.That(NHibernateUtil.IsInitialized(orderLine.Product), Is.True, "Expected the fetched Product to be initialized"); |
| 418 | + Assert.That(NHibernateUtil.IsInitialized(orderLine.Product.OrderLines), Is.True, "Expected the fetched OrderLines to be initialized"); |
| 419 | + } |
284 | 420 | }
|
285 | 421 | }
|
0 commit comments