Skip to content

Commit 6fde9ca

Browse files
committed
Move tests into separate fixture
1 parent 2b66e16 commit 6fde9ca

File tree

4 files changed

+193
-112
lines changed

4 files changed

+193
-112
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Linq;
13+
using NHibernate.Criterion;
14+
using NUnit.Framework;
15+
16+
namespace NHibernate.Test.Criteria
17+
{
18+
using System.Threading.Tasks;
19+
[TestFixture]
20+
public class ConditionalProjectionTestAsync : TestCase
21+
{
22+
protected override string MappingsAssembly => "NHibernate.Test";
23+
24+
protected override string[] Mappings => new [] {"Criteria.Enrolment.hbm.xml"};
25+
26+
protected override void OnSetUp()
27+
{
28+
using (var session = OpenSession())
29+
using (var transaction = session.BeginTransaction())
30+
{
31+
session.Save(new Student {StudentNumber = 6L, Name = "testa"});
32+
session.Save(new Student {StudentNumber = 5L, Name = "testz"});
33+
session.Save(new Student {StudentNumber = 4L, Name = "test1"});
34+
session.Save(new Student {StudentNumber = 3L, Name = "test2"});
35+
session.Save(new Student {StudentNumber = 2L, Name = "test998"});
36+
session.Save(new Student {StudentNumber = 1L, Name = "test999"});
37+
transaction.Commit();
38+
}
39+
}
40+
41+
protected override void OnTearDown()
42+
{
43+
using (var session = Sfi.OpenSession())
44+
using (var transaction = session.BeginTransaction())
45+
{
46+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
47+
transaction.Commit();
48+
}
49+
}
50+
51+
protected override bool AppliesTo(Dialect.Dialect dialect)
52+
{
53+
return !TestDialect.HasBrokenTypeInferenceOnSelectedParameters;
54+
}
55+
56+
[Test]
57+
public async Task UsingMultiConditionalsAsync()
58+
{
59+
using (var session = OpenSession())
60+
using (session.BeginTransaction())
61+
{
62+
// when Name = "testa" then 1 ...
63+
var orderOfNames = new Tuple<string, string>[]
64+
{
65+
System.Tuple.Create("test1", "1"),
66+
System.Tuple.Create("testz", "2"),
67+
System.Tuple.Create("test2", "3"),
68+
System.Tuple.Create("testa", "4")
69+
};
70+
71+
var criterionProjections =
72+
orderOfNames
73+
.Select(
74+
x => new ConditionalCriterionProjectionPair(
75+
Restrictions.Eq(nameof(Student.Name), x.Item1),
76+
Projections.Constant(x.Item2)))
77+
.ToArray();
78+
79+
// ... else 99
80+
var elseProjection = Projections.Constant("99");
81+
82+
var conditionalsProjection = Projections.Conditionals(criterionProjections, elseProjection);
83+
84+
var order = Order.Asc(conditionalsProjection);
85+
86+
var criteria = session.CreateCriteria(typeof(Student)).AddOrder(order);
87+
88+
var actuals = await (criteria.ListAsync<Student>());
89+
90+
Assert.That(actuals.Count, Is.GreaterThanOrEqualTo(orderOfNames.Length));
91+
for (var i = 0; i < orderOfNames.Length; i++)
92+
{
93+
var expected = orderOfNames[i];
94+
var actual = actuals[i];
95+
96+
Assert.That(actual.Name, Is.EqualTo(expected.Item1));
97+
}
98+
}
99+
}
100+
}
101+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.Linq;
3+
using NHibernate.Criterion;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.Criteria
7+
{
8+
[TestFixture]
9+
public class ConditionalProjectionTest : TestCase
10+
{
11+
protected override string MappingsAssembly => "NHibernate.Test";
12+
13+
protected override string[] Mappings => new [] {"Criteria.Enrolment.hbm.xml"};
14+
15+
protected override void OnSetUp()
16+
{
17+
using (var session = OpenSession())
18+
using (var transaction = session.BeginTransaction())
19+
{
20+
session.Save(new Student {StudentNumber = 6L, Name = "testa"});
21+
session.Save(new Student {StudentNumber = 5L, Name = "testz"});
22+
session.Save(new Student {StudentNumber = 4L, Name = "test1"});
23+
session.Save(new Student {StudentNumber = 3L, Name = "test2"});
24+
session.Save(new Student {StudentNumber = 2L, Name = "test998"});
25+
session.Save(new Student {StudentNumber = 1L, Name = "test999"});
26+
transaction.Commit();
27+
}
28+
}
29+
30+
protected override void OnTearDown()
31+
{
32+
using (var session = Sfi.OpenSession())
33+
using (var transaction = session.BeginTransaction())
34+
{
35+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
36+
transaction.Commit();
37+
}
38+
}
39+
40+
protected override bool AppliesTo(Dialect.Dialect dialect)
41+
{
42+
return !TestDialect.HasBrokenTypeInferenceOnSelectedParameters;
43+
}
44+
45+
[Test]
46+
public void UsingMultiConditionals()
47+
{
48+
using (var session = OpenSession())
49+
using (session.BeginTransaction())
50+
{
51+
// when Name = "testa" then 1 ...
52+
var orderOfNames = new Tuple<string, string>[]
53+
{
54+
System.Tuple.Create("test1", "1"),
55+
System.Tuple.Create("testz", "2"),
56+
System.Tuple.Create("test2", "3"),
57+
System.Tuple.Create("testa", "4")
58+
};
59+
60+
var criterionProjections =
61+
orderOfNames
62+
.Select(
63+
x => new ConditionalCriterionProjectionPair(
64+
Restrictions.Eq(nameof(Student.Name), x.Item1),
65+
Projections.Constant(x.Item2)))
66+
.ToArray();
67+
68+
// ... else 99
69+
var elseProjection = Projections.Constant("99");
70+
71+
var conditionalsProjection = Projections.Conditionals(criterionProjections, elseProjection);
72+
73+
var order = Order.Asc(conditionalsProjection);
74+
75+
var criteria = session.CreateCriteria(typeof(Student)).AddOrder(order);
76+
77+
var actuals = criteria.List<Student>();
78+
79+
Assert.That(actuals.Count, Is.GreaterThanOrEqualTo(orderOfNames.Length));
80+
for (var i = 0; i < orderOfNames.Length; i++)
81+
{
82+
var expected = orderOfNames[i];
83+
var actual = actuals[i];
84+
85+
Assert.That(actual.Name, Is.EqualTo(expected.Item1));
86+
}
87+
}
88+
}
89+
}
90+
}

src/NHibernate.Test/Criteria/ProjectionsTest.cs

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
3-
using System.Linq;
44
using System.Text.RegularExpressions;
55
using NHibernate.Criterion;
66
using NHibernate.Dialect;
@@ -78,7 +78,7 @@ public void UsingSqlFunctions_Concat()
7878
[Test]
7979
public void UsingSqlFunctions_Concat_WithCast()
8080
{
81-
if (Dialect is Oracle8iDialect)
81+
if(Dialect is Oracle8iDialect)
8282
{
8383
Assert.Ignore("Not supported by the active dialect:{0}.", Dialect);
8484
}
@@ -198,84 +198,6 @@ public void UsingConditionals()
198198
}
199199
}
200200

201-
[Test]
202-
public void UsingMultiConditionals()
203-
{
204-
if (TestDialect.HasBrokenTypeInferenceOnSelectedParameters)
205-
Assert.Ignore("Current dialect does not support this test");
206-
207-
var students = new[]
208-
{
209-
new Student() { StudentNumber = 6L, Name = "testa", },
210-
new Student() { StudentNumber = 5L, Name = "testz", },
211-
new Student() { StudentNumber = 4L, Name = "test1", },
212-
new Student() { StudentNumber = 3L, Name = "test2", },
213-
new Student() { StudentNumber = 2L, Name = "test998", },
214-
new Student() { StudentNumber = 1L, Name = "test999", },
215-
};
216-
217-
var expecteds = new[]
218-
{
219-
students[0],
220-
students[1],
221-
students[2],
222-
students[3],
223-
};
224-
225-
// student, sortingindex
226-
var testData = new Tuple<Student, string>[]
227-
{
228-
System.Tuple.Create(expecteds[0], "1"),
229-
System.Tuple.Create(expecteds[1], "2"),
230-
System.Tuple.Create(expecteds[2], "3"),
231-
System.Tuple.Create(expecteds[3], "4"),
232-
};
233-
234-
using (ISession session = this.Sfi.OpenSession())
235-
{
236-
using (ITransaction transaction = session.BeginTransaction())
237-
{
238-
session.Save<Student>(students);
239-
transaction.Commit();
240-
}
241-
242-
using (ITransaction transaction = session.BeginTransaction())
243-
{
244-
// when Name = "testa" then 1 ...
245-
var criterionProjections = testData
246-
.Select(x => new ConditionalCriterionProjectionPair(Expression.Eq(nameof(Student.Name), x.Item1.Name), Projections.Constant(x.Item2)))
247-
.ToArray();
248-
249-
// ... else 99
250-
var elseProjection = Projections.Constant("99");
251-
252-
var conditionalsProjection = Projections.Conditionals(criterionProjections, elseProjection);
253-
254-
var order = Order.Asc(conditionalsProjection);
255-
256-
var criteria = session.CreateCriteria(typeof(Student))
257-
.AddOrder(order);
258-
259-
var actuals = criteria.List<Student>();
260-
261-
Assert.GreaterOrEqual(actuals.Count, expecteds.Length);
262-
for (int i = 0; i < expecteds.Length; i++)
263-
{
264-
var expected = expecteds[i];
265-
var actual = actuals[i];
266-
267-
Assert.AreEqual(expected.Name, actual.Name);
268-
}
269-
}
270-
271-
using (ITransaction transaction = session.BeginTransaction())
272-
{
273-
session.Delete<Student>(students);
274-
transaction.Commit();
275-
}
276-
}
277-
}
278-
279201
[Test]
280202
public void UseInWithProjection()
281203
{

src/NHibernate.Test/Criteria/SessionExtender.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)