Esempio n. 1
0
        public void Constructor_IQueryable_ShouldApplyCustomParameters()
        {
            context = new PaginationContext<KeyValuePair<string, int>>(queryable, 2, 1, 5);

            Assert.That(context.PageNumber, Is.EqualTo(2));
            Assert.That(context.PageSize, Is.EqualTo(1));
            Assert.That(context.PageLinkMaxCount, Is.EqualTo(5));
        }
        public void Constructor_IEnumerable_ShouldApplyDefaultParameters()
        {
            context = new PaginationContext<KeyValuePair<string, int>>(queryable.AsEnumerable());

            Assert.That(context.Collection, Is.EqualTo(searchResult.AsQueryable()));
            Assert.That(context.PageNumber, Is.EqualTo(1));
            Assert.That(context.PageSize, Is.EqualTo(25));
            Assert.That(context.PageLinkMaxCount, Is.EqualTo(10));
        }
Esempio n. 3
0
 // TODO to be remove, for test case only
 public SelectCommandContext(SelectCommand sqlCommand, GroupByContext groupByContext,
                             OrderByContext orderByContext, ProjectionsContext projectionsContext, PaginationContext paginationContext) : base(sqlCommand)
 {
     _tablesContext           = new TablesContext(sqlCommand.GetSimpleTableSegments());
     this._groupByContext     = groupByContext;
     this._orderByContext     = orderByContext;
     this._projectionsContext = projectionsContext;
     this._paginationContext  = paginationContext;
     _containsSubQuery        = ContainsSubQuery();
 }
Esempio n. 4
0
        public void TakesCorrectElements()
        {
            var obj     = Get.People(100);
            var context = new PaginationContext(GetQueryForPage(1), 10);
            var result  = (Query.ApplyPagination(obj, context, new PersonResource())
                           as IEnumerable <Person>)?.ToList();

            Assert.Equal(10, result?.Count);
            Assert.Equal("10", result?.FirstOrDefault()?.Identifier);
        }
        /// <summary>
        /// See base class documentation.
        /// </summary>
        /// <param name="actionContext">The action context.</param>
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            var context = new PaginationContext(
                actionContext.HttpContext.Request.GetQueryNameValuePairs(),
                PerPage);

            var query = GetQueryContext(actionContext);

            query.Pagination = context;
            base.OnActionExecuting(actionContext);
        }
Esempio n. 6
0
        /// <summary>
        /// See base class documentation.
        /// </summary>
        /// <param name="actionContext">The action context.</param>
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var paginationContext = new PaginationContext(
                actionContext.Request.GetQueryNameValuePairs(),
                _perPage,
                _queryPageSizeLimit);

            var query = GetQueryContext(actionContext);

            query.Pagination = paginationContext;
            base.OnActionExecuting(actionContext);
        }
Esempio n. 7
0
 public ResourceSerializer(
     object value,
     ApiResource type,
     Uri baseUrl,
     IUrlPathBuilder urlBuilder,
     PaginationContext paginationContext)
 {
     _urlBuilder        = urlBuilder;
     _resource          = type;
     _value             = value;
     _baseUrl           = baseUrl;
     _paginationContext = paginationContext;
     _includedSection   = new JArray();
 }
        public void SetUp()
        {
            _searchResult = new List <KeyValuePair <string, int> >
            {
                new KeyValuePair <string, int>("a", 99),
                new KeyValuePair <string, int>("b", 98),
                new KeyValuePair <string, int>("c", 97),
                new KeyValuePair <string, int>("d", 96),
                new KeyValuePair <string, int>("e", 95)
            };

            _queryable = _searchResult.AsQueryable();
            _context   = new PaginationContext <KeyValuePair <string, int> >(_queryable, 1, 25, 10);
        }
Esempio n. 9
0
 public ResourceSerializer(
     object value,
     ApiResource type,
     Uri baseUrl,
     IUrlPathBuilder urlBuilder,
     PaginationContext paginationContext,
     IncludingContext includingContext)
 {
     _urlBuilder         = urlBuilder;
     _resource           = type;
     _value              = value;
     _baseUrl            = baseUrl;
     _paginationContext  = paginationContext;
     _includingContext   = includingContext;
     _includedGraphPaths = IncludedGraphPathsFromContext(includingContext);
 }
Esempio n. 10
0
        public void FirstPageWorks()
        {
            var context = new PaginationContext(EmptyQuery, 10);
            var target  = new PaginationQuery(context);

            Assert.Equal("?page[number]=0", target.FirstPage);

            context = new PaginationContext(GetQuery(Constants.PageNumberQueryName, "0"), 10);
            target  = new PaginationQuery(context);

            Assert.Equal("?page[number]=0", target.FirstPage);

            context = new PaginationContext(GetQuery(Constants.PageNumberQueryName, "4"), 10);
            target  = new PaginationQuery(context);

            Assert.Equal("?page[number]=0", target.FirstPage);
        }
Esempio n. 11
0
 public ResourceSerializer(
     object value,
     ApiResource type,
     Uri baseUrl,
     IUrlPathBuilder urlBuilder,
     PaginationContext paginationContext,
     IncludeContext includeContext,
     IPropertyNameConverter propertyNameConverter = null)
 {
     _propertyNameConverter = propertyNameConverter ?? new DefaultPropertyNameConverter();
     _urlBuilder            = urlBuilder;
     _resource           = type;
     _value              = value;
     _baseUrl            = baseUrl;
     _paginationContext  = paginationContext;
     _includeContext     = includeContext;
     _includedGraphPaths = IncludedGraphPathsFromContext(includeContext);
 }
Esempio n. 12
0
        private JsonApiResourceService <TodoItem> GetService()
        {
            var options                    = new JsonApiOptions();
            var changeTracker              = new ResourceChangeTracker <TodoItem>(options, _resourceGraph, new TargetedFields());
            var serviceProvider            = new ServiceContainer();
            var resourceFactory            = new ResourceFactory(serviceProvider);
            var resourceDefinitionProvider = new ResourceDefinitionProvider(_resourceGraph, new TestScopedServiceProvider(serviceProvider));
            var paginationContext          = new PaginationContext();
            var composer                   = new QueryLayerComposer(new List <IQueryConstraintProvider>(), _resourceGraph, resourceDefinitionProvider, options, paginationContext);
            var currentRequest             = new CurrentRequest
            {
                PrimaryResource   = _resourceGraph.GetResourceContext <TodoItem>(),
                SecondaryResource = _resourceGraph.GetResourceContext <TodoItemCollection>(),
                Relationship      = _resourceGraph.GetRelationships(typeof(TodoItem))
                                    .Single(x => x.PublicName == "collection")
            };

            return(new JsonApiResourceService <TodoItem>(_repositoryMock.Object, composer, paginationContext, options,
                                                         NullLoggerFactory.Instance, currentRequest, changeTracker, resourceFactory, null));
        }
Esempio n. 13
0
        public static object ApplyPagination(object data, PaginationContext context, ApiResource resource)
        {
            var queryable = data as IQueryable;

            if (queryable != null)
            {
                return(new PaginationInterpreter(context, resource).Apply(queryable));
            }

            var enumerable = data as IEnumerable;

            if (enumerable != null)
            {
                // all queryables are enumerable, so this needs to be after
                // the queryable case
                return(new PaginationInterpreter(context, resource).Apply(enumerable));
            }

            return(data);
        }
        public void PageLinkCount_ShouldLimitToPageLinkMaxCount()
        {
            var searchResult = new List <KeyValuePair <string, int> >();

            for (var i = 0; i < 500; i++)
            {
                searchResult.Add(new KeyValuePair <string, int>(i.ToString(CultureInfo.InvariantCulture), i));
            }

            _context = new PaginationContext <KeyValuePair <string, int> >(searchResult.AsQueryable(), 1, 50, 9);

            Assert.That(_context.PageCount, Is.EqualTo(10));
            Assert.That(_context.PageLinkCount, Is.EqualTo(9));

            _context.PageLinkMaxCount = 11;
            _context.PageSize         = 50;

            Assert.That(_context.PageCount, Is.EqualTo(10));
            Assert.That(_context.PageLinkCount, Is.EqualTo(10));
        }
Esempio n. 15
0
        public void Applies_cascading_settings_for_resource_links(LinkTypes linksInResourceContext, LinkTypes linksInOptions, LinkTypes expected)
        {
            // Arrange
            var exampleResourceContext = new ResourceContext
            {
                PublicName    = nameof(ExampleResource),
                ResourceType  = typeof(ExampleResource),
                ResourceLinks = linksInResourceContext
            };

            var resourceGraph = new ResourceGraph(new[]
            {
                exampleResourceContext
            });

            var request = new JsonApiRequest();

            var paginationContext = new PaginationContext();

            var queryStringAccessor = new EmptyRequestQueryStringAccessor();

            var options = new JsonApiOptions
            {
                ResourceLinks = linksInOptions
            };

            var linkBuilder = new LinkBuilder(options, request, paginationContext, resourceGraph, queryStringAccessor);

            // Act
            var resourceLinks = linkBuilder.GetResourceLinks(nameof(ExampleResource), "id");

            // Assert
            if (expected == LinkTypes.Self)
            {
                resourceLinks.Self.Should().NotBeNull();
            }
            else
            {
                resourceLinks.Should().BeNull();
            }
        }
Esempio n. 16
0
        /// <inheritdoc/>
        public IReadOnlyCollection <ExpressionInScope> GetConstraints()
        {
            var context = new PaginationContext();

            foreach (var element in _pageSizeConstraint?.Elements ?? Array.Empty <PaginationElementQueryStringValueExpression>())
            {
                var entry = context.ResolveEntryInScope(element.Scope);
                entry.PageSize       = element.Value == 0 ? null : new PageSize(element.Value);
                entry.HasSetPageSize = true;
            }

            foreach (var element in _pageNumberConstraint?.Elements ?? Array.Empty <PaginationElementQueryStringValueExpression>())
            {
                var entry = context.ResolveEntryInScope(element.Scope);
                entry.PageNumber = new PageNumber(element.Value);
            }

            context.ApplyOptions(_options);

            return(context.GetExpressionsInScope());
        }
        public void Applies_cascading_settings_for_resource_links(LinkTypes linksInResourceContext, LinkTypes linksInOptions, LinkTypes expected)
        {
            // Arrange
            var exampleResourceContext = new ResourceContext
            {
                PublicName    = nameof(ExampleResource),
                ResourceType  = typeof(ExampleResource),
                ResourceLinks = linksInResourceContext
            };

            var options = new JsonApiOptions
            {
                ResourceLinks = linksInOptions
            };

            var request                   = new JsonApiRequest();
            var paginationContext         = new PaginationContext();
            var resourceGraph             = new ResourceGraph(exampleResourceContext.AsArray());
            var httpContextAccessor       = new FakeHttpContextAccessor();
            var linkGenerator             = new FakeLinkGenerator();
            var controllerResourceMapping = new FakeControllerResourceMapping();

            var linkBuilder = new LinkBuilder(options, request, paginationContext, resourceGraph, httpContextAccessor, linkGenerator,
                                              controllerResourceMapping);

            // Act
            ResourceLinks resourceLinks = linkBuilder.GetResourceLinks(nameof(ExampleResource), "id");

            // Assert
            if (expected == LinkTypes.Self)
            {
                resourceLinks.Self.Should().NotBeNull();
            }
            else
            {
                resourceLinks.Should().BeNull();
            }
        }
Esempio n. 18
0
        public void SetUp()
        {
            searchResult = new List<KeyValuePair<string, int>>
            {
                new KeyValuePair<string, int>("a", 99),
                new KeyValuePair<string, int>("b", 98),
                new KeyValuePair<string, int>("c", 97),
                new KeyValuePair<string, int>("d", 96),
                new KeyValuePair<string, int>("e", 95)
            };

            queryable = searchResult.AsQueryable();
            context = new PaginationContext<KeyValuePair<string, int>>(queryable, 1, 25, 10);
        }
Esempio n. 19
0
 public Task <IEnumerable <Device> > GetDevicesAsync(PaginationContext paginationContext)
 {
     return(Task.FromResult(DeviceService.DataStore.Values.AsEnumerable()));
 }
Esempio n. 20
0
        public void Applies_cascading_settings_for_top_level_links(LinkTypes linksInResourceContext, LinkTypes linksInOptions, LinkTypes expected)
        {
            // Arrange
            var exampleResourceContext = new ResourceContext
            {
                PublicName    = nameof(ExampleResource),
                ResourceType  = typeof(ExampleResource),
                TopLevelLinks = linksInResourceContext
            };

            var resourceGraph = new ResourceGraph(new[]
            {
                exampleResourceContext
            });

            var request = new JsonApiRequest
            {
                PrimaryResource = exampleResourceContext,
                PrimaryId       = "1",
                IsCollection    = true,
                Kind            = EndpointKind.Relationship,
                Relationship    = new HasOneAttribute()
            };

            var paginationContext = new PaginationContext
            {
                PageSize           = new PageSize(1),
                PageNumber         = new PageNumber(2),
                TotalResourceCount = 10
            };

            var queryStringAccessor = new EmptyRequestQueryStringAccessor();

            var options = new JsonApiOptions
            {
                TopLevelLinks = linksInOptions
            };

            var linkBuilder = new LinkBuilder(options, request, paginationContext, resourceGraph, queryStringAccessor);

            // Act
            var topLevelLinks = linkBuilder.GetTopLevelLinks();

            // Assert
            if (expected == LinkTypes.None)
            {
                topLevelLinks.Should().BeNull();
            }
            else
            {
                if (expected.HasFlag(LinkTypes.Self))
                {
                    topLevelLinks.Self.Should().NotBeNull();
                }
                else
                {
                    topLevelLinks.Self.Should().BeNull();
                }

                if (expected.HasFlag(LinkTypes.Related))
                {
                    topLevelLinks.Related.Should().NotBeNull();
                }
                else
                {
                    topLevelLinks.Related.Should().BeNull();
                }

                if (expected.HasFlag(LinkTypes.Paging))
                {
                    topLevelLinks.First.Should().NotBeNull();
                    topLevelLinks.Last.Should().NotBeNull();
                    topLevelLinks.Prev.Should().NotBeNull();
                    topLevelLinks.Next.Should().NotBeNull();
                }
                else
                {
                    topLevelLinks.First.Should().BeNull();
                    topLevelLinks.Last.Should().BeNull();
                    topLevelLinks.Prev.Should().BeNull();
                    topLevelLinks.Next.Should().BeNull();
                }
            }
        }
Esempio n. 21
0
 private void RewriteOffset(PaginationContext pagination, int offsetParameterIndex, StandardParameterBuilder parameterBuilder)
 {
     parameterBuilder.AddReplacedParameters(offsetParameterIndex, pagination.GetRevisedOffset());
 }
Esempio n. 22
0
 public Task <IEnumerable <User> > GetUsersAsync(PaginationContext paginationContext)
 {
     return(Task.FromResult(UserService.DataStore.Values.AsEnumerable()));
 }
Esempio n. 23
0
        public void PageLinkCount_ShouldLimitToPageLinkMaxCount()
        {
            var searchResult = new List<KeyValuePair<string, int>>();
            for (var i=0; i<500; i++)
                searchResult.Add(new KeyValuePair<string, int>(i.ToString(), i));

            context = new PaginationContext<KeyValuePair<string, int>>(searchResult.AsQueryable(), 1, 50, 9);

            Assert.That(context.PageCount, Is.EqualTo(10));
            Assert.That(context.PageLinkCount, Is.EqualTo(9));

            context.PageLinkMaxCount = 11;
            context.PageSize = 50;

            Assert.That(context.PageCount, Is.EqualTo(10));
            Assert.That(context.PageLinkCount, Is.EqualTo(10));
        }
 public LimitDecoratorStreamDataReader(IStreamDataReader streamDataReader, PaginationContext pagination) : base(streamDataReader)
 {
     this.pagination = pagination;
     skipAll         = SkipOffset();
 }
Esempio n. 25
0
 private void rewriteRowCount(PaginationContext pagination, int rowCountParameterIndex, StandardParameterBuilder parameterBuilder, SelectCommandContext selectCommandContext)
 {
     parameterBuilder.AddReplacedParameters(rowCountParameterIndex, pagination.GetRevisedRowCount(selectCommandContext));
 }