Skip to content

Instantly share code, notes, and snippets.

@Danthar
Last active June 15, 2023 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Danthar/31a658f83a9bea8aa2e5cfb4371d249d to your computer and use it in GitHub Desktop.
Save Danthar/31a658f83a9bea8aa2e5cfb4371d249d to your computer and use it in GitHub Desktop.
RavenDB testcase
[TestFixture()]
public class ChangeModeTests : RavenTestDriver
{
public ChangeModeTests()
{
ConfigureServer(new TestServerOptions()
{
});
}
protected override void PreInitialize(IDocumentStore documentStore)
{
documentStore.Conventions.UseOptimisticConcurrency = true;
documentStore.Conventions.SaveEnumsAsIntegers = true;
documentStore.Conventions.SendApplicationIdentifier = true;
}
[Test]
public void TestRevisionRestoreOptimisticConcurrency()
{
const string testId = "orders/2369618";
using (var store = GetDocumentStore())
{
//prepare
using (var session = store.OpenSession())
{
var testorder = new Order();
testorder.Id = testId;
testorder.Header = "Original";
session.Store(testorder);
session.SaveChanges();
}
//prepare
CreateRevision(store, testId, 1);
CreateRevision(store, testId, 2);
CreateRevision(store, testId, 3);
//execute
using (var session = store.OpenSession())
{
var order = session.Load<Order>(testId);
//current order is indeed the one we saved
Assert.AreEqual( "AfterRevision 3", order.Header, "Existing instance is wrong version");
var revisions = session.Advanced.Revisions.GetFor<Order>(order.Id);
if (revisions.Count == 0)
throw new DomainException("Order changemode cannot be cancelled. No revisisions available");
//should be the newest revision
var lastState = revisions.FirstOrDefault();
//simulate a possible change on the existing order before we restore the revision
order.RandomProperty = true;
//current revision is indeed the last one we created
Assert.AreEqual( "AfterRevision 2", lastState.Header, "Last revision is wrong version");
Assert.IsTrue(session.Advanced.UseOptimisticConcurrency, "Optimistic concurrency is disabled?!");
session.Store(lastState, lastState.Id);
session.SaveChanges();
}
//verify
using (var session = store.OpenSession())
{
var order = session.Load<Order>(testId);
//verify we are restored to the last revision
Assert.AreEqual( "AfterRevision 2", order.Header, "Restore to last revision failed");
}
}
}
private static void CreateRevision(IDocumentStore store, string testId, int counter)
{
using (var session = store.OpenSession())
{
var order = session.Load<Order>(testId);
session.Advanced.Revisions.ForceRevisionCreationFor(order);
//simulate change after revision
order.Header = $"AfterRevision {counter}";
session.SaveChanges();
}
}
public class Order
{
public string Id { get; set; }
public string Header { get; set; }
public bool RandomProperty { get; set; }
}
}
@Danthar
Copy link
Author

Danthar commented Jun 15, 2023

RavenDB version used: 5.4.104

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment