generated from SwissLife-OSS/template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ToDefinitionString for Update and Find Definitions. (#67)
- Loading branch information
Showing
11 changed files
with
238 additions
and
7 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/Prime.Extensions.Tests/FilterDefinitionExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Prime.Extensions.Tests/UpdateDefinitionExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...apshots__/FilterDefinitionExtensionsTests.ToFilterString_FilterStringNoIdent_Success.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }] }] } |
11 changes: 11 additions & 0 deletions
11
...shots__/FilterDefinitionExtensionsTests.ToFilterString_FilterStringWithIdent_Success.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}] | ||
}] | ||
} |
1 change: 1 addition & 0 deletions
1
...uentExtensionsTests.ToQueryString_ToOneSingleQueryString_OriginalMongoDbQueryPrinted.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
find({ "$or" : [{ "Name" : "Bar1" }, { "Value" : "1234" }] }).limit(5) |
1 change: 1 addition & 0 deletions
1
..._/UpdateDefinitionExtensionsTests.ToDefinitionString_DefinitionStringNoIdent_Success.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "$set" : { "Name" : "Spain" } } |
5 changes: 5 additions & 0 deletions
5
...UpdateDefinitionExtensionsTests.ToDefinitionString_DefinitionStringWithIdent_Success.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"$set" : { | ||
"_id" : CSUUID("44752191-e10b-435a-a0e9-62e7f13d41cd") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |