public void VisitSearchQueryRule_DefaultOrDynamicValueFound_ReturnsExpression(
            SearchQueryPredicateBuilder <TestIndexableEntity> sut)
        {
            // Arrange
            var value = 1;
            Expression <Func <TestIndexableEntity, bool> > expected = x => (x.SomeInteger == value);

            var indexableEntity = new TestIndexableEntity();
            var searchQueryRule = new SearchQueryRule <TestIndexableEntity>(x => x.SomeInteger, ComparisonOperator.Equal);
            var searchRootItem  = new SearchQueryGrouping <TestIndexableEntity>(LogicalOperator.And);

            searchRootItem.SearchQueryElements.Add(searchQueryRule);

            sut
            .SearchQueryValueProvider
            .GetValueForSearchQueryRule(searchQueryRule)
            .Returns(x => value);

            // Act
            searchRootItem.Accept(sut);
            Expression <Func <TestIndexableEntity, bool> > actual = sut.GetOutput();

            // Assert
            Assert.Equal(actual.Compile().Invoke(indexableEntity), expected.Compile().Invoke(indexableEntity));
        }
        public void VisitSearchQueryGrouping_WithRules_ReturnExpression(
            SearchQueryPredicateBuilder <TestIndexableEntity> sut)
        {
            // Arrange
            Expression <Func <TestIndexableEntity, bool> > expected = x => (x.SomeInteger == 1 || x.SomeBoolean == false);

            var indexableEntity = new TestIndexableEntity();

            var integerEqualsRule = new SearchQueryRule <TestIndexableEntity>(
                x => x.SomeInteger, ComparisonOperator.Equal, null, "1"
                );
            var booleanEqualsRule = new SearchQueryRule <TestIndexableEntity>(
                x => x.SomeBoolean, ComparisonOperator.Equal, null, "false"
                );

            var searchQueryGrouping = new SearchQueryGrouping <TestIndexableEntity>(LogicalOperator.Or);

            searchQueryGrouping.SearchQueryElements.Add(integerEqualsRule);
            searchQueryGrouping.SearchQueryElements.Add(booleanEqualsRule);

            sut.SearchQueryValueProvider.GetValueForSearchQueryRule(integerEqualsRule).Returns(1);
            sut.SearchQueryValueProvider.GetValueForSearchQueryRule(booleanEqualsRule).Returns(false);

            // Act
            searchQueryGrouping.Accept(sut);
            Expression <Func <TestIndexableEntity, bool> > actual = sut.GetOutput();

            // Assert
            Assert.Equal(actual.Compile().Invoke(indexableEntity),
                         expected.Compile().Invoke(indexableEntity));
        }
        private void VisitSearchQueryRule_PropertyOfType_ReturnsExpression(Expression <Func <TestIndexableEntity, object> > propertySelector,
                                                                           TestIndexableEntity indexableEntity,
                                                                           object value,
                                                                           Expression <Func <TestIndexableEntity, bool> > expected)
        {
            // Arrange
            var searchQueryRule = new SearchQueryRule <TestIndexableEntity>(propertySelector, ComparisonOperator.Contains);

            var searchRootItem = new SearchQueryGrouping <TestIndexableEntity>(LogicalOperator.And);

            searchRootItem.SearchQueryElements.Add(searchQueryRule);

            var valueProviderMock = Substitute.For <ISearchQueryValueProvider>();

            valueProviderMock.GetValueForSearchQueryRule(searchQueryRule).Returns(value);

            var sut = new SearchQueryPredicateBuilder <TestIndexableEntity>(valueProviderMock);

            // Act
            searchRootItem.Accept(sut);
            var actual = sut.GetOutput();

            // Assert
            Assert.Equal(actual.Compile().Invoke(indexableEntity), expected.Compile().Invoke(indexableEntity));
        }
        public void VisitSearchQueryRule_PropertyIsOfNonStringType_ReturnsEqualsExpression()
        {
            Expression <Func <TestIndexableEntity, object> > propertySelector = x => x.ItemId;
            var itemId          = new ID(Guid.NewGuid());
            var indexableEntity = new TestIndexableEntity {
                ItemId = itemId
            };
            Expression <Func <TestIndexableEntity, bool> > expected = x => x.ItemId == itemId;

            VisitSearchQueryRule_PropertyOfType_ReturnsExpression(propertySelector, indexableEntity, itemId, expected);
        }
        public void VisitSearchQueryRule_PropertyIsOfTypeString_ReturnsContainsExpression()
        {
            Expression <Func <TestIndexableEntity, object> > propertySelector = x => x.Name;
            var value = "StringValue";
            Expression <Func <TestIndexableEntity, bool> > expected = x => x.Name.Contains(value);
            var indexableEntity = new TestIndexableEntity {
                Name = value
            };

            VisitSearchQueryRule_PropertyOfType_ReturnsExpression(propertySelector, indexableEntity, value, expected);
        }
        public void VisitSearchQueryRule_PropertyIsOfTypeEnumerable_ReturnsContainsExpression()
        {
            Expression <Func <TestIndexableEntity, object> > propertySelector = x => x.Paths;
            var itemId = new ID(Guid.NewGuid());
            Expression <Func <TestIndexableEntity, bool> > expected = x => (x.Paths.Contains(itemId));
            var indexableEntity = new TestIndexableEntity {
                Paths = new List <ID> {
                    itemId
                }
            };

            VisitSearchQueryRule_PropertyOfType_ReturnsExpression(propertySelector, indexableEntity, itemId, expected);
        }
예제 #7
0
        public void GetPropertySelector_PropertyNameIsValid_ReturnsPropertySelectorExpression()
        {
            // Arrange
            string propertyName = "Name";
            TestIndexableEntity indexableEntity = new TestIndexableEntity();
            Expression <Func <TestIndexableEntity, object> > expectedPropertySelector = (x => x.Name);

            // Act
            var actual = ExpressionUtils.GetPropertySelector <TestIndexableEntity, object>(propertyName);

            // Assert
            Assert.Equal(actual.Compile().Invoke(indexableEntity),
                         expectedPropertySelector.Compile().Invoke(indexableEntity));
        }
        public void ToGreaterThanOrEqual_PropertySelectorIsNotNullAndValueIsSet_ReturnsPropertyGreaterThanOrEqualsValueExpression()
        {
            // Arrange
            var value           = 1;
            var indexableEntity = new TestIndexableEntity();
            Expression <Func <TestIndexableEntity, bool> > expected = x => x.SomeInteger >= value;

            // Act
            var actual = ExpressionConversionService.ToGreaterThanOrEqual <TestIndexableEntity>(x => x.SomeInteger, value);

            // Assert
            Assert.Equal(actual.Compile().Invoke(indexableEntity),
                         expected.Compile().Invoke(indexableEntity));
        }
        public void VisitSearchQueryGrouping_EmptySearchQueryGrouping_ReturnExpression(
            SearchQueryPredicateBuilder <TestIndexableEntity> sut)
        {
            // Arrange
            Expression <Func <TestIndexableEntity, bool> > expected = x => true;

            var indexableEntity     = new TestIndexableEntity();
            var searchQueryGrouping = new SearchQueryGrouping <TestIndexableEntity>(LogicalOperator.And);

            // Act
            searchQueryGrouping.Accept(sut);
            Expression <Func <TestIndexableEntity, bool> > actual = sut.GetOutput();

            // Assert
            Assert.Equal(actual.Compile().Invoke(indexableEntity), expected.Compile().Invoke(indexableEntity));
        }
        public void ToContains_PropertySelectorIsNotNullAndValueIsSet_ReturnsPropertyContainsValueExpression()
        {
            // Arrange
            var value           = "Foo";
            var indexableEntity = new TestIndexableEntity {
                Name = value
            };
            Expression <Func <TestIndexableEntity, bool> > expected = x => x.Name.Contains(value);

            // Act
            var actual = ExpressionConversionService.ToContains <TestIndexableEntity>(x => x.Name, value);

            // Assert
            Assert.Equal(actual.Compile().Invoke(indexableEntity),
                         expected.Compile().Invoke(indexableEntity));
        }
예제 #11
0
        private TestIndexableEntity CreateTestIndexableEntity(
            string name,
            string path        = Constants.SearchOptions.DefaultSearchPath,
            List <ID> paths    = null,
            string language    = "en",
            bool latestVersion = true)
        {
            var entity = new TestIndexableEntity
            {
                Name               = name,
                Path               = path,
                Paths              = paths ?? new List <ID>(),
                Language           = language,
                ["_latestversion"] = latestVersion ? "1" : "0"
            };

            return(entity);
        }
        public void ToEnumerableContains_PropertySelectorIsNotNullAndValueIsSet_ReturnsEnumerablePropertyContainsValueExpression()
        {
            // Arrange
            var value           = new ID(Guid.Empty);
            var indexableEntity = new TestIndexableEntity {
                Paths = new List <ID> {
                    value
                }
            };
            Expression <Func <TestIndexableEntity, object> > expected = x => x.Paths.Contains(value);

            // Act
            var actual = ExpressionConversionService.ToEnumerableContains <TestIndexableEntity>(x => x.Paths, value);

            // Assert
            Assert.Equal(actual.Compile().Invoke(indexableEntity),
                         expected.Compile().Invoke(indexableEntity));
        }
        public void ToBetween_PropertySelectorIsNotNullAndLowerAndUpperValuesAreSet_ReturnsPropertyBetweenLowerAndUpperValuesExpression(string propertyName, object minValue, object maxValue, Inclusion inclusion)
        {
            // Arrange
            var propertySelector = ExpressionUtils.GetPropertySelector <TestIndexableEntity, object>(propertyName);
            var indexableEntity  = new TestIndexableEntity
            {
                SomeInteger = 1,
                SomeFloat   = 1f,
                SomeDouble  = 100,
                CreatedDate = DateTime.Now
            };

            Expression <Func <TestIndexableEntity, object> > expected = x => propertySelector.Between(minValue, maxValue, inclusion);

            // Act
            var actual = ExpressionConversionService.ToBetween(propertySelector, minValue, maxValue, inclusion);

            // Assert
            Assert.Equal(actual.Compile().Invoke(indexableEntity),
                         expected.Compile().Invoke(indexableEntity));
        }