public void Validator_Doesnot_Throw_For_ValidQueries(string filter)
        {
            // Arrange
            ComputeQueryOption option = new ComputeQueryOption(filter, _context);

            // Act & Assert
            ExceptionAssert.DoesNotThrow(() => _validator.Validate(option, _settings));
        }
        public void LongInputs_CauseMaxNodeCountExceededException(string filter)
        {
            // Arrange
            ODataValidationSettings settings = new ODataValidationSettings
            {
                MaxAnyAllExpressionDepth = Int32.MaxValue
            };

            ComputeQueryOption option = new ComputeQueryOption(filter, _productContext);

            // Act & Assert
            ExceptionAssert.Throws <ODataException>(() => _validator.Validate(option, settings), "The node count limit of '100' has been exceeded. To increase the limit, set the 'MaxNodeCount' property on EnableQueryAttribute or ODataValidationSettings.");
        }
Beispiel #3
0
        public void CtorComputeQueryOption_CanConstructValidComputeQuery()
        {
            // Arrange
            IEdmModel         model   = _model;
            ODataQueryContext context = new ODataQueryContext(model, typeof(ComputeCustomer));

            // Act
            ComputeQueryOption compute = new ComputeQueryOption("Price mul Qty as Total", context);

            // Assert
            Assert.Same(context, compute.Context);
            Assert.Equal("Price mul Qty as Total", compute.RawValue);
        }
        public void IncreaseMaxNodeCountWillAllowLongInputs(string filter)
        {
            // Arrange
            ODataValidationSettings settings = new ODataValidationSettings
            {
                MaxAnyAllExpressionDepth = Int32.MaxValue,
                MaxNodeCount             = 105,
            };

            ComputeQueryOption option = new ComputeQueryOption(filter, _productContext);

            // Act & Assert
            ExceptionAssert.DoesNotThrow(() => _validator.Validate(option, settings));
        }
Beispiel #5
0
        public void CanTurnOffValidationForFilter()
        {
            ODataValidationSettings settings = new ODataValidationSettings()
            {
                AllowedFunctions = AllowedFunctions.AllDateTimeFunctions
            };
            ODataQueryContext  context = ValidationTestHelper.CreateCustomerContext();
            ComputeQueryOption option  = new ComputeQueryOption("substring(Name,8,1) as NewProp", context);

            ExceptionAssert.Throws <ODataException>(() =>
                                                    option.Validate(settings),
                                                    "Function 'substring' is not allowed. To allow it, set the 'AllowedFunctions' property on EnableQueryAttribute or QueryValidationSettings.");

            option.Validator = null;
            ExceptionAssert.DoesNotThrow(() => option.Validate(settings));
        }
Beispiel #6
0
        public void CtorComputeQueryOption_GetQueryNodeParsesQuery()
        {
            // Arrange
            IEdmModel         model   = _model;
            ODataQueryContext context = new ODataQueryContext(model, typeof(ComputeCustomer))
            {
                RequestContainer = new MockServiceProvider()
            };

            // Act
            ComputeQueryOption compute       = new ComputeQueryOption("Price mul Qty as Total,Price mul 2.0 as Tax", context);
            ComputeClause      computeClause = compute.ComputeClause;

            // Assert
            Assert.Equal(2, computeClause.ComputedItems.Count());

            Assert.Collection(computeClause.ComputedItems,
                              e =>
            {
                Assert.Equal("Total", e.Alias);

                Assert.Equal(QueryNodeKind.BinaryOperator, e.Expression.Kind);
                BinaryOperatorNode binaryNode = e.Expression as BinaryOperatorNode;
                Assert.Equal(BinaryOperatorKind.Multiply, binaryNode.OperatorKind);
                Assert.Equal(QueryNodeKind.Convert, binaryNode.Right.Kind);
                ConvertNode convertNode = (ConvertNode)binaryNode.Right;
                Assert.Equal("Qty", ((SingleValuePropertyAccessNode)convertNode.Source).Property.Name);

                Assert.Equal(QueryNodeKind.SingleValuePropertyAccess, binaryNode.Left.Kind);
                var propertyAccessNode = binaryNode.Left as SingleValuePropertyAccessNode;
                Assert.Equal("Price", propertyAccessNode.Property.Name);
            },
                              e =>
            {
                Assert.Equal("Tax", e.Alias);

                Assert.Equal(QueryNodeKind.BinaryOperator, e.Expression.Kind);
                BinaryOperatorNode binaryNode = e.Expression as BinaryOperatorNode;
                Assert.Equal(BinaryOperatorKind.Multiply, binaryNode.OperatorKind);
                Assert.Equal(QueryNodeKind.Constant, binaryNode.Right.Kind);
                Assert.Equal(2.0, ((ConstantNode)binaryNode.Right).Value);

                Assert.Equal(QueryNodeKind.SingleValuePropertyAccess, binaryNode.Left.Kind);
                var propertyAccessNode = binaryNode.Left as SingleValuePropertyAccessNode;
                Assert.Equal("Price", propertyAccessNode.Property.Name);
            });
        }
Beispiel #7
0
        /// <summary>
        /// Validates a <see cref="ComputeQueryOption" />.
        /// </summary>
        /// <param name="computeQueryOption">The $compute query.</param>
        /// <param name="settings">The validation settings.</param>
        /// <remarks>
        /// Please note this method is not thread safe.
        /// </remarks>
        public virtual void Validate(ComputeQueryOption computeQueryOption, ODataValidationSettings settings)
        {
            if (computeQueryOption == null)
            {
                throw Error.ArgumentNull(nameof(computeQueryOption));
            }

            if (settings == null)
            {
                throw Error.ArgumentNull(nameof(settings));
            }

            if (computeQueryOption.Context.Path != null)
            {
                Property       = computeQueryOption.Context.TargetProperty;
                StructuredType = computeQueryOption.Context.TargetStructuredType;
            }

            Validate(computeQueryOption.ComputeClause, settings, computeQueryOption.Context.Model);
        }
 public override void Validate(ComputeQueryOption computeQueryOption, ODataValidationSettings settings)
 {
     IncrementCount("Validate");
     base.Validate(computeQueryOption, settings);
 }