forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionStatsFixture.cs
104 lines (90 loc) · 2.5 KB
/
SessionStatsFixture.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
using System.Collections;
using System.Collections.Generic;
using NHibernate.Stat;
using NUnit.Framework;
namespace NHibernate.Test.Stats
{
using Criterion;
[TestFixture]
public class SessionStatsFixture : TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override string[] Mappings
{
get { return new string[] { "Stats.Continent2.hbm.xml" }; }
}
private static Continent FillDb(ISession s)
{
Continent europe = new Continent();
europe.Name="Europe";
Country france = new Country();
france.Name="France";
europe.Countries= new HashSet<Country>();
europe.Countries.Add(france);
s.Save(france);
s.Save(europe);
return europe;
}
private static void CleanDb(ISession s)
{
s.Delete("from Country");
s.Delete("from Continent");
}
[Test]
public void Can_use_cached_query_that_return_no_results()
{
Assert.IsTrue(Sfi.Settings.IsQueryCacheEnabled);
using(ISession s = OpenSession())
{
IList list = s.CreateCriteria(typeof (Country))
.Add(Restrictions.Eq("Name", "Narnia"))
.SetCacheable(true)
.List();
Assert.AreEqual(0, list.Count);
}
using (ISession s = OpenSession())
{
IList list = s.CreateCriteria(typeof(Country))
.Add(Restrictions.Eq("Name", "Narnia"))
.SetCacheable(true)
.List();
Assert.AreEqual(0, list.Count);
}
}
[Test]
public void SessionStatistics()
{
ISession s = OpenSession();
ITransaction tx = s.BeginTransaction();
IStatistics stats = Sfi.Statistics;
stats.Clear();
bool isStats = stats.IsStatisticsEnabled;
stats.IsStatisticsEnabled = true;
Continent europe = FillDb(s);
tx.Commit();
s.Clear();
tx = s.BeginTransaction();
ISessionStatistics sessionStats = s.Statistics;
Assert.AreEqual(0, sessionStats.EntityKeys.Count);
Assert.AreEqual(0, sessionStats.EntityCount);
Assert.AreEqual(0, sessionStats.CollectionKeys.Count);
Assert.AreEqual(0, sessionStats.CollectionCount);
europe = s.Get<Continent>(europe.Id);
NHibernateUtil.Initialize(europe.Countries);
IEnumerator itr = europe.Countries.GetEnumerator();
itr.MoveNext();
NHibernateUtil.Initialize(itr.Current);
Assert.AreEqual(2, sessionStats.EntityKeys.Count);
Assert.AreEqual(2, sessionStats.EntityCount);
Assert.AreEqual(1, sessionStats.CollectionKeys.Count);
Assert.AreEqual(1, sessionStats.CollectionCount);
CleanDb(s);
tx.Commit();
s.Close();
stats.IsStatisticsEnabled = isStats;
}
}
}