Skip to content

Commit bc8424b

Browse files
illkostinfmaca88
authored andcommitted
Added unit test that checks caching query result set with ThenFetchMany statement
1 parent 186a88b commit bc8424b

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.CachingComplexQuery
4+
{
5+
public class Car
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
10+
public virtual Person Owner { get; set; }
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.CachingComplexQuery
5+
{
6+
public class Child
7+
{
8+
public virtual Guid Id { get; set; }
9+
public virtual string Name { get; set; }
10+
11+
public virtual Person Parent { get; set; }
12+
13+
public virtual ISet<Pet> Pets
14+
{
15+
get => _pets ?? (_pets = new HashSet<Pet>());
16+
set => _pets = value;
17+
}
18+
private ISet<Pet> _pets;
19+
}
20+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Mapping.ByCode;
4+
using NHibernate.Linq;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.CachingComplexQuery
8+
{
9+
/// <summary>
10+
/// Fixture using 'by code' mappings
11+
/// </summary>
12+
/// <remarks>
13+
/// This fixture is identical to <see cref="Fixture" /> except the <see cref="Person" /> mapping is performed
14+
/// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach
15+
/// if you prefer.
16+
/// </remarks>
17+
[TestFixture]
18+
public class ByCodeFixture : TestCaseMappingByCode
19+
{
20+
protected override HbmMapping GetMappings()
21+
{
22+
var mapper = new ModelMapper();
23+
mapper.Class<Person>(rc =>
24+
{
25+
rc.Id(x => x.Id, m => m.Generator(Generators.Guid));
26+
rc.Property(x => x.Name);
27+
rc.Property(x => x.Age);
28+
rc.Set(
29+
x => x.Children,
30+
colMap =>
31+
{
32+
colMap.Inverse(true);
33+
colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans);
34+
colMap.Cache(c => c.Usage(CacheUsage.ReadWrite));
35+
},
36+
rel => rel.OneToMany());
37+
rc.Set(
38+
x => x.Cars,
39+
colMap =>
40+
{
41+
colMap.Inverse(true);
42+
colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans);
43+
colMap.Cache(c => c.Usage(CacheUsage.ReadWrite));
44+
},
45+
rel => rel.OneToMany());
46+
rc.Cache(c => c.Usage(CacheUsage.ReadWrite));
47+
});
48+
mapper.Class<Child>(ch =>
49+
{
50+
ch.Id(x => x.Id, m => m.Generator(Generators.Guid));
51+
ch.Property(x => x.Name);
52+
ch.ManyToOne(c => c.Parent);
53+
54+
ch.Set(
55+
x => x.Pets,
56+
colMap =>
57+
{
58+
colMap.Inverse(true);
59+
colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans);
60+
colMap.Cache(c => c.Usage(CacheUsage.ReadWrite));
61+
},
62+
rel => rel.OneToMany());
63+
64+
ch.Cache(c => c.Usage(CacheUsage.ReadWrite));
65+
});
66+
mapper.Class<Pet>(ch =>
67+
{
68+
ch.Id(x => x.Id, m => m.Generator(Generators.Guid));
69+
ch.Property(x => x.Name);
70+
ch.ManyToOne(c => c.Owner);
71+
72+
ch.Cache(c => c.Usage(CacheUsage.ReadWrite));
73+
});
74+
mapper.Class<Car>(ch =>
75+
{
76+
ch.Id(x => x.Id, m => m.Generator(Generators.Guid));
77+
ch.Property(x => x.Name);
78+
ch.ManyToOne(c => c.Owner);
79+
80+
ch.Cache(c => c.Usage(CacheUsage.ReadWrite));
81+
});
82+
83+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
84+
}
85+
86+
protected override void OnSetUp()
87+
{
88+
using (var session = OpenSession())
89+
using (var transaction = session.BeginTransaction())
90+
{
91+
var person = new Person { Name = "Person 1", Age = 18 };
92+
93+
var car1 = new Car { Name = "Car1", Owner = person };
94+
var car2 = new Car { Name = "Car2", Owner = person };
95+
session.Save(car1);
96+
session.Save(car2);
97+
98+
session.Save(person);
99+
transaction.Commit();
100+
}
101+
}
102+
103+
protected override void OnTearDown()
104+
{
105+
using (var session = OpenSession())
106+
using (var transaction = session.BeginTransaction())
107+
{
108+
session.Delete("from Pet");
109+
session.Delete("from Child");
110+
session.Delete("from Car");
111+
session.Delete("from Person");
112+
113+
transaction.Commit();
114+
}
115+
}
116+
117+
[Test]
118+
public void TestQueryCachingWithThenFetchMany()
119+
{
120+
using (var session = OpenSession())
121+
{
122+
using (var transaction = session.BeginTransaction())
123+
{
124+
var query =
125+
session
126+
.Query<Person>()
127+
.FetchMany(p => p.Children)
128+
.ThenFetchMany(ch => ch.Pets)
129+
.FetchMany(p => p.Cars) as IQueryable<Person>;
130+
131+
query = query.WithOptions(opt =>
132+
opt.SetCacheable(true)
133+
.SetCacheMode(CacheMode.Normal)
134+
.SetCacheRegion("Long_Cache"));
135+
136+
query.ToList(); // First time the result will be cached
137+
Assert.That(() => query.ToList(), Throws.Nothing); //
138+
transaction.Commit();
139+
}
140+
}
141+
}
142+
}
143+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.CachingComplexQuery
5+
{
6+
public class Person
7+
{
8+
public virtual Guid Id { get; set; }
9+
public virtual string Name { get; set; }
10+
public virtual int Age { get; set; }
11+
12+
public virtual ISet<Car> Cars
13+
{
14+
get => _cars ?? (_cars = new HashSet<Car>());
15+
set => _cars = value;
16+
}
17+
private ISet<Car> _cars;
18+
19+
public virtual ISet<Child> Children
20+
{
21+
get => _children ?? (_children = new HashSet<Child>());
22+
set => _children = value;
23+
}
24+
private ISet<Child> _children;
25+
}
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.CachingComplexQuery
4+
{
5+
public class Pet
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
10+
public virtual Child Owner { get; set; }
11+
}
12+
}

0 commit comments

Comments
 (0)