public void MapsNestedStandardPropertyModel() { EntityMapping.AddMappingProcessor(new NestedTypeProcessor()); Assert.IsFalse(BsonClassMap.IsClassMapRegistered(typeof(PropertyNestedModel))); EntityMapping.RegisterType(typeof(PropertyBaseModel)); Assert.IsTrue(BsonClassMap.IsClassMapRegistered(typeof(PropertyNestedModel))); }
public void ObeysColumnAttributeRemap() { EntityMapping.AddMappingProcessor(new MappedPropertiesProcessor()); EntityMapping.AddMappingProcessor(new ClassMapPropertiesProcessor()); var definition = EntityMapping.RegisterType(typeof(ColumnAttributePropertyModel)); Assert.IsTrue(definition.Properties.Any(p => p.ElementName == "CustomPropertyName")); }
public void ObeysNotMappedAttribute() { EntityMapping.AddMappingProcessor(new MappedPropertiesProcessor()); EntityMapping.AddMappingProcessor(new ClassMapPropertiesProcessor()); var definition = EntityMapping.RegisterType(typeof(NotMappedPropertiesModel)); Assert.IsFalse(definition.Properties.Any(p => p.ElementName == "NotMapped")); }
public void AdapterRequiresParameterlessConstructor() { EntityMapping.AddMappingProcessor(new CollectionNameProcessor()); EntityMapping.AddMappingProcessor(new PropertyMappingProcessor()); EntityMapping.AddMappingProcessor(new EntityIdProcessor()); EntityMapping.AddMappingProcessor(new MappingAdapterProcessor()); Assert.ThrowsException <MissingMethodException>(() => EntityMapping.RegisterType(typeof(AdapterTestModelConstructor))); }
public void MapsNestedCollectionPropertyModel() { EntityMapping.AddMappingProcessor(new PropertyMappingProcessor()); EntityMapping.AddMappingProcessor(new NestedTypeProcessor()); Assert.IsFalse(BsonClassMap.IsClassMapRegistered(typeof(CollectionNestedModel))); EntityMapping.RegisterType(typeof(CollectionBaseModel)); Assert.IsTrue(BsonClassMap.IsClassMapRegistered(typeof(CollectionNestedModel))); }
public void IdMapsOnAttribute() { EntityMapping.AddMappingProcessor(new PropertyMappingProcessor()); EntityMapping.AddMappingProcessor(new EntityIdProcessor()); var definition = EntityMapping.RegisterType(typeof(IdByAttributeTestModel)); Assert.AreEqual("MyCustomId", definition.GetIdName()); }
public void AdapterRequiresIMappingProcessor() { EntityMapping.AddMappingProcessor(new CollectionNameProcessor()); EntityMapping.AddMappingProcessor(new PropertyMappingProcessor()); EntityMapping.AddMappingProcessor(new EntityIdProcessor()); EntityMapping.AddMappingProcessor(new MappingAdapterProcessor()); Assert.ThrowsException <ArgumentException>(() => EntityMapping.RegisterType(typeof(AdapterTestModelNoInterface))); }
public void IdentifyRelationshipsWithOtherIdTypes() { EntityMapping.AddMappingProcessor(new PropertyMappingProcessor()); EntityMapping.AddMappingProcessor(new EntityRelationshipProcessor()); var relationships = EntityMapping.RegisterType(typeof(BaseVariedIdModel)).Relationships; Assert.AreEqual(2, relationships.Count()); }
public void NavigationPropertiesUnmap() { EntityMapping.AddMappingProcessor(new PropertyMappingProcessor()); EntityMapping.AddMappingProcessor(new EntityRelationshipProcessor()); var definition = EntityMapping.RegisterType(typeof(BaseEntityModel)); Assert.IsFalse(definition.Properties.Any(e => e.FullPath == "CreatedBy" || e.FullPath == "UpdatedBy")); }
public void ValidInversePropertyMapping() { EntityMapping.AddMappingProcessor(new PropertyMappingProcessor()); EntityMapping.AddMappingProcessor(new EntityRelationshipProcessor()); var relationships = EntityMapping.RegisterType(typeof(ValidInversePropertyModel)).Relationships; Assert.IsTrue(relationships.Any(r => r.IsCollection && r.IdProperty.PropertyInfo.Name == "SecondaryId")); }
public void ObjectIdGeneratorOnObjectIdProperty() { EntityMapping.AddMappingProcessor(new EntityIdProcessor()); EntityMapping.RegisterType(typeof(ObjectIdGeneratorTestModel)); var classMap = BsonClassMap.GetRegisteredClassMaps() .Where(cm => cm.ClassType == typeof(ObjectIdGeneratorTestModel)).FirstOrDefault(); Assert.AreEqual(typeof(ObjectIdGenerator), classMap.IdMemberMap.IdGenerator?.GetType()); }
public void CollectionNameAndSchemaFromAttribute() { EntityMapping.AddMappingProcessor(new CollectionNameProcessor()); var definition = EntityMapping.RegisterType(typeof(CustomCollectionAndSchemaModel)); Assert.AreEqual("CustomSchema.CustomCollection", definition.CollectionName); definition = EntityMapping.RegisterType(typeof(EntityBucket <CustomCollectionAndSchemaModel, string>)); Assert.AreEqual("CustomSchema.CustomCollection", definition.CollectionName); }
public void CollectionNameFromClassName() { EntityMapping.AddMappingProcessor(new CollectionNameProcessor()); var definition = EntityMapping.RegisterType(typeof(DefaultCollectionNameModel)); Assert.AreEqual("DefaultCollectionNameModel", definition.CollectionName); definition = EntityMapping.RegisterType(typeof(EntityBucket <DefaultCollectionNameModel, string>)); Assert.AreEqual("DefaultCollectionNameModel", definition.CollectionName); }
public void ExplicitKeyOverridesImplicitId() { EntityMapping.AddMappingProcessor(new EntityIdProcessor()); EntityMapping.RegisterType(typeof(ExplicitKeyOverridesImplicitIdModel)); var classMap = BsonClassMap.GetRegisteredClassMaps() .Where(cm => cm.ClassType == typeof(ExplicitKeyOverridesImplicitIdModel)).FirstOrDefault(); Assert.AreEqual("ActualKey", classMap.IdMemberMap.MemberName); }
public void NotTypeDiscoverySerializerWhenAttributeNotDefined() { EntityMapping.AddMappingProcessor(new TypeDiscoveryProcessor()); EntityMapping.AddMappingProcessor(new ClassMapPropertiesProcessor()); EntityMapping.RegisterType(typeof(NoTypeDiscoveryAttributeModel)); var serializer = BsonSerializer.LookupSerializer <NoTypeDiscoveryAttributeModel>(); Assert.AreNotEqual(typeof(TypeDiscoverySerializer <>), serializer.GetType().GetGenericTypeDefinition()); }
public void AdapterOverridesAttributes() { EntityMapping.AddMappingProcessor(new CollectionNameProcessor()); EntityMapping.AddMappingProcessor(new PropertyMappingProcessor()); EntityMapping.AddMappingProcessor(new EntityIdProcessor()); EntityMapping.AddMappingProcessor(new MappingAdapterProcessor()); var definition = EntityMapping.RegisterType(typeof(AdapterTestModel)); Assert.AreEqual("Custom", definition.CollectionName); Assert.AreEqual(1, definition.Indexes.Count()); }
public void ParentClassIsMapped() { EntityMapping.AddMappingProcessor(new HierarchyProcessor()); Assert.IsFalse(BsonClassMap.IsClassMapRegistered(typeof(ChildTestModel))); Assert.IsFalse(BsonClassMap.IsClassMapRegistered(typeof(ParentTestModel))); EntityMapping.RegisterType(typeof(ChildTestModel)); Assert.IsTrue(BsonClassMap.IsClassMapRegistered(typeof(ChildTestModel))); Assert.IsTrue(BsonClassMap.IsClassMapRegistered(typeof(ParentTestModel))); }
public void ForeignKeyAttributeOnNavigationProperty() { EntityMapping.AddMappingProcessor(new PropertyMappingProcessor()); EntityMapping.AddMappingProcessor(new EntityRelationshipProcessor()); var definition = EntityMapping.RegisterType(typeof(BaseEntityModel)); var relationships = definition.Relationships; var attributeOnIdRelationship = relationships.Where(r => r.IdProperty.ElementName == "UpdatedById").FirstOrDefault(); Assert.IsFalse(attributeOnIdRelationship.IsCollection); Assert.AreEqual(typeof(UserEntityModel), attributeOnIdRelationship.EntityType); Assert.AreEqual("UpdatedBy", attributeOnIdRelationship.NavigationProperty.ElementName); }
public void DeserializeChildTypeDiscoveryForRootEntity() { EntityMapping.AddMappingProcessor(new TypeDiscoveryProcessor()); EntityMapping.RegisterType(typeof(KnownBaseModel)); var document = new BsonDocument { { "_t", "UnknownChildModel" } }; var deserializedResult = BsonSerializer.Deserialize <KnownBaseModel>(document); Assert.IsInstanceOfType(deserializedResult, typeof(UnknownChildModel)); }
public void DeserializeGrandChildTypeDiscoveryForRootEntity() { EntityMapping.AddMappingProcessor(new TypeDiscoveryProcessor()); EntityMapping.RegisterType(typeof(KnownBaseModel)); var document = new BsonDocument { { "_t", new BsonArray(new [] { "KnownBaseModel", "UnknownChildModel", "UnknownGrandChildModel" }) } }; var deserializedResult = BsonSerializer.Deserialize <KnownBaseModel>(document); Assert.AreEqual(typeof(UnknownGrandChildModel), deserializedResult.GetType()); }
public void AccessToDeclaredProperty() { EntityMapping.AddMappingProcessor(new HierarchyProcessor()); EntityMapping.AddMappingProcessor(new ClassMapPropertiesProcessor()); var definition = EntityMapping.RegisterType(typeof(ChildTestModel)); var mappedProperties = definition.GetAllProperties(); Assert.IsTrue(mappedProperties.Any(p => p.ElementName == "DeclaredProperty")); var inherittedProperties = definition.GetInheritedProperties(); Assert.IsFalse(inherittedProperties.Any(p => p.ElementName == "DeclaredProperty")); }
public void ObeysIgnoreExtraElementsAttribute() { EntityMapping.AddMappingProcessor(new ExtraElementsProcessor()); EntityMapping.RegisterType(typeof(IgnoreExtraElementsModel)); var classMap = BsonClassMap.GetRegisteredClassMaps() .Where(cm => cm.ClassType == typeof(IgnoreExtraElementsModel)).FirstOrDefault(); Assert.IsTrue(classMap.IgnoreExtraElements); EntityMapping.RegisterType(typeof(ExtraElementsAttrModel)); classMap = BsonClassMap.GetRegisteredClassMaps() .Where(cm => cm.ClassType == typeof(ExtraElementsAttrModel)).FirstOrDefault(); Assert.IsFalse(classMap.IgnoreExtraElements); }
public void DeserializeNullForUnknownPropertyType() { EntityMapping.AddMappingProcessor(new TypeDiscoveryProcessor()); EntityMapping.RegisterType(typeof(UnknownPropertyTypeSerializationModel)); var document = new BsonDocument { { "_t", "UnknownPropertyTypeSerializationModel" }, { "UnknownPropertyType", BsonNull.Value } }; var deserializedResult = BsonSerializer.Deserialize <UnknownPropertyTypeSerializationModel>(document); Assert.IsNull(deserializedResult.UnknownPropertyType); }
public void AccessToInherittedProperty() { EntityMapping.AddMappingProcessor(new HierarchyProcessor()); EntityMapping.AddMappingProcessor(new PropertyMappingProcessor()); var definition = EntityMapping.RegisterType(typeof(ChildTestModel)); var allProperties = definition.GetAllProperties(); Assert.IsTrue(allProperties.Any(p => p.ElementName == "Id")); var declaredProperties = definition.Properties; Assert.IsFalse(declaredProperties.Any(p => p.ElementName == "Id")); }
public void ObeysExtraElementsAttribute() { EntityMapping.AddMappingProcessor(new ExtraElementsProcessor()); EntityMapping.RegisterType(typeof(ExtraElementsAttrModel)); var classMap = BsonClassMap.GetRegisteredClassMaps() .Where(cm => cm.ClassType == typeof(ExtraElementsAttrModel)).FirstOrDefault(); Assert.AreEqual("AdditionalElements", classMap.ExtraElementsMemberMap.ElementName); EntityMapping.RegisterType(typeof(IgnoreExtraElementsModel)); classMap = BsonClassMap.GetRegisteredClassMaps() .Where(cm => cm.ClassType == typeof(IgnoreExtraElementsModel)).FirstOrDefault(); Assert.AreEqual(null, classMap.ExtraElementsMemberMap); }
public void DeserializeBooleanForUnknownPropertyType() { EntityMapping.AddMappingProcessor(new TypeDiscoveryProcessor()); EntityMapping.RegisterType(typeof(UnknownPropertyTypeSerializationModel)); var document = new BsonDocument { { "_t", "UnknownPropertyTypeSerializationModel" }, { "UnknownPropertyType", new BsonBoolean(true) } }; var deserializedResult = BsonSerializer.Deserialize <UnknownPropertyTypeSerializationModel>(document); Assert.IsInstanceOfType(deserializedResult.UnknownPropertyType, typeof(bool)); }
public void DeserializeWithoutTypeDiscovery() { EntityMapping.AddMappingProcessor(new TypeDiscoveryProcessor()); EntityMapping.RegisterType(typeof(KnownBaseModel)); TypeDiscoverySerializationProvider.Instance.Enabled = false; var document = new BsonDocument { { "_t", "UnknownChildModel" } }; var deserializedResult = BsonSerializer.Deserialize <KnownBaseModel>(document); Assert.IsNotInstanceOfType(deserializedResult, typeof(UnknownChildModel)); TypeDiscoverySerializationProvider.Instance.Enabled = true; }
public void IdentifyCollectionRelationships() { EntityMapping.AddMappingProcessor(new ClassMapPropertiesProcessor()); EntityMapping.AddMappingProcessor(new EntityRelationshipProcessor()); var definition = EntityMapping.RegisterType(typeof(CollectionMappingModel)); var relationships = definition.Relationships; Assert.IsTrue(relationships.All(r => r.IsCollection)); var relationship = relationships.Where(r => r.NavigationProperty.PropertyInfo.Name == "StringModelEntities").FirstOrDefault(); var stringIdModelDefinition = EntityMapping.GetOrCreateDefinition(typeof(StringIdModel)); Assert.IsTrue(relationship.IsCollection); Assert.AreEqual(typeof(StringIdModel), relationship.EntityType); Assert.IsTrue(stringIdModelDefinition.GetProperty("Id").Equals(relationship.IdProperty)); Assert.IsTrue(definition.GetProperty("StringModelEntities").Equals(relationship.NavigationProperty)); }