forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixture.cs
184 lines (158 loc) · 6.5 KB
/
Fixture.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
using System;
using NHibernate.Criterion;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Exceptions;
using NUnit.Framework;
namespace NHibernate.Test.ProjectionFixtures
{
[TestFixture]
public class Fixture : TestCase
{
protected override string[] Mappings
{
get { return new string[] { "ProjectionFixtures.Mapping.hbm.xml" }; }
}
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override void OnSetUp()
{
using(var s = Sfi.OpenSession())
using(var tx = s.BeginTransaction())
{
var root = new TreeNode
{
Key = new Key {Id = 1, Area = 2},
Type = NodeType.Plain
};
var child = new TreeNode
{
Key = new Key { Id = 11, Area = 2 },
Type = NodeType.Blue
};
var grandchild = new TreeNode
{
Key = new Key {Id = 111, Area = 2},
Type = NodeType.Smart
};
root.DirectChildren.Add(child);
child.Parent = root;
grandchild.Parent = child;
child.DirectChildren.Add(grandchild);
s.Save(root);
s.Save(child);
s.Save(grandchild);
tx.Commit();
}
}
protected override void OnTearDown()
{
using(var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
s.Delete("from TreeNode");
tx.Commit();
}
}
[Test]
public void ErrorFromDBWillGiveTheActualSQLExecuted()
{
if (!(Dialect is MsSql2000Dialect))
Assert.Ignore(
"Test checks for exact sql and expects an error to occur in a case which is not erroneous on all databases.");
string pName = ((ISqlParameterFormatter) Sfi.ConnectionProvider.Driver).GetParameterName(0);
string expectedMessagePart0 =
string.Format("could not execute query" + Environment.NewLine +
"[ SELECT this_.Id as y0_, count(this_.Area) as y1_ FROM TreeNode this_ WHERE this_.Id = {0} ]",
pName);
string expectedMessagePart1 =
string.Format(
@"[SQL: SELECT this_.Id as y0_, count(this_.Area) as y1_ FROM TreeNode this_ WHERE this_.Id = {0}]", pName);
DetachedCriteria projection = DetachedCriteria.For<TreeNode>("child")
.Add(Restrictions.Eq("child.Key.Id", 2))
.SetProjection(
Projections.ProjectionList()
.Add(Projections.Property("child.Key.Id"))
.Add(Projections.Count("child.Key.Area"))
);
var e = Assert.Throws<GenericADOException>(() =>
{
using (var s = Sfi.OpenSession())
using (var tx = s.BeginTransaction())
{
var criteria = projection.GetExecutableCriteria(s);
criteria.List();
tx.Commit();
}
});
Assert.That(e.Message, Does.Contain(expectedMessagePart0).Or.Contains(expectedMessagePart1));
}
[Test]
public void AggregatingHirearchyWithCount()
{
var root = new Key {Id = 1, Area = 2};
DetachedCriteria projection = DetachedCriteria.For<TreeNode>("child")
.Add(Restrictions.Eq("Parent.id", root))
.Add(Restrictions.Gt("Key.Id", 0))
.Add(Restrictions.Eq("Type", NodeType.Blue))
.CreateAlias("DirectChildren", "grandchild")
.SetProjection(
Projections.ProjectionList()
.Add(Projections.GroupProperty("child.Key.Id"))
.Add(Projections.GroupProperty("child.Key.Area"))
.Add(Projections.Count(Projections.Property("grandchild.Key.Id")))
);
using(var s = Sfi.OpenSession())
using(var tx = s.BeginTransaction())
{
var criteria = projection.GetExecutableCriteria(s);
var list = criteria.List();
Assert.AreEqual(1, list.Count);
var tuple = (object[]) list[0];
Assert.AreEqual(11, tuple[0]);
Assert.AreEqual(2, tuple[1]);
Assert.AreEqual(1, tuple[2]);
tx.Commit();
}
}
[Test]
public void LimitingResultSetOnQueryThatIsOrderedByProjection()
{
if (!Dialect.SupportsScalarSubSelects)
Assert.Ignore("Dialect does not support scalar sub-select");
using(var s = OpenSession())
{
ICriteria criteria = s.CreateCriteria(typeof(TreeNode), "parent")
.Add(Restrictions.Gt("Key.Id", 0));
var currentAssessment = DetachedCriteria.For<TreeNode>("child")
.Add(Restrictions.EqProperty("Key.Id", "parent.Key.Id"))
.Add(Restrictions.EqProperty("Key.Area", "parent.Key.Area"))
.Add(Restrictions.Eq("Type", NodeType.Smart))
.SetProjection(Projections.Property("Type"));
criteria.AddOrder(Order.Asc(Projections.SubQuery(currentAssessment)))
.SetMaxResults(1000);
criteria.List();
}
}
[Test]
public void QueryingWithParemetersAndParaemtersInOrderBy()
{
if (!Dialect.SupportsScalarSubSelects)
Assert.Ignore("Dialect does not support scalar sub-select");
using (var s = OpenSession())
{
ICriteria criteria = s.CreateCriteria(typeof(TreeNode), "parent")
.Add(Restrictions.Like("Name","ayende"))
.Add(Restrictions.Gt("Key.Id", 0));
var currentAssessment = DetachedCriteria.For<TreeNode>("child")
.Add(Restrictions.Eq("Type", NodeType.Smart))
.SetProjection(Projections.Property("Type"));
criteria.AddOrder(Order.Asc(Projections.SubQuery(currentAssessment)))
.SetMaxResults(1000);
criteria.List();
}
}
}
}