Created
February 29, 2012 13:15
-
-
Save rdingwall/1940759 to your computer and use it in GitHub Desktop.
Fast Raven DB sandbox database helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
rem Warning: this batch file deletes ALL Raven DB data and databases, leaving you with a totally empty (but ready to use) Raven DB instance. | |
net stop RavenDB | |
rd /S /Q C:\RavenDB\Server\Data | |
rd /S /Q C:\RavenDB\Server\Tenants | |
net start RavenDB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class When_doing_something | |
{ | |
[TestFixtureSetUp] | |
public void SetUp() | |
{ | |
RavenDB.SpinUpNewDatabase(); | |
using (var session = RavenDB.OpenSession()) | |
{ | |
// insert test data | |
} | |
} | |
[Test] | |
public void It_should_foo() | |
{ | |
using (var session = RavenDB.OpenSession()) | |
{ | |
// run tests | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using Raven.Client; | |
using Raven.Client.Document; | |
namespace YourApp | |
{ | |
/// <summary> | |
/// Test helpers for Raven DB sandbox Database management. | |
/// </summary> | |
public static class RavenDB | |
{ | |
static string databaseName = GenerateNextDatabaseName(); | |
/// <summary> | |
/// Gets the DocumentStore for the current Raven DB Database (Tenant). | |
/// </summary> | |
public static IDocumentStore DocumentStore | |
{ | |
get { return documentStore.Value; } | |
} | |
/// <summary> | |
/// Gets the current Raven DB Database (Tenant) name. | |
/// </summary> | |
public static string DatabaseName | |
{ | |
get { return databaseName; } | |
} | |
/// <summary> | |
/// Opens a new DocumentSession for the current Raven DB Database | |
/// (Tenant). | |
/// </summary> | |
/// <returns>A new DocumentSession.</returns> | |
public static IDocumentSession OpenSession() | |
{ | |
return DocumentStore.OpenSession(); | |
} | |
/// <summary> | |
/// Initializes DocumentStore using an empty Database (Tenant) in | |
/// Raven DB. Any subsequent calls to DocumentStore and OpenSession() | |
/// will use this new Database. | |
/// </summary> | |
public static void SpinUpNewDatabase() | |
{ | |
if (documentStore.IsValueCreated) | |
documentStore.Value.Dispose(); | |
databaseName = GenerateNextDatabaseName(); | |
Console.WriteLine("Using RavenDB database name = {0}", databaseName); | |
documentStore = new Lazy<IDocumentStore>(CreateStore); | |
} | |
private static Lazy<IDocumentStore> documentStore = | |
new Lazy<IDocumentStore>(CreateStore); | |
private static string GenerateNextDatabaseName() | |
{ | |
// Using test assembly name + test timestamp for unique DB name. | |
// For shared Raven DB servers, you could also use the machine | |
// name, current logged in user etc. | |
return String.Format("{0}-{1}", | |
Assembly.GetExecutingAssembly().GetName().Name, | |
DateTime.Now.Ticks); | |
} | |
private static IDocumentStore CreateStore() | |
{ | |
var store = new DocumentStore | |
{ | |
Url = "http://localhost:8080", | |
DefaultDatabase = databaseName | |
}; | |
store.Initialize(); | |
return store; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment