public
Last active

Using RavenDB Embadable Document Store with Machine.Fakes

  • Download Gist
CurrentSiteModelService.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
using IDM.CMS3.Service.Cache;
using IDM.CMS3.Service.Database.RavenDB.Indexes;
using IDM.CMS3.Service.Models;
using IDM.CMS3.Service.ViewModels;
using Ninject;
using Raven.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IDM.CMS3.Service.Services
{
public interface ICurrentSiteModelService
{
RedirectToResult SetSiteModel(string path, Uri url);
}
 
public class CurrentSiteModelService : ICurrentSiteModelService
{
readonly IDocumentSession _documentSession;
readonly ICookieService _cookieService;
 
public CurrentSiteModelService(
IDocumentSession documentSession,
ICookieService cookieService)
{
_documentSession = documentSession;
_cookieService = cookieService;
}
 
 
// cruft removed here
 
// load site in dev mode based on cookie.
internal SiteViewModel DetermineSiteModelForDevEnvironment()
{
var cookie = _cookieService.GetCookie(".CMS3DevSite");
if (cookie != null && !String.IsNullOrEmpty(cookie.Value))
{
SiteViewModel site = _documentSession.
Query<SiteViewModel, SiteViewIndex>()
.Where(s => s.Id == cookie.Value)
.AsProjection<SiteViewModel>()
.FirstOrDefault();
 
if (site != null)
{
return site;
}
}
 
return null;
}
}
}
CurrentSiteModelServiceSpecs.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
using IDM.CMS3.Service.App_Start;
using IDM.CMS3.Service.Database.RavenDB.Indexes;
using IDM.CMS3.Service.Models;
using IDM.CMS3.Service.Services;
using IDM.CMS3.Service.ViewModels;
using Machine.Fakes;
using Machine.Specifications;
using Raven.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IDM.CMS3.Service.Tests.Services
{
public class CurrentSiteModelServiceSpecs : WithSubject<CurrentSiteModelService>
{
Establish that = () =>
{
// use the raven in memory runner since
// we are using lots of raven magic in this service
With(new RavenInMemorySlowRunner());
};
}
 
/// <summary>
/// Determine Site Model for Dev
/// </summary>
public class When_Determining_SiteModel_for_dev : CurrentSiteModelServiceSpecs
{
public static SiteViewModel _siteViewModelResult;
public static IHttpCookie _cookie;
 
Because of = () =>
_siteViewModelResult = Subject.DetermineSiteModelForDevEnvironment();
}
 
public class And_Cookie_not_set : When_Determining_SiteModel_for_dev
{
It returns_null = () =>
_siteViewModelResult.ShouldBeNull();
}
 
public class And_Cookie_set : When_Determining_SiteModel_for_dev
{
Establish that = () =>
{
_cookie = An<IHttpCookie>();
_cookie.Value = "site/123";
 
The<ICookieService>()
.WhenToldTo(x => x.GetCookie(".CMS3DevSite"))
.Return(_cookie);
 
var site1 = new SiteModel{ Id = "site/123", HostName = "foo" };
var site2 = new SiteModel{ Id = "site/456", HostName = "bar" };
 
The<IDocumentSession>().Store(site1);
The<IDocumentSession>().Store(site2);
The<IDocumentSession>().SaveChanges();
 
};
 
It loads_site = () =>
_siteViewModelResult.HostName.ShouldEqual("foo");
}
 
public class And_Cookie_set_but_site_does_not_exist : When_Determining_SiteModel_for_dev
{
Establish that = () =>
{
_cookie = An<IHttpCookie>();
_cookie.Value = "site/123";
 
The<ICookieService>()
.WhenToldTo(x => x.GetCookie(".CMS3DevSite"))
.Return(_cookie);
};
 
It returns_null = () =>
_siteViewModelResult.ShouldBeNull();
}
}
RavenInMemorySlowRunner.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
using IDM.CMS3.Service.App_Start;
using Machine.Fakes;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Raven.Client.Listeners;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IDM.CMS3.Service.Tests
{
public class RavenInMemorySlowRunner
{
public class NoStaleQueriesAllowed : IDocumentQueryListener
{
public void BeforeQueryExecuted(IDocumentQueryCustomization queryCustomization)
{
queryCustomization.WaitForNonStaleResults();
}
}
 
public static EmbeddableDocumentStore Store { get; set; }
public static IDocumentSession Session { get; set; }
 
OnEstablish context = fakeAccessor =>
{
fakeAccessor.Configure(x => x.For<IDocumentSession>().Use(() =>
{
if (Store == null)
{
Store = new EmbeddableDocumentStore { RunInMemory = true };
Store.RegisterListener(new NoStaleQueriesAllowed());
Store.Initialize();
 
// RegisterServicesWithNinject is in the project where the indexes live
IndexCreation.CreateIndexes(typeof(RegisterServicesWithNinject).Assembly, Store);
IndexCreation.CreateIndexes(typeof(RavenInMemorySlowRunner).Assembly, Store);
 
}
 
if (Session == null)
{
Session = Store.OpenSession();
}
 
return Session;
}));
 
};
 
OnCleanup subject = subject =>
{
Session.Advanced.DocumentStore.DatabaseCommands.DeleteByIndex("AllDocumentsById", new IndexQuery());
Session.SaveChanges();
Session.Dispose();
//Store.Dispose();
};
 
public class AllDocumentsById : AbstractIndexCreationTask
{
public override IndexDefinition CreateIndexDefinition()
{
return new IndexDefinition
{
Name = "AllDocumentsById",
Map = "from doc in docs let DocId = doc[\"@metadata\"][\"@id\"] select new {DocId};"
};
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.