Created
May 6, 2013 23:31
-
-
Save PureKrome/5529128 to your computer and use it in GitHub Desktop.
Another autocomplete Ravendb test.
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.Collections.Generic; | |
using System.Linq; | |
using Raven.Abstractions.Indexing; | |
using Raven.Client; | |
using Raven.Client.Indexes; | |
using Raven.Tests.Helpers; | |
using Xunit; | |
namespace ConsoleApplication6 | |
{ | |
public class AutoCompleteFacts : RavenTestBase | |
{ | |
private IDocumentStore _documentStore; | |
private IDocumentStore DocumentStore | |
{ | |
get | |
{ | |
if (_documentStore == null) | |
{ | |
var documentStore = NewDocumentStore(); | |
// Create the index. | |
new Locations_AutoComplete().Execute(documentStore); | |
#region Seed fake data | |
using (var documentSession = documentStore.OpenSession()) | |
{ | |
foreach (var location in FakeLocations) | |
{ | |
documentSession.Store(location); | |
} | |
foreach (var crime in FakeCrimes) | |
{ | |
documentSession.Store(crime); | |
} | |
documentSession.SaveChanges(); | |
} | |
#endregion | |
// Force indexing to complete. | |
WaitForIndexing(documentStore); | |
_documentStore = documentStore; | |
} | |
return _documentStore; | |
} | |
} | |
private IEnumerable<Location> FakeLocations | |
{ | |
get | |
{ | |
return new List<Location> | |
{ | |
new Location {Name = "Carnegie Hill"}, | |
new Location {Name = "Gramercy Park"}, | |
new Location {Name = "Upper West Side"}, | |
new Location {Name = "Central Park"}, | |
new Location {Name = "Lenox Hill"}, | |
new Location {Name = "Lincoln Square"}, | |
new Location {Name = "West Village"}, | |
new Location {Name = "YorkVille"}, | |
new Location {Name = "Upper East Side"}, | |
new Location {Name = "Greenwich Village"}, | |
new Location {Name = "Chelsea"}, | |
new Location {Name = "East Village"}, | |
new Location {Name = "Bowery"}, | |
new Location {Name = "Soho"}, | |
new Location {Name = "Tribeca"} | |
}; | |
} | |
} | |
private IEnumerable<Crime> FakeCrimes | |
{ | |
get | |
{ | |
return new List<Crime> | |
{ | |
new Crime | |
{ | |
LocationId = "Locations/1", | |
Offense = OffenseType.Theft, | |
Details = "Theft of a bike." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/1", | |
Offense = OffenseType.Theft, | |
Details = "Kid stole bread." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/1", | |
Offense = OffenseType.Stabbing, | |
Details = "Man stabbed in butt cheek." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/2", | |
Offense = OffenseType.Theft, | |
Details = "Stollen mail." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/3", | |
Offense = OffenseType.Theft, | |
Details = "Lamppost stollen." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/3", | |
Offense = OffenseType.Murder, | |
Details = "Cat murdered. Hit and run." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/4", | |
Offense = OffenseType.CarJacking, | |
Details = "Blue car car-jacked." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/5", | |
Offense = OffenseType.CarJacking, | |
Details = "Red car car-jacked." | |
} | |
}; | |
} | |
} | |
[Fact] | |
public void GivenAnInvalidQueryAndSomeLocations_AutoComplete_ReturnsNoLocations() | |
{ | |
// Arrange. | |
var documentSession = DocumentStore.OpenSession(); | |
// Act. | |
var results = documentSession | |
.Query<Locations_AutoComplete.Result, Locations_AutoComplete>() | |
.Search(x => x.Name, "adasdasa") | |
.ToList(); | |
// Cleanup. | |
documentSession.Dispose(); | |
DocumentStore.Dispose(); | |
// Assert. | |
Assert.NotNull(results); | |
Assert.Empty(results); | |
} | |
[Fact] | |
public void GivenATheQueryHillAndSomeLocations_AutoComplete_ReturnsSomeLocations() | |
{ | |
// Arrange. | |
var documentSession = DocumentStore.OpenSession(); | |
// Act. | |
var results = documentSession | |
.Query<Locations_AutoComplete.Result, Locations_AutoComplete>() | |
.Search(x => x.Name, "hill") | |
.ToList(); | |
// Cleanup. | |
documentSession.Dispose(); | |
DocumentStore.Dispose(); | |
// Assert. | |
Assert.NotNull(results); | |
Assert.NotEmpty(results); | |
Assert.Equal(2, results.Count); | |
Assert.Equal("Carnegie Hill", results.First().Name); | |
} | |
[Fact] | |
public void GivenTheQueryTribecaAndSomeLocations_AutoComplete_ReturnsALocation() | |
{ | |
// Arrange. | |
var documentSession = DocumentStore.OpenSession(); | |
// Act. | |
var results = documentSession | |
.Query<Locations_AutoComplete.Result, Locations_AutoComplete>() | |
.Search(x => x.Name, "tribeca") | |
.ToList(); | |
// Cleanup. | |
documentSession.Dispose(); | |
DocumentStore.Dispose(); | |
// Assert. | |
Assert.NotNull(results); | |
Assert.NotEmpty(results); | |
Assert.Equal(1, results.Count); | |
Assert.Equal("Tribeca", results.First().Name); | |
} | |
[Fact] | |
public void GivenTheQueryVillageAndSomeLocations_AutoComplete_ReturnsSomeLocations() | |
{ | |
// Arrange. | |
var documentSession = DocumentStore.OpenSession(); | |
// Act. | |
var results = documentSession | |
.Query<Locations_AutoComplete.Result, Locations_AutoComplete>() | |
.Search(x => x.Name, "village") | |
.ToList(); | |
// Cleanup. | |
documentSession.Dispose(); | |
DocumentStore.Dispose(); | |
// Assert. | |
Assert.NotNull(results); | |
Assert.NotEmpty(results); | |
Assert.Equal(3, results.Count); | |
Assert.Equal("West Village", results.First().Name); | |
} | |
public class Locations_AutoComplete : AbstractMultiMapIndexCreationTask<Locations_AutoComplete.Result> | |
{ | |
public Locations_AutoComplete() | |
{ | |
AddMap<Location>(locations => | |
from location in locations | |
select new Result | |
{ | |
Name = location.Name | |
}); | |
// I'm guessing we'll be doing a REDUCE later, so i just hacked | |
// up an TransformResults so the other code that uses this index, | |
// is already coded :P | |
TransformResults = (database, results) => from r in results | |
select new Result | |
{ | |
Name = r.Name | |
}; | |
Index(x => x.Name, FieldIndexing.Analyzed); | |
} | |
public class Result | |
{ | |
public string Name { get; set; } | |
} | |
} | |
#region Models | |
private class Crime | |
{ | |
public Crime() | |
{ | |
CreatedOn = DateTime.UtcNow; | |
} | |
public string Id { get; set; } | |
public OffenseType Offense { get; set; } | |
public string Details { get; set; } | |
public string LocationId { get; set; } | |
public DateTime CreatedOn { get; private set; } | |
} | |
private class Location | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
} | |
private enum OffenseType | |
{ | |
Unknown, | |
Theft, | |
Murder, | |
CarJacking, | |
Stabbing | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment