Beispiel #1
0
        public void Test_can_map_binary_andexpression_to_one_criteria_collections()
        {
            // Arrange
            CmsqlQueryBinaryExpression orExpression = new CmsqlQueryBinaryExpression
            {
                Operator       = ConditionalOperator.And,
                LeftExpression = new CmsqlQueryCondition
                {
                    Identifier = "PageName",
                    Operator   = EqualityOperator.GreaterThan,
                    Value      = "5"
                },
                RightExpression = new CmsqlQueryCondition
                {
                    Identifier = "PageName",
                    Operator   = EqualityOperator.Equals,
                    Value      = "10"
                }
            };

            CmsqlExpressionVisitorContext context = new CmsqlExpressionVisitorContext(new ContentType());

            CmsqlExpressionVisitor cmsqlExpressionVisitor =
                new CmsqlExpressionVisitor(
                    new QueryConditionToPropertyCriteriaMapper(
                        new PropertyDataTypeResolver(new ContentType())), context);

            // Act
            cmsqlExpressionVisitor.VisitQueryExpression(orExpression);

            IEnumerable <PropertyCriteriaCollection> propertyCriteriaCollection = context.GetCriteria();

            // Assert
            propertyCriteriaCollection.Should().HaveCount(1);
        }
Beispiel #2
0
        public void Test_when_orexpression_visitor_visits_orexpression_push_criteria_collection_for_every_condition()
        {
            // Arrange
            CmsqlQueryBinaryExpression expression = new CmsqlQueryBinaryExpression
            {
                Operator       = ConditionalOperator.Or,
                LeftExpression = new CmsqlQueryCondition
                {
                    Identifier = "PageName",
                    Operator   = EqualityOperator.GreaterThan,
                    Value      = "5"
                },
                RightExpression = new CmsqlQueryCondition
                {
                    Identifier = "PageName",
                    Operator   = EqualityOperator.GreaterThan,
                    Value      = "5"
                }
            };

            CmsqlExpressionVisitorContext context = new CmsqlExpressionVisitorContext(new ContentType());

            CmsqlExpressionVisitor cmsqlExpressionVisitor =
                new CmsqlBinaryOrExpressionVisitor(
                    new QueryConditionToPropertyCriteriaMapper(
                        new PropertyDataTypeResolver(new ContentType())), context);

            // Act
            cmsqlExpressionVisitor.VisitQueryExpression(expression);

            IEnumerable <PropertyCriteriaCollection> propertyCriteriaCollection = context.GetCriteria();

            // Assert
            propertyCriteriaCollection.Should().HaveCount(2);
        }
Beispiel #3
0
        public void Test_can_map_query_condition_to_property_criteria()
        {
            // Arrange
            CmsqlQueryCondition condition = new CmsqlQueryCondition
            {
                Identifier = "PageName",
                Operator   = EqualityOperator.GreaterThan,
                Value      = "5"
            };

            CmsqlExpressionVisitorContext context = new CmsqlExpressionVisitorContext(new ContentType());

            CmsqlExpressionVisitor cmsqlExpressionVisitor =
                new CmsqlExpressionVisitor(
                    new QueryConditionToPropertyCriteriaMapper(
                        new PropertyDataTypeResolver(new ContentType())), context);

            // Act
            cmsqlExpressionVisitor.VisitQueryCondition(condition);

            PropertyCriteriaCollection propertyCriteriaCollection = context.GetCriteria().Single();

            PropertyCriteria propertyCriteria = propertyCriteriaCollection.Last();

            // Assert
            propertyCriteria.Value.ShouldBeEquivalentTo(condition.Value);
            propertyCriteria.Condition.ShouldBeEquivalentTo(CompareCondition.GreaterThan);
            propertyCriteria.Name.ShouldBeEquivalentTo(condition.Identifier);
        }
Beispiel #4
0
        public void Test_when_condition_cannot_be_mapped_criteria_should_be_empty()
        {
            // Arrange
            CmsqlQueryCondition condition = new CmsqlQueryCondition
            {
                Identifier = "ThisPropertyCannotBeFound",
                Operator   = EqualityOperator.GreaterThan,
                Value      = "5"
            };

            CmsqlExpressionVisitorContext context = new CmsqlExpressionVisitorContext(new ContentType());

            CmsqlExpressionVisitor cmsqlExpressionVisitor =
                new CmsqlExpressionVisitor(
                    new QueryConditionToPropertyCriteriaMapper(
                        new PropertyDataTypeResolver(new ContentType())), context);

            // Act
            cmsqlExpressionVisitor.VisitQueryCondition(condition);

            IEnumerable <PropertyCriteriaCollection> propertyCriteriaCollection = context.GetCriteria();

            // Assert
            propertyCriteriaCollection.Should().BeEmpty();
        }
        public void Test_can_add_criteria_when_criteria_collection_has_been_pushed()
        {
            CmsqlExpressionVisitorContext context = new CmsqlExpressionVisitorContext(new ContentType());

            context.AddPropertyCriteria(new PropertyCriteria());

            IEnumerable <PropertyCriteriaCollection> criteria = context.GetCriteria().ToList();

            criteria.Should().HaveCount(1);
            criteria.Single().Should().HaveCount(2);
        }
Beispiel #6
0
        public CmsqlQueryExecutionResult ExecuteQueries(IEnumerable <CmsqlQuery> queries)
        {
            List <CmsqlQueryExecutionError> errors = new List <CmsqlQueryExecutionError>();
            List <PageData> result = new List <PageData>();

            CmsqlExpressionParser expressionParser = new CmsqlExpressionParser();

            foreach (CmsqlQuery query in queries)
            {
                ContentType contentType = _contentTypeRepository.Load(query.ContentType);
                if (contentType == null)
                {
                    errors.Add(new CmsqlQueryExecutionError($"Couldn't load content-type '{query.ContentType}'."));
                    continue;
                }

                CmsqlExpressionVisitorContext visitorContext = expressionParser.Parse(contentType, query.Criteria);
                if (visitorContext.Errors.Any())
                {
                    errors.AddRange(visitorContext.Errors);
                    continue;
                }

                PageReference searchStartNodeRef = GetStartSearchFromNode(query.StartNode);
                if (PageReference.IsNullOrEmpty(searchStartNodeRef))
                {
                    errors.Add(new CmsqlQueryExecutionError($"Couldn't process start node '{query.StartNode}'."));
                    continue;
                }

                foreach (PropertyCriteriaCollection propertyCriteriaCollection in visitorContext.GetCriteria())
                {
                    PageDataCollection foundPages = _pageCriteriaQueryService.FindPagesWithCriteria(
                        searchStartNodeRef,
                        propertyCriteriaCollection);
                    if (foundPages != null && foundPages.Any())
                    {
                        result.AddRange(foundPages);
                    }
                }
            }

            IEnumerable <ICmsqlQueryResult> pageDataCmsqlQueryResults =
                result.Select(p => new PageDataCmsqlQueryResult(p)).ToList();

            return(new CmsqlQueryExecutionResult(pageDataCmsqlQueryResults, errors));
        }
Beispiel #7
0
        public void Test_when_condition_is_null_criteria_should_be_empty()
        {
            // Arrange
            CmsqlExpressionVisitorContext context = new CmsqlExpressionVisitorContext(new ContentType());

            CmsqlExpressionVisitor cmsqlExpressionVisitor =
                new CmsqlExpressionVisitor(
                    new QueryConditionToPropertyCriteriaMapper(
                        new PropertyDataTypeResolver(new ContentType())), context);

            // Act
            cmsqlExpressionVisitor.VisitQueryCondition(null);

            IEnumerable <PropertyCriteriaCollection> propertyCriteriaCollection = context.GetCriteria();

            // Assert
            propertyCriteriaCollection.Should().BeEmpty();
        }
Beispiel #8
0
        public void Test_can_map_nested_expressions()
        {
            // Arrange
            CmsqlQueryBinaryExpression expressions = new CmsqlQueryBinaryExpression
            {
                Operator       = ConditionalOperator.Or,
                LeftExpression = new CmsqlQueryBinaryExpression
                {
                    Operator       = ConditionalOperator.Or,
                    LeftExpression = new CmsqlQueryBinaryExpression
                    {
                        Operator       = ConditionalOperator.Or,
                        LeftExpression = new CmsqlQueryCondition
                        {
                            Identifier = "PageName",
                            Operator   = EqualityOperator.Equals,
                            Value      = "1"
                        },
                        RightExpression = new CmsqlQueryBinaryExpression
                        {
                            Operator       = ConditionalOperator.Or,
                            LeftExpression = new CmsqlQueryCondition
                            {
                                Identifier = "PageName",
                                Operator   = EqualityOperator.Equals,
                                Value      = "2"
                            },
                            RightExpression = new CmsqlQueryCondition
                            {
                                Identifier = "PageName",
                                Operator   = EqualityOperator.Equals,
                                Value      = "3"
                            }
                        }
                    },
                    RightExpression = new CmsqlQueryBinaryExpression
                    {
                        Operator       = ConditionalOperator.Or,
                        LeftExpression = new CmsqlQueryCondition
                        {
                            Identifier = "PageName",
                            Operator   = EqualityOperator.Equals,
                            Value      = "4"
                        },
                        RightExpression = new CmsqlQueryCondition
                        {
                            Identifier = "PageName",
                            Operator   = EqualityOperator.Equals,
                            Value      = "5"
                        }
                    }
                },
                RightExpression = new CmsqlQueryBinaryExpression
                {
                    Operator        = ConditionalOperator.And,
                    RightExpression = new CmsqlQueryCondition
                    {
                        Identifier = "PageName",
                        Operator   = EqualityOperator.Equals,
                        Value      = "6"
                    },
                    LeftExpression = new CmsqlQueryCondition
                    {
                        Identifier = "PageName",
                        Operator   = EqualityOperator.Equals,
                        Value      = "7"
                    }
                }
            };

            CmsqlExpressionVisitorContext context = new CmsqlExpressionVisitorContext(new ContentType());

            CmsqlExpressionVisitor cmsqlExpressionVisitor =
                new CmsqlExpressionVisitor(
                    new QueryConditionToPropertyCriteriaMapper(
                        new PropertyDataTypeResolver(new ContentType())), context);

            // Act
            cmsqlExpressionVisitor.VisitQueryExpression(expressions);

            IEnumerable <PropertyCriteriaCollection> propertyCriteriaCollection = context.GetCriteria();

            // Assert
            propertyCriteriaCollection.Should().HaveCount(6);
        }