forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryCacheFixture.cs
94 lines (82 loc) · 2.41 KB
/
QueryCacheFixture.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
using System.Collections;
using NHibernate.Cfg;
using NHibernate.DomainModel;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;
namespace NHibernate.Test.CacheTest
{
[TestFixture]
public class QueryCacheFixture : TestCase
{
protected override string[] Mappings => new[] { "Simple.hbm.xml" };
protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.UseQueryCache, "true");
}
protected override void OnSetUp()
{
using (var s = OpenSession())
{
s.Save(new Simple(), 1L);
s.Flush();
}
}
protected override void OnTearDown()
{
using (var s = OpenSession())
{
s.Delete("from Simple");
s.Flush();
}
}
[Test]
public void QueryCacheWithNullParameters()
{
using (var s = OpenSession())
{
const string query = "from Simple s where s = :s or s.Name = :name or s.Address = :address";
s
.CreateQuery(query)
.SetEntity("s", s.Load(typeof(Simple), 1L))
.SetString("name", null)
.SetString("address", null)
.SetCacheable(true)
.UniqueResult();
// Run a second time, just to test the query cache
var result = s
.CreateQuery(query)
.SetEntity("s", s.Load(typeof(Simple), 1L))
.SetString("name", null)
.SetString("address", null)
.SetCacheable(true)
.UniqueResult();
Assert.That(result, Is.Not.Null);
Assert.That(s.GetIdentifier(result), Is.EqualTo(1));
}
}
[Test]
public void QueryCacheWithScalarReturn()
{
// Using decimal because:
// - int is yielded back as decimal by Oracle, wrecking the cast back to int.
// - Oracle requires a cast to binary_double for yielding a double instead of decimal.
// - double is not castable in MySql.
// So long for SQLite which does not have a true decimal type.
if (TestDialect.HasBrokenDecimalType)
Assert.Ignore("Database does not support properly decimals.");
using (var s = OpenSession())
{
var result = s
.CreateSQLQuery("select cast(200012 as decimal) from Simple where id_ = 1")
.SetCacheable(true)
.UniqueResult<decimal>();
Assert.That(result, Is.EqualTo(200012), "Unexpected non-cached result");
result = s
.CreateSQLQuery("select cast(200012 as decimal) from Simple where id_ = 1")
.SetCacheable(true)
.UniqueResult<decimal>();
Assert.That(result, Is.EqualTo(200012), "Unexpected cached result");
}
}
}
}