Skip to content

Commit e767cb2

Browse files
committed
Adds tests of Prefetch for QueryResult<T>
1 parent 006154b commit e767cb2

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

Orm/Xtensive.Orm.Manual/Prefetch/PrefetchTest.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,93 @@ public async Task DelayedQueryAsyncTest()
375375
transactionScope.Complete();
376376
}
377377
}
378+
379+
[Test]
380+
public void CachedQueryTest()
381+
{
382+
using (var session = Domain.OpenSession())
383+
using (var transactionScope = session.OpenTransaction()) {
384+
var employee = new Person(session) { Name = "Employee", Photo = new byte[] { 8, 0 } };
385+
var manager = new Person(session) { Name = "Manager", Photo = new byte[] { 8, 0 } };
386+
_ = manager.Employees.Add(employee);
387+
transactionScope.Complete();
388+
}
389+
390+
using (var session = Domain.OpenSession())// no session activation!
391+
using (var transactionScope = session.OpenTransaction()) {
392+
var people = session.Query.Execute(q => q.All<Person>())
393+
.Prefetch(p => p.Photo) // Lazy load field
394+
.Prefetch(p => p.Employees // EntitySet Employees
395+
.Prefetch(e => e.Photo)) // and lazy load field of each of its items
396+
.Prefetch(p => p.Manager); // Referenced entity
397+
foreach (var person in people) {
398+
// some code here...
399+
}
400+
transactionScope.Complete();
401+
}
402+
403+
using (var session = Domain.OpenSession())// no session activation!
404+
using (var transactionScope = session.OpenTransaction()) {
405+
var people = session.Query.Execute(q => q.All<Person>())
406+
.Prefetch(p => p.Photo) // Lazy load field
407+
.Prefetch(p => p.Employees.Prefetch(e => e.Photo)) // EntitySet Employees and lazy load field of each of its items with the limit on number of items to be loaded
408+
.Prefetch(p => p.Manager.Photo); // Referenced entity and lazy load field for each of them
409+
foreach (var person in people) {
410+
var accessor = DirectStateAccessor.Get(person);
411+
Assert.That(accessor.GetFieldState("Photo"), Is.EqualTo(PersistentFieldState.Loaded));
412+
Assert.That(accessor.GetFieldState("Manager"), Is.EqualTo(PersistentFieldState.Loaded));
413+
if (person.ManagerKey != null) {
414+
Assert.IsNotNull(DirectStateAccessor.Get(session)[person.ManagerKey]);
415+
Assert.That(DirectStateAccessor.Get(person.Manager).GetFieldState("Photo"), Is.EqualTo(PersistentFieldState.Loaded));
416+
}
417+
// some code here...
418+
}
419+
transactionScope.Complete();
420+
}
421+
}
422+
423+
[Test]
424+
public async Task CachedQueryAsyncTest()
425+
{
426+
await using (var session = await Domain.OpenSessionAsync())
427+
using (var transactionScope = session.OpenTransaction()) {
428+
var employee = new Person(session) { Name = "Employee", Photo = new byte[] { 8, 0 } };
429+
var manager = new Person(session) { Name = "Manager", Photo = new byte[] { 8, 0 } };
430+
_ = manager.Employees.Add(employee);
431+
transactionScope.Complete();
432+
}
433+
434+
await using (var session = await Domain.OpenSessionAsync())// no session activation!
435+
using (var transactionScope = session.OpenTransaction()) {
436+
var people = (await session.Query.ExecuteAsync(q => q.All<Person>()))
437+
.Prefetch(p => p.Photo) // Lazy load field
438+
.Prefetch(p => p.Employees // EntitySet Employees
439+
.Prefetch(e => e.Photo)) // and lazy load field of each of its items
440+
.Prefetch(p => p.Manager); // Referenced entity
441+
foreach (var person in people) {
442+
// some code here...
443+
}
444+
transactionScope.Complete();
445+
}
446+
447+
await using (var session = await Domain.OpenSessionAsync())// no session activation!
448+
using (var transactionScope = session.OpenTransaction()) {
449+
var people = (await session.Query.ExecuteAsync(q => q.All<Person>()))
450+
.Prefetch(p => p.Photo) // Lazy load field
451+
.Prefetch(p => p.Employees.Prefetch(e => e.Photo)) // EntitySet Employees and lazy load field of each of its items with the limit on number of items to be loaded
452+
.Prefetch(p => p.Manager.Photo); // Referenced entity and lazy load field for each of them
453+
await foreach (var person in people.AsAsyncEnumerable()) {
454+
var accessor = DirectStateAccessor.Get(person);
455+
Assert.That(accessor.GetFieldState("Photo"), Is.EqualTo(PersistentFieldState.Loaded));
456+
Assert.That(accessor.GetFieldState("Manager"), Is.EqualTo(PersistentFieldState.Loaded));
457+
if (person.ManagerKey != null) {
458+
Assert.IsNotNull(DirectStateAccessor.Get(session)[person.ManagerKey]);
459+
Assert.That(DirectStateAccessor.Get(person.Manager).GetFieldState("Photo"), Is.EqualTo(PersistentFieldState.Loaded));
460+
}
461+
// some code here...
462+
}
463+
transactionScope.Complete();
464+
}
465+
}
378466
}
379467
}

0 commit comments

Comments
 (0)