Skip to content

Commit 366ae04

Browse files
committed
Split QueryOver integration fixture.
1 parent 84d36bf commit 366ae04

File tree

6 files changed

+557
-448
lines changed

6 files changed

+557
-448
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using System;
2+
using System.Collections;
3+
using NHibernate.Criterion;
4+
using NUnit.Framework;
5+
using SharpTestsEx;
6+
7+
namespace NHibernate.Test.Criteria.Lambda
8+
{
9+
[TestFixture]
10+
public class FunctionsIntegrationFixture : TestCase
11+
{
12+
protected override string MappingsAssembly
13+
{
14+
get { return "NHibernate.Test"; }
15+
}
16+
17+
protected override IList Mappings
18+
{
19+
get { return new[] { "Criteria.Lambda.Mappings.hbm.xml" }; }
20+
}
21+
22+
protected override void OnTearDown()
23+
{
24+
using (var s = OpenSession())
25+
using (var t = s.BeginTransaction())
26+
{
27+
s.Delete("from Person");
28+
t.Commit();
29+
}
30+
}
31+
32+
protected override void OnSetUp()
33+
{
34+
using (var s = OpenSession())
35+
using (var t = s.BeginTransaction())
36+
{
37+
s.Save(new Person { Name = "p2", BirthDate = new DateTime(2008, 07, 07) });
38+
s.Save(new Person { Name = "p1", BirthDate = new DateTime(2009, 08, 07), Age = 90 });
39+
s.Save(new Person { Name = "pP3", BirthDate = new DateTime(2007, 06, 05) });
40+
41+
t.Commit();
42+
}
43+
}
44+
45+
[Test]
46+
public void YearPartEqual()
47+
{
48+
using (var s = OpenSession())
49+
using (s.BeginTransaction())
50+
{
51+
var persons = s.QueryOver<Person>()
52+
.Where(p => p.BirthDate.YearPart() == 2008)
53+
.List();
54+
55+
persons.Count.Should().Be(1);
56+
persons[0].Name.Should().Be("p2");
57+
}
58+
}
59+
60+
[Test]
61+
public void YearPartIsIn()
62+
{
63+
using (var s = OpenSession())
64+
using (s.BeginTransaction())
65+
{
66+
var persons = s.QueryOver<Person>()
67+
.Where(p => p.BirthDate.YearPart().IsIn(new[] { 2008, 2009 }))
68+
.OrderBy(p => p.Name).Asc
69+
.List();
70+
71+
persons.Count.Should().Be(2);
72+
persons[0].Name.Should().Be("p1");
73+
persons[1].Name.Should().Be("p2");
74+
}
75+
}
76+
77+
[Test]
78+
public void YearPartSingleOrDefault()
79+
{
80+
using (var s = OpenSession())
81+
using (s.BeginTransaction())
82+
{
83+
var yearOfBirth = s.QueryOver<Person>()
84+
.Where(p => p.Name == "p2")
85+
.Select(p => p.BirthDate.YearPart())
86+
.SingleOrDefault<object>();
87+
88+
yearOfBirth.GetType().Should().Be(typeof (int));
89+
yearOfBirth.Should().Be(2008);
90+
}
91+
}
92+
93+
[Test]
94+
public void SelectAvg()
95+
{
96+
using (var s = OpenSession())
97+
using (s.BeginTransaction())
98+
{
99+
var avgYear = s.QueryOver<Person>()
100+
.SelectList(list => list.SelectAvg(p => p.BirthDate.YearPart()))
101+
.SingleOrDefault<object>();
102+
103+
avgYear.GetType().Should().Be(typeof (double));
104+
string.Format("{0:0}", avgYear).Should().Be("2008");
105+
}
106+
}
107+
108+
[Test]
109+
public void SqrtSingleOrDefault()
110+
{
111+
using (var s = OpenSession())
112+
using (s.BeginTransaction())
113+
{
114+
var sqrtOfAge = s.QueryOver<Person>()
115+
.Where(p => p.Name == "p1")
116+
.Select(p => p.Age.Sqrt())
117+
.SingleOrDefault<object>();
118+
119+
sqrtOfAge.Should().Be.InstanceOf<double>();
120+
string.Format("{0:0.00}", sqrtOfAge).Should().Be((9.49).ToString());
121+
}
122+
}
123+
124+
[Test]
125+
public void FunctionsToLowerToUpper()
126+
{
127+
using (var s = OpenSession())
128+
using (s.BeginTransaction())
129+
{
130+
var names = s.QueryOver<Person>()
131+
.Where(p => p.Name == "pP3")
132+
.Select(p => p.Name.Lower(), p => p.Name.Upper())
133+
.SingleOrDefault<object[]>();
134+
135+
names[0].Should().Be("pp3");
136+
names[1].Should().Be("PP3");
137+
}
138+
}
139+
140+
[Test]
141+
public void Concat()
142+
{
143+
using (var s = OpenSession())
144+
using (s.BeginTransaction())
145+
{
146+
var name = s.QueryOver<Person>()
147+
.Where(p => p.Name == "p1")
148+
.Select(p => Projections.Concat(p.Name, ", ", p.Name))
149+
.SingleOrDefault<string>();
150+
151+
name.Should().Be("p1, p1");
152+
}
153+
}
154+
155+
[Test]
156+
public void MonthPartEqualsDayPart()
157+
{
158+
using (var s = OpenSession())
159+
using (s.BeginTransaction())
160+
{
161+
var persons = s.QueryOver<Person>()
162+
.Where(p => p.BirthDate.MonthPart() == p.BirthDate.DayPart())
163+
.List();
164+
165+
persons.Count.Should().Be(1);
166+
persons[0].Name.Should().Be("p2");
167+
}
168+
}
169+
170+
[Test]
171+
public void Order()
172+
{
173+
using (var s = OpenSession())
174+
using (s.BeginTransaction())
175+
{
176+
var persons = s.QueryOver<Person>()
177+
.OrderBy(p => p.BirthDate.YearPart()).Desc
178+
.List();
179+
180+
persons.Count.Should().Be(3);
181+
persons[0].Name.Should().Be("p1");
182+
persons[1].Name.Should().Be("p2");
183+
persons[2].Name.Should().Be("pP3");
184+
}
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)