Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Created September 27, 2012 11:06
Show Gist options
  • Save DominicFinn/3793474 to your computer and use it in GitHub Desktop.
Save DominicFinn/3793474 to your computer and use it in GitHub Desktop.
Test for compiled linq queries
using System;
using System.Collections.Generic;
using System.Linq;
using Nddc.Www.Core.CustomLinq;
using Nddc.Www.Core.Readmodel;
using Nddc.Www.Core.Tests.Factories;
using North51.Commons.Cqrs.ReadModel;
using North51.Commons.Testing;
using NUnit.Framework;
using Rhino.Mocks;
namespace Nddc.Www.Core.Tests.CustomLinq.DynamicQueries
{
public class GivenAVariableTypeToBeQueriedOn : Specification
{
private object all;
private Type typeToQueryOn;
protected override void Given()
{
base.Given();
var volunteers = new[]
{
VolunteerFactory.NormalVolunteer("Jim", "Finn", new DateTime(1987, 10, 10), false),
VolunteerFactory.NormalVolunteer("Will", "Finn", new DateTime(1982,10,10),false),
VolunteerFactory.NormalVolunteer("Mick", "Finn", new DateTime(1982,10,10),false)
};
Dependency<IReadModelRepository>()
.Stub(r => r.All<Volunteer>())
.Return(volunteers);
this.typeToQueryOn = typeof(Volunteer);
}
protected override void When()
{
var compiledLinqQuery = typeof(CustomQuery).GetMethod("DynamicQuery").MakeGenericMethod(this.typeToQueryOn).Invoke(null, new object[] { "DateOfBirth.Year >= 1986" });
var genericAllMethodToInvoke = Dependency<IReadModelRepository>().GetType().GetMethod("All").MakeGenericMethod(this.typeToQueryOn);
var objectArrayOfVolunteers = (IEnumerable<object>)genericAllMethodToInvoke.Invoke(Dependency<IReadModelRepository>(), null);
//this is what I am currently trying to fiddle with, I have tried a dynamic[] thing as well without success....
var castMethod = typeof (Enumerable).GetMethod("Cast").MakeGenericMethod(this.typeToQueryOn);
var stuff = castMethod.Invoke(null, new[] {objectArrayOfVolunteers});
//this where won't work
//var results = stuff.Where(compiledLinqQuery);
}
[Test]
public void ShouldNotBeNull()
{
this.all.ShouldNotBeNull();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment