Skip to content

Commit 067f730

Browse files
committed
Add tests to ensure query cache correctly caches 'is null' query parts.
1 parent ce18831 commit 067f730

File tree

1 file changed

+113
-7
lines changed

1 file changed

+113
-7
lines changed

src/NHibernate.Test/NHSpecificTest/NH3634/FixtureByCode.cs

Lines changed: 113 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,29 @@ protected override void OnSetUp()
4646
};
4747
session.Save(e2);
4848

49-
var cachedConnection = new Connection
49+
var cachedNullConnection = new Connection
5050
{
5151
Address = "test.com",
5252
ConnectionType = "http",
5353
};
54-
var cachedPerson = new CachedPerson
54+
var cachedNullConnectionPerson = new CachedPerson
5555
{
56-
Name = "Cached",
57-
Connection = cachedConnection
56+
Name = "CachedNull",
57+
Connection = cachedNullConnection
5858
};
59-
session.Save(cachedPerson);
59+
var cachedNotNullConnection = new Connection
60+
{
61+
Address = "test.com",
62+
ConnectionType = "http",
63+
PortName = "port"
64+
};
65+
var cachedNotNullConnectionPerson = new CachedPerson
66+
{
67+
Name = "CachedNotNull",
68+
Connection = cachedNotNullConnection
69+
};
70+
session.Save(cachedNullConnectionPerson);
71+
session.Save(cachedNotNullConnectionPerson);
6072

6173
session.Flush();
6274
transaction.Commit();
@@ -145,6 +157,100 @@ public void QueryAgainstComponentWithANullPropertyUsingCriteria()
145157
}
146158
}
147159

160+
[Test]
161+
public void CachedQueryMissesWithDifferentNotNullComponent()
162+
{
163+
var componentToCompare = new Connection
164+
{
165+
ConnectionType = "http",
166+
Address = "test.com",
167+
PortName = null
168+
};
169+
170+
using (ISession session = OpenSession())
171+
using (ITransaction tx = session.BeginTransaction())
172+
{
173+
var cached = session.CreateCriteria<CachedPerson>()
174+
.Add(Restrictions.Eq("Connection", componentToCompare))
175+
.SetCacheable(true)
176+
.UniqueResult<CachedPerson>();
177+
178+
Assert.That(cached.Name, Is.EqualTo("CachedNull"));
179+
Assert.That(cached.Connection.PortName, Is.Null);
180+
181+
using (var dbCommand = session.Connection.CreateCommand())
182+
{
183+
dbCommand.CommandText = "DELETE FROM cachedpeople";
184+
tx.Enlist(dbCommand);
185+
dbCommand.ExecuteNonQuery();
186+
}
187+
188+
tx.Commit();
189+
}
190+
191+
componentToCompare.PortName = "port";
192+
using (ISession session = OpenSession())
193+
using (ITransaction tx = session.BeginTransaction())
194+
{
195+
//Cache should not return cached entity, because it no longer matches criteria
196+
var cachedPeople = session.CreateCriteria<CachedPerson>()
197+
.Add(Restrictions.Eq("Connection", componentToCompare))
198+
.SetCacheable(true)
199+
.List<CachedPerson>();
200+
201+
Assert.That(cachedPeople, Is.Empty);
202+
203+
tx.Commit();
204+
}
205+
}
206+
207+
[Test]
208+
public void CachedQueryMissesWithDifferentNullComponent()
209+
{
210+
var componentToCompare = new Connection
211+
{
212+
ConnectionType = "http",
213+
Address = "test.com",
214+
PortName = "port"
215+
};
216+
217+
using (ISession session = OpenSession())
218+
using (ITransaction tx = session.BeginTransaction())
219+
{
220+
var cached = session.CreateCriteria<CachedPerson>()
221+
.Add(Restrictions.Eq("Connection", componentToCompare))
222+
.SetCacheable(true)
223+
.UniqueResult<CachedPerson>();
224+
225+
Assert.That(cached.Name, Is.EqualTo("CachedNotNull"));
226+
Assert.That(cached.Connection.PortName, Is.Not.Null);
227+
228+
using (var dbCommand = session.Connection.CreateCommand())
229+
{
230+
dbCommand.CommandText = "DELETE FROM cachedpeople";
231+
tx.Enlist(dbCommand);
232+
dbCommand.ExecuteNonQuery();
233+
}
234+
235+
tx.Commit();
236+
}
237+
238+
componentToCompare.PortName = null;
239+
using (ISession session = OpenSession())
240+
using (ITransaction tx = session.BeginTransaction())
241+
{
242+
//Cache should not return cached entity, because it no longer matches criteria
243+
var cachedPeople = session.CreateCriteria<CachedPerson>()
244+
.Add(Restrictions.Eq("Connection", componentToCompare))
245+
.SetCacheable(true)
246+
.List<CachedPerson>();
247+
248+
Assert.That(cachedPeople, Is.Empty);
249+
250+
tx.Commit();
251+
}
252+
}
253+
148254
[Test]
149255
public void CachedQueryAgainstComponentWithANullPropertyUsingCriteria()
150256
{
@@ -163,7 +269,7 @@ public void CachedQueryAgainstComponentWithANullPropertyUsingCriteria()
163269
.SetCacheable(true)
164270
.UniqueResult<CachedPerson>();
165271

166-
Assert.That(cached.Name, Is.EqualTo("Cached"));
272+
Assert.That(cached.Name, Is.EqualTo("CachedNull"));
167273
Assert.That(cached.Connection.PortName, Is.Null);
168274

169275
using (var dbCommand = session.Connection.CreateCommand())
@@ -185,7 +291,7 @@ public void CachedQueryAgainstComponentWithANullPropertyUsingCriteria()
185291
.SetCacheable(true)
186292
.UniqueResult<CachedPerson>();
187293

188-
Assert.That(cached.Name, Is.EqualTo("Cached"));
294+
Assert.That(cached.Name, Is.EqualTo("CachedNull"));
189295
Assert.That(cached.Connection.PortName, Is.Null);
190296

191297
tx.Commit();

0 commit comments

Comments
 (0)