11using System . Collections ;
22using NHibernate . Criterion ;
3- using NHibernate . Dialect ;
43using NUnit . Framework ;
54
65namespace NHibernate . Test . NHSpecificTest . NH1280
@@ -13,233 +12,229 @@ public override string BugNumber
1312 get { return "NH1280" ; }
1413 }
1514
16- protected override void OnTearDown ( )
15+ protected override void OnSetUp ( )
1716 {
18- base . OnTearDown ( ) ;
19- using ( ISession session = OpenSession ( ) )
17+ using ( ISession s = OpenSession ( ) )
18+ using ( ITransaction tx = s . BeginTransaction ( ) )
2019 {
21- using ( ITransaction tx = session . BeginTransaction ( ) )
22- {
23- session . Delete ( "from Person" ) ;
24- tx . Commit ( ) ;
25- }
20+ Person e1 = new Person ( "Joe" , 10 , 9 ) ;
21+ Person e2 = new Person ( "Sally" , 20 , 8 ) ;
22+ Person e3 = new Person ( "Tim" , 20 , 7 ) ; //20
23+ Person e4 = new Person ( "Fred" , 40 , 40 ) ;
24+ Person e5 = new Person ( "Fred" , 50 , 50 ) ;
25+ s . Save ( e1 ) ;
26+ s . Save ( e2 ) ;
27+ s . Save ( e3 ) ;
28+ s . Save ( e4 ) ;
29+ s . Save ( e5 ) ;
30+ tx . Commit ( ) ;
2631 }
2732 }
2833
29- protected override void OnSetUp ( )
34+ protected override void OnTearDown ( )
3035 {
31- using ( ISession s = OpenSession ( ) )
36+ using ( ISession session = OpenSession ( ) )
37+ using ( ITransaction tx = session . BeginTransaction ( ) )
3238 {
33- using ( ITransaction tx = s . BeginTransaction ( ) )
34- {
35- Person e1 = new Person ( "Joe" , 10 , 9 ) ;
36- Person e2 = new Person ( "Sally" , 20 , 8 ) ;
37- Person e3 = new Person ( "Tim" , 20 , 7 ) ; //20
38- Person e4 = new Person ( "Fred" , 40 , 40 ) ;
39- Person e5 = new Person ( "Fred" , 50 , 50 ) ;
40- s . Save ( e1 ) ;
41- s . Save ( e2 ) ;
42- s . Save ( e3 ) ;
43- s . Save ( e4 ) ;
44- s . Save ( e5 ) ;
45- tx . Commit ( ) ;
46- }
39+ session . Delete ( "from Person" ) ;
40+ tx . Commit ( ) ;
4741 }
42+ base . OnTearDown ( ) ;
4843 }
4944
50-
5145 [ Test ]
5246 public void HavingUsingSqlFunctions_Concat ( )
5347 {
54- using ( ISession s = OpenSession ( ) )
48+ using ( ISession s = OpenSession ( ) )
49+ using ( ITransaction tx = s . BeginTransaction ( ) )
5550 {
56- using ( ITransaction tx = s . BeginTransaction ( ) )
57- {
58- IList list = s . CreateCriteria ( typeof ( Person ) )
59- . SetProjection ( Projections . ProjectionList ( ) . Add (
60- new SqlFunctionProjection ( "concat" ,
61- NHibernateUtil . String ,
62- Projections . GroupProperty ( "Name" ) ,
63- new ConstantProjection ( " " ) ,
64- Projections . GroupProperty ( "Name" )
65- ) )
66- . Add ( Projections . Conditional ( Restrictions . IsNotNull ( Projections . GroupProperty ( "Id" ) ) ,
67- new ConstantProjection ( "yes" ) , new ConstantProjection ( "No" ) ) )
68- )
69- . Add ( Restrictions . Eq ( Projections . GroupProperty ( "Name" ) , "Fred" ) )
70- . Add ( Restrictions . Gt ( "Id" , 2 ) ) . List ( ) ;
71- Assert . AreEqual ( 2 , list . Count ) ;
72- Assert . AreEqual ( "Fred Fred" , ( ( object [ ] ) list [ 0 ] ) [ 0 ] ) ;
73- tx . Commit ( ) ;
74- }
51+ IList list = s . CreateCriteria ( typeof ( Person ) )
52+ . SetProjection (
53+ Projections . ProjectionList ( )
54+ . Add (
55+ new SqlFunctionProjection (
56+ "concat" ,
57+ NHibernateUtil . String ,
58+ Projections . GroupProperty ( "Name" ) ,
59+ new ConstantProjection ( " " ) ,
60+ Projections . GroupProperty ( "Name" ) ) )
61+ . Add (
62+ Projections . Conditional (
63+ Restrictions . IsNotNull ( Projections . GroupProperty ( "Id" ) ) , new ConstantProjection ( "yes" ) , new ConstantProjection ( "No" ) ) ) )
64+ . Add ( Restrictions . Eq ( Projections . GroupProperty ( "Name" ) , "Fred" ) )
65+ . Add ( Restrictions . Gt ( "Id" , 2 ) )
66+ . List ( ) ;
67+
68+ Assert . AreEqual ( 2 , list . Count ) ;
69+ Assert . AreEqual ( "Fred Fred" , ( ( object [ ] ) list [ 0 ] ) [ 0 ] ) ;
70+ tx . Commit ( ) ;
7571 }
7672 }
7773
7874 [ Test ]
7975 public void HavingOnGtCount ( )
8076 {
81- using ( ISession s = OpenSession ( ) )
77+ using ( ISession s = OpenSession ( ) )
78+ using ( ITransaction tx = s . BeginTransaction ( ) )
8279 {
83- using ( ITransaction tx = s . BeginTransaction ( ) )
84- {
85- //Find the iq that two people share
86- int iq = s . CreateCriteria ( typeof ( Person ) )
87- . SetProjection ( Projections . GroupProperty ( "IQ" ) )
88- . Add ( Restrictions . Gt ( Projections . Count ( "IQ" ) , 1 ) ) . UniqueResult < int > ( ) ;
89- Assert . AreEqual ( 20 , iq ) ;
90- tx . Commit ( ) ;
91- }
80+ //Find the iq that two people share
81+ int iq = s . CreateCriteria ( typeof ( Person ) )
82+ . SetProjection ( Projections . GroupProperty ( "IQ" ) )
83+ . Add ( Restrictions . Gt ( Projections . Count ( "IQ" ) , 1 ) )
84+ . UniqueResult < int > ( ) ;
85+
86+ Assert . AreEqual ( 20 , iq ) ;
87+ tx . Commit ( ) ;
9288 }
9389 }
9490
9591 [ Test ]
9692 public void HavingOnLtAverage ( )
9793 {
98- using ( ISession s = OpenSession ( ) )
94+ using ( ISession s = OpenSession ( ) )
95+ using ( ITransaction tx = s . BeginTransaction ( ) )
9996 {
100- using ( ITransaction tx = s . BeginTransaction ( ) )
101- {
102- //Find the iq that two people share
103- string name = s . CreateCriteria ( typeof ( Person ) )
104- . SetProjection ( Projections . GroupProperty ( "Name" ) )
105- . Add ( Restrictions . Lt ( Projections . Avg ( "IQ" ) , 20 ) ) . UniqueResult < string > ( ) ;
106- Assert . AreEqual ( "Joe" , name ) ;
107- tx . Commit ( ) ;
108- }
97+ //Find the iq that two people share
98+ string name = s . CreateCriteria ( typeof ( Person ) )
99+ . SetProjection ( Projections . GroupProperty ( "Name" ) )
100+ . Add ( Restrictions . Lt ( Projections . Avg ( "IQ" ) , 20 ) )
101+ . UniqueResult < string > ( ) ;
102+
103+ Assert . AreEqual ( "Joe" , name ) ;
104+ tx . Commit ( ) ;
109105 }
110106 }
111107
112108 [ Test ]
113109 public void HavingOnEqProjection ( )
114110 {
115- using ( ISession s = OpenSession ( ) )
111+ using ( ISession s = OpenSession ( ) )
112+ using ( ITransaction tx = s . BeginTransaction ( ) )
116113 {
117- using ( ITransaction tx = s . BeginTransaction ( ) )
118- {
119- //SELECT this_.Name as y0_ FROM Person this_ GROUP BY this_.Name HAVING this_.Name = @p0; @p0 = 'Joe'
120- string name = s . CreateCriteria ( typeof ( Person ) )
121- . SetProjection ( Projections . GroupProperty ( "Name" ) )
122- . Add ( Restrictions . Eq ( Projections . GroupProperty ( "Name" ) , "Joe" ) ) . UniqueResult < string > ( ) ;
123- Assert . AreEqual ( "Joe" , name ) ;
124- tx . Commit ( ) ;
125- }
114+ //SELECT this_.Name as y0_ FROM Person this_ GROUP BY this_.Name HAVING this_.Name = @p0; @p0 = 'Joe'
115+ string name = s . CreateCriteria ( typeof ( Person ) )
116+ . SetProjection ( Projections . GroupProperty ( "Name" ) )
117+ . Add ( Restrictions . Eq ( Projections . GroupProperty ( "Name" ) , "Joe" ) )
118+ . UniqueResult < string > ( ) ;
119+
120+ Assert . AreEqual ( "Joe" , name ) ;
121+ tx . Commit ( ) ;
126122 }
127123 }
128124
129125 [ Test ]
130126 public void NonHavingOnEqProperty ( )
131127 {
132- using ( ISession s = OpenSession ( ) )
128+ using ( ISession s = OpenSession ( ) )
129+ using ( ITransaction tx = s . BeginTransaction ( ) )
133130 {
134- using ( ITransaction tx = s . BeginTransaction ( ) )
135- {
136- string name = s . CreateCriteria ( typeof ( Person ) )
137- . SetProjection ( Projections . GroupProperty ( "Name" ) )
138- . Add ( Restrictions . EqProperty ( "IQ" , "ShoeSize" ) ) . UniqueResult < string > ( ) ;
139- Assert . AreEqual ( "Fred" , name ) ;
140- tx . Commit ( ) ;
141- }
131+ string name = s . CreateCriteria ( typeof ( Person ) )
132+ . SetProjection ( Projections . GroupProperty ( "Name" ) )
133+ . Add ( Restrictions . EqProperty ( "IQ" , "ShoeSize" ) )
134+ . UniqueResult < string > ( ) ;
135+
136+ Assert . AreEqual ( "Fred" , name ) ;
137+ tx . Commit ( ) ;
142138 }
143139 }
144140
145141 [ Test ]
146142 public void NotExpressionShouldNotAddCriteriaTwice ( )
147143 {
148- using ( ISession s = OpenSession ( ) )
144+ using ( ISession s = OpenSession ( ) )
145+ using ( ITransaction tx = s . BeginTransaction ( ) )
149146 {
150- using ( ITransaction tx = s . BeginTransaction ( ) )
151- {
152- IList list = s . CreateCriteria ( typeof ( Person ) )
153- . Add ( Restrictions . Not ( Restrictions . Eq ( Projections . Property ( "IQ" ) , 40 ) ) )
154- . Add ( Restrictions . Eq ( Projections . Property ( "Name" ) , "Fred" ) )
155- . List ( ) ;
156-
157- Assert . AreEqual ( 1 , list . Count ) ;
158- Assert . AreEqual ( "Fred" , ( ( Person ) list [ 0 ] ) . Name ) ;
159- tx . Commit ( ) ;
160- }
147+ IList list = s . CreateCriteria ( typeof ( Person ) )
148+ . Add ( Restrictions . Not ( Restrictions . Eq ( Projections . Property ( "IQ" ) , 40 ) ) )
149+ . Add ( Restrictions . Eq ( Projections . Property ( "Name" ) , "Fred" ) )
150+ . List ( ) ;
151+
152+ Assert . AreEqual ( 1 , list . Count ) ;
153+ Assert . AreEqual ( "Fred" , ( ( Person ) list [ 0 ] ) . Name ) ;
154+ tx . Commit ( ) ;
161155 }
162156 }
163157
164158 [ Test ]
165159 public void MultipleSubqueriesShouldStayInOrder ( )
166160 {
167- using ( ISession s = OpenSession ( ) )
161+ using ( ISession s = OpenSession ( ) )
162+ using ( ITransaction tx = s . BeginTransaction ( ) )
168163 {
169- using ( ITransaction tx = s . BeginTransaction ( ) )
170- {
171- DetachedCriteria dc1 = DetachedCriteria . For ( typeof ( Person ) )
172- . Add ( Property . ForName ( "IQ" ) . Eq ( 10 ) )
173- . SetProjection ( Property . ForName ( "Name" ) ) ;
174-
175- DetachedCriteria dc2 = DetachedCriteria . For ( typeof ( Person ) )
176- . Add ( Property . ForName ( "ShoeSize" ) . Eq ( 7 ) )
177- . SetProjection ( Projections . Property ( "Name" ) ) ;
178-
179- IList list = s . CreateCriteria ( typeof ( Person ) , "p" )
180- . Add ( Subqueries . PropertyEq ( "Name" , dc1 ) )
181- . Add ( Restrictions . Not ( Subqueries . Eq ( "Sally" , dc2 ) ) )
182- . List ( ) ;
183-
184- Assert . AreEqual ( 1 , list . Count ) ;
185- Assert . AreEqual ( "Joe" , ( ( Person ) list [ 0 ] ) . Name ) ;
186- tx . Commit ( ) ;
187- }
164+ DetachedCriteria dc1 = DetachedCriteria . For ( typeof ( Person ) )
165+ . Add ( Property . ForName ( "IQ" ) . Eq ( 10 ) )
166+ . SetProjection ( Property . ForName ( "Name" ) ) ;
167+
168+ DetachedCriteria dc2 = DetachedCriteria . For ( typeof ( Person ) )
169+ . Add ( Property . ForName ( "ShoeSize" ) . Eq ( 7 ) )
170+ . SetProjection ( Projections . Property ( "Name" ) ) ;
171+
172+ IList list = s . CreateCriteria ( typeof ( Person ) , "p" )
173+ . Add ( Subqueries . PropertyEq ( "Name" , dc1 ) )
174+ . Add ( Restrictions . Not ( Subqueries . Eq ( "Sally" , dc2 ) ) )
175+ . List ( ) ;
176+
177+ Assert . AreEqual ( 1 , list . Count ) ;
178+ Assert . AreEqual ( "Joe" , ( ( Person ) list [ 0 ] ) . Name ) ;
179+ tx . Commit ( ) ;
188180 }
189-
190181 }
191182
192-
193183 [ Test ]
194184 public void NestedSubqueriesShouldStayInOrder ( )
195185 {
196- using ( ISession s = OpenSession ( ) )
186+ using ( ISession s = OpenSession ( ) )
187+ using ( ITransaction tx = s . BeginTransaction ( ) )
197188 {
198- using ( ITransaction tx = s . BeginTransaction ( ) )
199- {
200- DetachedCriteria dc1 = DetachedCriteria . For ( typeof ( Person ) )
201- . Add ( Property . ForName ( "IQ" ) . Eq ( 40 ) )
202- . SetProjection ( Property . ForName ( "IQ" ) ) ;
203-
204- DetachedCriteria dc2 = DetachedCriteria . For ( typeof ( Person ) )
205- . Add ( Subqueries . PropertyEq ( "ShoeSize" , dc1 ) )
206- . SetProjection (
207- new SqlFunctionProjection ( "concat" ,
208- NHibernateUtil . String ,
209- Projections . GroupProperty ( "Name" ) ,
210- new ConstantProjection ( " " ) ,
211- Projections . GroupProperty ( "Name" )
212- ) ) ;
213-
214- IList list = s . CreateCriteria ( typeof ( Person ) )
215- . Add ( Subqueries . Eq ( "Fred Fred" , dc2 ) )
216- . List ( ) ;
217-
218- Assert . AreEqual ( 5 , list . Count ) ; //yeah, it returns all five results. The key is that it didn't crash
219- tx . Commit ( ) ;
220- }
189+ DetachedCriteria dc1 = DetachedCriteria . For ( typeof ( Person ) )
190+ . Add ( Property . ForName ( "IQ" ) . Eq ( 40 ) )
191+ . SetProjection ( Property . ForName ( "IQ" ) ) ;
192+
193+ DetachedCriteria dc2 = DetachedCriteria . For ( typeof ( Person ) )
194+ . Add ( Subqueries . PropertyEq ( "ShoeSize" , dc1 ) )
195+ . SetProjection (
196+ new SqlFunctionProjection (
197+ "concat" ,
198+ NHibernateUtil . String ,
199+ Projections . GroupProperty ( "Name" ) ,
200+ new ConstantProjection ( " " ) ,
201+ Projections . GroupProperty ( "Name" ) ) ) ;
202+
203+ IList list = s . CreateCriteria ( typeof ( Person ) )
204+ . Add ( Subqueries . Eq ( "Fred Fred" , dc2 ) )
205+ . List ( ) ;
206+
207+ Assert . AreEqual ( 5 , list . Count ) ; //yeah, it returns all five results. The key is that it didn't crash
208+ tx . Commit ( ) ;
221209 }
222210 }
223211
224212 [ Test ]
225213 public void SubstringShouldUseAllParameters ( )
226214 {
227215 using ( ISession s = OpenSession ( ) )
216+ using ( ITransaction tx = s . BeginTransaction ( ) )
228217 {
229- using ( ITransaction tx = s . BeginTransaction ( ) )
230- {
231- IList list = s . CreateCriteria ( typeof ( Person ) )
232- . SetProjection ( new SqlFunctionProjection ( "LEFT" , NHibernateUtil . String ,
233- Projections . Property ( "Name" ) ,
234- new ConstantProjection ( 3 ) ) )
235- . Add ( Restrictions . Eq ( new SqlFunctionProjection ( "substring" ,
236- NHibernateUtil . String , Projections . Property ( "Name" ) ,
237- new ConstantProjection ( 1 )
238- , new ConstantProjection ( 2 ) ) , "Fr" ) ) . List ( ) ;
239- Assert . AreEqual ( 2 , list . Count ) ;
240- Assert . AreEqual ( "Fre" , list [ 0 ] ) ;
241- tx . Commit ( ) ;
242- }
218+ IList list = s . CreateCriteria ( typeof ( Person ) )
219+ . SetProjection (
220+ new SqlFunctionProjection (
221+ "LEFT" ,
222+ NHibernateUtil . String ,
223+ Projections . Property ( "Name" ) ,
224+ new ConstantProjection ( 3 ) ) )
225+ . Add ( Restrictions . Eq (
226+ new SqlFunctionProjection (
227+ "substring" ,
228+ NHibernateUtil . String ,
229+ Projections . Property ( "Name" ) ,
230+ new ConstantProjection ( 1 ) ,
231+ new ConstantProjection ( 2 ) ) ,
232+ "Fr" ) )
233+ . List ( ) ;
234+
235+ Assert . AreEqual ( 2 , list . Count ) ;
236+ Assert . AreEqual ( "Fre" , list [ 0 ] ) ;
237+ tx . Commit ( ) ;
243238 }
244239 }
245240 }
0 commit comments