Created
June 20, 2012 02:44
-
-
Save kijanawoodard/2957871 to your computer and use it in GitHub Desktop.
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
public class SummarizeTests2 : IDisposable | |
{ | |
protected DateTimeOffset Now { get; private set; } | |
protected EmbeddableDocumentStore DocumentStore { get; private set; } | |
protected IDocumentSession Session { get; private set; } | |
public SummarizeTests2() | |
{ | |
Now = DateTimeOffset.Now; | |
DocumentStore = new EmbeddableDocumentStore { RunInMemory = true }; | |
DocumentStore.RegisterListener(new NoStaleQueriesAllowed()); | |
DocumentStore.Initialize(); | |
IndexCreation.CreateIndexes(typeof(BatchSummary).Assembly, DocumentStore); | |
Session = DocumentStore.OpenSession(); | |
Setup(); | |
} | |
public void Dispose() | |
{ | |
Session.Dispose(); | |
DocumentStore.Dispose(); | |
} | |
private void Setup() | |
{ | |
var batches = new List<Batch>() | |
{ | |
new Batch {Id = "batches-1", Name = "Morning Batch", JobIds = new[] {"jobs-1", "jobs-2", "jobs-3"}}, | |
new Batch {Id = "batches-2", Name = "Afternoon Batch", JobIds = new[] {"jobs-4"}}, | |
new Batch {Id = "batches-3", Name = "Night Batch", JobIds = new[] {"jobs-5", "jobs-6", "jobs-7", "jobs-8"}}, | |
}; | |
batches.ForEach(batch => Session.Store(batch)); | |
var jobs = new List<Job> | |
{ | |
new Job() {Id = "jobs-1", BatchId="batches-1", IsComplete = false}, | |
new Job() {Id = "jobs-2", BatchId="batches-1", IsComplete = true}, | |
new Job() {Id = "jobs-3", BatchId="batches-1", IsComplete = true}, | |
new Job() {Id = "jobs-4", BatchId="batches-2", IsComplete = false}, | |
new Job() {Id = "jobs-5", BatchId="batches-3", IsComplete = true}, | |
new Job() {Id = "jobs-6", BatchId="batches-3", IsComplete = false}, | |
new Job() {Id = "jobs-7", BatchId="batches-3", IsComplete = true}, | |
new Job() {Id = "jobs-8", BatchId="batches-3", IsComplete = true}, | |
new Job() {Id = "jobs-9", IsComplete = true}, | |
new Job() {Id = "jobs-10", IsComplete = true}, | |
new Job() {Id = "jobs-11", IsComplete = false} | |
}; | |
jobs.ForEach(job => Session.Store(job)); | |
Session.SaveChanges(); | |
} | |
[Fact] | |
public void BatchSummaryShouldIncludeProperCounts() | |
{ | |
var batches = | |
Session | |
.Query<BatchSummary.Result, BatchSummary>() | |
.Where(x => x.CompletedJobCount > 0) | |
.ToList(); | |
Assert.Equal(2, batches.Count); | |
Assert.Equal(2, batches[0].CompletedJobCount); | |
Assert.Equal(3, batches[1].CompletedJobCount); | |
} | |
internal class Batch | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public IEnumerable<string> JobIds { get; set; } | |
} | |
internal class Job | |
{ | |
public string Id { get; set; } | |
public string BatchId { get; set; } | |
public bool IsComplete { get; set; } | |
} | |
internal class BatchSummary : AbstractIndexCreationTask<Job, BatchSummary.Result> | |
{ | |
public class Result | |
{ | |
public string BatchId { get; set; } | |
public string BatchName { get; set; } | |
public int CompletedJobCount { get; set; } | |
} | |
public BatchSummary() | |
{ | |
Map = jobs => from job in jobs | |
select new | |
{ | |
job.BatchId, | |
CompletedJobCount = job.IsComplete ? 1 : 0 | |
}; | |
Reduce = results => from result in results | |
where result.BatchId != null | |
group result by result.BatchId into g | |
select new | |
{ | |
BatchId = g.Key, | |
CompletedJobCount = g.Sum(x => x.CompletedJobCount) | |
}; | |
TransformResults = | |
(database, results) => from result in results | |
let batch = database.Load<Batch>(result.BatchId) | |
select new | |
{ | |
result.BatchId, | |
BatchName = batch.Name, | |
result.CompletedJobCount | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment