示例#1
0
        public void ShouldBeSucceededWhenSuccessResult()
        {
            var result = PaginatedResult <int>
                         .Success(new List <int>() { 1 }, 1, 1, 1);

            Assert.True(result.Succeeded);
        }
示例#2
0
        public void ShouldAddErrorsCorrectly()
        {
            var result = PaginatedResult <int> .Success(null, 1, 1, 1);

            var result2 = new PaginatedResult <int>(null, 1, 1, 1);

            result.AddError("Error");
            result.AddError("Error");
            result.AddError("Error");

            result2.AddError("Error");
            result2.AddError("Error");
            result2.AddError("Error");
            result2.AddError("Error");
            result2.AddError("Error");

            Assert.Equal(3, result.Errors.Count);
            Assert.Equal(5, result2.Errors.Count);

            var resultError = PaginatedResult <int> .Error("Error");

            var resultError2 = new PaginatedResult <int>("Error");

            resultError.AddError("Error");
            resultError2.AddError("Error");
            resultError2.AddError("Error");

            Assert.Equal(2, resultError.Errors.Count);
            Assert.Equal(3, resultError2.Errors.Count);
        }
示例#3
0
        public void ShouldCalculateLastPageCorrectly(long total, int pageSize, int lastPage)
        {
            var result = PaginatedResult <int> .Success(new List <int> {
                1
            }, total, 1, pageSize);

            Assert.Equal(result.LastPage, lastPage);
        }
示例#4
0
        public void ShouldCalculateTotalPagesCorrectly(long total, int pageSize, int totalPages)
        {
            var result = PaginatedResult <int> .Success(new List <int> {
                1
            }, total, 1, pageSize);

            Assert.Equal(result.TotalPages, totalPages);
        }
        public static async Task <PaginatedResult <T> > ToPaginatedListAsync <T>(this IQueryable <T> source, int pageNumber, int pageSize) where T : class
        {
            Throw.Exception.IfNull(source, nameof(source));
            pageNumber = pageNumber == 0 ? 1 : pageNumber;
            pageSize   = pageSize == 0 ? 10 : pageSize;
            long count = await source.LongCountAsync();

            pageNumber = pageNumber <= 0 ? 1 : pageNumber;
            List <T> items = await source.Skip((pageNumber - 1) *pageSize).Take(pageSize).ToListAsync();

            return(PaginatedResult <T> .Success(items, count, pageNumber, pageSize));
        }
示例#6
0
        public void ShouldNotBeSucceededWhenAddError()
        {
            var result = PaginatedResult <int> .Success(null, 1, 1, 1);

            var result2 = new PaginatedResult <int>(null, 1, 1, 1);

            result.AddError("Error");
            result2.AddError("Error");

            Assert.False(result.Succeeded);
            Assert.False(result2.Succeeded);
        }
        public static async Task <PaginatedResult <T> > ToPaginatedListAsync <T>(this IQueryable <T> source, int pageNumber, int pageSize) where T : class
        {
            if (source == null)
            {
                throw new ApiException();
            }
            pageNumber = pageNumber == 0 ? 1 : pageNumber;
            pageSize   = pageSize == 0 ? 10 : pageSize;
            int count = await source.CountAsync();

            pageNumber = pageNumber <= 0 ? 1 : pageNumber;
            List <T> items = await source.Skip((pageNumber - 1) *pageSize).Take(pageSize).ToListAsync();

            return(PaginatedResult <T> .Success(items, count, pageNumber, pageSize));
        }
示例#8
0
        public void DataShouldNeverBeNull()
        {
            var result = PaginatedResult <int> .Success(null, 1, 1, 1);

            var result2 = PaginatedResult <int>
                          .Error("Error 1");

            var result3 = PaginatedResult <int> .Error(new List <string> {
                "Erro 1, Erro 2"
            });

            Assert.NotNull(result.Data);
            Assert.NotNull(result2.Data);
            Assert.NotNull(result3.Data);
        }
示例#9
0
        public static PaginatedResult <string> SelectPaginated()
        {
            var page     = 0;
            var pageSize = 2;

            var totalInDb = FakeService.CountNames();

            var result = FakeService.SelectPaginated(page, pageSize);

            return(PaginatedResult <string> .Success(
                       result,
                       totalInDb,
                       page,
                       pageSize));
        }
示例#10
0
        public void ShouldBeEmptyWhenAddError()
        {
            var list = new List <int> {
                1, 2, 3
            };

            var result = PaginatedResult <int> .Success(list, 3, 1, 3);

            var result2 = new PaginatedResult <int>(list, 3, 1, 3);

            result.AddError("Error");
            result2.AddError("Error");

            Assert.Empty(result.Data);
            Assert.Empty(result2.Data);
        }