Skip to content

Commit 754629c

Browse files
committed
Hql/Ast/BulkManipulation.cs: Strengthen some tests to avoid testing the corner case that happens to work.
1 parent 06b9727 commit 754629c

File tree

4 files changed

+70
-36
lines changed

4 files changed

+70
-36
lines changed

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

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ public void InsertWithGeneratedVersionAndId()
228228

229229
s = OpenSession();
230230
t = s.BeginTransaction();
231-
int count = s.CreateQuery("insert into IntegerVersioned ( name ) select name from IntegerVersioned").ExecuteUpdate();
231+
int count =
232+
s.CreateQuery("insert into IntegerVersioned ( name, Data ) select name, Data from IntegerVersioned where id = :id")
233+
.SetInt64("id", entity.Id)
234+
.ExecuteUpdate();
232235
t.Commit();
233236
s.Close();
234237

@@ -277,7 +280,10 @@ public void InsertWithGeneratedTimestampVersion()
277280
s = OpenSession();
278281
t = s.BeginTransaction();
279282
int count =
280-
s.CreateQuery("insert into TimestampVersioned ( name ) select name from TimestampVersioned").ExecuteUpdate();
283+
s.CreateQuery(
284+
"insert into TimestampVersioned ( name, Data ) select name, Data from TimestampVersioned where id = :id")
285+
.SetInt64("id", entity.Id)
286+
.ExecuteUpdate();
281287
t.Commit();
282288
s.Close();
283289

@@ -393,59 +399,81 @@ public void UpdateWithWhereExistsSubquery()
393399
[Test]
394400
public void IncrementCounterVersion()
395401
{
396-
ISession s = OpenSession();
397-
ITransaction t = s.BeginTransaction();
402+
IntegerVersioned entity;
398403

399-
var entity = new IntegerVersioned {Name = "int-vers"};
400-
s.Save(entity);
401-
t.Commit();
402-
s.Close();
404+
using (ISession s = OpenSession())
405+
using (ITransaction t = s.BeginTransaction())
406+
{
407+
entity = new IntegerVersioned {Name = "int-vers", Data = "foo"};
408+
s.Save(entity);
409+
t.Commit();
410+
}
403411

404412
int initialVersion = entity.Version;
405413

406-
s = OpenSession();
407-
t = s.BeginTransaction();
408-
int count = s.CreateQuery("update versioned IntegerVersioned set name = name").ExecuteUpdate();
409-
Assert.That(count, Is.EqualTo(1), "incorrect exec count");
410-
t.Commit();
414+
using (ISession s = OpenSession())
415+
{
416+
using (ITransaction t = s.BeginTransaction())
417+
{
418+
// Note: Update more than one column to showcase NH-3624, which involved losing some columns. /2014-07-26
419+
int count = s.CreateQuery("update versioned IntegerVersioned set name = concat(name, 'upd'), Data = concat(Data, 'upd')")
420+
.ExecuteUpdate();
421+
Assert.That(count, Is.EqualTo(1), "incorrect exec count");
422+
t.Commit();
423+
}
424+
425+
using (ITransaction t = s.BeginTransaction())
426+
{
427+
entity = s.Get<IntegerVersioned>(entity.Id);
428+
s.Delete(entity);
429+
t.Commit();
430+
}
431+
}
411432

412-
t = s.BeginTransaction();
413-
entity = s.Load<IntegerVersioned>(entity.Id);
414433
Assert.That(entity.Version, Is.EqualTo(initialVersion + 1), "version not incremented");
415-
416-
s.Delete(entity);
417-
t.Commit();
418-
s.Close();
434+
Assert.That(entity.Name, Is.EqualTo("int-versupd"));
435+
Assert.That(entity.Data, Is.EqualTo("fooupd"));
419436
}
420437

421438
[Test]
422439
public void IncrementTimestampVersion()
423440
{
424-
ISession s = OpenSession();
425-
ITransaction t = s.BeginTransaction();
441+
TimestampVersioned entity;
426442

427-
var entity = new TimestampVersioned {Name = "ts-vers"};
428-
s.Save(entity);
429-
t.Commit();
430-
s.Close();
443+
using (ISession s = OpenSession())
444+
using(ITransaction t = s.BeginTransaction())
445+
{
446+
entity = new TimestampVersioned { Name = "ts-vers", Data = "foo" };
447+
s.Save(entity);
448+
t.Commit();
449+
}
431450

432451
DateTime initialVersion = entity.Version;
433452

434453
Thread.Sleep(1300);
435454

436-
s = OpenSession();
437-
t = s.BeginTransaction();
438-
int count = s.CreateQuery("update versioned TimestampVersioned set name = name").ExecuteUpdate();
439-
Assert.That(count, Is.EqualTo(1), "incorrect exec count");
440-
t.Commit();
455+
using (ISession s = OpenSession())
456+
{
457+
using (ITransaction t = s.BeginTransaction())
458+
{
459+
// Note: Update more than one column to showcase NH-3624, which involved losing some columns. /2014-07-26
460+
int count = s.CreateQuery("update versioned TimestampVersioned set name = concat(name, 'upd'), Data = concat(Data, 'upd')")
461+
.ExecuteUpdate();
462+
Assert.That(count, Is.EqualTo(1), "incorrect exec count");
463+
t.Commit();
464+
}
465+
466+
using (ITransaction t = s.BeginTransaction())
467+
{
468+
entity = s.Load<TimestampVersioned>(entity.Id);
469+
s.Delete(entity);
470+
t.Commit();
471+
}
472+
}
441473

442-
t = s.BeginTransaction();
443-
entity = s.Load<TimestampVersioned>(entity.Id);
444474
Assert.That(entity.Version, Is.GreaterThan(initialVersion), "version not incremented");
445-
446-
s.Delete(entity);
447-
t.Commit();
448-
s.Close();
475+
Assert.That(entity.Name, Is.EqualTo("ts-versupd"));
476+
Assert.That(entity.Data, Is.EqualTo("fooupd"));
449477
}
450478

451479
[Test]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ public virtual string Name
2323
get { return name; }
2424
set { name = value; }
2525
}
26+
27+
public virtual string Data { get; set; }
2628
}
2729
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ public virtual string Name
2525
get { return name; }
2626
set { name = value; }
2727
}
28+
29+
public virtual string Data { get; set; }
2830
}
2931
}

src/NHibernate.Test/Hql/Ast/Versions.hbm.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</id>
1111
<version name="version" column="vers"/>
1212
<property name="name"/>
13+
<property name="Data" access="property"/>
1314
</class>
1415

1516
<class name="TimestampVersioned">
@@ -18,6 +19,7 @@
1819
</id>
1920
<timestamp name="version" column="vers"/>
2021
<property name="name"/>
22+
<property name="Data" access="property"/>
2123
</class>
2224

2325
</hibernate-mapping>

0 commit comments

Comments
 (0)