forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionStatsFixture.cs
116 lines (100 loc) · 3.33 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
105
106
107
108
109
110
111
112
113
114
115
116
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using NHibernate.Stat;
using NUnit.Framework;
namespace NHibernate.Test.Stats
{
using Criterion;
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
public class SessionStatsFixtureAsync : TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override string[] Mappings
{
get { return new string[] { "Stats.Continent2.hbm.xml" }; }
}
private static async Task<Continent> FillDbAsync(ISession s, CancellationToken cancellationToken = default(CancellationToken))
{
Continent europe = new Continent();
europe.Name="Europe";
Country france = new Country();
france.Name="France";
europe.Countries= new HashSet<Country>();
europe.Countries.Add(france);
await (s.SaveAsync(france, cancellationToken));
await (s.SaveAsync(europe, cancellationToken));
return europe;
}
private static async Task CleanDbAsync(ISession s, CancellationToken cancellationToken = default(CancellationToken))
{
await (s.DeleteAsync("from Country", cancellationToken));
await (s.DeleteAsync("from Continent", cancellationToken));
}
[Test]
public async Task Can_use_cached_query_that_return_no_resultsAsync()
{
Assert.IsTrue(Sfi.Settings.IsQueryCacheEnabled);
using(ISession s = OpenSession())
{
IList list = await (s.CreateCriteria(typeof (Country))
.Add(Restrictions.Eq("Name", "Narnia"))
.SetCacheable(true)
.ListAsync());
Assert.AreEqual(0, list.Count);
}
using (ISession s = OpenSession())
{
IList list = await (s.CreateCriteria(typeof(Country))
.Add(Restrictions.Eq("Name", "Narnia"))
.SetCacheable(true)
.ListAsync());
Assert.AreEqual(0, list.Count);
}
}
[Test]
public async Task SessionStatisticsAsync()
{
ISession s = OpenSession();
ITransaction tx = s.BeginTransaction();
IStatistics stats = Sfi.Statistics;
stats.Clear();
bool isStats = stats.IsStatisticsEnabled;
stats.IsStatisticsEnabled = true;
Continent europe = await (FillDbAsync(s));
await (tx.CommitAsync());
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 = await (s.GetAsync<Continent>(europe.Id));
await (NHibernateUtil.InitializeAsync(europe.Countries));
IEnumerator itr = europe.Countries.GetEnumerator();
itr.MoveNext();
await (NHibernateUtil.InitializeAsync(itr.Current));
Assert.AreEqual(2, sessionStats.EntityKeys.Count);
Assert.AreEqual(2, sessionStats.EntityCount);
Assert.AreEqual(1, sessionStats.CollectionKeys.Count);
Assert.AreEqual(1, sessionStats.CollectionCount);
await (CleanDbAsync(s));
await (tx.CommitAsync());
s.Close();
stats.IsStatisticsEnabled = isStats;
}
}
}