From fa741735b1bb9771723031ef2ecf7eac41f15389 Mon Sep 17 00:00:00 2001 From: Normen Scheiber <46715105+nscheibe@users.noreply.github.com> Date: Tue, 22 Nov 2022 09:46:53 +0100 Subject: [PATCH] Added ToDefinitionString for Update and Find Definitions. (#67) --- .../FilterDefinitionExtensionsTests.cs | 77 +++++++++++++++++++ .../FindFluentExtensionsTests.cs | 17 ++++ .../UpdateDefinitionExtensionsTests.cs | 39 ++++++++++ ...terString_FilterStringNoIdent_Success.snap | 1 + ...rString_FilterStringWithIdent_Success.snap | 11 +++ ...eryString_OriginalMongoDbQueryPrinted.snap | 1 + ...tring_DefinitionStringNoIdent_Success.snap | 1 + ...ing_DefinitionStringWithIdent_Success.snap | 5 ++ .../FilterDefinitionExtensions.cs | 36 +++++++++ src/Prime.Extensions/FindFluentExtensions.cs | 21 +++-- .../UpdateDefinitionExtensions.cs | 36 +++++++++ 11 files changed, 238 insertions(+), 7 deletions(-) create mode 100644 src/Prime.Extensions.Tests/FilterDefinitionExtensionsTests.cs create mode 100644 src/Prime.Extensions.Tests/UpdateDefinitionExtensionsTests.cs create mode 100644 src/Prime.Extensions.Tests/__snapshots__/FilterDefinitionExtensionsTests.ToFilterString_FilterStringNoIdent_Success.snap create mode 100644 src/Prime.Extensions.Tests/__snapshots__/FilterDefinitionExtensionsTests.ToFilterString_FilterStringWithIdent_Success.snap create mode 100644 src/Prime.Extensions.Tests/__snapshots__/FindFluentExtensionsTests.ToQueryString_ToOneSingleQueryString_OriginalMongoDbQueryPrinted.snap create mode 100644 src/Prime.Extensions.Tests/__snapshots__/UpdateDefinitionExtensionsTests.ToDefinitionString_DefinitionStringNoIdent_Success.snap create mode 100644 src/Prime.Extensions.Tests/__snapshots__/UpdateDefinitionExtensionsTests.ToDefinitionString_DefinitionStringWithIdent_Success.snap create mode 100644 src/Prime.Extensions/FilterDefinitionExtensions.cs create mode 100644 src/Prime.Extensions/UpdateDefinitionExtensions.cs diff --git a/src/Prime.Extensions.Tests/FilterDefinitionExtensionsTests.cs b/src/Prime.Extensions.Tests/FilterDefinitionExtensionsTests.cs new file mode 100644 index 0000000..fa58e2c --- /dev/null +++ b/src/Prime.Extensions.Tests/FilterDefinitionExtensionsTests.cs @@ -0,0 +1,77 @@ +using System; +using MongoDB.Driver; +using Snapshooter.Xunit; +using Xunit; + +namespace MongoDB.Prime.Extensions.Tests +{ + public class FilterDefinitionExtensionsTests + { + [Fact] + public void ToFilterString_FilterStringNoIdent_Success() + { + // Arrange + FilterDefinition filter = + Builders.Filter.Or( + Builders.Filter.And( + Builders.Filter.Eq(u => u.Id, + Guid.Parse("44752191-E10B-435A-A0E9-62E7F13D41CD")), + Builders.Filter.Eq(b => b.Name, "Walse"), + Builders.Filter.Eq(b => b.Name, "Toronto"))); + + // Act + string filterString = filter.ToDefinitionString(); + + // Assert + Snapshot.Match(filterString); + } + + [Fact] + public void ToFilterString_FilterStringWithIdent_Success() + { + // Arrange + FilterDefinition filter = + Builders.Filter.Or( + Builders.Filter.And( + Builders.Filter.Eq(u => u.Id, + Guid.Parse("44752191-E10B-435A-A0E9-62E7F13D41CD")), + Builders.Filter.Eq(b => b.Name, "Walse"), + Builders.Filter.Eq(b => b.Name, "Toronto"))); + + // Act + string filterString = filter.ToDefinitionString(indent: true); + + // Assert + Snapshot.Match(filterString); + } + + [Fact] + public void ToDefinitionString_DefinitionStringNoIdent_Success() + { + // Arrange + UpdateDefinition filter = + Builders.Update.Set(field => field.Name, "Spain"); + + // Act + string filterString = filter.ToDefinitionString(); + + // Assert + Snapshot.Match(filterString); + } + + [Fact] + public void ToDefinitionString_DefinitionStringWithIdent_Success() + { + // Arrange + UpdateDefinition filter = + Builders.Update.Set(field => field.Id, + Guid.Parse("44752191-E10B-435A-A0E9-62E7F13D41CD")); + + // Act + string filterString = filter.ToDefinitionString(indent: true); + + // Assert + Snapshot.Match(filterString); + } + } +} diff --git a/src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs b/src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs index 426f027..1a42d8b 100644 --- a/src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs +++ b/src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs @@ -34,5 +34,22 @@ public void PrintQuery_PrintOneSingleQuery_OriginalMongoDbQueryPrinted() // Assert Snapshot.Match(mongodbQuery); } + + [Fact] + public void ToQueryString_ToOneSingleQueryString_OriginalMongoDbQueryPrinted() + { + // Arrange + IMongoCollection barCollection = + _mongoDatabase.GetCollection(); + + // Act + string mongodbQuery = barCollection + .Find(bar => bar.Name == "Bar1" || bar.Value == "1234") + .Limit(5) + .ToQueryString(); + + // Assert + Snapshot.Match(mongodbQuery); + } } } diff --git a/src/Prime.Extensions.Tests/UpdateDefinitionExtensionsTests.cs b/src/Prime.Extensions.Tests/UpdateDefinitionExtensionsTests.cs new file mode 100644 index 0000000..54ecaba --- /dev/null +++ b/src/Prime.Extensions.Tests/UpdateDefinitionExtensionsTests.cs @@ -0,0 +1,39 @@ +using System; +using MongoDB.Driver; +using Snapshooter.Xunit; +using Xunit; + +namespace MongoDB.Prime.Extensions.Tests +{ + public class UpdateDefinitionExtensionsTests + { + [Fact] + public void ToDefinitionString_DefinitionStringNoIdent_Success() + { + // Arrange + UpdateDefinition filter = + Builders.Update.Set(field => field.Name, "Spain"); + + // Act + string filterString = filter.ToDefinitionString(); + + // Assert + Snapshot.Match(filterString); + } + + [Fact] + public void ToDefinitionString_DefinitionStringWithIdent_Success() + { + // Arrange + UpdateDefinition filter = + Builders.Update.Set(field => field.Id, + Guid.Parse("44752191-E10B-435A-A0E9-62E7F13D41CD")); + + // Act + string filterString = filter.ToDefinitionString(indent: true); + + // Assert + Snapshot.Match(filterString); + } + } +} diff --git a/src/Prime.Extensions.Tests/__snapshots__/FilterDefinitionExtensionsTests.ToFilterString_FilterStringNoIdent_Success.snap b/src/Prime.Extensions.Tests/__snapshots__/FilterDefinitionExtensionsTests.ToFilterString_FilterStringNoIdent_Success.snap new file mode 100644 index 0000000..b3ae337 --- /dev/null +++ b/src/Prime.Extensions.Tests/__snapshots__/FilterDefinitionExtensionsTests.ToFilterString_FilterStringNoIdent_Success.snap @@ -0,0 +1 @@ +{ "$or" : [{ "$and" : [{ "_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd") }, { "Name" : "Walse" }, { "Name" : "Toronto" }] }] } diff --git a/src/Prime.Extensions.Tests/__snapshots__/FilterDefinitionExtensionsTests.ToFilterString_FilterStringWithIdent_Success.snap b/src/Prime.Extensions.Tests/__snapshots__/FilterDefinitionExtensionsTests.ToFilterString_FilterStringWithIdent_Success.snap new file mode 100644 index 0000000..b850636 --- /dev/null +++ b/src/Prime.Extensions.Tests/__snapshots__/FilterDefinitionExtensionsTests.ToFilterString_FilterStringWithIdent_Success.snap @@ -0,0 +1,11 @@ +{ + "$or" : [{ + "$and" : [{ + "_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd") + }, { + "Name" : "Walse" + }, { + "Name" : "Toronto" + }] + }] +} diff --git a/src/Prime.Extensions.Tests/__snapshots__/FindFluentExtensionsTests.ToQueryString_ToOneSingleQueryString_OriginalMongoDbQueryPrinted.snap b/src/Prime.Extensions.Tests/__snapshots__/FindFluentExtensionsTests.ToQueryString_ToOneSingleQueryString_OriginalMongoDbQueryPrinted.snap new file mode 100644 index 0000000..16ec922 --- /dev/null +++ b/src/Prime.Extensions.Tests/__snapshots__/FindFluentExtensionsTests.ToQueryString_ToOneSingleQueryString_OriginalMongoDbQueryPrinted.snap @@ -0,0 +1 @@ +find({ "$or" : [{ "Name" : "Bar1" }, { "Value" : "1234" }] }).limit(5) diff --git a/src/Prime.Extensions.Tests/__snapshots__/UpdateDefinitionExtensionsTests.ToDefinitionString_DefinitionStringNoIdent_Success.snap b/src/Prime.Extensions.Tests/__snapshots__/UpdateDefinitionExtensionsTests.ToDefinitionString_DefinitionStringNoIdent_Success.snap new file mode 100644 index 0000000..5cdb4c7 --- /dev/null +++ b/src/Prime.Extensions.Tests/__snapshots__/UpdateDefinitionExtensionsTests.ToDefinitionString_DefinitionStringNoIdent_Success.snap @@ -0,0 +1 @@ +{ "$set" : { "Name" : "Spain" } } diff --git a/src/Prime.Extensions.Tests/__snapshots__/UpdateDefinitionExtensionsTests.ToDefinitionString_DefinitionStringWithIdent_Success.snap b/src/Prime.Extensions.Tests/__snapshots__/UpdateDefinitionExtensionsTests.ToDefinitionString_DefinitionStringWithIdent_Success.snap new file mode 100644 index 0000000..606c4b9 --- /dev/null +++ b/src/Prime.Extensions.Tests/__snapshots__/UpdateDefinitionExtensionsTests.ToDefinitionString_DefinitionStringWithIdent_Success.snap @@ -0,0 +1,5 @@ +{ + "$set" : { + "_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd") + } +} diff --git a/src/Prime.Extensions/FilterDefinitionExtensions.cs b/src/Prime.Extensions/FilterDefinitionExtensions.cs new file mode 100644 index 0000000..904e199 --- /dev/null +++ b/src/Prime.Extensions/FilterDefinitionExtensions.cs @@ -0,0 +1,36 @@ +using MongoDB.Bson.IO; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDB.Bson.Serialization; + +namespace MongoDB.Prime.Extensions +{ + public static class FilterDefinitionExtensions + { + public static string ToDefinitionString( + this FilterDefinition filter, bool indent = false) + { + BsonDocument bson = ToBsonDocument(filter); + + var settings = new JsonWriterSettings + { + Indent = indent, + OutputMode = JsonOutputMode.Shell, + }; + + string json = bson.ToJson(writerSettings: settings); + + return json; + } + + public static BsonDocument ToBsonDocument( + this FilterDefinition filter) + { + IBsonSerializerRegistry serializerRegistry = + BsonSerializer.SerializerRegistry; + + return filter.Render(serializerRegistry + .GetSerializer(), serializerRegistry); + } + } +} diff --git a/src/Prime.Extensions/FindFluentExtensions.cs b/src/Prime.Extensions/FindFluentExtensions.cs index e911c06..b68fc88 100644 --- a/src/Prime.Extensions/FindFluentExtensions.cs +++ b/src/Prime.Extensions/FindFluentExtensions.cs @@ -1,13 +1,20 @@ +using System; using MongoDB.Driver; -namespace MongoDB.Prime.Extensions +namespace MongoDB.Prime.Extensions; + +public static class FindFluentExtensions { - public static class FindFluentExtensions + [Obsolete("Depreceated, use ToQueryString instead.")] + public static string PrintQuery( + this IFindFluent findFluent) + { + return findFluent.ToString() ?? string.Empty; + } + + public static string ToQueryString( + this IFindFluent findFluent) { - public static string PrintQuery( - this IFindFluent findFluent) - { - return findFluent.ToString(); - } + return findFluent!.ToString() ?? string.Empty; } } diff --git a/src/Prime.Extensions/UpdateDefinitionExtensions.cs b/src/Prime.Extensions/UpdateDefinitionExtensions.cs new file mode 100644 index 0000000..4e6fa46 --- /dev/null +++ b/src/Prime.Extensions/UpdateDefinitionExtensions.cs @@ -0,0 +1,36 @@ +using MongoDB.Bson.IO; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDB.Bson.Serialization; + +namespace MongoDB.Prime.Extensions +{ + public static class UpdateDefinitionExtensions + { + public static string ToDefinitionString( + this UpdateDefinition filter, bool indent = false) + { + BsonValue bson = ToBsonValue(filter); + + var settings = new JsonWriterSettings + { + Indent = indent, + OutputMode = JsonOutputMode.Shell, + }; + + string json = bson.ToJson(writerSettings: settings); + + return json; + } + + public static BsonValue ToBsonValue( + this UpdateDefinition filter) + { + IBsonSerializerRegistry serializerRegistry = + BsonSerializer.SerializerRegistry; + + return filter.Render(serializerRegistry + .GetSerializer(), serializerRegistry); + } + } +}