diff --git a/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs b/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs index ecd9371..7103f9d 100644 --- a/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs +++ b/src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs @@ -246,6 +246,46 @@ public async Task CleanAllCollections_CleanAllDocumentsOfAllCollections_AllColle Assert.Equal(0, fooCollection.CountDocuments()); } + [Fact] + public async Task CleanAllCollections_IgnoreOneCollectionToClean_SuccessfullyIgnored() + { + // Arrange + IMongoCollection barCollection = _mongoDatabase.GetCollection(); + IMongoCollection fooCollection = _mongoDatabase.GetCollection(); + + var arrangedBars = new List { + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903041"), "Bar1", "Value1"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903042"), "Bar2", "Value2"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903043"), "Bar3", "Value3"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903044"), "Bar3", "Value4"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903045"), "Bar5", "Value5"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903046"), "Bar6", "Value6"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903047"), "Bar7", "Value7"), + }; + + var arrangedFoo = new List { + new Foo("Foo1", "Value1"), + new Foo("Foo2", "Value2"), + new Foo("Foo3", "Value3"), + new Foo("Foo4", "Value4"), + new Foo("Foo5", "Value5") + }; + + await barCollection.InsertManyAsync(arrangedBars); + await fooCollection.InsertManyAsync(arrangedFoo); + + Assert.Equal(7, barCollection.CountDocuments()); + Assert.Equal(5, fooCollection.CountDocuments()); + + // Act + _mongoDatabase.CleanAllCollections( + barCollection.CollectionNamespace.CollectionName); + + // Assert + Assert.Equal(7, barCollection.CountDocuments()); + Assert.Equal(0, fooCollection.CountDocuments()); + } + [Fact] public async Task CleanAllCollectionsAsync_CleanAllDocumentsOfAllCollections_AllCollectionsEmpty() { @@ -285,6 +325,46 @@ public async Task CleanAllCollectionsAsync_CleanAllDocumentsOfAllCollections_All Assert.Equal(0, fooCollection.CountDocuments()); } + [Fact] + public async Task CleanAllCollectionsAsync_IgnoreOneCollectionToClean_SuccessfullyIgnored() + { + // Arrange + IMongoCollection barCollection = _mongoDatabase.GetCollection(); + IMongoCollection fooCollection = _mongoDatabase.GetCollection(); + + var arrangedBars = new List { + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903041"), "Bar1", "Value1"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903042"), "Bar2", "Value2"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903043"), "Bar3", "Value3"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903044"), "Bar3", "Value4"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903045"), "Bar5", "Value5"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903046"), "Bar6", "Value6"), + new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903047"), "Bar7", "Value7"), + }; + + var arrangedFoo = new List { + new Foo("Foo1", "Value1"), + new Foo("Foo2", "Value2"), + new Foo("Foo3", "Value3"), + new Foo("Foo4", "Value4"), + new Foo("Foo5", "Value5") + }; + + await barCollection.InsertManyAsync(arrangedBars); + await fooCollection.InsertManyAsync(arrangedFoo); + + Assert.Equal(7, barCollection.CountDocuments()); + Assert.Equal(5, fooCollection.CountDocuments()); + + // Act + await _mongoDatabase.CleanAllCollectionsAsync( + ignoreCollectionNames: fooCollection.CollectionNamespace.CollectionName); + + // Assert + Assert.Equal(0, barCollection.CountDocuments()); + Assert.Equal(5, fooCollection.CountDocuments()); + } + #endregion CleanAllCollections Tests } } diff --git a/src/Prime.Extensions/MongoDatabaseExtensions.cs b/src/Prime.Extensions/MongoDatabaseExtensions.cs index c51afda..b601f21 100644 --- a/src/Prime.Extensions/MongoDatabaseExtensions.cs +++ b/src/Prime.Extensions/MongoDatabaseExtensions.cs @@ -1,3 +1,5 @@ +using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text.Json; @@ -91,33 +93,46 @@ public static IMongoCollection GetCollection( /// /// The database to clean the collections. public static void CleanAllCollections( - this IMongoDatabase mongoDatabase) + this IMongoDatabase mongoDatabase, + params string[] ignoreCollectionNames) { - foreach (var name in mongoDatabase.ListCollectionNames().ToList()) + foreach (string collectionName in mongoDatabase.ListCollectionNames().ToList()) { + if (IgnoreCollection(collectionName, ignoreCollectionNames)) + { + continue; + } + IMongoCollection collection = - mongoDatabase.GetCollection(name); + mongoDatabase.GetCollection(collectionName); collection.CleanCollection(); } } + /// /// Deletes all entries of every collection of the mongo database. /// The collections will NOT be dropped and the indexes stay unmodified. /// /// The database to clean the collections. public static async Task CleanAllCollectionsAsync( - this IMongoDatabase mongoDatabase, - CancellationToken cancellationToken = default) + this IMongoDatabase mongoDatabase, + CancellationToken cancellationToken = default, + params string[] ignoreCollectionNames) { IAsyncCursor cursor = await mongoDatabase .ListCollectionNamesAsync(cancellationToken: cancellationToken); - foreach (var name in await cursor.ToListAsync(cancellationToken)) + foreach (var collectionName in await cursor.ToListAsync(cancellationToken)) { + if (IgnoreCollection(collectionName, ignoreCollectionNames)) + { + continue; + } + IMongoCollection collection = - mongoDatabase.GetCollection(name); + mongoDatabase.GetCollection(collectionName); await collection.CleanCollectionAsync(cancellationToken); } @@ -145,4 +160,18 @@ public static IEnumerable>> DumpA return dumpedCollections.OrderBy(entry => entry.Key); } + + private static bool IgnoreCollection( + string collectionName, + string[] ignoreCollectionNames) + { + bool isIgnoredCollection = ignoreCollectionNames + .Any(ignoredCollectionName => ignoredCollectionName + .Equals(collectionName, StringComparison.OrdinalIgnoreCase)); + + bool isSystemCollection = collectionName + .StartsWith("system", StringComparison.OrdinalIgnoreCase); + + return isSystemCollection || isIgnoredCollection; + } }