Skip to content

Instantly share code, notes, and snippets.

@PureKrome
Created February 4, 2015 01:28
Show Gist options
  • Save PureKrome/b433a653d483c6a4699f to your computer and use it in GitHub Desktop.
Save PureKrome/b433a653d483c6a4699f to your computer and use it in GitHub Desktop.
Ceremony Comparison between two ways of writing RavenDb tests.
using System.Collections;
using System.Linq;
using System.Threading.Tasks;
using FizzWare.NBuilder;
using Raven.Client.Document;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Shouldly;
using WorldDomination.Raven.Tests.Helpers;
using Xunit;
namespace ConsoleApplication4
{
public class ComparisonFacts
{
public class OldWayFacts
{
[Fact]
public async Task OldWayWithLotsOfCeremony()
{
// Arrange.
var conventions = new DocumentConvention
{
DefaultQueryingConsistency = ConsistencyOptions.AlwaysWaitForNonStaleResultsAsOfLastWrite
};
var documentStore = new EmbeddableDocumentStore
{
RunInMemory = true,
Conventions = conventions
}.Initialize();
// Define any indexes (if required).
new SampleData_Index().Execute(documentStore);
// Seed the data.
using (var session = documentStore.OpenAsyncSession())
{
var fakeData = Builder<SampleData>.CreateListOfSize(1000).Build();
foreach (var item in fakeData)
{
await session.StoreAsync(item);
}
await session.SaveChangesAsync();
}
// Now setup the rest of your test stuff, like mocks and the actual 'thing' you're going to test.
var foo = new SampleData { Name = "Hi Franj0!" };
// Act.
using (var session = documentStore.OpenAsyncSession())
{
session.Delete("sampleDatas/1");
await session.StoreAsync(foo);
await session.SaveChangesAsync();
}
// Assert.
using (var session = documentStore.OpenAsyncSession())
{
var missingSampleData = await session.LoadAsync<SampleData>("sampleDatas/1");
missingSampleData.ShouldBe(null);
}
using (var session = documentStore.OpenAsyncSession())
{
var existingSampleData = await session.LoadAsync<SampleData>(foo.Id);
existingSampleData.Id.ShouldBe(foo.Id);
}
}
}
public class MyFancyWayFacts : RavenDbTestBase
{
[Fact]
public async Task MyFancyWayWithLittleCeremony()
{
// Arrange.
DataToBeSeeded = new IEnumerable[] {Builder<SampleData>.CreateListOfSize(1000).Build()};
IndexesToExecute = new[] {typeof (SampleData_Index)};
// Now setup the rest of your test stuff, like mocks and the actual 'thing' you're going to test.
var foo = new SampleData {Name = "Hi Franj0!"};
// Act.
AsyncDocumentSession.Delete("sampleDatas/1");
await AsyncDocumentSession.StoreAsync(foo);
await AsyncDocumentSession.SaveChangesAsync();
// Assert.
var missingSampleData = await AsyncDocumentSessions("pewpew").LoadAsync<SampleData>("sampleDatas/1");
missingSampleData .ShouldBe(null);
// I can reuse the existing session ('pewpew') -or- create a new one (just give it a new name). Here, i'm re-using.
var existingSampleData = await AsyncDocumentSessions("pewpew").LoadAsync<SampleData>(foo.Id);
existingSampleData.Id.ShouldBe(foo.Id);
}
}
#region Setup crap
public class SampleData
{
public string Id { get; set; }
public string Name { get; set; }
}
public class SampleData_Index : AbstractIndexCreationTask<SampleData>
{
public SampleData_Index()
{
Map = docs => from doc in docs
select new
{
doc.Name
};
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment