Ejemplo n.º 1
0
        public void HasPrevious_WhenStartOffsetIsZero_ReturnsFalse()
        {
            // Arrange
            var context = CreateContext();

            // Act
            var slice = SliceMetrics.Create(DbContext.Blogs, context);

            // Assert
            Assert.Equal(0, slice.StartIndex);
            Assert.False(slice.HasPrevious);
        }
Ejemplo n.º 2
0
        public void HasPrevious_WhenStartOffsetIsGreater0_ReturnsTrue()
        {
            // Arrange
            var context = CreateContext(
                after: ConnectionUtils.OffsetToCursor(0)
                );

            // Act
            var slice = SliceMetrics.Create(DbContext.Blogs, context);

            // Assert
            Assert.Equal(1, slice.StartIndex);
            Assert.True(slice.HasPrevious);
        }
Ejemplo n.º 3
0
        public void HasNext_WhenDataIsSliceAndEndOffsetIsLastIndex_ReturnsFalse()
        {
            // Arrange
            var context = CreateContext(
                after: ConnectionUtils.OffsetToCursor(999)
                );

            // Act
            var slice = SliceMetrics.Create(DbContext.Blogs, context);

            // Assert
            Assert.Equal(1000, slice.StartIndex);
            Assert.Equal(0, slice.SliceSize);
            Assert.False(slice.HasNext);
        }
        public void HasPrevious_WhenDataIsSliceAndStartOffsetIsZero_ReturnsFalse()
        {
            // Arrange
            var context = CreateContext(
                first: 2
                );

            // Act
            var slice = SliceMetrics.Create(Blogs, context);

            // Assert
            Assert.Equal(0, slice.StartIndex);
            Assert.Equal(2, slice.SliceSize);
            Assert.False(slice.HasPrevious);
        }
Ejemplo n.º 5
0
        public void HasNext_WhenEndOffsetIsLessThanLastIndex_ReturnsTrue()
        {
            // Arrange
            var context = CreateContext(
                first: 10,
                after: ConnectionUtils.OffsetToCursor(0)
                );

            // Act
            var slice = SliceMetrics.Create(DbContext.Blogs, context);

            // Assert
            Assert.Equal(1, slice.StartIndex);
            Assert.Equal(10, slice.SliceSize);
            Assert.True(slice.HasNext);
        }
        public void HasPrevious_WhenDataIsSliceAndStartOffsetIsGreater0_ReturnsTrue()
        {
            // Arrange
            var context = CreateContext(
                first: 2,
                after: ConnectionUtils.OffsetToCursor(9)
                );

            // Act
            var slice = SliceMetrics.Create(Blogs, context);

            // Assert
            Assert.Equal(10, slice.StartIndex);
            Assert.Equal(2, slice.SliceSize);
            Assert.True(slice.HasPrevious);
        }
Ejemplo n.º 7
0
        public void Create_WhenCreateFromFullList_ReturnsListCount()
        {
            // Arrange
            var context = CreateContext <Blog>();

            // Act
            var slice = SliceMetrics.Create(
                DbContext.Blogs,
                context
                );

            // Assert
            Assert.Equal(fixture.TotalCount, slice.SliceSize);
            Assert.Equal(fixture.TotalCount, slice.TotalCount);
            Assert.Equal(0, slice.StartIndex);
            Assert.False(slice.HasNext);
            Assert.False(slice.HasPrevious);
        }
Ejemplo n.º 8
0
        public void Create_WhenDataSliceStartsAfterEnd_ReturnsEmptyResult()
        {
            // Arrange
            var context = CreateContext(
                first: 10,
                after: ConnectionUtils.OffsetToCursor(999)
                );

            // Act
            var slice = SliceMetrics.Create(
                DbContext.Blogs,
                context
                );

            // Assert
            Assert.Equal(0, slice.SliceSize);
            Assert.Equal(1000, slice.StartIndex);

            Assert.False(slice.HasNext);
            Assert.True(slice.HasPrevious);
        }
Ejemplo n.º 9
0
        public void Create_WhenDataDoesNotCoverRequestedRange_AdjustsStartAndEndOffsets()
        {
            // Arrange
            var context = CreateContext(
                first: 10,
                after: ConnectionUtils.OffsetToCursor(995)
                );

            // Act
            var slice = SliceMetrics.Create(
                DbContext.Blogs,
                context
                );

            // Assert
            Assert.Equal(4, slice.SliceSize);
            Assert.Equal(fixture.TotalCount, slice.TotalCount);
            Assert.Equal(996, slice.StartIndex);
            Assert.False(slice.HasNext);
            Assert.True(slice.HasPrevious);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// From the connection context, <see cref="IResolveConnectionContext"/>,
        /// it creates a <see cref="Connection{TSource}"/> based on the given <see cref="IEnumerable{TSource}"/>
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="context"></param>
        /// <param name="items"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static Connection <TSource> ToConnection <TSource>(
            this IResolveConnectionContext context,
            IEnumerable <TSource> items,
            int?totalCount = null
            )
        {
            var metrics = SliceMetrics.Create(
                items,
                context,
                totalCount
                );

            var edges = metrics.Slice
                        .Select((item, i) => new Edge <TSource>
            {
                Node   = item,
                Cursor = ConnectionUtils.OffsetToCursor(metrics.StartIndex + i)
            })
                        .ToList();

            var firstEdge = edges.FirstOrDefault();
            var lastEdge  = edges.LastOrDefault();

            return(new Connection <TSource>
            {
                Edges = edges,
                TotalCount = metrics.TotalCount,
                PageInfo = new PageInfo
                {
                    StartCursor = firstEdge?.Cursor,
                    EndCursor = lastEdge?.Cursor,
                    HasPreviousPage = metrics.HasPrevious,
                    HasNextPage = metrics.HasNext,
                }
            });
        }