Skip to content

Instantly share code, notes, and snippets.

@lindstromhenrik
Created January 18, 2013 14:40
Show Gist options
  • Save lindstromhenrik/4564976 to your computer and use it in GitHub Desktop.
Save lindstromhenrik/4564976 to your computer and use it in GitHub Desktop.
Filtering on a GeoLocation dictionary with Dictionary2Find
public class DictionariesWithGeolocation
{
[Fact]
public void FilterByValueInDictionary()
{
new Story("Filter by matching a key/geolocation in a dictionary")
.InOrderTo("be able to filter on a geolocation of a specific key in a dictionary")
.AsA("developer")
.IWant("to be able to map dictionaries to their correct value type")
.WithScenario("map dictionaries to their correct value type")
.Given(IHaveAClient)
.And(IHaveMappedTypesToDictionaryKeys)
.And(IHaveAStore)
.And(TheStoreHasALocationInStockholm)
.And(IHaveIndexedTheStoreObject)
.And(IHaveWaitedForASecond)
.When(ISearchForAStoreWithin1kmFromSergelsTorg)
.Then(IShouldGetASingleHit)
.Execute();
}
protected IClient client;
void IHaveAClient()
{
client = Client.CreateFromConfig();
}
void IHaveMappedTypesToDictionaryKeys()
{
client.Conventions.ContractResolver.ContractInterceptors.Add(new IncludeTypeNameInDictionaryKeyFieldNameConvention());
}
private Store store;
void IHaveAStore()
{
store = new Store();
}
void TheStoreHasALocationInStockholm()
{
store.Locations.Add("Stockholm", new GeoLocation(59.33265, 18.06468));
}
void IHaveIndexedTheStoreObject()
{
client.Index(store);
}
void IHaveWaitedForASecond()
{
Thread.Sleep(1000);
}
SearchResults<Store> result;
void ISearchForAStoreWithin1kmFromSergelsTorg()
{
result = client.Search<Store>()
.Filter(x => x.Locations["Stockholm"].WithinDistanceFrom(new GeoLocation(59.33234, 18.06291), 1.Kilometers()))
.GetResult();
}
void IShouldGetASingleHit()
{
result.TotalMatching.Should().Be(1);
}
public class Store
{
public Store()
{
Locations = new Dictionary<string, GeoLocation>();
}
public string Name { get; set; }
public Dictionary<string, GeoLocation> Locations { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment