diff --git a/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs index a6e38d3..feb2d10 100644 --- a/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs +++ b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs @@ -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(documents); + + // Act + var result = SUT.GroupBy( + 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 { + new Child(guid1, guid2) + }; + } + + SUT.AddMany(documents); + + // Act + var result = SUT.GroupBy( + 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)] diff --git a/CoreIntegrationTests/Infrastructure/MongoDbTestFixture.cs b/CoreIntegrationTests/Infrastructure/MongoDbTestFixture.cs index 1b28bb1..bc360a8 100644 --- a/CoreIntegrationTests/Infrastructure/MongoDbTestFixture.cs +++ b/CoreIntegrationTests/Infrastructure/MongoDbTestFixture.cs @@ -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(e => docIds.Contains(e.Id)); + TestRepository.Instance.DeleteMany(DocsToDelete.ToList()); } } @@ -46,8 +45,9 @@ namespace CoreIntegrationTests.Infrastructure var docs = new List(); 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; } diff --git a/CoreIntegrationTests/Infrastructure/TestClasses.cs b/CoreIntegrationTests/Infrastructure/TestClasses.cs index c9ad2b9..13d02d4 100644 --- a/CoreIntegrationTests/Infrastructure/TestClasses.cs +++ b/CoreIntegrationTests/Infrastructure/TestClasses.cs @@ -82,6 +82,7 @@ namespace CoreIntegrationTests.Infrastructure Children = new List(); } + public int GroupingKey { get; set; } public string SomeContent { get; set; } public string SomeContent4 { get; set; } public string SomeContent5 { get; set; } diff --git a/MongoDbGenericRepository/BaseMongoRepository.Main.cs b/MongoDbGenericRepository/BaseMongoRepository.Main.cs index b7df9cc..19b9e86 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Main.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Main.cs @@ -3,7 +3,6 @@ using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Utils; using System; using System.Collections.Generic; -using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks;