1717using MongoDB . Bson ;
1818using MongoDB . Bson . Serialization ;
1919using MongoDB . Bson . Serialization . Attributes ;
20+ using MongoDB . Bson . Serialization . Conventions ;
2021using MongoDB . Bson . Serialization . Serializers ;
22+ using MongoDB . Driver . Core . TestHelpers . XunitExtensions ;
2123using Xunit ;
2224
2325namespace MongoDB . Driver . Tests
2426{
2527 public class MultipleRegistriesTests
2628 {
27- // [Fact]
29+ [ Fact ]
2830 public void TestSerialization ( )
2931 {
32+ RequireServer . Check ( ) ;
33+
3034 {
3135 var client = DriverTestConfiguration . CreateMongoClient ( ) ;
3236 var db = client . GetDatabase ( DriverTestConfiguration . DatabaseNamespace . DatabaseName ) ;
@@ -73,9 +77,11 @@ public void TestSerialization()
7377 }
7478 }
7579
76- // [Fact]
80+ [ Fact ]
7781 public void TestDeserialization ( )
7882 {
83+ RequireServer . Check ( ) ;
84+
7985 {
8086 var client = DriverTestConfiguration . CreateMongoClient ( ) ;
8187 var db = client . GetDatabase ( DriverTestConfiguration . DatabaseNamespace . DatabaseName ) ;
@@ -99,9 +105,11 @@ public void TestDeserialization()
99105 }
100106 }
101107
102- // [Fact]
108+ [ Fact ]
103109 public void TestLinq ( )
104110 {
111+ RequireServer . Check ( ) ;
112+
105113 var customDomain = BsonSerializer . CreateSerializationDomain ( ) ;
106114 customDomain . RegisterSerializer ( new CustomStringSerializer ( ) ) ;
107115
@@ -125,6 +133,40 @@ public void TestLinq()
125133 Assert . NotEmpty ( retrievedTyped ) ;
126134 }
127135
136+ [ Fact ]
137+ public void TestConventions ( )
138+ {
139+ RequireServer . Check ( ) ;
140+
141+ var customDomain = BsonSerializer . CreateSerializationDomain ( ) ;
142+
143+ // Register an id generator convention that uses a custom ObjectIdGenerator
144+ customDomain . RegisterIdGenerator ( typeof ( ObjectId ) , new CustomObjectIdGenerator ( ) ) ;
145+
146+ //Register a convention to use lowercase for all fields on the Person class
147+ var pack = new ConventionPack ( ) ;
148+ pack . AddMemberMapConvention (
149+ "LowerCaseElementName" ,
150+ m => m . SetElementName ( m . MemberName . ToLower ( ) ) ) ;
151+ customDomain . ConventionRegistry . Register ( "myPack" , pack , t => t == typeof ( Person ) ) ;
152+
153+ var client = DriverTestConfiguration . CreateMongoClient ( c => c . SerializationDomain = customDomain ) ;
154+ var db = client . GetDatabase ( DriverTestConfiguration . DatabaseNamespace . DatabaseName ) ;
155+ db . DropCollection ( DriverTestConfiguration . CollectionNamespace . CollectionName ) ;
156+ var collection = db . GetCollection < Person > ( DriverTestConfiguration . CollectionNamespace . CollectionName ) ;
157+ var untypedCollection = db . GetCollection < BsonDocument > ( DriverTestConfiguration . CollectionNamespace . CollectionName ) ;
158+
159+ var person = new Person { Name = "Mario" , Age = 24 } ; //Id is not set, so the custom ObjectIdGenerator should be used
160+ collection . InsertOne ( person ) ;
161+
162+ var retrievedAsBson = untypedCollection . FindSync ( "{}" ) . ToList ( ) . Single ( ) ;
163+ var toString = retrievedAsBson . ToString ( ) ;
164+
165+ var expectedVal =
166+ """{ "_id" : { "$oid" : "6797b56bf5495bf53aa3078f" }, "name" : "Mario", "age" : 24 }""" ;
167+ Assert . Equal ( expectedVal , toString ) ;
168+ }
169+
128170 public class Person
129171 {
130172 [ BsonId ] public ObjectId Id { get ; set ; }
@@ -174,5 +216,18 @@ protected override void SerializeValue(BsonSerializationContext context, BsonSer
174216 bsonWriter . WriteString ( value + "test" ) ;
175217 }
176218 }
219+
220+ public class CustomObjectIdGenerator : IIdGenerator
221+ {
222+ public object GenerateId ( object container , object document )
223+ {
224+ return ObjectId . Parse ( "6797b56bf5495bf53aa3078f" ) ;
225+ }
226+
227+ public bool IsEmpty ( object id )
228+ {
229+ return true ;
230+ }
231+ }
177232 }
178233}
0 commit comments