public void BsonField_Test() { var mapper = new BsonMapper(); mapper.UseLowerCaseDelimiter('_'); var obj = CreateModel(); var doc = mapper.ToDocument(obj); var json = JsonSerializer.Serialize(doc, true); var nobj = mapper.ToObject<MyBsonFieldTestClass>(doc); Assert.AreEqual(doc["MY-STRING"].AsString, obj.MyString); Assert.AreEqual(doc["INTERNAL-PROPERTY"].AsString, obj.MyInternalPropertyNamed); Assert.AreEqual(doc["PRIVATE-PROPERTY"].AsString, obj.GetMyPrivatePropertyNamed()); Assert.AreEqual(doc["PROTECTED-PROPERTY"].AsString, obj.GetMyProtectedPropertyNamed()); Assert.AreEqual(obj.MyString, nobj.MyString); //Internal Assert.AreEqual(obj.MyInternalPropertyNamed, nobj.MyInternalPropertyNamed); Assert.AreEqual(obj.MyInternalPropertySerializable, nobj.MyInternalPropertySerializable); Assert.AreEqual(nobj.MyInternalPropertyNotSerializable,null); //Private Assert.AreEqual(obj.GetMyPrivatePropertyNamed(), nobj.GetMyPrivatePropertyNamed()); Assert.AreEqual(obj.GetMyPrivatePropertySerializable(), nobj.GetMyPrivatePropertySerializable()); Assert.AreEqual(nobj.GetMyPrivatePropertyNotSerializable(), null); //protected Assert.AreEqual(obj.GetMyProtectedPropertyNamed(), nobj.GetMyProtectedPropertyNamed()); Assert.AreEqual(obj.GetMyProtectedPropertySerializable(), nobj.GetMyProtectedPropertySerializable()); Assert.AreEqual(nobj.GetMyProtectedPropertyNotSerializable(), null); }
internal override bool FilterDocument(BsonDocument doc) { // must deserialize documento into class T to apply _where func var obj = _mapper.ToObject <T>(doc); return(_where(obj)); }
public void Mapper_Test() { var mapper = new BsonMapper(); mapper.UseLowerCaseDelimiter('_'); var obj = CreateModel(); var doc = mapper.ToDocument(obj); var json = JsonSerializer.Serialize(doc, true); var nobj = mapper.ToObject<MyClass>(doc); // compare object to document Assert.AreEqual(doc["_id"].AsInt32, obj.MyId); Assert.AreEqual(doc["MY-STRING"].AsString, obj.MyString); Assert.AreEqual(doc["my_guid"].AsGuid, obj.MyGuid); // compare 2 objects Assert.AreEqual(obj.MyId, nobj.MyId); Assert.AreEqual(obj.MyString, nobj.MyString); Assert.AreEqual(obj.MyProperty, nobj.MyProperty); Assert.AreEqual(obj.MyGuid, nobj.MyGuid); Assert.AreEqual(obj.MyDateTime, nobj.MyDateTime); Assert.AreEqual(obj.MyDateTimeNullable, nobj.MyDateTimeNullable); Assert.AreEqual(obj.MyIntNullable, nobj.MyIntNullable); Assert.AreEqual(obj.MyEnumProp, nobj.MyEnumProp); Assert.AreEqual(obj.MyChar, nobj.MyChar); Assert.AreEqual(obj.MyByte, nobj.MyByte); Assert.AreEqual(obj.MyDecimal, nobj.MyDecimal); Assert.AreEqual(obj.MyUri, nobj.MyUri); Assert.AreEqual(obj.MyNameValueCollection["key-1"], nobj.MyNameValueCollection["key-1"]); Assert.AreEqual(obj.MyNameValueCollection["KeyNumber2"], nobj.MyNameValueCollection["KeyNumber2"]); // list Assert.AreEqual(obj.MyStringArray[0], nobj.MyStringArray[0]); Assert.AreEqual(obj.MyStringArray[1], nobj.MyStringArray[1]); Assert.AreEqual(obj.MyDict[2], nobj.MyDict[2]); // interfaces Assert.AreEqual(obj.MyInterface.Name, nobj.MyInterface.Name); Assert.AreEqual(obj.MyListInterface[0].Name, nobj.MyListInterface[0].Name); Assert.AreEqual(obj.MyIListInterface[0].Name, nobj.MyIListInterface[0].Name); // objects Assert.AreEqual(obj.MyObjectString, nobj.MyObjectString); Assert.AreEqual(obj.MyObjectInt, nobj.MyObjectInt); Assert.AreEqual((obj.MyObjectImpl as MyImpl).Name, (nobj.MyObjectImpl as MyImpl).Name); Assert.AreEqual(obj.MyObjectList[0], obj.MyObjectList[0]); Assert.AreEqual(obj.MyObjectList[1], obj.MyObjectList[1]); Assert.AreEqual(obj.MyObjectList[3], obj.MyObjectList[3]); Assert.AreEqual(nobj.MyInternalProperty, null); }
public void MapInterfaces_Test() { var mapper = new BsonMapper(); var c1 = new MyClassWithInterface { Id = 1, Impl = new MyClassImpl { Name = "John Doe" } }; var c2 = new MyClassWithObject { Id = 1, Impl = new MyClassImpl { Name = "John Doe" } }; var c3 = new MyClassWithClassName { Id = 1, Impl = new MyClassImpl { Name = "John Doe" } }; var bson1 = mapper.ToDocument(c1); // add _type in Impl property var bson2 = mapper.ToDocument(c2); // add _type in Impl property var bson3 = mapper.ToDocument(c3); // do not add _type in Impl property Assert.AreEqual("UnitTest.MapperInterfaceTest+MyClassImpl, UnitTest", bson1["Impl"].AsDocument["_type"].AsString); Assert.AreEqual("UnitTest.MapperInterfaceTest+MyClassImpl, UnitTest", bson2["Impl"].AsDocument["_type"].AsString); Assert.AreEqual(false, bson3["Impl"].AsDocument.ContainsKey("_type")); var k1 = mapper.ToObject<MyClassWithInterface>(bson1); var k2 = mapper.ToObject<MyClassWithObject>(bson2); var k3 = mapper.ToObject<MyClassWithClassName>(bson3); Assert.AreEqual(c1.Impl.Name, k1.Impl.Name); Assert.AreEqual((c2.Impl as MyClassImpl).Name, (k2.Impl as MyClassImpl).Name); Assert.AreEqual(c3.Impl.Name, k3.Impl.Name); }