public void ToDictionary_AppliesMappingToAllProperties_IfInstanceIsNotNull() { // Arrange EdmEntityType entityType = new EdmEntityType("NS", "Name"); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); EdmModel model = new EdmModel(); model.AddElement(entityType); model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); SelectExpandWrapper <TestEntity> testWrapper = new SelectExpandWrapper <TestEntity> { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model) }; Mock <IPropertyMapper> mapperMock = new Mock <IPropertyMapper>(); mapperMock.Setup(m => m.MapProperty("SampleProperty")).Returns("Sample"); Func <IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider = (IEdmModel m, IEdmStructuredType t) => mapperMock.Object; // Act var result = testWrapper.ToDictionary(mapperProvider); // Assert Assert.Equal(42, result["Sample"]); }
public void ToDictionary_Throws_IfMapperProvider_ReturnsNullPropertyMapper() { // Arrange EdmEntityType entityType = new EdmEntityType("NS", "Name"); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); EdmModel model = new EdmModel(); model.AddElement(entityType); model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); SelectExpandWrapper <TestEntity> wrapper = new SelectExpandWrapper <TestEntity> { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model) }; Func <IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider = (IEdmModel m, IEdmStructuredType t) => null; // Act & Assert Assert.Throws <InvalidOperationException>(() => wrapper.ToDictionary(mapperProvider: mapperProvider), "The mapper provider must return a valid 'System.Web.Http.OData.Query.IPropertyMapper' instance for the given 'NS.Name' IEdmType."); }
public void ToDictionary_Throws_IfMappingIsNullOrEmpty_ForAGivenProperty(string propertyMapping) { // Arrange EdmEntityType entityType = new EdmEntityType("NS", "Name"); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); EdmModel model = new EdmModel(); model.AddElement(entityType); model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); SelectExpandWrapper <TestEntity> testWrapper = new SelectExpandWrapper <TestEntity> { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model) }; Mock <IPropertyMapper> mapperMock = new Mock <IPropertyMapper>(); mapperMock.Setup(m => m.MapProperty("SampleProperty")).Returns(propertyMapping); Func <IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider = (IEdmModel m, IEdmStructuredType t) => mapperMock.Object; // Act & Assert Assert.Throws <InvalidOperationException>(() => testWrapper.ToDictionary(mapperProvider), "The key mapping for the property 'SampleProperty' can't be null or empty."); }
public void GetModelID_AndThen_GetModel_ReturnsOriginalModel() { EdmModel model = new EdmModel(); string modelID = ModelContainer.GetModelID(model); Assert.Same(model, ModelContainer.GetModel(modelID)); }
public void ToDictionary_ContainsAllProperties_FromContainer() { // Arrange EdmEntityType entityType = new EdmEntityType("NS", "Name"); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); EdmModel model = new EdmModel(); model.AddElement(entityType); model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); MockPropertyContainer container = new MockPropertyContainer(); container.Properties.Add("Property", 42); SelectExpandWrapper <TestEntity> wrapper = new SelectExpandWrapper <TestEntity> { Container = container, ModelID = ModelContainer.GetModelID(model) }; // Act var result = wrapper.ToDictionary(); // Assert Assert.Equal(42, result["Property"]); }
public void GetModelID_Returns_DifferentIDForDifferentModels() { EdmModel model1 = new EdmModel(); EdmModel model2 = new EdmModel(); Assert.NotEqual(ModelContainer.GetModelID(model1), ModelContainer.GetModelID(model2)); }
public void GetEdmType_ThrowsODataException_IfTypeFromTypeNameIsNotFoundInModel() { _modelID = ModelContainer.GetModelID(EdmCoreModel.Instance); SelectExpandWrapper <int> wrapper = new SelectExpandWrapper <int> { TypeName = _model.Customer.FullName(), ModelID = _modelID }; Assert.Throws <InvalidOperationException>( () => wrapper.GetEdmType(), "Cannot find the entity type 'NS.Customer' in the model."); }
public SelectExpandBinder(ODataQuerySettings settings, SelectExpandQueryOption selectExpandQuery) { Contract.Assert(settings != null); Contract.Assert(selectExpandQuery != null); Contract.Assert(selectExpandQuery.Context != null); Contract.Assert(selectExpandQuery.Context.Model != null); Contract.Assert(settings.HandleNullPropagation != HandleNullPropagationOption.Default); _selectExpandQuery = selectExpandQuery; _context = selectExpandQuery.Context; _model = _context.Model; _modelID = ModelContainer.GetModelID(_model); _settings = settings; }
public void ToDictionary_ContainsAllStructuralProperties_IfInstanceIsNotNull() { // Arrange EdmModel model = new EdmModel(); EdmEntityType entityType = new EdmEntityType("NS", "Name"); model.AddElement(entityType); model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); SelectExpandWrapper <TestEntity> testWrapper = new SelectExpandWrapper <TestEntity> { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model) }; // Act var result = testWrapper.ToDictionary(); // Assert Assert.Equal(42, result["SampleProperty"]); }
public SelectExpandWrapperTest() { _model = new CustomersModelWithInheritance(); _modelID = ModelContainer.GetModelID(_model.Model); }
public void GetModelID_Returns_SameIDForSameModel() { EdmModel model = new EdmModel(); Assert.Equal(ModelContainer.GetModelID(model), ModelContainer.GetModelID(model)); }