Skip to content

Instantly share code, notes, and snippets.

@DavidBoike
Created May 16, 2012 20:50
Show Gist options
  • Save DavidBoike/2713847 to your computer and use it in GitHub Desktop.
Save DavidBoike/2713847 to your computer and use it in GitHub Desktop.
RavenDB Unit Test - Saving changes to document should not alter doc id casing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Client;
using Raven.Client.Embedded;
using Xunit;
using System.Text.RegularExpressions;
namespace RavenCasingTest
{
public class CasingTest
{
[Fact]
public void Saving_changes_to_document_should_not_alter_doc_id_casing()
{
using (IDocumentStore store = new EmbeddableDocumentStore { RunInMemory = true })
{
store.Initialize();
TestModel tm1 = new TestModel
{
Name = "Test #1"
};
Assert.Null(tm1.Id);
using (IDocumentSession session = store.OpenSession())
{
session.Store(tm1);
AssertCasing(tm1.Id, "After initial store, DocId should be mixed case due to conventions.");
session.SaveChanges();
}
AssertCasing(tm1.Id, "DocId should be unchnaged after SaveChanges is called.");
string lastPart = tm1.Id.Split('/')[1];
Assert.True(Regex.IsMatch(lastPart, @"^\d+$"), "The segment of the Id after the slash should match an integer pattern.");
int intId = int.Parse(lastPart);
using (IDocumentSession session = store.OpenSession())
{
var tm2 = session.Load<TestModel>(intId);
AssertCasing(tm2.Id, "After loading the doc from store, the casing should still be mixed.");
tm2.DeletedUtc = DateTime.UtcNow;
AssertCasing(tm2.Id, "Setting the DeletedUtc property should have no effect on the casing.");
session.SaveChanges();
AssertCasing(tm2.Id, "Saving changes on the Set DeletedUtc operation should have no effect on the casing.");
}
using (IDocumentSession session = store.OpenSession())
{
var tm3 = session.Load<TestModel>(intId);
AssertCasing(tm3.Id, "When reloading doc after updating the DeletedUtc property, the casing should still be mixed.");
}
}
}
private void AssertCasing(string docId, string errorMsg)
{
Assert.True(docId != null && docId.StartsWith("TestModels/", StringComparison.InvariantCulture), errorMsg);
}
public class TestModel
{
public string Id { get; set; }
public string Name { get; set; }
public DateTime DeletedUtc { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment