public void CombineWithSingleCriteriaReturnsThatCriteria()
        {
            var rangeCriteria = new RangeCriteria("field", memberInfo, RangeComparison.LessThan, 1);
            var andCriteria   = AndCriteria.Combine(rangeCriteria);

            Assert.Same(rangeCriteria, andCriteria);
        }
        private JProperty Build(IFacet facet, ICriteria primaryFilter, long?defaultSize)
        {
            Argument.EnsureNotNull("facet", facet);

            var specificBody = Build(facet);

            if (facet is IOrderableFacet)
            {
                var facetSize = ((IOrderableFacet)facet).Size ?? defaultSize;
                if (facetSize.HasValue)
                {
                    specificBody["size"] = facetSize.Value.ToString(transportCulture);
                }
            }

            var namedBody = new JObject(new JProperty(facet.Type, specificBody));

            var combinedFilter = AndCriteria.Combine(primaryFilter, facet.Filter);

            if (combinedFilter != null)
            {
                namedBody[facet is FilterFacet ? "filter" : "facet_filter"] = Build(combinedFilter);
            }

            return(new JProperty(facet.Name, namedBody));
        }
Exemple #3
0
        void ApplyTypeSelectionCriteria()
        {
            var typeCriteria = Mapping.GetTypeSelectionCriteria(SourceType);

            this.searchRequest.Filter = this.searchRequest.Filter == null || this.searchRequest.Filter == ConstantCriteria.True
                ? typeCriteria
                : AndCriteria.Combine(typeCriteria, this.searchRequest.Filter);
        }
        void ApplyTypeSelectionCriteria()
        {
            var typeCriteria = Mapping.GetTypeSelectionCriteria(SourceType);

            searchRequest.Query = searchRequest.Query == null || searchRequest.Query == ConstantCriteria.True
                ? typeCriteria
                : AndCriteria.Combine(typeCriteria, searchRequest.Query);
        }
Exemple #5
0
        private void ApplyTypeSelectionCriteria()
        {
            var typeCriteria = Mapping.GetTypeSelectionCriteria(sourceType);

            searchRequest.Filter = searchRequest.Filter == null || searchRequest.Filter == ConstantCriteria.True
                ? typeCriteria
                : AndCriteria.Combine(typeCriteria, searchRequest.Filter);
        }
Exemple #6
0
        public void AndWithFalseOptimizesToFalse()
        {
            var exists1  = new ExistsCriteria("1");
            var exists2  = new ExistsCriteria("2");
            var criteria = AndCriteria.Combine(exists1, ConstantCriteria.False, exists2);

            var actual = ConstantCriteriaFilterReducer.Reduce(criteria);

            Assert.Same(ConstantCriteria.False, actual);
        }
        public void ToStringContainsSubfields()
        {
            var existsCriteria = new ExistsCriteria("thisIsAMissingField");
            var termCriteria   = TermsCriteria.Build("termField", memberInfo, "some value");

            var andCriteria = AndCriteria.Combine(existsCriteria, termCriteria);
            var result      = andCriteria.ToString();

            Assert.Contains(existsCriteria.ToString(), result);
            Assert.Contains(termCriteria.ToString(), result);
        }
Exemple #8
0
        Expression VisitQueryString(Expression source, Expression queryExpression, Expression fieldsExpression = null)
        {
            var constantQueryExpression = (ConstantExpression)queryExpression;
            var constantFieldExpression = fieldsExpression as ConstantExpression;
            var constantFields          = constantFieldExpression == null ? null : (string[])constantFieldExpression.Value;
            var criteriaExpression      = new CriteriaExpression(new QueryStringCriteria(constantQueryExpression.Value.ToString(), constantFields));

            searchRequest.Query = AndCriteria.Combine(searchRequest.Query, criteriaExpression.Criteria);

            return(Visit(source));
        }
        public void CombineWithCriteriaCombinesIntoAndCriteria()
        {
            var criteria = AndCriteria.Combine(sampleCriteria1, sampleCriteria2);

            Assert.IsType <AndCriteria>(criteria);
            var andCriteria = (AndCriteria)criteria;

            Assert.Contains(sampleCriteria1, andCriteria.Criteria);
            Assert.Contains(sampleCriteria2, andCriteria.Criteria);
            Assert.Equal(2, andCriteria.Criteria.Count);
        }
Exemple #10
0
        public void AndWithNotsBecomesBoolWithMustNots()
        {
            var expectedMustNots = new[] { new RangeCriteria("field1", memberInfo, RangeComparison.LessThan, 2), new RangeCriteria("field2", memberInfo, RangeComparison.GreaterThan, 4) };

            var actual = QueryCriteriaRewriter.Compensate(AndCriteria.Combine(expectedMustNots.Select(NotCriteria.Create).ToArray()));

            var boolActual = Assert.IsType <BoolCriteria>(actual);

            Assert.Equal(boolActual.MustNot.AsEnumerable(), expectedMustNots);
            Assert.Empty(boolActual.Should);
            Assert.Empty(boolActual.Must);
        }
Exemple #11
0
        public void AndBecomesBoolWithMust()
        {
            ICriteria[] expected = { new RangeCriteria("fieldOne", memberInfo, RangeComparison.LessThan, 2), new RangeCriteria("fieldTwo", memberInfo, RangeComparison.GreaterThan, 4) };

            var actual = QueryCriteriaRewriter.Compensate(AndCriteria.Combine(expected));

            var boolActual = Assert.IsType <BoolCriteria>(actual);

            Assert.Equal(boolActual.Must.AsEnumerable(), expected);
            Assert.Empty(boolActual.Should);
            Assert.Empty(boolActual.MustNot);
        }
        public void AndWithOrsBecomesBoolWithShould()
        {
            var expected1 = new[] { new RangeCriteria("field1", memberInfo, RangeComparison.LessThan, 2), new RangeCriteria("field2", memberInfo, RangeComparison.GreaterThan, 4) };
            var expected2 = new[] { new RangeCriteria("field3", memberInfo, RangeComparison.LessThan, 2), new RangeCriteria("field4", memberInfo, RangeComparison.GreaterThan, 4) };

            var actual = QueryCriteriaRewriter.Compensate(AndCriteria.Combine(OrCriteria.Combine(expected1), OrCriteria.Combine(expected2)));

            var boolActual = Assert.IsType <BoolCriteria>(actual);

            Assert.Equal(boolActual.Should.AsEnumerable(), expected1.Concat(expected2));
            Assert.Empty(boolActual.Must);
            Assert.Empty(boolActual.MustNot);
        }
Exemple #13
0
        public void AndWithTrueRemovesTrue()
        {
            var exists1  = new ExistsCriteria("1");
            var exists2  = new ExistsCriteria("2");
            var criteria = AndCriteria.Combine(exists1, ConstantCriteria.True, exists2);

            var actual = ConstantCriteriaFilterReducer.Reduce(criteria);

            var andActual = Assert.IsType <AndCriteria>(actual);

            Assert.DoesNotContain(andActual.Criteria, c => c == ConstantCriteria.True);
            Assert.Single(andActual.Criteria, exists1);
            Assert.Single(andActual.Criteria, exists2);
        }
Exemple #14
0
        Expression VisitWhere(Expression source, Expression lambdaPredicate)
        {
            var lambda             = lambdaPredicate.GetLambda();
            var criteriaExpression = lambda.Body as CriteriaExpression ?? BooleanMemberAccessBecomesEquals(lambda.Body) as CriteriaExpression;

            if (criteriaExpression == null)
            {
                throw new NotSupportedException($"Where expression '{lambda.Body}' could not be translated");
            }

            searchRequest.Criteria = AndCriteria.Combine(searchRequest.Criteria, criteriaExpression.Criteria);

            return(Visit(source));
        }
Exemple #15
0
        public void AndWithMixedContentBecomesBoolWithShouldMustAndMustNot()
        {
            ICriteria[] expectedShould  = { new RangeCriteria("field1", memberInfo, RangeComparison.LessThan, 2), new RangeCriteria("field2", memberInfo, RangeComparison.GreaterThan, 4) };
            var         expectedMust    = new RangeCriteria("field3", memberInfo, RangeComparison.LessThan, 2);
            var         expectedMustNot = new PrefixCriteria("field5", "prefix");

            var actual = QueryCriteriaRewriter.Compensate(AndCriteria.Combine(OrCriteria.Combine(expectedShould), expectedMust, NotCriteria.Create(expectedMustNot)));

            var boolActual = Assert.IsType <BoolCriteria>(actual);

            Assert.Equal(boolActual.Should.AsEnumerable(), expectedShould);
            Assert.Single(boolActual.Must, expectedMust);
            Assert.Single(boolActual.MustNot, expectedMustNot);
        }
        internal void CombineSameFieldRangeNotCombineIntoSingleRangeCriteriaIfConflictingComparisons(RangeComparison leftRangeComparison, RangeComparison rightRangeComparison)
        {
            var leftRangeCriteria  = new RangeCriteria("a", memberInfo, leftRangeComparison, "leftValue");
            var rightRangeCriteria = new RangeCriteria("a", memberInfo, rightRangeComparison, "rightValue");

            var criteria = AndCriteria.Combine(leftRangeCriteria, rightRangeCriteria);

            var rangeCriteria = Assert.IsType <AndCriteria>(criteria).Criteria.OfType <RangeCriteria>().ToList();

            Assert.Equal(2, rangeCriteria.Count());
            Assert.All(rangeCriteria, c => Assert.True(Assert.IsType <RangeCriteria>(c).Member == memberInfo));

            Assert.Single(rangeCriteria, leftRangeCriteria);
            Assert.Single(rangeCriteria, rightRangeCriteria);
        }
        public void CombineWithTwoSameFieldRangeCriteriaCombinesIntoSingleRangeCriteria()
        {
            var lowerRangeCriteria = new RangeCriteria("first", memberInfo, RangeComparison.GreaterThan, "lower");
            var upperRangeCriteria = new RangeCriteria("first", memberInfo, RangeComparison.LessThanOrEqual, "upper");

            var andCriteria = AndCriteria.Combine(lowerRangeCriteria, upperRangeCriteria);

            Assert.IsType <RangeCriteria>(andCriteria);
            var rangeCriteria = (RangeCriteria)andCriteria;

            Assert.Equal(rangeCriteria.Field, lowerRangeCriteria.Field);
            Assert.Same(memberInfo, lowerRangeCriteria.Member);
            Assert.Single(rangeCriteria.Specifications, s => s.Comparison == lowerRangeCriteria.Specifications.First().Comparison);
            Assert.Single(rangeCriteria.Specifications, s => s.Comparison == upperRangeCriteria.Specifications.First().Comparison);
        }
        public void NotWithAndBecomesBoolWithMustNotBool()
        {
            var expected = new[] { new RangeCriteria("fieldOne", memberInfo, RangeComparison.LessThan, 2), new RangeCriteria("fieldTwo", memberInfo, RangeComparison.GreaterThan, 4) };

            var actual = QueryCriteriaRewriter.Compensate(NotCriteria.Create(AndCriteria.Combine(expected)));

            var boolActual = Assert.IsType <BoolCriteria>(actual);

            Assert.Empty(boolActual.Should);
            Assert.Empty(boolActual.Must);
            Assert.Single(boolActual.MustNot);
            var subBoolActual = Assert.IsType <BoolCriteria>(boolActual.MustNot.First());

            Assert.Equal(subBoolActual.Must.AsEnumerable(), expected);
            Assert.Empty(subBoolActual.Should);
            Assert.Empty(subBoolActual.MustNot);
        }
Exemple #19
0
        Expression VisitQuery(Expression source, Expression predicate)
        {
            var lambda    = predicate.GetLambda();
            var wasWithin = within;

            within = CriteriaWithin.Query;
            var body = BooleanMemberAccessBecomesEquals(lambda.Body);

            var criteriaExpression = body as CriteriaExpression;

            if (criteriaExpression == null)
            {
                throw new NotSupportedException(string.Format("Query expression '{0}' could not be translated", body));
            }

            searchRequest.Query = AndCriteria.Combine(searchRequest.Query, criteriaExpression.Criteria);
            within = wasWithin;

            return(Visit(source));
        }
        public void CombineWithDifferentFieldRangeCriteriaCombinesRangesIntoAndCriteria()
        {
            var firstMemberInfo  = typeof(string).GetProperty("Length");
            var secondMemberInfo = typeof(string).GetMethod("Clone");
            var lowerFirstRange  = new RangeCriteria("first", firstMemberInfo, RangeComparison.GreaterThan, "lower");
            var upperFirstRange  = new RangeCriteria("first", firstMemberInfo, RangeComparison.LessThanOrEqual, "upper");
            var secondRange      = new RangeCriteria("second", secondMemberInfo, RangeComparison.GreaterThanOrEqual, "lower2");

            var criteria = AndCriteria.Combine(lowerFirstRange, secondRange, upperFirstRange);

            Assert.IsType <AndCriteria>(criteria);
            var andCriteria = (AndCriteria)criteria;

            Assert.Equal(2, andCriteria.Criteria.Count);
            Assert.Contains(secondRange, andCriteria.Criteria);

            var combinedRange = andCriteria.Criteria.OfType <RangeCriteria>().FirstOrDefault(r => r.Specifications.Count == 2);

            Assert.NotNull(combinedRange);
            Assert.Equal(lowerFirstRange.Field, combinedRange.Field);
            Assert.Same(firstMemberInfo, combinedRange.Member);
            Assert.Single(combinedRange.Specifications, s => s.Comparison == lowerFirstRange.Specifications.First().Comparison);
            Assert.Single(combinedRange.Specifications, s => s.Comparison == upperFirstRange.Specifications.First().Comparison);
        }
Exemple #21
0
        public void AndWithNestedOrsBecomesBoolWithMustAndNestedShould()
        {
            ICriteria[] expected1 = { new RangeCriteria("field1", memberInfo, RangeComparison.LessThan, 2), new RangeCriteria("field2", memberInfo, RangeComparison.GreaterThan, 4) };
            ICriteria[] expected2 = { new RangeCriteria("field3", memberInfo, RangeComparison.LessThan, 2), new RangeCriteria("field4", memberInfo, RangeComparison.GreaterThan, 4) };

            var actual = QueryCriteriaRewriter.Compensate(AndCriteria.Combine(OrCriteria.Combine(expected1), OrCriteria.Combine(expected2)));

            var boolActual = Assert.IsType <BoolCriteria>(actual);

            Assert.Empty(boolActual.Should);
            Assert.Empty(boolActual.MustNot);
            Assert.Equal(2, boolActual.Must.Count);
            Assert.All(boolActual.Must, c =>
            {
                var boolSub = Assert.IsType <BoolCriteria>(c);
                Assert.Empty(boolSub.Must);
                Assert.Empty(boolSub.MustNot);
                Assert.Equal(2, boolSub.Should.Count);
                Assert.All(boolSub.Should, s =>
                {
                    Assert.IsType <RangeCriteria>(s);
                });
            });
        }
        private Expression VisitAndAlso(BinaryExpression b)
        {
            var criteria = AssertExpressionsOfType <CriteriaExpression>(b.Left, b.Right).Select(f => f.Criteria).ToArray();

            return(new CriteriaExpression(AndCriteria.Combine(criteria)));
        }
 private static ICriteria ApplyCriteria(ICriteria currentRoot, ICriteria newCriteria)
 {
     return(currentRoot == null
         ? newCriteria
         : AndCriteria.Combine(currentRoot, newCriteria));
 }
Exemple #24
0
 Expression VisitAndAlso(BinaryExpression b)
 {
     return(new CriteriaExpression(
                AndCriteria.Combine(CombineExpressions <CriteriaExpression>(b.Left, b.Right).Select(f => f.Criteria).ToArray())));
 }
 [ExcludeFromCodeCoverage] // Expression isn't "executed"
 public void CombineThrowArgumentNullExceptionWhenCriteriaIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => AndCriteria.Combine(null));
 }
        public void CombineWithEmptyListReturnsNull()
        {
            var criteria = AndCriteria.Combine(new ICriteria[] { });

            Assert.Null(criteria);
        }