Skip to content

Instantly share code, notes, and snippets.

@pkrakowiak
Last active December 15, 2015 02:29
Show Gist options
  • Save pkrakowiak/cc8addf5725193a01f2d to your computer and use it in GitHub Desktop.
Save pkrakowiak/cc8addf5725193a01f2d to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Indexes;
using Raven.Tests.Helpers;
using Xunit;
namespace SponsorTests.RavenTests
{
public class CanQueryForSponsorsUsingDates : RavenTestBase
{
#region entities
private class Location
{
public string Id { get; set; }
public string Name { get; set; }
public IList<Sponsorship> Sponsors { get; set; }
}
private class Review
{
public string Id { get; set; }
public string LocationId { get; set; }
public string Comment { get; set; }
public int Rating { get; set; }
}
private class Sponsorship
{
public string Name { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
}
#endregion
// ReSharper disable InconsistentNaming
private class Locations_ByCoordinates : AbstractIndexCreationTask<Location>
// ReSharper restore InconsistentNaming
{
public Locations_ByCoordinates()
{
Map = locations => from l in locations
select new
{
l.Id,
l.Name,
l.Sponsors
};
Index(x => x.Name, FieldIndexing.Analyzed);
Index(x => x.Sponsors, FieldIndexing.NotAnalyzed);
}
}
[Fact]
public void CanGetCurrentlySponsoredLocations()
{
using (var store = NewDocumentStore())
{
using (var session = store.OpenSession())
{
PrepareTestData(session);
}
new Locations_ByCoordinates().Execute(store);
using (var session = store.OpenSession())
{
var today = new DateTime(2013, 3, 15); // fake today
var query = session.Query<Location>()
.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
.Where(x => x.Sponsors.Any(s => s.From <= today && s.To >= today));
var locations = query.ToList();
Assert.Empty(store.DatabaseCommands.GetStatistics().Errors);
Assert.Equal(1, locations.Count);
Assert.Equal("locations/2", locations.SingleOrDefault().Id);
}
}
}
[Fact]
public void CanGetCurrentlySponsoredLocationsUsingStaticIndex()
{
using (var store = NewDocumentStore())
{
using (var session = store.OpenSession())
{
PrepareTestData(session);
}
new Locations_ByCoordinates().Execute(store);
using (var session = store.OpenSession())
{
var today = new DateTime(2013, 3, 15); // fake today
var query = session.Query<Location, Locations_ByCoordinates>()
.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
.Where(x => x.Sponsors.Any(s => s.From <= today && s.To >= today));
var locations = query.ToList();
Assert.Empty(store.DatabaseCommands.GetStatistics().Errors);
Assert.Equal(1, locations.Count);
Assert.Equal("locations/2", locations.SingleOrDefault().Id);
}
}
}
[Fact]
public void CanSortOnSponsorshipStatus()
{
using (var store = NewDocumentStore())
{
using (var sesssion = store.OpenSession())
{
PrepareTestData(sesssion);
}
new Locations_ByCoordinates().Execute(store);
using (var session = store.OpenSession())
{
var today = new DateTime(2013, 3, 15); // fake today
var locations = session.Query<Location>()
.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
.OrderByDescending(x => x.Sponsors.Any(s => s.From <= today && s.To >= today)) // not feasible in RavenDB
.ToList();
Assert.Empty(store.DatabaseCommands.GetStatistics().Errors);
Assert.Equal(3, locations.Count);
Assert.Equal("locations/2", locations.First().Id);
}
}
}
private static void PrepareTestData(IDocumentSession session)
{
// Add a bunch of locations.
session.Store(new Location
{
Name = "Non Sponsored #1",
Sponsors = new List<Sponsorship>()
}, "locations/1");
session.Store(new Location
{
Name = "Sponsored #1",
Sponsors = new List<Sponsorship>
{
new Sponsorship {From = new DateTime(2013, 1, 1), To = new DateTime(2013, 1, 31)},
new Sponsorship {From = new DateTime(2013, 3, 11), To = new DateTime(2013, 3, 15)},
new Sponsorship {From = new DateTime(2013, 6, 1), To = new DateTime(2013, 6, 30)}
}
}, "locations/2");
session.Store(new Location
{
Name = "Non Sponsored #2",
Sponsors = new List<Sponsorship>()
}, "locations/3");
// Add a bunch of reviews.
session.Store(new Review
{
LocationId = "locations/1",
Comment = "test",
Rating = 3
}, "reviews/1");
session.Store(new Review
{
LocationId = "locations/3",
Comment = "test",
Rating = 4
}, "reviews/2");
session.Store(new Review
{
LocationId = "locations/3",
Comment = "test",
Rating = 5
}, "reviews/3");
session.SaveChanges();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment