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), UseInstanceForProperties = true, }; 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 ExceptionAssert.Throws <InvalidOperationException>(() => testWrapper.ToDictionary(mapperProvider), "The key mapping for the property 'SampleProperty' can't be null or empty."); }
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), UseInstanceForProperties = true, }; 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_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_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 ExceptionAssert.Throws <InvalidOperationException>(() => wrapper.ToDictionary(mapperProvider: mapperProvider), "The mapper provider must return a valid 'Microsoft.AspNetCore.OData.Query.Container.IPropertyMapper' instance for the given 'NS.Name' IEdmType."); }
public void GetModelID_AndThen_GetModel_ReturnsOriginalModel() { EdmModel model = new EdmModel(); string modelID = ModelContainer.GetModelID(model); Assert.Same(model, ModelContainer.GetModel(modelID)); }
public void GetModelID_Returns_DifferentIDForDifferentModels() { EdmModel model1 = new EdmModel(); EdmModel model2 = new EdmModel(); Assert.NotEqual(ModelContainer.GetModelID(model1), ModelContainer.GetModelID(model2)); }
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, ModelID = ModelContainer.GetModelID(model) }; // Act var result = wrapper.ToDictionary(); // Assert Assert.Equal(42, result["Property"]); }
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)); }