Skip to content

Commit

Permalink
Added MongoDB Profiling Helpers (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
nscheibe authored Oct 25, 2022
1 parent 9452f47 commit df5da05
Show file tree
Hide file tree
Showing 22 changed files with 894 additions and 9 deletions.
38 changes: 38 additions & 0 deletions src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;
using Snapshooter.Xunit;
using Squadron;
using System.Threading.Tasks;
using Xunit;

namespace MongoDB.Prime.Extensions.Tests
{
public class FindFluentExtensionsTests : IClassFixture<MongoResource>
{
private readonly IMongoDatabase _mongoDatabase;

public FindFluentExtensionsTests(MongoResource mongoResource)
{
_mongoDatabase = mongoResource.CreateDatabase();
}

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

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

// Assert
Snapshot.Match(mongodbQuery);
}
}
}
14 changes: 14 additions & 0 deletions src/Prime.Extensions.Tests/Helpers/Bar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace MongoDB.Prime.Extensions.Tests
{
public class Bar
{
public Bar(string name)
{
Name = name;
}

public string Id { get; set; }

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

public string Id { get; set; }
public string Name { get; set; }
}
}
47 changes: 47 additions & 0 deletions src/Prime.Extensions.Tests/MongoCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;
using Snapshooter.Xunit;
using Squadron;
using System.Threading.Tasks;
using Xunit;

namespace MongoDB.Prime.Extensions.Tests
{
public class MongoCollectionExtensionsTests : IClassFixture<MongoResource>
{
private readonly IMongoDatabase _mongoDatabase;

public MongoCollectionExtensionsTests(MongoResource mongoResource)
{
_mongoDatabase = mongoResource.CreateDatabase();
}

[Fact(Skip = "not ready yet.")]
public async Task Explain_ExplainSingleQuery_SuccessfullyExplained()
{
// Arrange
IMongoCollection<Bar> barCollection =
_mongoDatabase.GetCollection<Bar>();

FindOptions findOptions = new FindOptions
{
Collation = new Collation(
locale: "en",
strength: CollationStrength.Secondary)
};

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

// Act
string? mongodbExplain = barCollection.Explain(filter, findOptions);

// Assert
Snapshot.Match(mongodbExplain);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,11 +11,11 @@

namespace MongoDB.Prime.Extensions.Tests
{
public class MongoCollectionExtensionsTests : IClassFixture<MongoResource>
public class MongoCollectionFindExtensionsTests : IClassFixture<MongoResource>
{
private readonly IMongoDatabase _mongoDatabase;

public MongoCollectionExtensionsTests(MongoResource mongoResource)
public MongoCollectionFindExtensionsTests(MongoResource mongoResource)
{
_mongoDatabase = mongoResource.CreateDatabase();
}
Expand Down Expand Up @@ -623,4 +623,4 @@ public Bar(Guid id, string name, string value)

#endregion
}
}
}
79 changes: 78 additions & 1 deletion src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using MongoDB.Bson;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using MongoDB.Bson;
using MongoDB.Driver;
using Snapshooter.Xunit;
using Squadron;
using Xunit;

Expand Down Expand Up @@ -140,5 +145,77 @@ public void GetProfilingStatus_GetEnabledProfileStatusAll_StatusAll()
}

#endregion GetProfilingStatus Tests

#region GetProfiledOperations Tests

[Fact]
public void GetProfiledOperations_GetOneExecutedOperations_ReturnsOneMongoDBOperation()
{
// Arrange
_mongoDatabase.EnableProfiling();

_mongoDatabase.CreateCollection("Bar");

// Act
IEnumerable<string> results =
_mongoDatabase.GetProfiledOperations();

// 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")
);
}

[Fact]
public void GetProfiledOperations_GetAllExecutedOperations_ReturnsAllMongoDBOperations()
{
// Arrange
_mongoDatabase.EnableProfiling();

_mongoDatabase.CreateCollection("Bar");
_mongoDatabase.CreateCollection("Foo");
_mongoDatabase.GetCollection<Bar>().InsertOne(new Bar("bar1"));
_mongoDatabase.GetCollection<Foo>().InsertOne(new Foo("foo1"));
_mongoDatabase.GetCollection<Foo>().Find(foo => foo.Name == "foo1").ToList();

// Act
string[] results =
_mongoDatabase.GetProfiledOperations().ToArray();

// 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")
);
}

#endregion
}
}
4 changes: 4 additions & 0 deletions src/Prime.Extensions.Tests/Prime.Extensions.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<ProjectReference Include="..\Prime.Extensions\Prime.Extensions.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="__snapshots__\__mismatch__\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
find({ "$or" : [{ "Name" : "Bar1" }, { "_id" : "1234" }] }).limit(5)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"Key": "a1c9e3e8-b448-42da-a684-716932903041",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903041",
"Name": "Bar1",
"Value": "Value1"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903044",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903044",
"Name": "Bar4",
"Value": "Value4"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903046",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903046",
"Name": "Bar6",
"Value": "Value6"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903047",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903047",
"Name": "Bar7",
"Value": "Value7"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"Key": "a1c9e3e8-b448-42da-a684-716932903041",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903041",
"Name": "Bar1",
"Value": "Value1"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903044",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903044",
"Name": "Bar4",
"Value": "Value4"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903046",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903046",
"Name": "Bar6",
"Value": "Value6"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"Key": "a1c9e3e8-b448-42da-a684-716932903043",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903043",
"Name": "Bar3",
"Value": "Value3"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903044",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903044",
"Name": "Bar4",
"Value": "Value4"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903045",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903045",
"Name": "Bar5",
"Value": "Value5"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903046",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903046",
"Name": "Bar6",
"Value": "Value6"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"Key": "a1c9e3e8-b448-42da-a684-716932903043",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903043",
"Name": "Bar3",
"Value": "Value3"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903044",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903044",
"Name": "Bar4",
"Value": "Value4"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903045",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903045",
"Name": "Bar5",
"Value": "Value5"
}
},
{
"Key": "a1c9e3e8-b448-42da-a684-716932903046",
"Value": {
"Id": "a1c9e3e8-b448-42da-a684-716932903046",
"Name": "Bar6",
"Value": "Value6"
}
}
]
Loading

0 comments on commit df5da05

Please sign in to comment.