Exemplo n.º 1
0
        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,
                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.");
        }
Exemplo n.º 2
0
        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,
                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"]);
        }
Exemplo n.º 3
0
        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
                },
                Model = model,
                UseInstanceForProperties = true,
            };

            // Act
            var result = testWrapper.ToDictionary();

            // Assert
            Assert.Equal(42, result["SampleProperty"]);
        }
Exemplo n.º 4
0
        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
            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.");
        }
Exemplo n.º 5
0
        public void ToDictionary_Throws_IfMapperProviderIsNull()
        {
            // Arrange
            SelectExpandWrapper <TestEntity> wrapper = new SelectExpandWrapper <TestEntity>();

            // Act & Assert
            ExceptionAssert.Throws <ArgumentNullException>(() => wrapper.ToDictionary(mapperProvider: null));
        }
Exemplo n.º 6
0
        public void SelectExpandBinder_BindsComputedPropertyInNestedSelect_FromDollarCompute()
        {
            // Arrange
            QueryClause clause = CreateQueryClause("$select=Location($select=StateCode;$compute=Zipcode div 1000 as StateCode)", _model, typeof(ComputeCustomer));

            ODataQuerySettings querySettings = new ODataQuerySettings();
            SelectExpandBinder binder        = new SelectExpandBinder();
            QueryBinderContext context       = new QueryBinderContext(_model, querySettings, typeof(ComputeCustomer));

            if (clause.Compute != null)
            {
                context.AddComputedProperties(clause.Compute.ComputedItems);
            }

            // Act
            Expression selectExp = binder.BindSelectExpand(clause.SelectExpand, context);

            // Assert
            Assert.NotNull(selectExp);
            string resultExpression = ExpressionStringBuilder.ToString(selectExp);

            Assert.Equal("$it => new SelectSome`1() {" +
                         "Model = Microsoft.OData.Edm.EdmModel, " +
                         "Container = new NamedPropertyWithNext0`1() {" +
                         "Name = Location, " +
                         "Value = new SelectSome`1() {" +
                         "Model = Microsoft.OData.Edm.EdmModel, " +
                         "Container = new NamedProperty`1() {" +
                         "Name = StateCode, " +
                         "Value = ($it.Location.Zipcode / 1000), " +
                         "}, " +
                         "}, " +
                         "Next0 = new AutoSelectedNamedProperty`1() {Name = Id, Value = Convert($it.Id), }, }, }", resultExpression);

            ComputeCustomer customer = new ComputeCustomer
            {
                Location = new ComputeAddress {
                    Zipcode = 98029
                }
            };
            IDictionary <string, object> result = SelectExpandBinderTest.InvokeSelectExpand(customer, selectExp);

            var location = Assert.Single(result);

            Assert.Equal("Location", location.Key);
            SelectExpandWrapper          itemWrapper    = location.Value as SelectExpandWrapper;
            IDictionary <string, object> locationResult = itemWrapper.ToDictionary();

            var stateCode = Assert.Single(locationResult);

            Assert.Equal("StateCode", stateCode.Key);
            Assert.Equal(98, stateCode.Value);
        }
Exemplo n.º 7
0
        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"]);
        }
        public void ToDictionary_ContainsAllProperties_FromContainer()
        {
            // Arrange
            MockPropertyContainer container = new MockPropertyContainer();
            container.Properties.Add("Property", 42);
            SelectExpandWrapper<TestEntity> wrapper = new SelectExpandWrapper<TestEntity> { Container = container };

            // Act
            var result = wrapper.ToDictionary();

            // Assert
            Assert.Equal(42, result["Property"]);
        }
        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"]);
        }
Exemplo n.º 10
0
        public void SelectExpandBinder_BindsComputedPropertyInExpand_FromDollarCompute()
        {
            // Arrange
            QueryClause clause = CreateQueryClause("$expand=Orders($select=Title,Tax;$compute=Amount mul TaxRate as Tax)", _model, typeof(ComputeCustomer));

            ODataQuerySettings querySettings = new ODataQuerySettings();
            SelectExpandBinder binder        = new SelectExpandBinder();
            QueryBinderContext context       = new QueryBinderContext(_model, querySettings, typeof(ComputeCustomer));

            if (clause.Compute != null)
            {
                context.AddComputedProperties(clause.Compute.ComputedItems);
            }

            // Act
            Expression selectExp = binder.BindSelectExpand(clause.SelectExpand, context);

            // Assert
            Assert.NotNull(selectExp);
            string resultExpression = ExpressionStringBuilder.ToString(selectExp);

            Assert.Equal("$it => new SelectAllAndExpand`1() {Model = Microsoft.OData.Edm.EdmModel, " +
                         "Instance = $it, UseInstanceForProperties = True, " +
                         "Container = new NamedPropertyWithNext0`1() " +
                         "{" +
                         "Name = Orders, " +
                         "Value = $it.Orders.Select($it => new SelectSome`1() " +
                         "{" +
                         "Model = Microsoft.OData.Edm.EdmModel, " +
                         "Container = new NamedPropertyWithNext1`1() " +
                         "{" +
                         "Name = Title, " +
                         "Value = $it.Title, " +
                         "Next0 = new NamedProperty`1() {Name = Tax, Value = (Convert($it.Amount) * $it.TaxRate), }, " +
                         "Next1 = new AutoSelectedNamedProperty`1() " +
                         "{" +
                         "Name = Id, Value = Convert($it.Id), " +
                         "}, " +
                         "}, " +
                         "}), " +
                         "Next0 = new NamedProperty`1() {Name = Dynamics, Value = $it.Dynamics, }, }, }", resultExpression);

            ComputeCustomer customer = new ComputeCustomer
            {
                Orders = new List <ComputeOrder>
                {
                    new ComputeOrder {
                        Title = "Kerry", Amount = 4, TaxRate = 0.35
                    },
                    new ComputeOrder {
                        Title = "WU", Amount = 6, TaxRate = 0.5
                    },
                    new ComputeOrder {
                        Title = "XU", Amount = 5, TaxRate = 0.12
                    },
                }
            };
            IDictionary <string, object> result = SelectExpandBinderTest.InvokeSelectExpand(customer, selectExp);

            Assert.Equal(8, result.Count); // Because it's select-all

            int idx         = 0;
            var ordersValue = result["Orders"] as IEnumerable;

            foreach (var order in ordersValue)
            {
                SelectExpandWrapper itemWrapper = order as SelectExpandWrapper;

                var orderDic = itemWrapper.ToDictionary();
                Assert.Equal(customer.Orders[idx].Title, orderDic["Title"]);
                Assert.Equal(customer.Orders[idx].Amount * customer.Orders[idx].TaxRate, orderDic["Tax"]);
                idx++;
            }
        }
        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_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 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_IfMapperProviderIsNull()
        {
            // Arrange
            SelectExpandWrapper<TestEntity> wrapper = new SelectExpandWrapper<TestEntity>();

            // Act & Assert
            Assert.Throws<ArgumentNullException>(() => wrapper.ToDictionary(mapperProvider: null));
        }
        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"]);
        }