public void CombineWithSingleCriteriaReturnsThatCriteria() { var rangeCriteria = new RangeCriteria("field", memberInfo, RangeComparison.LessThan, 1); var andCriteria = OrCriteria.Combine(rangeCriteria); Assert.Same(rangeCriteria, andCriteria); }
public void CombineWithEmptyListReturnsEmptyOr() { var criteria = OrCriteria.Combine(new ICriteria[] { }); Assert.IsType <OrCriteria>(criteria); Assert.Empty(((OrCriteria)criteria).Criteria); }
public void OrWithOrWithTrueOptimizesToTrue() { var exists1 = new ExistsCriteria("1"); var exists2 = new ExistsCriteria("2"); var criteria = OrCriteria.Combine(exists1, OrCriteria.Combine(ConstantCriteria.True, exists2)); var actual = ConstantCriteriaFilterReducer.Reduce(criteria); Assert.Same(ConstantCriteria.True, actual); }
public void ToStringContainsSubfields() { var existsCriteria = new ExistsCriteria("thisIsAMissingField"); var termCriteria = TermsCriteria.Build("termField", memberInfo, "some value"); var orCriteria = OrCriteria.Combine(existsCriteria, termCriteria); var result = orCriteria.ToString(); Assert.Contains(existsCriteria.ToString(), result); Assert.Contains(termCriteria.ToString(), result); }
public void CombineWithDifferTermCriteriaFieldsDoesNotCombine() { var criteria = OrCriteria.Combine(salutationMr, salutationMrs, area408); Assert.IsType <OrCriteria>(criteria); var orCriteria = (OrCriteria)criteria; Assert.Contains(salutationMr, orCriteria.Criteria); Assert.Contains(salutationMrs, orCriteria.Criteria); Assert.Contains(area408, orCriteria.Criteria); Assert.Equal(3, orCriteria.Criteria.Count); }
public void CombineWithAllTermFieldsSame_OrIncompatibleExecutionMode_ReturnsOrCriteria(TermsExecutionMode executionMode) { var salutationMs = TermsCriteria.Build(executionMode, "salutation", memberInfo, "Miss", "Ms"); var criteria = OrCriteria.Combine(salutationMr, salutationMrs, salutationMs); var orCriteria = Assert.IsType <OrCriteria>(criteria); Assert.Contains(salutationMr, orCriteria.Criteria); Assert.Contains(salutationMrs, orCriteria.Criteria); Assert.Contains(salutationMs, orCriteria.Criteria); Assert.Equal(3, orCriteria.Criteria.Count); }
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); }
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); }
public void OrWithFalseRemovesFalse() { var exists1 = new ExistsCriteria("1"); var exists2 = new ExistsCriteria("2"); var criteria = OrCriteria.Combine(exists1, ConstantCriteria.False, exists2); var actual = ConstantCriteriaFilterReducer.Reduce(criteria); var orActual = Assert.IsType <OrCriteria>(actual); Assert.DoesNotContain(orActual.Criteria, c => c == ConstantCriteria.False); Assert.Single(orActual.Criteria, exists1); Assert.Single(orActual.Criteria, exists2); }
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 BodyContainsExistsFieldSingleCollapsedOr() { const string expectedFieldName = "fieldShouldExist"; var existsCriteria = new ExistsCriteria(expectedFieldName); var orCriteria = OrCriteria.Combine(existsCriteria); var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { IndexType = "type1", Query = orCriteria }); var body = JObject.Parse(formatter.Body); var field = body.TraverseWithAssert("query", "exists", "field"); Assert.Equal(expectedFieldName, field); }
public void BodyContainsFilterSingleCollapsedOr() { const string expectedFieldName = "fieldShouldExist"; var existsCriteria = new ExistsCriteria(expectedFieldName); var orCriteria = OrCriteria.Combine(existsCriteria); var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Filter = orCriteria }); var body = JObject.Parse(formatter.Body); var field = TraverseWithAssert(body, "filter", "exists", "field"); Assert.Equal(expectedFieldName, field); }
public void CombineWithAllTermFieldsSame_OrCompatibleExecutionMode_CombinesIntoSingleTerm(TermsExecutionMode?executionMode) { var salutationMs = TermsCriteria.Build(executionMode, "salutation", memberInfo, "Miss", "Ms"); var criteria = OrCriteria.Combine(salutationMr, salutationMrs, salutationMs); var termsCriteria = Assert.IsType <TermsCriteria>(criteria); Assert.Equal(salutationMr.Field, termsCriteria.Field); Assert.Same(memberInfo, termsCriteria.Member); var allValues = salutationMr.Values.Concat(salutationMrs.Values).Concat(salutationMs.Values).Distinct().ToArray(); foreach (var value in allValues) { Assert.Contains(value, termsCriteria.Values); } Assert.Equal(allValues.Length, termsCriteria.Values.Count); }
public void BodyContainsBoolShould() { var minCriteria = new RangeCriteria("minField", memberInfo, RangeComparison.GreaterThanOrEqual, 100); var maxCriteria = new RangeCriteria("maxField", memberInfo, RangeComparison.LessThan, 32768); var orCriteria = OrCriteria.Combine(minCriteria, maxCriteria); var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { IndexType = "type1", Query = orCriteria }); var body = JObject.Parse(formatter.Body); var result = body.TraverseWithAssert("query", "bool", "should"); Assert.Equal(2, result.Children().Count()); foreach (var child in result) { Assert.True(((JProperty)(child.First)).Name == "range"); } }
public void BodyContainsFilterOr() { var minCriteria = new RangeCriteria("minField", memberInfo, RangeComparison.GreaterThanOrEqual, 100); var maxCriteria = new RangeCriteria("maxField", memberInfo, RangeComparison.LessThan, 32768); var orCriteria = OrCriteria.Combine(minCriteria, maxCriteria); var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Filter = orCriteria }); var body = JObject.Parse(formatter.Body); var result = TraverseWithAssert(body, "filter", "or"); Assert.Equal(2, result.Children().Count()); foreach (var child in result) { Assert.True(((JProperty)(child.First)).Name == "range"); } }
Expression VisitEnumerableContainsMethodCall(Expression source, Expression match) { var matched = Visit(match); // Where(x => constantsList.Contains(x.Property)) if (source is ConstantExpression && matched is MemberExpression) { var memberExpression = (MemberExpression)matched; if (Mapping.TryGetFieldName(SourceType, memberExpression, out string field)) { var containsSource = ((IEnumerable)((ConstantExpression)source).Value); // If criteria contains a null create an Or criteria with Terms on one // side and Missing on the other. var values = containsSource.Cast <object>().Distinct().ToList(); var nonNullValues = values.Where(v => v != null).ToList(); ICriteria criteria = TermsCriteria.Build(field, memberExpression.Member, nonNullValues); if (values.Count != nonNullValues.Count) { criteria = OrCriteria.Combine(criteria, new MissingCriteria(field)); } return(new CriteriaExpression(criteria)); } } // Where(x => x.SomeList.Contains(constantValue)) if (source is MemberExpression && matched is ConstantExpression) { var memberExpression = (MemberExpression)source; if (Mapping.TryGetFieldName(SourceType, memberExpression, out string field)) { var value = ((ConstantExpression)matched).Value; return(new CriteriaExpression(TermsCriteria.Build(field, memberExpression.Member, value))); } } throw new NotSupportedException(source is MemberExpression ? $"Match '{match}' in Contains operation must be a constant" : $"Unknown source '{source}' for Contains operation"); }
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 VisitOrElse(BinaryExpression b) { var criteria = AssertExpressionsOfType <CriteriaExpression>(b.Left, b.Right).Select(f => f.Criteria).ToArray(); return(new CriteriaExpression(OrCriteria.Combine(criteria))); }
Expression VisitOrElse(BinaryExpression b) { return(new CriteriaExpression( OrCriteria.Combine(CombineExpressions <CriteriaExpression>(b.Left, b.Right).Select(f => f.Criteria).ToArray()))); }
public void CombineThrowArgumentNullExceptionWhenCriteriaIsNull() { Assert.Throws <ArgumentNullException>(() => OrCriteria.Combine(null)); }
public void CombineWithEmptyListReturnsNull() { var criteria = OrCriteria.Combine(new ICriteria[] { }); Assert.Null(criteria); }