Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 9cce4b1

Browse files
BruceCowan-AIjessemcdowell-AI
authored andcommitted
A few more row version tests.
1 parent ec926e6 commit 9cce4b1

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tests/ServiceStack.OrmLite.Tests/RowVersionTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public class ModelWithRowVersion
1919
public ulong RowVersion { get; set; }
2020
}
2121

22+
public class ModelWithRowVersionBase : ModelWithRowVersion
23+
{
24+
public string MoreData { get; set; }
25+
}
26+
2227
public class ModelWithRowVersionAlias
2328
{
2429
[AutoIncrement]
@@ -80,6 +85,7 @@ public void FixtureSetUp()
8085
using (var dbConn = OpenDbConnection())
8186
{
8287
dbConn.DropAndCreateTable<ModelWithRowVersion>();
88+
dbConn.DropAndCreateTable<ModelWithRowVersionBase>();
8389
}
8490
}
8591

@@ -252,6 +258,20 @@ public void Can_update_with_current_rowversion()
252258
Assert.That(actual.RowVersion, Is.Not.EqualTo(row.RowVersion));
253259
}
254260

261+
[Test]
262+
public void Can_update_with_current_rowversion_base()
263+
{
264+
var rowId = db.Insert(new ModelWithRowVersionBase { Text = "Two", MoreData = "Fred" }, selectIdentity: true);
265+
var row = db.SingleById<ModelWithRowVersionBase>(rowId);
266+
267+
row.Text = "Three";
268+
db.Update(row);
269+
270+
var actual = db.SingleById<ModelWithRowVersionBase>(rowId);
271+
Assert.That(actual.Text, Is.EqualTo("Three"));
272+
Assert.That(actual.RowVersion, Is.Not.EqualTo(row.RowVersion));
273+
}
274+
255275
[Test]
256276
public void Can_update_multiple_with_current_rowversions()
257277
{
@@ -355,6 +375,50 @@ public void Update_with_outdated_rowversion_throws()
355375
Assert.That(actual.Text, Is.Not.EqualTo("Six"));
356376
}
357377

378+
[Test]
379+
public void Update_with_outdated_rowversionbase_throws()
380+
{
381+
var rowId = db.Insert(new ModelWithRowVersionBase { Text = "Five", MoreData = "George" }, selectIdentity: true);
382+
var row = db.SingleById<ModelWithRowVersionBase>(rowId);
383+
TouchRowBase(rowId);
384+
385+
row.Text = "Six";
386+
Assert.Throws<OptimisticConcurrencyException>(() => db.Update(row));
387+
388+
var actual = db.SingleById<ModelWithRowVersionBase>(rowId);
389+
Assert.That(actual.Text, Is.Not.EqualTo("Six"));
390+
}
391+
392+
[Test]
393+
public void Update_with_outdated_rowversion_base_and_explicit_id_check_bypasses_rowversion_check()
394+
{
395+
var rowId = db.Insert(new ModelWithRowVersionBase { Text = "Two", MoreData = "Fred" }, selectIdentity: true);
396+
var row = db.SingleById<ModelWithRowVersionBase>(rowId);
397+
TouchRowBase(rowId);
398+
399+
row.Text = "Six";
400+
db.Update(row, x => x.Id == rowId);
401+
402+
var actual = db.SingleById<ModelWithRowVersionBase>(rowId);
403+
Assert.That(actual.Text, Is.EqualTo("Six"));
404+
Assert.That(actual.RowVersion, Is.Not.EqualTo(row.RowVersion));
405+
}
406+
407+
[Test]
408+
public void Update_with_outdated_rowversion_base_and_explicit_rowversion_check_bypasses_update_with_no_throw()
409+
{
410+
var rowId = db.Insert(new ModelWithRowVersionBase { Text = "Two", MoreData = "Fred" }, selectIdentity: true);
411+
var row = db.SingleById<ModelWithRowVersionBase>(rowId);
412+
TouchRowBase(rowId);
413+
414+
row.Text = "Six";
415+
db.Update(row, x => x.Id == rowId && x.RowVersion == row.RowVersion);
416+
417+
var actual = db.SingleById<ModelWithRowVersionBase>(rowId);
418+
Assert.That(actual.Text, Is.EqualTo("Touched"));
419+
Assert.That(actual.RowVersion, Is.Not.EqualTo(row.RowVersion));
420+
}
421+
358422
[Test]
359423
public void Update_multiple_with_single_outdated_rowversion_throws_and_all_changes_are_rejected()
360424
{
@@ -455,5 +519,12 @@ private void TouchRow(long rowId)
455519
row.Text = "Touched";
456520
db.Update(row);
457521
}
522+
523+
private void TouchRowBase(long rowId)
524+
{
525+
var row = db.SingleById<ModelWithRowVersionBase>(rowId);
526+
row.Text = "Touched";
527+
db.Update(row);
528+
}
458529
}
459530
}

0 commit comments

Comments
 (0)