コード例 #1
0
        public async Task GetWithPaginationAsync__Number_of_elements_on_single_page_is_less_than_page_size__Should_return_all_these_elements()
        {
            using (var factory = new DbContextFactory())
            {
                using (var context = await factory.CreateContextAsync())
                {
                    context.Tickets.RemoveRange(await context.Tickets.ToArrayAsync());
                    await context.SaveChangesAsync();
                }

                using (var context = await factory.CreateContextAsync())
                {
                    for (int i = 0; i < 10; i++)
                    {
                        await context.Tickets.AddAsync(new Ticket { Id = i.ToString() });
                    }
                    await context.SaveChangesAsync();
                }

                using (var context = await factory.CreateContextAsync())
                {
                    var service = new TicketDbService(context, _logger);

                    // Page will be first with 6 elements and second with 4 elements. Second page will be return.
                    var result = await service.GetWithPaginationAsync(2, 6);

                    // More generic solution is: Count() should be count of elements in resource % pageSize.
                    result.Count().Should().Be(4);
                }
            }
        }
コード例 #2
0
        public async Task GetWithPaginationAsync__Page_size_is_negative__Should_throw_ArgumentOutOfRangeException()
        {
            var service = new TicketDbService(_dbContextMock.Object, _logger);

            Func <Task> action = async() => await service.GetWithPaginationAsync(3, -10);

            await action.Should().ThrowExactlyAsync <ArgumentOutOfRangeException>("Because page size cannot be negative number.");
        }
コード例 #3
0
        public async Task GetWithPaginationAsync__Page_number_is_less_than_1__Should_throw_ArgumentOutOfRangeException()
        {
            var service = new TicketDbService(_dbContextMock.Object, _logger);

            Func <Task> action = async() => await service.GetWithPaginationAsync(0, 10);

            await action.Should().ThrowExactlyAsync <ArgumentOutOfRangeException>("Because page number cannot be less than 1.");
        }
コード例 #4
0
        public async Task GetWithPaginationAsync__Resource_is_null__Should_throw_InternalDbServiceException()
        {
            using (var factory = new DbContextFactory())
            {
                using (var context = await factory.CreateContextAsync())
                {
                    context.Tickets = null as DbSet <Ticket>;
                    var service = new TicketDbService(context, _logger);

                    Func <Task> action = async() => await service.GetWithPaginationAsync(1, 1);

                    await action.Should().ThrowExactlyAsync <InternalDbServiceException>("Because resource reference is set to null");
                }
            }
        }
コード例 #5
0
        public async Task GetWithPaginationAsync__Resource_is_empty__Should_return_empty_IEnumerable()
        {
            using (var factory = new DbContextFactory())
            {
                using (var context = await factory.CreateContextAsync())
                {
                    context.Tickets.RemoveRange(await context.Tickets.ToArrayAsync());
                    await context.SaveChangesAsync();
                }

                using (var context = await factory.CreateContextAsync())
                {
                    var service = new TicketDbService(context, _logger);

                    var result = await service.GetWithPaginationAsync(4, 5);

                    result.Count().Should().Be(0);
                }
            }
        }
コード例 #6
0
        public async Task GetWithPaginationAsync__Resource_doesnt_exist__Should_throw_InternalDbServiceException()
        {
            using (var factory = new DbContextFactory())
            {
                using (var context = await factory.CreateContextAsync())
                {
                    // Drop Tickets table.
                    context.Database.ExecuteSqlCommand("DROP TABLE [Tickets]");
                }

                using (var context = await factory.CreateContextAsync())
                {
                    var service = new TicketDbService(context, _logger);

                    Func <Task> action = async() => await service.GetWithPaginationAsync(1, 1);

                    await action.Should().ThrowExactlyAsync <InternalDbServiceException>("Because resource doesnt exist and cannot get single instance of Ticket. " +
                                                                                         "NOTE Excaption actually is type of 'SqLiteError' only if database provider is SQLite.");
                }
            }
        }
コード例 #7
0
        public async Task GetWithPaginationAsync__There_are_not_elements_on_single_page__Should_return_empty_IEnumerable()
        {
            using (var factory = new DbContextFactory())
            {
                using (var context = await factory.CreateContextAsync())
                {
                    for (int i = 0; i < 10; i++)
                    {
                        await context.Tickets.AddAsync(new Ticket { Id = i.ToString() });
                    }
                    await context.SaveChangesAsync();
                }

                using (var context = await factory.CreateContextAsync())
                {
                    var service = new TicketDbService(context, _logger);

                    // In this case is only 2 pages with any data.
                    var result = await service.GetWithPaginationAsync(4, 5);

                    result.Count().Should().Be(0);
                }
            }
        }