Skip to content

Commit 1eb50dd

Browse files
NH-4004 - More SqlCe tests fixes.
1 parent 38a0aa7 commit 1eb50dd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+640
-438
lines changed

src/NHibernate.Test/CompositeId/CompositeIdFixture.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using NHibernate.Dialect;
34
using NUnit.Framework;
45

56
namespace NHibernate.Test.CompositeId
@@ -29,6 +30,12 @@ protected override string CacheConcurrencyStrategy
2930
get { return null; }
3031
}
3132

33+
protected override bool AppliesTo(Dialect.Dialect dialect)
34+
{
35+
// Order uses a scalar sub-select formula.
36+
return Dialect.SupportsScalarSubSelects;
37+
}
38+
3239
[Test]
3340
public void CompositeIds()
3441
{

src/NHibernate.Test/Hql/Ast/HqlFixture.cs

Lines changed: 102 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -74,188 +74,95 @@ public void OrderByPropertiesImplicitlySpecifiedInTheSelect()
7474
{
7575
s.CreateQuery("select distinct z from Animal a join a.zoo as z order by z.name").List();
7676
}
77-
}
77+
}
7878

7979
[Test]
8080
public void CaseClauseInSelect()
8181
{
8282
// NH-322
83-
using (ISession s = OpenSession())
84-
using (s.BeginTransaction())
85-
{
86-
s.Save(new Animal {BodyWeight = 12, Description = "Polliwog"});
87-
s.Transaction.Commit();
88-
}
89-
90-
using (ISession s = OpenSession())
83+
using (var s = OpenSession())
9184
{
92-
var l = s.CreateQuery("select a.id, case when a.description = 'Polliwog' then 2 else 0 end from Animal a").List();
85+
var l = s
86+
.CreateQuery("select a.id, case when a.description = 'Polliwog' then 2 else 0 end from Animal a where a.id = :id")
87+
.SetInt64("id", _polliwog.Id)
88+
.List();
9389
var element = (IList)l[0];
9490
Assert.That(element[1], Is.EqualTo(2));
9591

9692
// work with alias
97-
l = s.CreateQuery("select a.id, case when a.description = 'Polliwog' then 2 else 0 end as value from Animal a").List();
93+
l = s
94+
.CreateQuery("select a.id, case when a.description = 'Polliwog' then 2 else 0 end as value from Animal a where a.id = :id")
95+
.SetInt64("id", _polliwog.Id)
96+
.List();
9897
element = (IList)l[0];
9998
Assert.That(element[1], Is.EqualTo(2));
10099
}
101-
102-
using (ISession s = OpenSession())
103-
using (s.BeginTransaction())
104-
{
105-
s.CreateQuery("delete from Animal").ExecuteUpdate();
106-
s.Transaction.Commit();
107-
}
108100
}
109101

110102
[Test]
111103
public void MultipleParametersInCaseStatement()
112104
{
113-
using (ISession s = OpenSession())
114-
using (s.BeginTransaction())
115-
{
116-
s.Save(new Animal { BodyWeight = 12, Description = "Polliwog" });
117-
s.Transaction.Commit();
118-
}
119-
120-
try
121-
{
122-
using (ISession s = OpenSession())
123-
{
124-
var result = s.CreateQuery("select case when 'b' = ? then 2 when 'b' = ? then 1 else 0 end from Animal a")
125-
.SetParameter(0, "a")
126-
.SetParameter(1, "b")
127-
.SetMaxResults(1)
128-
.UniqueResult();
129-
Assert.AreEqual(1, result);
130-
}
131-
}
132-
finally
105+
using (var s = OpenSession())
133106
{
134-
using (ISession s = OpenSession())
135-
using (s.BeginTransaction())
136-
{
137-
s.CreateQuery("delete from Animal").ExecuteUpdate();
138-
s.Transaction.Commit();
139-
}
107+
var result = s.CreateQuery("select case when 'b' = ? then 2 when 'b' = ? then 1 else 0 end from Animal a")
108+
.SetParameter(0, "a")
109+
.SetParameter(1, "b")
110+
.SetMaxResults(1)
111+
.UniqueResult();
112+
Assert.AreEqual(1, result);
140113
}
141114
}
142115

143116
[Test]
144117
public void ParameterInCaseThenClause()
145118
{
146-
using (ISession s = OpenSession())
147-
using (s.BeginTransaction())
148-
{
149-
s.Save(new Animal { BodyWeight = 12, Description = "Polliwog" });
150-
s.Transaction.Commit();
151-
}
152-
153-
try
154-
{
155-
using (ISession s = OpenSession())
156-
{
157-
var result = s.CreateQuery("select case when 2=2 then ? else 0 end from Animal a")
158-
.SetParameter(0, 1)
159-
.UniqueResult();
160-
Assert.AreEqual(1, result);
161-
}
162-
}
163-
finally
119+
using (var s = OpenSession())
164120
{
165-
using (ISession s = OpenSession())
166-
using (s.BeginTransaction())
167-
{
168-
s.CreateQuery("delete from Animal").ExecuteUpdate();
169-
s.Transaction.Commit();
170-
}
121+
var result = s.CreateQuery("select case when 2=2 then ? else 0 end from Animal a")
122+
.SetParameter(0, 1)
123+
.SetMaxResults(1)
124+
.UniqueResult();
125+
Assert.AreEqual(1, result);
171126
}
172127
}
173128

174129
[Test]
175130
public void ParameterInCaseThenAndElseClausesWithCast()
176131
{
177-
using (ISession s = OpenSession())
178-
using (s.BeginTransaction())
179-
{
180-
s.Save(new Animal { BodyWeight = 12, Description = "Polliwog" });
181-
s.Transaction.Commit();
182-
}
183-
184-
try
185-
{
186-
using (ISession s = OpenSession())
187-
{
188-
var result = s.CreateQuery("select case when 2=2 then cast(? as integer) else ? end from Animal a")
189-
.SetParameter(0, 1)
190-
.SetParameter(1, 0)
191-
.UniqueResult();
192-
Assert.AreEqual(1, result);
193-
}
194-
}
195-
finally
132+
using (var s = OpenSession())
196133
{
197-
using (ISession s = OpenSession())
198-
using (s.BeginTransaction())
199-
{
200-
s.CreateQuery("delete from Animal").ExecuteUpdate();
201-
s.Transaction.Commit();
202-
}
134+
var result = s.CreateQuery("select case when 2=2 then cast(? as integer) else ? end from Animal a")
135+
.SetParameter(0, 1)
136+
.SetParameter(1, 0)
137+
.SetMaxResults(1)
138+
.UniqueResult();
139+
Assert.AreEqual(1, result);
203140
}
204141
}
205142

206143
[Test]
207144
public void SubselectAddition()
208145
{
209-
using (ISession s = OpenSession())
210-
using (s.BeginTransaction())
211-
{
212-
s.Save(new Animal { BodyWeight = 12, Description = "Polliwog" });
213-
s.Transaction.Commit();
214-
}
146+
if (!Dialect.SupportsScalarSubSelects)
147+
Assert.Ignore("Dialect does not support scalar sub-select.");
215148

216-
try
217-
{
218-
using (ISession s = OpenSession())
219-
{
220-
var result = s.CreateQuery("select count(a) from Animal a where (select count(a2) from Animal a2) + 1 > 1")
221-
.UniqueResult();
222-
Assert.AreEqual(1, result);
223-
}
224-
}
225-
finally
149+
using (var s = OpenSession())
226150
{
227-
using (ISession s = OpenSession())
228-
using (s.BeginTransaction())
229-
{
230-
s.CreateQuery("delete from Animal").ExecuteUpdate();
231-
s.Transaction.Commit();
232-
}
151+
var result = s.CreateQuery("select count(a) from Animal a where (select count(a2) from Animal a2) + 1 > 1")
152+
.UniqueResult();
153+
Assert.AreEqual(4, result);
233154
}
234155
}
235156

236157
[Test, Ignore("Not fixed yet.")]
237158
public void SumShouldReturnDouble()
238159
{
239160
// NH-1734
240-
using (ISession s = OpenSession())
241-
using (s.BeginTransaction())
242-
{
243-
s.Save(new Human{ IntValue = 11, BodyWeight = 12.5f, Description = "Polliwog" });
244-
s.Transaction.Commit();
245-
}
246-
247-
using (ISession s = OpenSession())
161+
using (var s = OpenSession())
248162
{
249163
var l = s.CreateQuery("select sum(a.intValue * a.bodyWeight) from Animal a group by a.id").List();
250164
Assert.That(l[0], Is.InstanceOf<Double>());
251165
}
252-
253-
using (ISession s = OpenSession())
254-
using (s.BeginTransaction())
255-
{
256-
s.CreateQuery("delete from Animal").ExecuteUpdate();
257-
s.Transaction.Commit();
258-
}
259166
}
260167

261168
[Test]
@@ -290,47 +197,92 @@ public void InvalidJoinOnProperty()
290197
[Test]
291198
public void InsertIntoFromSelect_WithSelectClauseParameters()
292199
{
200+
CheckSupportOfBulkInsertionWithGeneratedId<Animal>();
201+
293202
using (ISession s = OpenSession())
294203
{
295204
using (s.BeginTransaction())
296205
{
297-
// arrange
298-
s.Save(new Animal() {Description = "cat1", BodyWeight = 2.1f});
299-
s.Save(new Animal() {Description = "cat2", BodyWeight = 2.5f});
300-
s.Save(new Animal() {Description = "cat3", BodyWeight = 2.7f});
301-
302-
// act
303206
s.CreateQuery("insert into Animal (description, bodyWeight) select a.description, :weight from Animal a where a.bodyWeight < :weight")
304-
.SetParameter<float>("weight", 5.7f).ExecuteUpdate();
207+
.SetParameter("weight", 5.7f).ExecuteUpdate();
305208

306209
// assert
307-
Assert.AreEqual(3, s.CreateCriteria<Animal>().SetProjection(Projections.RowCount())
308-
.Add(Restrictions.Gt("bodyWeight", 5.5f)).UniqueResult<int>());
309-
310-
s.CreateQuery("delete from Animal").ExecuteUpdate();
210+
Assert.AreEqual(3, s
211+
.CreateCriteria<Animal>()
212+
.SetProjection(Projections.RowCount())
213+
.Add(Restrictions.Gt("bodyWeight", 5.5f))
214+
.Add(Restrictions.Lt("bodyWeight", 6f))
215+
.UniqueResult<int>());
311216
s.Transaction.Commit();
312217
}
313218
}
314219
}
315220

316-
317221
[Test]
318222
public void UnaryMinusBeforeParenthesesHandledCorrectly()
319223
{
320-
using (ISession s = OpenSession())
321-
using (ITransaction txn = s.BeginTransaction())
224+
using (var s = OpenSession())
225+
using (var txn = s.BeginTransaction())
322226
{
323-
s.Save(new Animal {Description = "cat1", BodyWeight = 1});
324-
325227
// NH-2290: Unary minus before parentheses wasn't handled correctly (this query returned 0).
326-
int actual = s.CreateQuery("select -(1+1) from Animal as h")
327-
.List<int>().Single();
228+
var actual = s.CreateQuery("select -(1+1) from Animal as h")
229+
.List<int>().First();
328230
Assert.That(actual, Is.EqualTo(-2));
329231

330232
// This was the workaround, which of course should still work.
331-
int actualWorkaround = s.CreateQuery("select -1*(1+1) from Animal as h")
332-
.List<int>().Single();
233+
var actualWorkaround = s.CreateQuery("select -1*(1+1) from Animal as h")
234+
.List<int>().First();
333235
Assert.That(actualWorkaround, Is.EqualTo(-2));
236+
txn.Commit();
237+
}
238+
}
239+
240+
private void CheckSupportOfBulkInsertionWithGeneratedId<T>()
241+
{
242+
// Make sure the env supports bulk inserts with generated ids...
243+
var persister = Sfi.GetEntityPersister(typeof(T).FullName);
244+
var generator = persister.IdentifierGenerator;
245+
if (!HqlSqlWalker.SupportsIdGenWithBulkInsertion(generator))
246+
{
247+
Assert.Ignore($"Identifier generator {generator.GetType().Name} for entity {typeof(T).FullName} does not support bulk insertions.");
248+
}
249+
}
250+
251+
private Animal _polliwog;
252+
253+
protected override void OnSetUp()
254+
{
255+
using (var s = OpenSession())
256+
using (var txn = s.BeginTransaction())
257+
{
258+
_polliwog = new Animal { BodyWeight = 12, Description = "Polliwog" };
259+
s.Save(_polliwog);
260+
261+
s.Save(new Animal { Description = "cat1", BodyWeight = 2.1f });
262+
s.Save(new Animal { Description = "cat2", BodyWeight = 2.5f });
263+
s.Save(new Animal { Description = "cat3", BodyWeight = 2.7f });
264+
265+
txn.Commit();
266+
}
267+
}
268+
269+
protected override void OnTearDown()
270+
{
271+
if (!Dialect.SupportsTemporaryTables)
272+
{
273+
// Give-up usual cleanup due to TPC: cannot perform multi-table deletes using dialect not supporting temp tables
274+
DropSchema();
275+
CreateSchema();
276+
return;
277+
}
278+
279+
using (var s = OpenSession())
280+
using (var txn = s.BeginTransaction())
281+
{
282+
s.CreateQuery("delete from Animal").ExecuteUpdate();
283+
284+
txn.Commit();
285+
s.Close();
334286
}
335287
}
336288
}

0 commit comments

Comments
 (0)