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 }, Model = 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_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 }, Model = 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 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 }, Model = 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.OData.Query.IPropertyMapper' instance for the given 'NS.Name' IEdmType."); }
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), UseInstanceForProperties = true, }; // Act var result = testWrapper.ToDictionary(); // Assert Assert.Equal(42, result["SampleProperty"]); }
public void ToDictionary_Throws_IfMapperProviderIsNull() { // Arrange SelectExpandWrapper <TestEntity> wrapper = new SelectExpandWrapper <TestEntity>(); // Act & Assert Assert.Throws <ArgumentNullException>(() => wrapper.ToDictionary(mapperProvider: null)); }
public void ToDictionary_ContainsAllProperties_FromContainer() { // 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); MockPropertyContainer container = new MockPropertyContainer(); container.Properties.Add("Property", 42); SelectExpandWrapper <TestEntity> wrapper = new SelectExpandWrapper <TestEntity> { Container = container, Model = model }; // Act var result = wrapper.ToDictionary(); // Assert Assert.Equal(42, result["Property"]); }