forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeverCachedEntityTests.cs
212 lines (177 loc) · 5.38 KB
/
NeverCachedEntityTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Impl;
using NHibernate.Linq;
using NHibernate.Test.SecondLevelCacheTests;
using NSubstitute;
using NUnit.Framework;
namespace NHibernate.Test.SecondLevelCacheTest
{
[TestFixture]
public class NeverCachedEntityTests : TestCase
{
protected override string MappingsAssembly => "NHibernate.Test";
protected override string[] Mappings => new[] { "SecondLevelCacheTest.Item.hbm.xml" };
protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.CacheProvider, typeof(HashtableCacheProvider).AssemblyQualifiedName);
configuration.SetProperty(Environment.UseQueryCache, "true");
}
[Test]
public void NeverInvalidateEntities()
{
var debugSessionFactory = (DebugSessionFactory) Sfi;
var cache = Substitute.For<UpdateTimestampsCache>(Sfi.Settings, new Dictionary<string, string>());
var updateTimestampsCacheField = typeof(SessionFactoryImpl).GetField(
"updateTimestampsCache",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
updateTimestampsCacheField.SetValue(debugSessionFactory.ActualFactory, cache);
//"Received" assertions can not be used since the collection is reused and cleared between calls.
//The received args are cloned and stored
var preInvalidations = new List<IReadOnlyCollection<string>>();
var invalidations = new List<IReadOnlyCollection<string>>();
cache.PreInvalidate(Arg.Do<IReadOnlyCollection<string>>(x => preInvalidations.Add(x.ToList())));
cache.Invalidate(Arg.Do<IReadOnlyCollection<string>>(x => invalidations.Add(x.ToList())));
using (var session = OpenSession())
{
List<int> ids = new List<int>();
//Add NeverItem
using (var tx = session.BeginTransaction())
{
foreach (var i in Enumerable.Range(1, 10))
{
var item = new NeverItem { Name = "Abatay" };
item.Childrens.Add(new NeverChildItem()
{
Name = "Child",
Parent = item
});
session.Save(item);
ids.Add(item.Id);
}
tx.Commit();
}
//Update NeverItem
using (var tx = session.BeginTransaction())
{
foreach (var i in ids)
{
var item = session.Get<NeverItem>(i);
item.Name = item.Id.ToString();
}
tx.Commit();
}
//Delete NeverItem
using (var tx = session.BeginTransaction())
{
foreach (var i in ids)
{
var item = session.Get<NeverItem>(i);
session.Delete(item);
}
tx.Commit();
}
//Update NeverItem using HQL
using (var tx = session.BeginTransaction())
{
session.CreateQuery("UPDATE NeverItem SET Name='Test'").ExecuteUpdate();
tx.Commit();
}
//Update NeverItem using LINQ
using (var tx = session.BeginTransaction())
{
session.Query<NeverItem>()
.UpdateBuilder()
.Set(x => x.Name, "Test")
.Update();
tx.Commit();
}
}
//Should receive none preinvalidation when Cache is configured as never
Assert.That(preInvalidations, Has.Count.EqualTo(0));
//Should receive none invalidation when Cache is configured as never
Assert.That(invalidations, Has.Count.EqualTo(0));
}
[Test]
public void QueryCache_ThrowsException()
{
using (var session = OpenSession())
{
//Linq
using (var tx = session.BeginTransaction())
{
Assert.Throws<QueryException>(() => session
.Query<NeverItem>().WithOptions(x => x.SetCacheable(true)).ToList());
tx.Commit();
}
//Linq Multiple with error message we will quarantied that gets 2 class in error message
using (var tx = session.BeginTransaction())
{
Assert.Throws<QueryException>(() => session
.Query<NeverItem>().Where(x => x.Childrens.Any())
.WithOptions(x => x.SetCacheable(true))
.ToList(),
$"Never cached entity:{string.Join(", ", typeof(NeverItem).FullName, typeof(NeverChildItem).FullName)} cannot be used in cacheable query");
tx.Commit();
}
//Hql
using (var tx = session.BeginTransaction())
{
Assert.Throws<QueryException>(() => session
.CreateQuery("from NeverItem").SetCacheable(true).List<NeverItem>());
tx.Commit();
}
//ICriteria
using (var tx = session.BeginTransaction())
{
Assert.Throws<QueryException>(() => session
.CreateCriteria<NeverItem>()
.SetCacheable(true)
.List<NeverItem>());
tx.Commit();
}
//Native Sql
using (var tx = session.BeginTransaction())
{
Assert.Throws<QueryException>(() => session
.CreateSQLQuery("select * from NeverItem")
.AddSynchronizedQuerySpace("NeverItem")
.SetCacheable(true)
.List<NeverItem>());
tx.Commit();
}
}
}
[Test]
public void ShouldAutoFlush()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var e1 = new NeverItem { Name = "Abatay" };
e1.Childrens.Add(new NeverChildItem()
{
Name = "Child",
Parent = e1
});
session.Save(e1);
var result = (from e in session.Query<NeverItem>()
where e.Name == "Abatay"
select e).ToList();
Assert.That(result.Count, Is.EqualTo(1));
}
}
protected override void OnTearDown()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Delete("from NeverItem");
tx.Commit();
}
}
}
}