forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectionsTest.cs
363 lines (327 loc) · 9.31 KB
/
ProjectionsTest.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//------------------------------------------------------------------------------
// <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.Criterion;
using NHibernate.Dialect;
using NUnit.Framework;
namespace NHibernate.Test.Criteria
{
using System.Threading.Tasks;
[TestFixture]
public class ProjectionsTestAsync : TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override IList Mappings
{
get
{
return new string[]
{
"Criteria.Enrolment.hbm.xml",
"Criteria.Animal.hbm.xml",
"Criteria.MaterialResource.hbm.xml"
};
}
}
protected override void OnSetUp()
{
using (ISession session = OpenSession())
{
ITransaction t = session.BeginTransaction();
Student gavin = new Student();
gavin.Name = "ayende";
gavin.StudentNumber = 27;
session.Save(gavin);
t.Commit();
}
}
protected override void OnTearDown()
{
using (ISession session = Sfi.OpenSession())
{
session.Delete("from System.Object");
session.Flush();
}
}
[Test]
public async Task UsingSqlFunctions_ConcatAsync()
{
using (ISession session = Sfi.OpenSession())
{
string result = await (session.CreateCriteria(typeof(Student))
.SetProjection(new SqlFunctionProjection("concat",
NHibernateUtil.String,
Projections.Property("Name"),
new ConstantProjection(" "),
Projections.Property("Name")
))
.UniqueResultAsync<string>());
Assert.AreEqual("ayende ayende", result);
}
}
[Test]
public async Task UsingSqlFunctions_Concat_WithCastAsync()
{
if(Dialect is Oracle8iDialect)
{
Assert.Ignore("Not supported by the active dialect:{0}.", Dialect);
}
using (ISession session = Sfi.OpenSession())
{
string result = await (session.CreateCriteria(typeof(Student))
.SetProjection(Projections.SqlFunction("concat",
NHibernateUtil.String,
Projections.Cast(NHibernateUtil.String, Projections.Id()),
Projections.Constant(" "),
Projections.Property("Name")
))
.UniqueResultAsync<string>());
Assert.AreEqual("27 ayende", result);
}
}
[Test]
public async Task CanUseParametersWithProjectionsAsync()
{
using (ISession session = Sfi.OpenSession())
{
long result = await (session.CreateCriteria(typeof(Student))
.SetProjection(new AddNumberProjection("id", 15))
.UniqueResultAsync<long>());
Assert.AreEqual(42L, result);
}
}
[Test]
public async Task UsingConditionalsAsync()
{
using (ISession session = Sfi.OpenSession())
{
string result = await (session.CreateCriteria(typeof(Student))
.SetProjection(
Projections.Conditional(
Expression.Eq("id", 27L),
Projections.Constant("yes"),
Projections.Constant("no"))
)
.UniqueResultAsync<string>());
Assert.AreEqual("yes", result);
result = await (session.CreateCriteria(typeof(Student))
.SetProjection(
Projections.Conditional(
Expression.Eq("id", 42L),
Projections.Constant("yes"),
Projections.Constant("no"))
)
.UniqueResultAsync<string>());
Assert.AreEqual("no", result);
}
}
[Test]
public async Task UseInWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.In(Projections.Id(), new object[] { 27 }))
.ListAsync<Student>());
Assert.AreEqual(27L, list[0].StudentNumber);
}
}
[Test]
public async Task UseLikeWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.Like(Projections.Property("Name"), "aye", MatchMode.Start))
.ListAsync<Student>());
Assert.AreEqual(27L, list[0].StudentNumber);
}
}
[Test]
public async Task UseInsensitiveLikeWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.InsensitiveLike(Projections.Property("Name"), "AYE", MatchMode.Start))
.ListAsync<Student>());
Assert.AreEqual(27L, list[0].StudentNumber);
}
}
[Test]
public async Task UseIdEqWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.IdEq(Projections.Id()))
.ListAsync<Student>());
Assert.AreEqual(27L, list[0].StudentNumber);
}
}
[Test]
public async Task UseEqWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.Eq(Projections.Id(), 27L))
.ListAsync<Student>());
Assert.AreEqual(27L, list[0].StudentNumber);
}
}
[Test]
public async Task UseGtWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.Gt(Projections.Id(), 2L))
.ListAsync<Student>());
Assert.AreEqual(27L, list[0].StudentNumber);
}
}
[Test]
public async Task UseLtWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.Lt(Projections.Id(), 200L))
.ListAsync<Student>());
Assert.AreEqual(27L, list[0].StudentNumber);
}
}
[Test]
public async Task UseLeWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.Le(Projections.Id(), 27L))
.ListAsync<Student>());
Assert.AreEqual(27L, list[0].StudentNumber);
}
}
[Test]
public async Task UseGeWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.Ge(Projections.Id(), 27L))
.ListAsync<Student>());
Assert.AreEqual(27L, list[0].StudentNumber);
}
}
[Test]
public async Task UseBetweenWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.Between(Projections.Id(), 10L, 28L))
.ListAsync<Student>());
Assert.AreEqual(27L, list[0].StudentNumber);
}
}
[Test]
public async Task UseIsNullWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.IsNull(Projections.Id()))
.ListAsync<Student>());
Assert.AreEqual(0, list.Count);
}
}
[Test]
public async Task UseIsNotNullWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.IsNotNull(Projections.Id()))
.ListAsync<Student>());
Assert.AreEqual(1, list.Count);
}
}
[Test]
public async Task UseEqPropertyWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.EqProperty(Projections.Id(), Projections.Id()))
.ListAsync<Student>());
Assert.AreEqual(1, list.Count);
}
}
[Test]
public async Task UseGePropertyWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.GeProperty(Projections.Id(), Projections.Id()))
.ListAsync<Student>());
Assert.AreEqual(1, list.Count);
}
}
[Test]
public async Task UseGtPropertyWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.GtProperty(Projections.Id(), Projections.Id()))
.ListAsync<Student>());
Assert.AreEqual(0, list.Count);
}
}
[Test]
public async Task UseLtPropertyWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.LtProperty(Projections.Id(), Projections.Id()))
.ListAsync<Student>());
Assert.AreEqual(0, list.Count);
}
}
[Test]
public async Task UseLePropertyWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.LeProperty(Projections.Id(), Projections.Id()))
.ListAsync<Student>());
Assert.AreEqual(1, list.Count);
}
}
[Test]
public async Task UseNotEqPropertyWithProjectionAsync()
{
using (ISession session = Sfi.OpenSession())
{
IList<Student> list = await (session.CreateCriteria(typeof(Student))
.Add(Expression.NotEqProperty("id", Projections.Id()))
.ListAsync<Student>());
Assert.AreEqual(0, list.Count);
}
}
}
}