Exemplo n.º 1
0
        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()));
        }
Exemplo n.º 2
0
        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()));
        }