Skip to content

Commit 48ba1a3

Browse files
committed
Improved tests
1 parent 643408a commit 48ba1a3

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

tests/MongoDB.Driver.Tests/MultipleRegistriesTests.cs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,29 @@ namespace MongoDB.Driver.Tests
2828
[Trait("Category", "Integration")]
2929
public class MultipleRegistriesTests
3030
{
31+
private readonly string _defaultObjectIdString = "6797b56bf5495bf53aa3078f";
32+
private readonly ObjectId _defaultId = ObjectId.Parse("6797b56bf5495bf53aa3078f");
33+
3134
[Fact]
3235
public void TestSerialization()
3336
{
3437
RequireServer.Check();
3538

36-
// {
37-
// var client = CreateClient();
38-
// var collection = GetTypedCollection<Person>(client);
39-
// var bsonCollection = GetUntypedCollection(client);
40-
//
41-
// var person = new Person { Id = ObjectId.Parse("6797b56bf5495bf53aa3078f"), Name = "Mario", Age = 24 };
42-
// collection.InsertOne(person);
43-
//
44-
// var retrieved = bsonCollection.FindSync(FilterDefinition<BsonDocument>.Empty).ToList().Single();
45-
// var toString = retrieved.ToString();
46-
//
47-
// var expectedVal =
48-
// """{ "_id" : { "$oid" : "6797b56bf5495bf53aa3078f" }, "Name" : "Mario", "Age" : 24 }""";
49-
// Assert.Equal(expectedVal, toString);
50-
// }
39+
{
40+
var client = CreateClient();
41+
var collection = GetTypedCollection<Person>(client);
42+
var bsonCollection = GetUntypedCollection(client);
43+
44+
var person = new Person { Id = _defaultId, Name = "Mario", Age = 24 };
45+
collection.InsertOne(person);
46+
47+
var retrieved = bsonCollection.FindSync(FilterDefinition<BsonDocument>.Empty).ToList().Single();
48+
var toString = retrieved.ToString();
49+
50+
var expectedVal =
51+
$$"""{ "_id" : { "$oid" : "{{_defaultObjectIdString}}" }, "Name" : "Mario", "Age" : 24 }""";
52+
Assert.Equal(expectedVal, toString);
53+
}
5154

5255
//The first section demonstrates that the class maps are also separated
5356
{
@@ -58,14 +61,14 @@ public void TestSerialization()
5861
var collection = GetTypedCollection<Person>(client);
5962
var bsonCollection = GetUntypedCollection(client);
6063

61-
var person = new Person { Id = ObjectId.Parse("6797b56bf5495bf53aa3078f"), Name = "Mario", Age = 24 };
64+
var person = new Person { Id = _defaultId, Name = "Mario", Age = 24 };
6265
collection.InsertOne(person);
6366

6467
var retrievedAsBson = bsonCollection.FindSync(FilterDefinition<BsonDocument>.Empty).ToList().Single();
6568
var toString = retrievedAsBson.ToString();
6669

6770
var expectedVal =
68-
"""{ "_id" : { "$oid" : "6797b56bf5495bf53aa3078f" }, "Name" : "Mariotest", "Age" : 24 }""";
71+
$$"""{ "_id" : { "$oid" : "{{_defaultObjectIdString}}" }, "Name" : "Mariotest", "Age" : 24 }""";
6972
Assert.Equal(expectedVal, toString);
7073

7174
var retrievedTyped = collection.FindSync(FilterDefinition<Person>.Empty).ToList().Single();
@@ -82,7 +85,7 @@ public void TestDeserialization()
8285
var client = CreateClient();
8386
var collection = GetTypedCollection<Person1>(client);
8487

85-
var person = new Person1 { Id = ObjectId.Parse("6797b56bf5495bf53aa3078f"), Name = "Mariotest", Age = 24 };
88+
var person = new Person1 { Id = _defaultId, Name = "Mariotest", Age = 24 };
8689
collection.InsertOne(person);
8790
}
8891

@@ -110,14 +113,14 @@ public void TestLinq()
110113
var collection = GetTypedCollection<Person>(client);
111114
var untypedCollection = GetUntypedCollection(client);
112115

113-
var person = new Person { Id = ObjectId.Parse("6797b56bf5495bf53aa3078f"), Name = "Mario", Age = 24 };
116+
var person = new Person { Id = _defaultId, Name = "Mario", Age = 24 };
114117
collection.InsertOne(person);
115118

116119
var retrievedAsBson = untypedCollection.FindSync(FilterDefinition<BsonDocument>.Empty).ToList().Single();
117120
var toString = retrievedAsBson.ToString();
118121

119122
var expectedVal =
120-
"""{ "_id" : { "$oid" : "6797b56bf5495bf53aa3078f" }, "Name" : "Mariotest", "Age" : 24 }""";
123+
$$"""{ "_id" : { "$oid" : "{{_defaultObjectIdString}}" }, "Name" : "Mariotest", "Age" : 24 }""";
121124
Assert.Equal(expectedVal, toString);
122125

123126
var retrievedTyped = collection.AsQueryable().Where(x => x.Name == "Mario").ToList(); //The string serializer is correctly serializing "Mario" to "Mariotest"
@@ -152,7 +155,7 @@ public void TestConventions()
152155
var toString = retrievedAsBson.ToString();
153156

154157
var expectedVal =
155-
"""{ "_id" : { "$oid" : "6797b56bf5495bf53aa3078f" }, "name" : "Mario", "age" : 24 }""";
158+
$$"""{ "_id" : { "$oid" : "{{_defaultObjectIdString}}" }, "Name" : "Mario", "Age" : 24 }""";
156159
Assert.Equal(expectedVal, toString);
157160
}
158161

@@ -314,7 +317,9 @@ public class DerivedPerson2 : BasePerson
314317
}
315318

316319

317-
public class CustomStringSerializer : SealedClassSerializerBase<string> //This serializer just adds "test" to any serialised string
320+
// This serializer adds the _appended variable to any serialised string
321+
public class CustomStringSerializer(string appended = "test")
322+
: SealedClassSerializerBase<string>
318323
{
319324
/// <inheritdoc/>
320325
public override int GetHashCode() => 0;
@@ -326,24 +331,23 @@ protected override string DeserializeValue(BsonDeserializationContext context, B
326331
var bsonType = bsonReader.GetCurrentBsonType();
327332
return bsonType switch
328333
{
329-
BsonType.String => bsonReader.ReadString().Replace("test", ""),
334+
BsonType.String => bsonReader.ReadString().Replace(appended, ""),
330335
_ => throw CreateCannotDeserializeFromBsonTypeException(bsonType)
331336
};
332337
}
333338

334-
protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args,
335-
string value)
339+
protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, string value)
336340
{
337341
var bsonWriter = context.Writer;
338-
bsonWriter.WriteString(value + "test");
342+
bsonWriter.WriteString(value + appended);
339343
}
340344
}
341345

342346
public class CustomObjectIdGenerator : IIdGenerator
343347
{
344348
public object GenerateId(object container, object document)
345349
{
346-
return ObjectId.Parse("6797b56bf5495bf53aa3078f");
350+
return ObjectId.Parse("6797b56bf5495bf53aa3078f");
347351
}
348352

349353
public bool IsEmpty(object id)

0 commit comments

Comments
 (0)