adding group by test

This commit is contained in:
Alexandre SPIESER
2019-04-14 20:07:56 +01:00
parent 2207f2f1a1
commit 8ccced66ea
4 changed files with 91 additions and 6 deletions
@@ -1127,6 +1127,91 @@ namespace CoreIntegrationTests.Infrastructure
#endregion Math
#region Group By
[Fact]
public void GroupByTProjection()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
for (var i = 0; i < documents.Count - 2; i++)
{
documents[i].GroupingKey = 1;
documents[i].SomeContent = $"{content}-{i}";
}
for (var i = 3; i < documents.Count; i++)
{
documents[i].GroupingKey = 2;
documents[i].SomeContent = $"{content}-{i}";
}
SUT.AddMany<T, TKey>(documents);
// Act
var result = SUT.GroupBy<T, int, ProjectedGroup, TKey>(
e => e.GroupingKey, g => new ProjectedGroup
{
Key = g.Key,
Content = g.Select(doc => doc.SomeContent).ToList()
},
PartitionKey);
// Assert
var key1Group = result.First(e => e.Key == 1);
Assert.NotNull(key1Group);
Assert.Equal(3, key1Group.Content.Count);
var key2Group = result.First(e => e.Key == 2);
Assert.NotNull(key2Group);
Assert.Equal(2, key2Group.Content.Count);
}
[Fact]
public void FilteredGroupByTProjection()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
for (var i = 0; i < documents.Count - 2; i++)
{
documents[i].GroupingKey = 4;
documents[i].SomeContent = $"{content}-{i}";
}
for (var i = 3; i < documents.Count; i++)
{
documents[i].GroupingKey = 5;
documents[i].SomeContent = $"{content}-{i}";
}
var guid1 = Guid.NewGuid().ToString("n");
var guid2 = Guid.NewGuid().ToString("n");
for (var i = 0; i < documents.Count - 1; i++)
{
documents[i].Children = new List<Child> {
new Child(guid1, guid2)
};
}
SUT.AddMany<T, TKey>(documents);
// Act
var result = SUT.GroupBy<T, int, ProjectedGroup, TKey>(
e => e.Children.Any(c => c.Type == guid1),
e => e.GroupingKey, g => new ProjectedGroup
{
Key = g.Key,
Content = g.Select(doc => doc.SomeContent).ToList()
}, PartitionKey);
// Assert
var key1Group = result.First(e => e.Key == 4);
Assert.NotNull(key1Group);
Assert.Equal(3, key1Group.Content.Count);
var key2Group = result.First(e => e.Key == 5);
Assert.NotNull(key2Group);
Assert.Single(key2Group.Content);
}
#endregion Group By
#region Test Utils
[MethodImpl(MethodImplOptions.NoInlining)]
@@ -27,10 +27,9 @@ namespace CoreIntegrationTests.Infrastructure
public virtual void Dispose()
{
var docIds = DocsToDelete.ToList().Select(e => e.Id);
if (docIds.Any())
if (DocsToDelete.Any())
{
TestRepository.Instance.DeleteMany<T, TKey>(e => docIds.Contains(e.Id));
TestRepository.Instance.DeleteMany<T, TKey>(DocsToDelete.ToList());
}
}
@@ -46,8 +45,9 @@ namespace CoreIntegrationTests.Infrastructure
var docs = new List<T>();
for (var i = 0; i < numberOfDocumentsToCreate; i++)
{
docs.Add(new T());
DocsToDelete.Add(docs.Last());
var doc = new T();
docs.Add(doc);
DocsToDelete.Add(doc);
}
return docs;
}
@@ -82,6 +82,7 @@ namespace CoreIntegrationTests.Infrastructure
Children = new List<Child>();
}
public int GroupingKey { get; set; }
public string SomeContent { get; set; }
public string SomeContent4 { get; set; }
public string SomeContent5 { get; set; }