예제 #1
0
        public void ProcessLevelsCorrectly_WithMaxNestedLevels()
        {
            // Arrange
            var model   = ODataLevelsTest.GetEdmModel();
            var context = new ODataQueryContext(
                model,
                model.FindDeclaredType("Microsoft.AspNetCore.OData.Tests.Query.LevelsEntity"));

            context.RequestContainer = new MockServiceProvider();
            var selectExpand = new SelectExpandQueryOption(
                select: null,
                expand: "Parent($expand=DerivedAncestors($levels=max);$levels=2)",
                context: context);

            // Act
            SelectExpandClause clause = selectExpand.ProcessLevels();

            // Assert
            Assert.True(clause.AllSelected);
            Assert.Single(clause.SelectedItems);

            // Level 1 of Parent.
            var parent = Assert.IsType <ExpandedNavigationSelectItem>(clause.SelectedItems.Single());

            Assert.Equal(
                "Parent",
                ((NavigationPropertySegment)parent.PathToNavigationProperty.FirstSegment).NavigationProperty.Name);
            Assert.Null(parent.LevelsOption);

            var clauseOfParent = parent.SelectAndExpand;

            Assert.True(clauseOfParent.AllSelected);
            Assert.Equal(2, clauseOfParent.SelectedItems.Count());

            // Level 1 of DerivedAncestors.
            var derivedAncestors = Assert.Single(
                clauseOfParent.SelectedItems.OfType <ExpandedNavigationSelectItem>().Where(
                    item => item.PathToNavigationProperty.FirstSegment is NavigationPropertySegment &&
                    ((NavigationPropertySegment)item.PathToNavigationProperty.FirstSegment).NavigationProperty.Name == "DerivedAncestors")
                );

            Assert.Null(derivedAncestors.LevelsOption);

            var clauseOfDerivedAncestors = derivedAncestors.SelectAndExpand;

            Assert.True(clauseOfDerivedAncestors.AllSelected);
            Assert.Empty(clauseOfDerivedAncestors.SelectedItems);

            // Level 2 of Parent.
            parent = Assert.Single(
                clauseOfParent.SelectedItems.OfType <ExpandedNavigationSelectItem>().Where(
                    item => item.PathToNavigationProperty.FirstSegment is NavigationPropertySegment &&
                    ((NavigationPropertySegment)item.PathToNavigationProperty.FirstSegment).NavigationProperty.Name == "Parent")
                );
            Assert.Null(parent.LevelsOption);

            clauseOfParent = parent.SelectAndExpand;
            Assert.True(clauseOfParent.AllSelected);
            Assert.Empty(clauseOfParent.SelectedItems);
        }
예제 #2
0
        public void ProcessLevelsCorrectly_PreserveOtherOptions(bool autoSelect)
        {
            // Arrange
            var model      = ODataLevelsTest.GetEdmModel();
            var entityType = model.FindDeclaredType("Microsoft.AspNetCore.OData.Tests.Query.LevelsEntity");
            var context    = new ODataQueryContext(
                model,
                entityType);

            if (autoSelect)
            {
                var modelBound = model.GetAnnotationValue <ModelBoundQuerySettings>(entityType) ?? new ModelBoundQuerySettings();
                modelBound.DefaultSelectType = SelectExpandType.Automatic;
                model.SetAnnotationValue(entityType, modelBound);
            }

            context.RequestContainer = new MockServiceProvider();
            var selectExpand = new SelectExpandQueryOption(
                select: null,
                expand: "Parent($filter=Cnt gt 1;$apply=aggregate($count as Cnt))",
                context: context);

            selectExpand.LevelsMaxLiteralExpansionDepth = 1;

            // Act
            SelectExpandClause clause = selectExpand.ProcessLevels();

            // Assert
            var item = Assert.IsType <ExpandedNavigationSelectItem>(clause.SelectedItems.Single());

            Assert.NotNull(item.FilterOption);
            Assert.NotNull(item.ApplyOption);
        }
예제 #3
0
        public void ProcessLevelsCorrectly_AllSelected()
        {
            // Arrange
            var model   = ODataLevelsTest.GetEdmModel();
            var context = new ODataQueryContext(
                model,
                model.FindDeclaredType("Microsoft.AspNetCore.OData.Tests.Query.LevelsEntity"));

            context.RequestContainer = new MockServiceProvider();
            var selectExpand = new SelectExpandQueryOption(
                select: null,
                expand: "Parent($expand=Parent($levels=2))",
                context: context);

            selectExpand.LevelsMaxLiteralExpansionDepth = 3;

            // Act
            SelectExpandClause clause = selectExpand.ProcessLevels();

            // Assert
            // Level 1.
            Assert.True(clause.AllSelected);
            Assert.Single(clause.SelectedItems);

            var item = Assert.IsType <ExpandedNavigationSelectItem>(clause.SelectedItems.Single());

            Assert.Equal(
                "Parent",
                ((NavigationPropertySegment)item.PathToNavigationProperty.FirstSegment).NavigationProperty.Name);
            Assert.Null(item.LevelsOption);

            // Level 2.
            clause = item.SelectAndExpand;
            Assert.True(clause.AllSelected);
            Assert.Single(clause.SelectedItems);

            item = Assert.IsType <ExpandedNavigationSelectItem>(clause.SelectedItems.Single());
            Assert.Equal(
                "Parent",
                ((NavigationPropertySegment)item.PathToNavigationProperty.FirstSegment).NavigationProperty.Name);
            Assert.Null(item.LevelsOption);

            // Level 3.
            clause = item.SelectAndExpand;
            Assert.True(clause.AllSelected);
            Assert.Single(clause.SelectedItems);

            item = Assert.IsType <ExpandedNavigationSelectItem>(clause.SelectedItems.Single());
            Assert.Equal(
                "Parent",
                ((NavigationPropertySegment)item.PathToNavigationProperty.FirstSegment).NavigationProperty.Name);
            Assert.Null(item.LevelsOption);

            clause = item.SelectAndExpand;
            Assert.True(clause.AllSelected);
            Assert.Empty(clause.SelectedItems);
        }
예제 #4
0
        public void ProcessLevelsCorrectly_WithMultipleProperties()
        {
            // Arrange
            var model   = ODataLevelsTest.GetEdmModel();
            var context = new ODataQueryContext(
                model,
                model.FindDeclaredType("Microsoft.AspNetCore.OData.Tests.Query.LevelsEntity"));

            context.RequestContainer = new MockServiceProvider();
            var selectExpand = new SelectExpandQueryOption(
                select: null,
                expand: "Parent($expand=Parent($levels=max),DerivedAncestors($levels=2;$select=ID)),BaseEntities($levels=2)",
                context: context);

            selectExpand.LevelsMaxLiteralExpansionDepth = 3;

            // Act
            SelectExpandClause clause = selectExpand.ProcessLevels();

            // Assert
            Assert.True(clause.AllSelected);
            Assert.Equal(2, clause.SelectedItems.Count());

            // Top level Parent.
            var parent = Assert.Single(clause.SelectedItems.OfType <ExpandedNavigationSelectItem>().Where(
                                           item => item.PathToNavigationProperty.FirstSegment is NavigationPropertySegment &&
                                           ((NavigationPropertySegment)item.PathToNavigationProperty.FirstSegment).NavigationProperty.Name == "Parent"));

            Assert.Null(parent.LevelsOption);

            var clauseOfParent = parent.SelectAndExpand;

            Assert.True(clauseOfParent.AllSelected);
            Assert.Equal(2, clauseOfParent.SelectedItems.Count());

            // Level 1 of inline Parent.
            var inlineParent = Assert.Single(clauseOfParent.SelectedItems.OfType <ExpandedNavigationSelectItem>().Where(
                                                 item => item.PathToNavigationProperty.FirstSegment is NavigationPropertySegment &&
                                                 ((NavigationPropertySegment)item.PathToNavigationProperty.FirstSegment).NavigationProperty.Name == "Parent"));

            Assert.Null(inlineParent.LevelsOption);

            // Level 2 of inline Parent.
            var inlineParentClause = inlineParent.SelectAndExpand;

            Assert.True(inlineParentClause.AllSelected);
            Assert.Single(inlineParentClause.SelectedItems);

            inlineParent = Assert.IsType <ExpandedNavigationSelectItem>(inlineParentClause.SelectedItems.Single());
            Assert.Equal(
                "Parent",
                ((NavigationPropertySegment)inlineParent.PathToNavigationProperty.FirstSegment).NavigationProperty.Name);
            Assert.Null(inlineParent.LevelsOption);

            inlineParentClause = inlineParent.SelectAndExpand;
            Assert.True(inlineParentClause.AllSelected);
            Assert.Empty(inlineParentClause.SelectedItems);

            // Level 1 of inline DerivedAncestors.
            var inlineDerivedAncestors = Assert.Single(clauseOfParent.SelectedItems.OfType <ExpandedNavigationSelectItem>().Where(
                                                           item => item.PathToNavigationProperty.FirstSegment is NavigationPropertySegment &&
                                                           ((NavigationPropertySegment)item.PathToNavigationProperty.FirstSegment).NavigationProperty.Name == "DerivedAncestors"));

            Assert.Null(inlineDerivedAncestors.LevelsOption);

            // Level 2 of inline DerivedAncestors.
            var inlineDerivedAncestorsClause = inlineDerivedAncestors.SelectAndExpand;

            Assert.False(inlineDerivedAncestorsClause.AllSelected);
            Assert.Equal(3, inlineDerivedAncestorsClause.SelectedItems.Count());

            var idItem = Assert.Single(inlineDerivedAncestorsClause.SelectedItems.OfType <PathSelectItem>().Where(
                                           item => item.SelectedPath.FirstSegment is PropertySegment));

            Assert.Equal("ID", ((PropertySegment)idItem.SelectedPath.FirstSegment).Property.Name);

            var derivedAncestorsItem = Assert.Single(inlineDerivedAncestorsClause.SelectedItems.OfType <PathSelectItem>().Where(
                                                         item => item.SelectedPath.FirstSegment is NavigationPropertySegment));

            Assert.Equal(
                "DerivedAncestors",
                ((NavigationPropertySegment)derivedAncestorsItem.SelectedPath.FirstSegment).NavigationProperty.Name);

            inlineDerivedAncestors = Assert.Single(inlineDerivedAncestorsClause.SelectedItems.OfType <ExpandedNavigationSelectItem>());
            Assert.Equal(
                "DerivedAncestors",
                ((NavigationPropertySegment)inlineDerivedAncestors.PathToNavigationProperty.FirstSegment).NavigationProperty.Name);
            Assert.Null(inlineDerivedAncestors.LevelsOption);

            inlineDerivedAncestorsClause = inlineDerivedAncestors.SelectAndExpand;
            Assert.False(inlineDerivedAncestorsClause.AllSelected);
            Assert.Single(inlineDerivedAncestorsClause.SelectedItems);

            idItem = Assert.Single(inlineDerivedAncestorsClause.SelectedItems.OfType <PathSelectItem>());
            Assert.Equal("ID", ((PropertySegment)idItem.SelectedPath.FirstSegment).Property.Name);

            // Level 1 of BaseEntities.
            var baseEntities = Assert.Single(clause.SelectedItems.OfType <ExpandedNavigationSelectItem>().Where(
                                                 item => item.PathToNavigationProperty.FirstSegment is NavigationPropertySegment &&
                                                 ((NavigationPropertySegment)item.PathToNavigationProperty.FirstSegment).NavigationProperty.Name == "BaseEntities"));

            Assert.Null(baseEntities.LevelsOption);

            // Level 2 of BaseEntities.
            var baseEntitiesClause = baseEntities.SelectAndExpand;

            Assert.True(baseEntitiesClause.AllSelected);
            Assert.Single(baseEntitiesClause.SelectedItems);

            baseEntities = Assert.IsType <ExpandedNavigationSelectItem>(baseEntitiesClause.SelectedItems.Single());
            Assert.Equal(
                "BaseEntities",
                ((NavigationPropertySegment)baseEntities.PathToNavigationProperty.FirstSegment).NavigationProperty.Name);
            Assert.Null(baseEntities.LevelsOption);

            baseEntitiesClause = baseEntities.SelectAndExpand;
            Assert.True(baseEntitiesClause.AllSelected);
            Assert.Empty(baseEntitiesClause.SelectedItems);
        }
예제 #5
0
        public void ProcessLevelsCorrectly_NotAllSelected()
        {
            // Arrange
            var model   = ODataLevelsTest.GetEdmModel();
            var context = new ODataQueryContext(
                model,
                model.FindDeclaredType("Microsoft.AspNetCore.OData.Tests.Query.LevelsEntity"));

            context.RequestContainer = new MockServiceProvider();
            var selectExpand = new SelectExpandQueryOption(
                select: "Name",
                expand: "Parent($select=ID;$levels=max)",
                context: context);

            // Act
            SelectExpandClause clause = selectExpand.ProcessLevels();

            // Assert
            // Level 1.
            Assert.False(clause.AllSelected);
            Assert.Equal(2, clause.SelectedItems.Count());

            var nameSelectItem = Assert.Single(clause.SelectedItems.OfType <PathSelectItem>().Where(
                                                   item => item.SelectedPath.FirstSegment is PropertySegment));

            Assert.Equal("Name", ((PropertySegment)nameSelectItem.SelectedPath.FirstSegment).Property.Name);

            // Before ODL 7.6, the expand navigation property will be added as a select item (PathSelectItem).
            // After ODL 7.6 (include 7.6), the expand navigation property will not be added.
            // Comment the following codes for visibility later.

            /*
             * var parentSelectItem = Assert.Single(clause.SelectedItems.OfType<PathSelectItem>().Where(
             *  item => item.SelectedPath.FirstSegment is NavigationPropertySegment));
             * Assert.Equal(
             *  "Parent",
             *  ((NavigationPropertySegment)parentSelectItem.SelectedPath.FirstSegment).NavigationProperty.Name);
             */
            Assert.Empty(clause.SelectedItems.OfType <PathSelectItem>().Where(item => item.SelectedPath.FirstSegment is NavigationPropertySegment));

            var expandedItem = Assert.Single(clause.SelectedItems.OfType <ExpandedNavigationSelectItem>());

            Assert.Equal(
                "Parent",
                ((NavigationPropertySegment)expandedItem.PathToNavigationProperty.FirstSegment).NavigationProperty.Name);
            Assert.Null(expandedItem.LevelsOption);

            // Level 2.
            clause = expandedItem.SelectAndExpand;
            Assert.False(clause.AllSelected);
            Assert.Equal(3, clause.SelectedItems.Count());

            var idSelectItem = Assert.Single(clause.SelectedItems.OfType <PathSelectItem>().Where(
                                                 item => item.SelectedPath.FirstSegment is PropertySegment));

            Assert.Equal("ID", ((PropertySegment)idSelectItem.SelectedPath.FirstSegment).Property.Name);

            var parentSelectItem = Assert.Single(clause.SelectedItems.OfType <PathSelectItem>().Where(
                                                     item => item.SelectedPath.FirstSegment is NavigationPropertySegment));

            Assert.Equal(
                "Parent",
                ((NavigationPropertySegment)parentSelectItem.SelectedPath.FirstSegment).NavigationProperty.Name);

            expandedItem = Assert.Single(clause.SelectedItems.OfType <ExpandedNavigationSelectItem>());
            Assert.Equal(
                "Parent",
                ((NavigationPropertySegment)expandedItem.PathToNavigationProperty.FirstSegment).NavigationProperty.Name);
            Assert.Null(expandedItem.LevelsOption);

            clause = expandedItem.SelectAndExpand;
            Assert.False(clause.AllSelected);
            Assert.Single(clause.SelectedItems);

            idSelectItem = Assert.IsType <PathSelectItem>(clause.SelectedItems.Single());
            Assert.Equal("ID", ((PropertySegment)idSelectItem.SelectedPath.FirstSegment).Property.Name);
        }