Skip to content

Commit

Permalink
Added ToDefinitionString for Update and Find Definitions. (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
nscheibe authored Nov 22, 2022
1 parent 0256e45 commit fa74173
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 7 deletions.
77 changes: 77 additions & 0 deletions src/Prime.Extensions.Tests/FilterDefinitionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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<Bar> filter =
Builders<Bar>.Filter.Or(
Builders<Bar>.Filter.And(
Builders<Bar>.Filter.Eq(u => u.Id,
Guid.Parse("44752191-E10B-435A-A0E9-62E7F13D41CD")),
Builders<Bar>.Filter.Eq(b => b.Name, "Walse"),
Builders<Bar>.Filter.Eq(b => b.Name, "Toronto")));

// Act
string filterString = filter.ToDefinitionString();

// Assert
Snapshot.Match(filterString);
}

[Fact]
public void ToFilterString_FilterStringWithIdent_Success()
{
// Arrange
FilterDefinition<Bar> filter =
Builders<Bar>.Filter.Or(
Builders<Bar>.Filter.And(
Builders<Bar>.Filter.Eq(u => u.Id,
Guid.Parse("44752191-E10B-435A-A0E9-62E7F13D41CD")),
Builders<Bar>.Filter.Eq(b => b.Name, "Walse"),
Builders<Bar>.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<Bar> filter =
Builders<Bar>.Update.Set(field => field.Name, "Spain");

// Act
string filterString = filter.ToDefinitionString();

// Assert
Snapshot.Match(filterString);
}

[Fact]
public void ToDefinitionString_DefinitionStringWithIdent_Success()
{
// Arrange
UpdateDefinition<Bar> filter =
Builders<Bar>.Update.Set(field => field.Id,
Guid.Parse("44752191-E10B-435A-A0E9-62E7F13D41CD"));

// Act
string filterString = filter.ToDefinitionString(indent: true);

// Assert
Snapshot.Match(filterString);
}
}
}
17 changes: 17 additions & 0 deletions src/Prime.Extensions.Tests/FindFluentExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,22 @@ public void PrintQuery_PrintOneSingleQuery_OriginalMongoDbQueryPrinted()
// Assert
Snapshot.Match(mongodbQuery);
}

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

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

// Assert
Snapshot.Match(mongodbQuery);
}
}
}
39 changes: 39 additions & 0 deletions src/Prime.Extensions.Tests/UpdateDefinitionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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<Bar> filter =
Builders<Bar>.Update.Set(field => field.Name, "Spain");

// Act
string filterString = filter.ToDefinitionString();

// Assert
Snapshot.Match(filterString);
}

[Fact]
public void ToDefinitionString_DefinitionStringWithIdent_Success()
{
// Arrange
UpdateDefinition<Bar> filter =
Builders<Bar>.Update.Set(field => field.Id,
Guid.Parse("44752191-E10B-435A-A0E9-62E7F13D41CD"));

// Act
string filterString = filter.ToDefinitionString(indent: true);

// Assert
Snapshot.Match(filterString);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "$or" : [{ "$and" : [{ "_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd") }, { "Name" : "Walse" }, { "Name" : "Toronto" }] }] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$or" : [{
"$and" : [{
"_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd")
}, {
"Name" : "Walse"
}, {
"Name" : "Toronto"
}]
}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
find({ "$or" : [{ "Name" : "Bar1" }, { "Value" : "1234" }] }).limit(5)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "$set" : { "Name" : "Spain" } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$set" : {
"_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd")
}
}
36 changes: 36 additions & 0 deletions src/Prime.Extensions/FilterDefinitionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<T>(
this FilterDefinition<T> 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<T>(
this FilterDefinition<T> filter)
{
IBsonSerializerRegistry serializerRegistry =
BsonSerializer.SerializerRegistry;

return filter.Render(serializerRegistry
.GetSerializer<T>(), serializerRegistry);
}
}
}
21 changes: 14 additions & 7 deletions src/Prime.Extensions/FindFluentExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<TDocument, TProjection>(
this IFindFluent<TDocument, TProjection> findFluent)
{
return findFluent.ToString() ?? string.Empty;
}

public static string ToQueryString<TDocument, TProjection>(
this IFindFluent<TDocument, TProjection> findFluent)
{
public static string PrintQuery<TDocument, TProjection>(
this IFindFluent<TDocument, TProjection> findFluent)
{
return findFluent.ToString();
}
return findFluent!.ToString() ?? string.Empty;
}
}
36 changes: 36 additions & 0 deletions src/Prime.Extensions/UpdateDefinitionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<T>(
this UpdateDefinition<T> 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<T>(
this UpdateDefinition<T> filter)
{
IBsonSerializerRegistry serializerRegistry =
BsonSerializer.SerializerRegistry;

return filter.Render(serializerRegistry
.GetSerializer<T>(), serializerRegistry);
}
}
}

0 comments on commit fa74173

Please sign in to comment.