Skip to content

Commit

Permalink
Added new clean functionality for MongoDB collections. (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
nscheibe authored Nov 21, 2022
1 parent df5da05 commit 0256e45
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 451 deletions.
5 changes: 5 additions & 0 deletions src/MongoDB.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Migration", "Migration\Migr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Migration.Tests", "Migration.Tests\Migration.Tests.csproj", "{A7D17D8A-99BC-40AC-92B4-996790F7F26E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".SolutionFiles", ".SolutionFiles", "{A21A1FF3-25C1-4FE1-994E-E16CDF98BB99}"
ProjectSection(SolutionItems) = preProject
TestProject.props = TestProject.props
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
2 changes: 1 addition & 1 deletion src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void PrintQuery_PrintOneSingleQuery_OriginalMongoDbQueryPrinted()

// Act
string mongodbQuery = barCollection
.Find<Bar>(bar => bar.Name == "Bar1" || bar.Id == "1234")
.Find<Bar>(bar => bar.Name == "Bar1" || bar.Value == "1234")
.Limit(5)
.PrintQuery();

Expand Down
22 changes: 13 additions & 9 deletions src/Prime.Extensions.Tests/Helpers/Bar.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace MongoDB.Prime.Extensions.Tests
using System;

namespace MongoDB.Prime.Extensions.Tests;

public class Bar
{
public class Bar
public Bar(Guid id, string name, string value)
{
public Bar(string name)
{
Name = name;
}
Id = id;
Name = name;
Value = value;
}

public string Id { get; set; }
public Guid Id { get; }

public string Name { get; set; }
}
public string Name { get; }
public string Value { get; }
}
18 changes: 9 additions & 9 deletions src/Prime.Extensions.Tests/Helpers/Foo.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace MongoDB.Prime.Extensions.Tests
namespace MongoDB.Prime.Extensions.Tests;

public class Foo
{
public class Foo
public Foo(string id, string name)
{
public Foo(string name)
{
Name = name;
}

public string Id { get; set; }
public string Name { get; set; }
Id = id;
Name = name;
}

public string Id { get; set; }
public string Name { get; set; }
}
57 changes: 56 additions & 1 deletion src/Prime.Extensions.Tests/MongoCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,61 @@ public MongoCollectionExtensionsTests(MongoResource mongoResource)
_mongoDatabase = mongoResource.CreateDatabase();
}

[Fact]
public void CleanCollection_CleanAllDocumentsOfACollection_Cleaned()
{
// Arrange
IMongoCollection<Bar> barCollection =
_mongoDatabase.GetCollection<Bar>();

barCollection.InsertMany(new[]
{
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"), "Bar4", "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"),
});

Assert.Equal(7, _mongoDatabase.GetCollection<Bar>().CountDocuments());

// Act
barCollection.CleanCollection<Bar>();

// Assert
Assert.Equal(0, _mongoDatabase.GetCollection<Bar>().CountDocuments());
}

[Fact]
public async Task CleanCollectionAsync_CleanAllDocumentsOfACollection_Cleaned()
{
// Arrange
IMongoCollection<Bar> barCollection =
_mongoDatabase.GetCollection<Bar>();

barCollection.InsertMany(new[]
{
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"), "Bar4", "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"),
});

Assert.Equal(7, _mongoDatabase.GetCollection<Bar>().CountDocuments());

// Act
await barCollection.CleanCollectionAsync<Bar>();

// Assert
Assert.Equal(0, _mongoDatabase.GetCollection<Bar>().CountDocuments());
}


[Fact(Skip = "not ready yet.")]
public async Task Explain_ExplainSingleQuery_SuccessfullyExplained()
{
Expand All @@ -34,7 +89,7 @@ public async Task Explain_ExplainSingleQuery_SuccessfullyExplained()

FilterDefinition<Bar> filter =
Builders<Bar>.Filter.And(
Builders<Bar>.Filter.Eq(u => u.Id, "1234"),
Builders<Bar>.Filter.Eq(u => u.Id, Guid.NewGuid()),
Builders<Bar>.Filter.Eq(b => b.Name, "NN"));

// Act
Expand Down
17 changes: 1 addition & 16 deletions src/Prime.Extensions.Tests/MongoCollectionFindExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -605,22 +605,7 @@ private IEnumerable<Bar> CreateRandomBars(int count)
$"BarValue-{Guid.NewGuid()}"))
.ToList();
}

private class Bar
{
public Bar(Guid id, string name, string value)
{
Id = id;
Name = name;
Value = value;
}

public Guid Id { get; }

public string Name { get; }
public string Value { get; }
}


#endregion
}
}
133 changes: 101 additions & 32 deletions src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using Snapshooter.Xunit;
Expand Down Expand Up @@ -163,20 +163,13 @@ public void GetProfiledOperations_GetOneExecutedOperations_ReturnsOneMongoDBOper
// Assert
Snapshot.Match(results.Single(),
matchOptions => matchOptions
.IgnoreField("**.ns")
.IgnoreField("**.$db")
.IgnoreField("**.flowControl")
.IgnoreField("**.millis")
.IgnoreField("**.ts")
.IgnoreField("**.base64")
.IgnoreField("**.client")
.IgnoreField("**.locks")
.IgnoreField("**.ReplicationStateTransition")
.IgnoreField("**.FeatureCompatibilityVersion")
.IgnoreField("**.queryHash")
.IgnoreField("**.planCacheKey")
.IgnoreField("**.queryExecutionEngine")
.IgnoreField("**.readConcern")
.IncludeField("**.command")
.IncludeField("**.keysExamined")
.IncludeField("**.docsExamined")
.IncludeField("**.planSummary")
.IncludeField("**.execStats")
.ExcludeField("**.$db")
.ExcludeField("**.lsid")
);
}

Expand All @@ -188,8 +181,9 @@ public void GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOper

_mongoDatabase.CreateCollection("Bar");
_mongoDatabase.CreateCollection("Foo");
_mongoDatabase.GetCollection<Bar>().InsertOne(new Bar("bar1"));
_mongoDatabase.GetCollection<Foo>().InsertOne(new Foo("foo1"));
_mongoDatabase.GetCollection<Bar>()
.InsertOne(new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903041"), "Bar1", "Value1"));
_mongoDatabase.GetCollection<Foo>().InsertOne(new Foo("Foo1", "Value1"));
_mongoDatabase.GetCollection<Foo>().Find(foo => foo.Name == "foo1").ToList();

// Act
Expand All @@ -199,23 +193,98 @@ public void GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOper
// Assert
Snapshot.Match(results.ToJsonArray(),
matchOptions => matchOptions
.IgnoreField("**.ns")
.IgnoreField("**.$db")
.IgnoreField("**.flowControl")
.IgnoreField("**.millis")
.IgnoreField("**.ts")
.IgnoreField("**.base64")
.IgnoreField("**.client")
.IgnoreField("**.locks")
.IgnoreField("**.ReplicationStateTransition")
.IgnoreField("**.FeatureCompatibilityVersion")
.IgnoreField("**.queryHash")
.IgnoreField("**.planCacheKey")
.IgnoreField("**.queryExecutionEngine")
.IgnoreField("**.readConcern")
.IncludeField("**.command")
.IncludeField("**.keysExamined")
.IncludeField("**.docsExamined")
.IncludeField("**.planSummary")
.IncludeField("**.execStats")
.ExcludeField("**.$db")
.ExcludeField("**.lsid")
);
}

#endregion

#region CleanAllCollections Tests

[Fact]
public async Task CleanAllCollections_CleanAllDocumentsOfAllCollections_AllCollectionsEmpty()
{
// Arrange
IMongoCollection<Bar> barCollection = _mongoDatabase.GetCollection<Bar>();
IMongoCollection<Foo> fooCollection = _mongoDatabase.GetCollection<Foo>();

var arrangedBars = new List<Bar> {
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<Foo> {
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();

// Assert
Assert.Equal(0, barCollection.CountDocuments());
Assert.Equal(0, fooCollection.CountDocuments());
}

[Fact]
public async Task CleanAllCollectionsAsync_CleanAllDocumentsOfAllCollections_AllCollectionsEmpty()
{
// Arrange
IMongoCollection<Bar> barCollection = _mongoDatabase.GetCollection<Bar>();
IMongoCollection<Foo> fooCollection = _mongoDatabase.GetCollection<Foo>();

var arrangedBars = new List<Bar> {
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<Foo> {
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();

// Assert
Assert.Equal(0, barCollection.CountDocuments());
Assert.Equal(0, fooCollection.CountDocuments());
}

#endregion CleanAllCollections Tests
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
find({ "$or" : [{ "Name" : "Bar1" }, { "_id" : "1234" }] }).limit(5)
find({ "$or" : [{ "Name" : "Bar1" }, { "Value" : "1234" }] }).limit(5)
Loading

0 comments on commit 0256e45

Please sign in to comment.