public void TestClassMap() { // this test passes normally when the Test class is automapped // uncomment all or parts of the class map initialization code to test // the exceptions thrown for each non-compliant field or property if (firstTime) { BsonClassMap.RegisterClassMap<Test>(cm => { cm.AutoMap(); // cm.MapField("literal"); // cm.MapField("readOnly"); // cm.MapField("notfound"); // cm.MapProperty("GetOnly"); // cm.MapProperty("SetOnly"); // cm.MapProperty("notfound"); // cm.MapMember(null); }); firstTime = false; } var test = new Test("x") { SetOnly = "y" }; var json = test.ToJson(); var expected = "{ '_id' : { '$oid' : '000000000000000000000000' } }".Replace("'", "\""); // Assert.AreEqual(expected, json); var bson = test.ToBson(); var rehydrated = BsonSerializer.Deserialize<Test>(bson); Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); }
public void TestRoundTripping() { var id1 = ObjectId.GenerateNewId().ToString(); var id2 = ObjectId.GenerateNewId().ToString(); var test = new Test(); test.OtherIds = new string[] { id1, id2 }; var bson = test.ToBson(); var rehydrated = BsonSerializer.Deserialize<Test>(bson); Assert.AreEqual(id1, test.OtherIds[0]); Assert.AreEqual(id2, test.OtherIds[1]); }
public void TestClassMap() { var classMap = BsonClassMap.LookupClassMap(typeof(Test)); Assert.AreEqual(2, classMap.MemberMaps.Count()); Assert.IsTrue(classMap.MemberMaps.Any(m => m.MemberName == "Id")); Assert.IsTrue(classMap.MemberMaps.Any(m => m.MemberName == "Normal")); Assert.AreEqual("Id", classMap.IdMemberMap.MemberName); var test = new Test { Normal = "normal" }; var json = test.ToJson(); var expected = "{ '_id' : { '$oid' : '000000000000000000000000' }, 'Normal' : 'normal' }".Replace("'", "\""); Assert.AreEqual(expected, json); var bson = test.ToBson(); var rehydrated = BsonSerializer.Deserialize<Test>(bson); Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); }