public void CreateReturnsNotCriteriaWithChildCriteriaSet()
        {
            var criteria = NotCriteria.Create(sampleTerm);

            Assert.IsType <NotCriteria>(criteria);
            Assert.Equal(sampleTerm, ((NotCriteria)criteria).Criteria);
        }
        public void CreateUnwrapsNestedNotCriteria()
        {
            var criteria = NotCriteria.Create(NotCriteria.Create(sampleTerm));

            Assert.IsType <TermCriteria>(criteria);
            Assert.Equal(sampleTerm, criteria);
        }
        public void ToStringContainsSubfields()
        {
            var termCriteria = TermsCriteria.Build("termField", memberInfo, "some value");

            var notCriteria = NotCriteria.Create(termCriteria);
            var result      = notCriteria.ToString();

            Assert.Contains(termCriteria.ToString(), result);
        }
Exemple #4
0
        public void NotWithOrBecomesBoolWithMustNot()
        {
            ICriteria[] expected = { new RangeCriteria("fieldOne", memberInfo, RangeComparison.LessThan, 2), new RangeCriteria("fieldTwo", memberInfo, RangeComparison.GreaterThan, 4) };

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

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

            Assert.Equal(boolActual.MustNot.AsEnumerable(), expected);
            Assert.Empty(boolActual.Should);
            Assert.Empty(boolActual.Must);
        }
        protected virtual Expression VisitIsNullOrEmptyMethodCall(Expression source, string methodName)
        {
            if (source is MemberExpression || source is MethodCallExpression)
            {
                if (Mapping.TryGetFieldName(SourceType, source, out string field))
                {
                    IsPropertyHasKeyword(field, out field);
                    return(new CriteriaExpression(NotCriteria.Create(new ExistsCriteria(field))));
                }
            }

            throw new NotSupportedException($"Unknown source '{source}' for {methodName} operation");
        }
        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);
        }
        public void BodyContainsBoolMustNot()
        {
            var termCriteria = TermsCriteria.Build("term1", memberInfo, "alpha", "bravo", "charlie", "delta", "echo");
            var notCriteria  = NotCriteria.Create(termCriteria);

            var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest {
                IndexType = "type1", Query = notCriteria
            });
            var body = JObject.Parse(formatter.Body);

            var result      = body.TraverseWithAssert("query", "bool", "must_not")[0]["terms"];
            var actualTerms = result.TraverseWithAssert(termCriteria.Field);

            foreach (var criteria in termCriteria.Values)
            {
                Assert.Contains("!!! " + criteria + " !!!", actualTerms.Select(t => t.ToString()).ToArray());
            }
        }
Exemple #8
0
        public void BodyContainsFilterNot()
        {
            var termCriteria = TermsCriteria.Build("term1", memberInfo, "alpha", "bravo", "charlie", "delta", "echo");
            var notCriteria  = NotCriteria.Create(termCriteria);

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest {
                Type = "type1", Filter = notCriteria
            });
            var body = JObject.Parse(formatter.Body);

            var result      = TraverseWithAssert(body, "filter", "not", "terms");
            var actualTerms = TraverseWithAssert(result, termCriteria.Field);

            foreach (var criteria in termCriteria.Values)
            {
                Assert.Contains("!!! " + criteria + " !!!", actualTerms.Select(t => t.ToString()).ToArray());
            }
        }
        private Expression VisitNotEqual(Expression left, Expression right)
        {
            var cm = ConstantMemberPair.Create(left, right);

            if (cm == null)
            {
                throw new NotSupportedException("A not-equal expression must consist of a constant and a member");
            }

            if (cm.IsNullTest)
            {
                return(CreateExists(cm, false));
            }

            var member    = cm.MemberExpression.Member;
            var fieldName = mapping.GetFieldName(prefix, member);

            return(new CriteriaExpression(NotCriteria.Create(TermsCriteria.Build(fieldName, member, cm.ConstantExpression.Value))));
        }
Exemple #10
0
        Expression VisitNotEqual(Expression left, Expression right)
        {
            var booleanEquals = VisitCriteriaEquals(left, right, false);

            if (booleanEquals != null)
            {
                return(booleanEquals);
            }

            var cm = ConstantMemberPair.Create(left, right);

            if (cm == null)
            {
                throw new NotSupportedException("A not-equal expression must be between a constant and a member");
            }

            return(cm.IsNullTest
                ? CreateExists(cm, false)
                : new CriteriaExpression(NotCriteria.Create(new TermCriteria(Mapping.GetFieldName(SourceType, cm.MemberExpression), cm.MemberExpression.Member, cm.ConstantExpression.Value))));
        }
        protected override Expression VisitUnary(UnaryExpression node)
        {
            switch (node.NodeType)
            {
            case ExpressionType.Convert:
                return(node.Operand);

            case ExpressionType.Not:
            {
                var subExpression = Visit(node.Operand) as CriteriaExpression;
                if (subExpression != null)
                {
                    return(new CriteriaExpression(NotCriteria.Create(subExpression.Criteria)));
                }
                break;
            }
            }

            return(base.VisitUnary(node));
        }
        protected virtual Expression VisitStringPatternCheckMethodCall(Expression source, Expression match, string pattern,
                                                                       string methodName, bool notMatch = false)
        {
            var matched = Visit(match);

            if ((source is MemberExpression || source is MethodCallExpression) && matched is ConstantExpression)
            {
                if (Mapping.TryGetFieldName(SourceType, source, out string field))
                {
                    var value = ((ConstantExpression)matched).Value;
                    IsPropertyHasKeyword(field, out field);

                    var wildcardCriteria = new WildCardCriteria(string.Format(pattern, value), field);
                    return(new CriteriaExpression(!notMatch? wildcardCriteria: NotCriteria.Create(wildcardCriteria)));
                }
            }

            throw new NotSupportedException(source is MemberExpression
                ? $"Match '{match}' in Contains operation must be a constant"
                : $"Unknown source '{source}' for Contains operation");
        }
Exemple #13
0
        static Expression VisitCriteriaEquals(Expression left, Expression right, bool positiveCondition)
        {
            var criteria = left as CriteriaExpression ?? right as CriteriaExpression;
            var constant = left as ConstantExpression ?? right as ConstantExpression;

            if (criteria == null || constant == null)
            {
                return(null);
            }

            if (constant.Value.Equals(positiveCondition))
            {
                return(criteria);
            }

            if (constant.Value.Equals(!positiveCondition))
            {
                return(new CriteriaExpression(NotCriteria.Create(criteria.Criteria)));
            }

            return(null);
        }
        Expression VisitCriteriaEqualsForFields(ConstantMemberPair constantMemberPair, bool equal = true)
        {
            if (Mapping.TryGetFieldName(SourceType, constantMemberPair.Expression, out string fieldName))
            {
                var propertyMappings = Mapping.ElasticPropertyMappings();

                ICriteria criteria;

                if (IsPropertyTypeText(fieldName, propertyMappings))
                {
                    if (propertyMappings.ContainsKey($"{fieldName}.keyword"))
                    {
                        fieldName = $"{fieldName}.keyword";
                        criteria  = new TermCriteria(fieldName,
                                                     constantMemberPair.GetMemberFromExpression(), constantMemberPair.ConstantExpression.Value);
                    }
                    else
                    {
                        criteria = new MatchCriteria(fieldName,
                                                     constantMemberPair.GetMemberFromExpression(), constantMemberPair.ConstantExpression.Value);
                    }
                }
                else
                {
                    criteria = new TermCriteria(fieldName,
                                                constantMemberPair.GetMemberFromExpression(), constantMemberPair.ConstantExpression.Value);
                }

                if (!equal)
                {
                    criteria = NotCriteria.Create(criteria);
                }
                return(new CriteriaExpression(criteria));
            }

            return(null);
        }
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);
        }
 public void ConstructorThrowsArgumentNullExceptionWhenFieldIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => NotCriteria.Create(null));
 }
        public void NamePropertyIsNot()
        {
            var criteria = NotCriteria.Create(sampleTerm);

            Assert.Equal("not", criteria.Name);
        }