Ejemplo n.º 1
0
        public void SetsNameAndMember()
        {
            var criteria = new RangeCriteria("field", memberInfo, RangeComparison.GreaterThan, 1);

            Assert.Equal("range", criteria.Name);
            Assert.Equal(memberInfo, criteria.Member);
        }
Ejemplo n.º 2
0
        public void CombineWithSingleCriteriaReturnsThatCriteria()
        {
            var rangeCriteria = new RangeCriteria("field", memberInfo, RangeComparison.LessThan, 1);
            var andCriteria = OrCriteria.Combine(rangeCriteria);

            Assert.Same(rangeCriteria, andCriteria);
        }
Ejemplo n.º 3
0
        public void ToStringContainsFieldComparisonAndValue()
        {
            var criteria = new RangeCriteria("thisIsMyFieldName", memberInfo, RangeComparison.LessThan, "500");
            var result = criteria.ToString();

            Assert.Contains(criteria.Field, result);
            Assert.Contains(criteria.Specifications[0].Comparison.ToString(), result);
            Assert.Contains((string)criteria.Specifications[0].Value, result);
        }
Ejemplo n.º 4
0
        public void SingleConstructorSetsSingleSpecification()
        {
            var criteria = new RangeCriteria("field", memberInfo, RangeComparison.GreaterThan, 1);

            Assert.Equal("field", criteria.Field);
            Assert.Equal(1, criteria.Specifications.Count);

            var specification = criteria.Specifications.Single();
            Assert.Equal("gt", specification.Name);
            Assert.Equal(1, specification.Value);
        }
        public void BodyContainsBoolMustQuery()
        {
            var rangeCriteria = new RangeCriteria("ranged", memberInfo, RangeComparison.GreaterThanOrEqual, 88);
            var boolMust = new BoolCriteria(new[] { rangeCriteria }, null, null);

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

            var mustItems = body.TraverseWithAssert("query", "bool", "must");
            Assert.Equal(1, mustItems.Count());
            mustItems[0].TraverseWithAssert("range");
        }
Ejemplo n.º 6
0
        public void EnumerableConstructorSetsMultipleSpecifications()
        {
            var greater = new RangeSpecificationCriteria(RangeComparison.GreaterThanOrEqual, "D");
            var less = new RangeSpecificationCriteria(RangeComparison.LessThan, "H");

            var criteria = new RangeCriteria("initials", memberInfo, new[] { greater, less });

            Assert.Equal("initials", criteria.Field);
            Assert.Equal(memberInfo, criteria.Member);
            Assert.Contains(greater, criteria.Specifications);
            Assert.Contains(less, criteria.Specifications);
            Assert.Equal(2, criteria.Specifications.Count);
        }
Ejemplo n.º 7
0
        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 AndWithMixedContentBecomesBoolWithShouldMustAndMustNot()
        {
            var expectedShould = new[] { 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 BodyContainsQueryRange()
        {
            var rangeCriteria = new RangeCriteria("someField", memberInfo,
                new[]
                {
                    new RangeSpecificationCriteria(RangeComparison.LessThan, 100),
                    new RangeSpecificationCriteria(RangeComparison.GreaterThan, 200)
                });

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

            var result = body.TraverseWithAssert("query", "range");
            var actualRange = result.TraverseWithAssert(rangeCriteria.Field);
            Assert.Equal("!!! 100 !!!", actualRange["lt"]);
            Assert.Equal("!!! 200 !!!", actualRange["gt"]);
        }
        public void BodyContainsBoolShouldQuery()
        {
            var range1 = new RangeCriteria("range1", memberInfo, RangeComparison.GreaterThanOrEqual, 88);
            var range2 = new RangeCriteria("range2", memberInfo, RangeComparison.GreaterThanOrEqual, 88);
            var boolMust = new BoolCriteria(null, new[] { range1, range2 }, null);

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

            var boolBody = body.TraverseWithAssert("query", "bool");

            var minMatch = boolBody.TraverseWithAssert("minimum_should_match");
            Assert.Equal(1, minMatch.Value<int>());

            var mustNotItems = boolBody.TraverseWithAssert("should");
            Assert.Equal(2, mustNotItems.Count());
            mustNotItems[0].TraverseWithAssert("range");
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Combine range criteria for the same field into an upper-lower range for that criteria.
        /// </summary>
        /// <param name="criteria">Collection of <see cref="ICriteria"/> to have ranges combined.</param>
        static void CombineRanges(ICollection <ICriteria> criteria)
        {
            var candidates = criteria.OfType <RangeCriteria>().GroupBy(r => r.Field).Where(g => g.Count() > 1).ToArray();

            foreach (var range in candidates)
            {
                var specifications = range.SelectMany(r => r.Specifications).ToList();

                if (RangeCriteria.SpecificationsCanBeCombined(specifications))
                {
                    foreach (var rangeCriteria in range)
                    {
                        criteria.Remove(rangeCriteria);
                    }

                    criteria.Add(new RangeCriteria(range.Key, range.First().Member, specifications));
                }
            }
        }
Ejemplo n.º 12
0
        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);
        }
 JObject Build(RangeCriteria criteria)
 {
     // Range filters can be combined by field
     return new JObject(
         new JProperty(criteria.Name,
             new JObject(new JProperty(criteria.Field,
                 new JObject(criteria.Specifications.Select(s =>
                     new JProperty(s.Name, mapping.FormatValue(criteria.Member, s.Value))).ToList())))));
 }
Ejemplo n.º 14
0
        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 BodyContainsRangeFilter()
        {
            const string expectedField = "capacity";
            const decimal expectedRange = 2.0m;
            var rangeCriteria = new RangeCriteria(expectedField, memberInfo, RangeComparison.GreaterThanOrEqual, expectedRange);

            var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { DocumentType = "type1", Filter = rangeCriteria });
            var body = JObject.Parse(formatter.Body);

            var result = body.TraverseWithAssert("query", "filtered", "filter", "range", expectedField, "gte");
            Assert.Equal("!!! 2.0 !!!", result);
        }
        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 SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { DocumentType = "type1", Filter = orCriteria });
            var body = JObject.Parse(formatter.Body);

            var result = body.TraverseWithAssert("query", "filtered", "filter", "or");
            Assert.Equal(2, result.Children().Count());
            foreach (var child in result)
                Assert.True(((JProperty)(child.First)).Name == "range");
        }