public void TestCamelCaseElementNameConvention() { var convention = new CamelCaseElementNameConvention(); var classMap = new BsonClassMap<TestClass>(); convention.Apply(classMap.MapMember(x => x.FirstName)); convention.Apply(classMap.MapMember(x => x.Age)); convention.Apply(classMap.MapMember(x => x._DumbName)); convention.Apply(classMap.MapMember(x => x.lowerCase)); Assert.AreEqual("firstName", classMap.GetMemberMap(x => x.FirstName).ElementName); Assert.AreEqual("age", classMap.GetMemberMap(x => x.Age).ElementName); Assert.AreEqual("_DumbName", classMap.GetMemberMap(x => x._DumbName).ElementName); Assert.AreEqual("lowerCase", classMap.GetMemberMap(x => x.lowerCase).ElementName); }
public void TestMapsAllTheReadAndWriteFieldsAndProperties() { var classMap = new BsonClassMap<TestClass>(); _subject.Apply(classMap); Assert.AreEqual(3, classMap.DeclaredMemberMaps.Count()); Assert.IsNotNull(classMap.GetMemberMap(x => x.Mapped1)); Assert.IsNotNull(classMap.GetMemberMap(x => x.Mapped2)); Assert.IsNotNull(classMap.GetMemberMap(x => x.Mapped3)); Assert.IsNull(classMap.GetMemberMap(x => x.NotMapped1)); Assert.IsNull(classMap.GetMemberMap(x => x.NotMapped2)); }
public void TestIsReadOnlyPropertyOfAPrivateSettableProperty() { var classMap = new BsonClassMap <TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("PrivateSettableProperty"); Assert.IsFalse(memberMap.IsReadOnly); }
public void TestIsReadOnlyPropertyOfAField() { var classMap = new BsonClassMap<TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Field"); Assert.IsFalse(memberMap.IsReadOnly); }
public void TestSetElementNameThrowsWhenElementNameIsNull() { var classMap = new BsonClassMap <TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Property"); Assert.Throws <ArgumentNullException>(() => { memberMap.SetElementName(null); }); }
public void TestReset() { var classMap = new BsonClassMap <TestClass>(cm => { var mm = cm.MapMember(c => c.Property); }); var memberMap = classMap.GetMemberMap(x => x.Property); memberMap.SetDefaultValue(42); memberMap.SetElementName("oops"); memberMap.SetIdGenerator(new GuidGenerator()); memberMap.SetIgnoreIfDefault(true); memberMap.SetIsRequired(true); memberMap.SetOrder(21); memberMap.SetSerializationOptions(new RepresentationSerializationOptions(BsonType.Int64)); memberMap.SetSerializer(new BsonInt64Serializer()); memberMap.SetShouldSerializeMethod(o => false); memberMap.Reset(); Assert.AreEqual(0, (int)memberMap.DefaultValue); Assert.AreEqual("Property", memberMap.ElementName); Assert.IsNull(memberMap.IdGenerator); Assert.IsFalse(memberMap.IgnoreIfDefault); Assert.IsFalse(memberMap.IgnoreIfNull); Assert.IsFalse(memberMap.IsRequired); Assert.AreEqual(int.MaxValue, memberMap.Order); Assert.IsNull(memberMap.SerializationOptions); Assert.IsNotInstanceOf <BsonInt64Serializer>(memberMap.GetSerializer(memberMap.MemberType)); Assert.IsNull(memberMap.ShouldSerializeMethod); }
public void TestMapField() { var classMap = new BsonClassMap<C>(cm => cm.MapField("f")); var memberMap = classMap.GetMemberMap("f"); Assert.IsNotNull(memberMap); Assert.AreEqual("f", memberMap.ElementName); Assert.AreEqual("f", memberMap.MemberName); }
public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) { var entityType = definition.EntityType; var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (var property in properties) { //Unmap fields with the "NotMappedAttribute" var notMappedAttribute = property.GetCustomAttribute <NotMappedAttribute>(); if (notMappedAttribute != null) { classMap.UnmapProperty(property.Name); continue; } //Remap fields with the "ColumnAttribute" var columnAttribute = property.GetCustomAttribute <ColumnAttribute>(); if (columnAttribute != null) { var mappedName = columnAttribute.Name; var memberMap = classMap.GetMemberMap(property.Name); memberMap?.SetElementName(mappedName); } } }
public void TestIsReadOnlyPropertyOfAField() { var classMap = new BsonClassMap <TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Field"); Assert.IsFalse(memberMap.IsReadOnly); }
public void GivenACamelCaseConventionPack_WhenAppliedToAClassInheritingFromIndexIdentityBase_ThenPropertyNamesComeOutAsCamelCase() { var convention = new CamelCaseElementNameConvention(); var classMap = new BsonClassMap <TestClass>(); convention.Apply(classMap.MapMember(x => x.FirstNames)); convention.Apply(classMap.MapMember(x => x.LastName)); convention.Apply(classMap.MapMember(x => x.Title)); convention.Apply(classMap.MapMember(x => x.Salutation)); Assert.Equal("firstNames", classMap.GetMemberMap(x => x.FirstNames).ElementName); Assert.Equal("lastName", classMap.GetMemberMap(x => x.LastName).ElementName); Assert.Equal("title", classMap.GetMemberMap(x => x.Title).ElementName); Assert.Equal("salutation", classMap.GetMemberMap(x => x.Salutation).ElementName); }
protected override void ConfigureClassMap(BsonClassMap <User> cm) { base.ConfigureClassMap(cm); cm.GetMemberMap(p => p.OrganizationIds).SetSerializationOptions(new ArraySerializationOptions(new RepresentationSerializationOptions(BsonType.ObjectId))); cm.GetMemberMap(c => c.IsActive).SetIgnoreIfDefault(true); cm.GetMemberMap(c => c.IsEmailAddressVerified).SetIgnoreIfDefault(true); cm.GetMemberMap(c => c.Password).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.PasswordResetToken).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.PasswordResetTokenExpiration).SetIgnoreIfDefault(true); cm.GetMemberMap(c => c.Salt).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.VerifyEmailAddressToken).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.VerifyEmailAddressTokenExpiration).SetIgnoreIfDefault(true); }
protected override void ConfigureClassMap(BsonClassMap <Project> cm) { base.ConfigureClassMap(cm); cm.GetMemberMap(c => c.Name).SetElementName(FieldNames.Name); cm.GetMemberMap(c => c.TimeZone).SetElementName(FieldNames.TimeZone); cm.GetMemberMap(c => c.Configuration).SetElementName(FieldNames.Configuration); cm.GetMemberMap(c => c.CustomContent).SetElementName(FieldNames.CustomContent).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.TotalEventCount).SetElementName(FieldNames.TotalEventCount); cm.GetMemberMap(c => c.LastEventDate).SetElementName(FieldNames.LastEventDate).SetIgnoreIfDefault(true); cm.GetMemberMap(c => c.NextSummaryEndOfDayTicks).SetElementName(FieldNames.NextSummaryEndOfDayTicks); cm.GetMemberMap(c => c.PromotedTabs).SetElementName(FieldNames.PromotedTabs).SetIgnoreIfNull(true).SetShouldSerializeMethod(obj => ((Project)obj).PromotedTabs.Any()); cm.GetMemberMap(c => c.NotificationSettings).SetElementName(FieldNames.NotificationSettings).SetIgnoreIfNull(true).SetShouldSerializeMethod(obj => ((Project)obj).NotificationSettings.Any()); }
public void TestMapField() { var classMap = new BsonClassMap <C>(cm => cm.MapField(c => c.F)); var memberMap = classMap.GetMemberMap("F"); Assert.IsNotNull(memberMap); Assert.AreEqual("F", memberMap.ElementName); Assert.AreEqual("F", memberMap.MemberName); }
public void TestMapProperty() { var classMap = new BsonClassMap <C>(cm => cm.MapProperty(c => c.P)); var memberMap = classMap.GetMemberMap("P"); Assert.IsNotNull(memberMap); Assert.AreEqual("P", memberMap.ElementName); Assert.AreEqual("P", memberMap.MemberName); }
public void TestSettingAProperty() { var instance = new TestClass(); var classMap = new BsonClassMap<TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Property"); memberMap.Setter(instance, 42); Assert.AreEqual(42, instance.Property); }
public void TestGettingAProperty() { var instance = new TestClass { Property = 42 }; var classMap = new BsonClassMap<TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Property"); int value = (int)memberMap.Getter(instance); Assert.AreEqual(42, value); }
public void TestOptsInMembers() { var convention = AttributeConventionPack.Instance; var classMap = new BsonClassMap<TestClass>(); new ConventionRunner(convention).Apply(classMap); Assert.AreEqual(1, classMap.DeclaredMemberMaps.Count()); Assert.AreEqual("fn", classMap.GetMemberMap("_firstName").ElementName); }
public void TestConventionProfileStillUsesDefaults() { var classMap = new BsonClassMap<A>(); classMap.AutoMap(); var memberMap = classMap.GetMemberMap(x => x.S); Assert.IsNotNull(memberMap); }
public void SetOrder_Y_1_called() { var cm = new BsonClassMap <C>(); cm.AutoMap(); cm.GetMemberMap("Y").SetOrder(1); cm.Freeze(); cm.AllMemberMaps.Select(m => m.MemberName).Should().Equal("Y", "Id", "X"); }
public void TestConventionProfileStillUsesDefaults() { var classMap = new BsonClassMap <A>(); classMap.AutoMap(); var memberMap = classMap.GetMemberMap(x => x.S); Assert.IsNotNull(memberMap); }
protected override void ConfigureClassMap(BsonClassMap <Organization> cm) { base.ConfigureClassMap(cm); cm.GetMemberMap(c => c.StripeCustomerId).SetElementName(FieldNames.StripeCustomerId).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.PlanId).SetElementName(FieldNames.PlanId).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.CardLast4).SetElementName(FieldNames.CardLast4).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.SubscribeDate).SetElementName(FieldNames.SubscribeDate).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.BillingChangeDate).SetElementName(FieldNames.BillingChangeDate).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.BillingChangedByUserId).SetElementName(FieldNames.BillingChangedByUserId).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.Usage).SetElementName(FieldNames.Usage).SetIgnoreIfNull(true).SetShouldSerializeMethod(obj => ((Organization)obj).Usage.Any()); cm.GetMemberMap(c => c.OverageHours).SetElementName(FieldNames.OverageHours).SetIgnoreIfNull(true).SetShouldSerializeMethod(obj => ((Organization)obj).OverageHours.Any()); }
public void TestOptsInMembers() { var convention = AttributeConventionPack.Instance; var classMap = new BsonClassMap <TestClass>(); new ConventionRunner(convention).Apply(classMap); Assert.Equal(1, classMap.DeclaredMemberMaps.Count()); Assert.Equal("fn", classMap.GetMemberMap("_firstName").ElementName); }
public void TestSettingAField() { var instance = new TestClass(); var classMap = new BsonClassMap <TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Field"); memberMap.Setter(instance, 42); Assert.AreEqual(42, instance.Field); }
public void TestMapMember() { var fieldInfo = typeof(C).GetField("f", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); var classMap = new BsonClassMap <C>(cm => cm.MapMember(fieldInfo)); var memberMap = classMap.GetMemberMap("f"); Assert.IsNotNull(memberMap); Assert.AreEqual("f", memberMap.ElementName); Assert.AreEqual("f", memberMap.MemberName); }
public void TestGettingAField() { var instance = new TestClass { Field = 42 }; var classMap = new BsonClassMap<TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Field"); int value = (int)memberMap.Getter(instance); Assert.Equal(42, value); }
public void TestSettingAPrivateSettableProperty() { var instance = new TestClass(); var classMap = new BsonClassMap <TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("PrivateSettableProperty"); memberMap.Setter(instance, 42); Assert.AreEqual(42, instance.PrivateSettableProperty); }
public void TestGettingAPrivateSettableProperty() { var instance = new TestClass(); var classMap = new BsonClassMap <TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("PrivateSettableProperty"); int value = (int)memberMap.Getter(instance); Assert.AreEqual(10, value); }
public static void Test_LoadMongoDetail_01() { Trace.WriteLine("Test_LoadMongoDetail_01"); Trace.WriteLine(); RapideDdl.InitMongoClassMap(); if (BsonClassMap.IsClassMapRegistered(typeof(RapideDdl_Base))) { BsonClassMap map = BsonClassMap.LookupClassMap(typeof(RapideDdl_Base)); Trace.WriteLine("change existing class map"); BsonMemberMap memberMap = map.GetMemberMap("infos"); memberMap.SetSerializationOptions(DictionarySerializationOptions.ArrayOfDocuments); } else { Trace.WriteLine("register class map"); BsonClassMap.RegisterClassMap <RapideDdl_Base>(cm => { cm.AutoMap(); cm.GetMemberMap(c => c.infos).SetSerializationOptions(DictionarySerializationOptions.ArrayOfDocuments); }); } Trace.WriteLine(); string query = "{ _id: 35105 }"; MongoCursor <BsonDocument> cursor = MongoCommand.GetDatabase(null, "dl").GetCollection("RapideDdl_Detail").zFind <BsonDocument>(new QueryDocument(BsonSerializer.Deserialize <BsonDocument>(query))); int i = 1; foreach (BsonDocument document in cursor) { BsonDocument document2 = (BsonDocument)document["download"]; RapideDdl_PostDetail postDetail = BsonSerializer.Deserialize <RapideDdl_PostDetail>(document2); //MongoDB.Bson.Serialization.IBsonSerializationOptions options; //MongoDB.Bson.Serialization.BsonSerializer.Serialize() //Serialization.Options //SerializationOptions //MongoDB.Bson.Serialization.Options.RepresentationSerializationOptions //document2 = postDetail.ToBsonDocument(new DictionarySerializationOptions(DictionaryRepresentation.ArrayOfDocuments)); //DictionarySerializationOptions. //BsonClassMap.RegisterClassMap<RapideDdl_PostDetail>(cm => { cm.MapProperty(c => c.SomeProperty); cm.MapProperty(c => c.AnotherProperty); }); //document2 = postDetail.ToBsonDocument(); Trace.WriteLine("document no {0}", i++); //DocumentSerializationOptions options = new DocumentSerializationOptions(); //Trace.WriteLine(postDetail.ToJson(new DictionarySerializationOptions(DictionaryRepresentation.ArrayOfDocuments))); //RapideDdl_PostDetail RapideDdl_Base //BsonClassMap<RapideDdl_Base> map = BsonClassMap.LookupClassMap(typeof(RapideDdl_Base)); //Trace.WriteLine(document.zToJson()); Trace.WriteLine(postDetail.zToJson()); Trace.WriteLine(); } }
public void TestMappingUsesMemberSerializationOptionsConventionDoesNotMatchWrongProperty() { var pack = new ConventionPack(); pack.Add(new MemberSerializationOptionsConvention(typeof(ObjectId), new RepresentationSerializationOptions(BsonType.JavaScriptWithScope))); ConventionRegistry.Register("test", pack, t => t == typeof(A)); var classMap = new BsonClassMap<A>(cm => cm.AutoMap()); var options = classMap.GetMemberMap("NoMatch").SerializationOptions; Assert.IsNull(options); }
public void TestIsReadOnlyPropertyOfAReadOnlyProperty() { var classMap = new BsonClassMap <TestClass>(cm => { cm.AutoMap(); cm.MapMember(c => c.ReadOnlyProperty); }); var memberMap = classMap.GetMemberMap("ReadOnlyProperty"); Assert.IsTrue(memberMap.IsReadOnly); }
public void TestMappingUsesMemberDefaultValueConventionDoesNotMatchWrongProperty() { var pack = new ConventionPack(); pack.Add(new MemberDefaultValueConvention(typeof(int), 1)); ConventionRegistry.Register("test", pack, t => t == typeof(A)); var classMap = new BsonClassMap<A>(cm => cm.AutoMap()); var defaultValue = classMap.GetMemberMap("NoMatch").DefaultValue; Assert.Equal(0L, defaultValue); }
public static string GetElementName <T>(string propertyName) { BsonClassMap _classMap = BsonClassMap.LookupClassMap(typeof(T)); BsonMemberMap _memberMap = _classMap.GetMemberMap(propertyName); if (_memberMap == null) { throw new ArgumentNullException(string.Format("The element name for property {0} could not be found for class type {1}.", propertyName, typeof(T).FullName)); } return(_memberMap.ElementName); }
public void TestSettingAReadOnlyField() { var instance = new TestClass(); var classMap = new BsonClassMap <TestClass>(cm => { cm.AutoMap(); cm.MapMember(c => c.ReadOnlyField); }); var memberMap = classMap.GetMemberMap("ReadOnlyField"); memberMap.Setter(instance, 12); }
public void TestMappingUsesMemberDefaultValueConvention() { var pack = new ConventionPack(); pack.Add(new MemberDefaultValueConvention(typeof(int), 1)); ConventionRegistry.Register("test", pack, t => t == typeof(A)); var classMap = new BsonClassMap<A>(cm => cm.AutoMap()); var defaultValue = classMap.GetMemberMap("Match").DefaultValue; Assert.IsInstanceOf<int>(defaultValue); Assert.AreEqual(1, defaultValue); }
public void TestMappingUsesMemberDefaultValueConventionDoesNotOverrideAttribute() { var pack = new ConventionPack(); pack.Add(new MemberDefaultValueConvention(typeof(int), 1)); ConventionRegistry.Register("test", pack, t => t == typeof(B)); var classMap = new BsonClassMap<B>(cm => cm.AutoMap()); var defaultValue = classMap.GetMemberMap("Match").DefaultValue; Assert.IsType<int>(defaultValue); Assert.Equal(2, defaultValue); }
public void TestGettingAField() { var instance = new TestClass { Field = 42 }; var classMap = new BsonClassMap <TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Field"); int value = (int)memberMap.Getter(instance); Assert.AreEqual(42, value); }
public void TestMappingUsesMemberSerializationOptionsConventionDoesNotOverrideAttribute() { var pack = new ConventionPack(); pack.Add(new MemberSerializationOptionsConvention(typeof(ObjectId), new RepresentationSerializationOptions(BsonType.JavaScriptWithScope))); ConventionRegistry.Register("test", pack, t => t == typeof(B)); var classMap = new BsonClassMap<B>(cm => cm.AutoMap()); var options = classMap.GetMemberMap("Match").SerializationOptions; Assert.IsInstanceOf<RepresentationSerializationOptions>(options); Assert.AreEqual(BsonType.ObjectId, ((RepresentationSerializationOptions)options).Representation); }
public void TestGettingAProperty() { var instance = new TestClass { Property = 42 }; var classMap = new BsonClassMap <TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Property"); int value = (int)memberMap.Getter(instance); Assert.Equal(42, value); }
public void TestMappingUsesMemberSerializationOptionsConventionDoesNotMatchWrongProperty() { var pack = new ConventionPack(); pack.Add(new MemberSerializationOptionsConvention(typeof(ObjectId), new RepresentationSerializationOptions(BsonType.JavaScriptWithScope))); ConventionRegistry.Register("test", pack, t => t == typeof(A)); var classMap = new BsonClassMap <A>(cm => cm.AutoMap()); var options = classMap.GetMemberMap("NoMatch").SerializationOptions; Assert.IsNull(options); }
public void TestMappingUsesMemberDefaultValueConventionDoesNotMatchWrongProperty() { var pack = new ConventionPack(); pack.Add(new MemberDefaultValueConvention(typeof(int), 1)); ConventionRegistry.Register("test", pack, t => t == typeof(A)); var classMap = new BsonClassMap <A>(cm => cm.AutoMap()); var defaultValue = classMap.GetMemberMap("NoMatch").DefaultValue; Assert.Equal(0L, defaultValue); }
protected override void ConfigureClassMap(BsonClassMap <Organization> cm) { base.ConfigureClassMap(cm); cm.GetMemberMap(c => c.StripeCustomerId).SetElementName(FieldNames.StripeCustomerId).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.PlanId).SetElementName(FieldNames.PlanId).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.CardLast4).SetElementName(FieldNames.CardLast4).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.SubscribeDate).SetElementName(FieldNames.SubscribeDate).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.BillingChangeDate).SetElementName(FieldNames.BillingChangeDate).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.BillingChangedByUserId).SetElementName(FieldNames.BillingChangedByUserId).SetIgnoreIfNull(true); cm.GetMemberMap(c => c.OverageDays).SetElementName(FieldNames.OverageDays).SetIgnoreIfNull(true); }
public void TestConventionProfileStillUsesDefaults() { #pragma warning disable 618 var conventions = new ConventionProfile(); conventions.SetElementNameConvention(new CamelCaseElementNameConvention()); BsonClassMap.RegisterConventions(conventions, t => t == typeof(A)); #pragma warning restore 618 var classMap = new BsonClassMap<A>(); classMap.AutoMap(); var memberMap = classMap.GetMemberMap(x => x.S); Assert.IsNotNull(memberMap); }
public void TestMappingUsesMemberDefaultValueConventionDoesNotOverrideAttribute() { var pack = new ConventionPack(); pack.Add(new MemberDefaultValueConvention(typeof(int), 1)); ConventionRegistry.Register("test", pack, t => t == typeof(B)); var classMap = new BsonClassMap <B>(cm => cm.AutoMap()); var defaultValue = classMap.GetMemberMap("Match").DefaultValue; Assert.IsType <int>(defaultValue); Assert.Equal(2, defaultValue); }
public void TestSettingAReadOnlyProperty() { var instance = new TestClass { Property = 10 }; var classMap = new BsonClassMap <TestClass>(cm => { cm.AutoMap(); cm.MapMember(c => c.ReadOnlyProperty); }); var memberMap = classMap.GetMemberMap("ReadOnlyProperty"); memberMap.Setter(instance, 12); }
public void TestGettingAReadOnlyField() { var instance = new TestClass(); var classMap = new BsonClassMap <TestClass>(cm => { cm.AutoMap(); cm.MapMember(c => c.ReadOnlyField); }); var memberMap = classMap.GetMemberMap("ReadOnlyField"); int value = (int)memberMap.Getter(instance); Assert.AreEqual(13, value); }
public void TestSettingAReadOnlyField() { var instance = new TestClass(); var classMap = new BsonClassMap<TestClass>(cm => { cm.AutoMap(); cm.MapMember(c => c.ReadOnlyField); }); var memberMap = classMap.GetMemberMap("ReadOnlyField"); memberMap.Setter(instance, 12); }
public void TestThatPrivateSettersAreValid() { var classMap = new BsonClassMap<TestClass>(c => c.AutoMap()); var setter = classMap.GetMemberMap(x => x.PrivateSetter).Setter; }
public void TestMapMember() { var fieldInfo = typeof(C).GetField("f", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); var classMap = new BsonClassMap<C>(cm => cm.MapMember(fieldInfo)); var memberMap = classMap.GetMemberMap("f"); Assert.IsNotNull(memberMap); Assert.AreEqual("f", memberMap.ElementName); Assert.AreEqual("f", memberMap.MemberName); }
public void TestSettingAField() { var instance = new TestClass(); var classMap = new BsonClassMap<TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Field"); memberMap.Setter(instance, 42); Assert.Equal(42, instance.Field); }
public void TestGettingAReadOnlyField() { var instance = new TestClass(); var classMap = new BsonClassMap<TestClass>(cm => { cm.AutoMap(); cm.MapMember(c => c.ReadOnlyField); }); var memberMap = classMap.GetMemberMap("ReadOnlyField"); int value = (int)memberMap.Getter(instance); Assert.Equal(13, value); }
public void TestIsReadOnlyPropertyOfAPrivateSettableProperty() { var classMap = new BsonClassMap<TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("PrivateSettableProperty"); Assert.IsFalse(memberMap.IsReadOnly); }
public void TestGettingAReadOnlyProperty() { var instance = new TestClass { Property = 10 }; var classMap = new BsonClassMap<TestClass>(cm => { cm.AutoMap(); cm.MapMember(c => c.ReadOnlyProperty); }); var memberMap = classMap.GetMemberMap("ReadOnlyProperty"); int value = (int)memberMap.Getter(instance); Assert.AreEqual(11, value); }
public void TestSetElementNameThrowsWhenElementNameIsNull() { var classMap = new BsonClassMap<TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("Property"); Assert.Throws<ArgumentNullException>(() => { memberMap.SetElementName(null); }); }
public void TestIsReadOnlyPropertyOfAReadOnlyProperty() { var classMap = new BsonClassMap<TestClass>(cm => { cm.AutoMap(); cm.MapMember(c => c.ReadOnlyProperty); }); var memberMap = classMap.GetMemberMap("ReadOnlyProperty"); Assert.IsTrue(memberMap.IsReadOnly); }
public void TestReset() { var classMap = new BsonClassMap<TestClass>(cm => { var mm = cm.MapMember(c => c.Property); }); var originalSerializer = new Int32Serializer(); var memberMap = classMap.GetMemberMap(x => x.Property); memberMap.SetDefaultValue(42); memberMap.SetElementName("oops"); memberMap.SetIdGenerator(new GuidGenerator()); memberMap.SetIgnoreIfDefault(true); memberMap.SetIsRequired(true); memberMap.SetOrder(21); memberMap.SetSerializer(originalSerializer); memberMap.SetShouldSerializeMethod(o => false); memberMap.Reset(); Assert.AreEqual(0, (int)memberMap.DefaultValue); Assert.AreEqual("Property", memberMap.ElementName); Assert.IsNull(memberMap.IdGenerator); Assert.IsFalse(memberMap.IgnoreIfDefault); Assert.IsFalse(memberMap.IgnoreIfNull); Assert.IsFalse(memberMap.IsRequired); Assert.AreEqual(int.MaxValue, memberMap.Order); Assert.AreNotSame(originalSerializer, memberMap.GetSerializer()); Assert.IsNull(memberMap.ShouldSerializeMethod); }
public void TestSettingAReadOnlyProperty() { var instance = new TestClass { Property = 10 }; var classMap = new BsonClassMap<TestClass>(cm => { cm.AutoMap(); cm.MapMember(c => c.ReadOnlyProperty); }); var memberMap = classMap.GetMemberMap("ReadOnlyProperty"); memberMap.Setter(instance, 12); }
public void TestGettingAPrivateSettableProperty() { var instance = new TestClass(); var classMap = new BsonClassMap<TestClass>(cm => cm.AutoMap()); var memberMap = classMap.GetMemberMap("PrivateSettableProperty"); int value = (int)memberMap.Getter(instance); Assert.Equal(10, value); }
public void TestReset() { var classMap = new BsonClassMap<TestClass>(cm => { var mm = cm.MapMember(c => c.Property); }); var memberMap = classMap.GetMemberMap(x => x.Property); memberMap.SetDefaultValue(42); memberMap.SetElementName("oops"); memberMap.SetIdGenerator(new GuidGenerator()); memberMap.SetIgnoreIfDefault(true); memberMap.SetIsRequired(true); memberMap.SetOrder(21); memberMap.SetSerializationOptions(new RepresentationSerializationOptions(BsonType.Int64)); memberMap.SetSerializer(new BsonInt64Serializer()); memberMap.SetShouldSerializeMethod(o => false); memberMap.Reset(); Assert.AreEqual(0, (int)memberMap.DefaultValue); Assert.AreEqual("Property", memberMap.ElementName); Assert.IsNull(memberMap.IdGenerator); Assert.IsFalse(memberMap.IgnoreIfDefault); Assert.IsFalse(memberMap.IgnoreIfNull); Assert.IsFalse(memberMap.IsRequired); Assert.AreEqual(int.MaxValue, memberMap.Order); Assert.IsNull(memberMap.SerializationOptions); Assert.IsNotInstanceOf<BsonInt64Serializer>(memberMap.GetSerializer(memberMap.MemberType)); Assert.IsNull(memberMap.ShouldSerializeMethod); }
public void TestMapProperty() { var classMap = new BsonClassMap<C>(cm => cm.MapProperty(c => c.P)); var memberMap = classMap.GetMemberMap("P"); Assert.IsNotNull(memberMap); Assert.AreEqual("P", memberMap.ElementName); Assert.AreEqual("P", memberMap.MemberName); }
public void TestSettingAReadOnlyProperty() { var instance = new TestClass { Property = 10 }; var classMap = new BsonClassMap<TestClass>(cm => { cm.AutoMap(); cm.MapMember(c => c.ReadOnlyProperty); }); var memberMap = classMap.GetMemberMap("ReadOnlyProperty"); var ex = Record.Exception(() => memberMap.Setter(instance, 12)); var expectedMessage = "The property 'System.Int32 ReadOnlyProperty' of class 'MongoDB.Bson.Tests.Serialization.BsonMemberMapTests+TestClass' has no 'set' accessor. To avoid this exception, call IsReadOnly to ensure that setting a value is allowed."; Assert.IsType<BsonSerializationException>(ex); Assert.Equal(expectedMessage, ex.Message); }