Esempio n. 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.ActivityLogs.RemoveRange(await context.ActivityLogs.ToArrayAsync());
                    await context.SaveChangesAsync();
                }

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

                using (var context = await factory.CreateContextAsync())
                {
                    var service = new ActivityLogDbService(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);
                }
            }
        }
Esempio n. 2
0
        public async Task GetWithPaginationAsync__Page_size_is_negative__Should_throw_ArgumentOutOfRangeException()
        {
            var service = new ActivityLogDbService(_dbContextMock.Object, _logger);

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

            await action.Should().ThrowExactlyAsync <ArgumentOutOfRangeException>("Because page size cannot be negative number.");
        }
Esempio n. 3
0
        public async Task GetWithPaginationAsync__Page_number_is_less_than_1__Should_throw_ArgumentOutOfRangeException()
        {
            var service = new ActivityLogDbService(_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.");
        }
Esempio n. 4
0
        public async Task GetWithPaginationAsync__Resource_is_null__Should_throw_InternalDbServiceException()
        {
            using (var factory = new DbContextFactory())
            {
                using (var context = await factory.CreateContextAsync())
                {
                    context.ActivityLogs = null as DbSet <ActivityLog>;
                    var service = new ActivityLogDbService(context, _logger);

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

                    await action.Should().ThrowExactlyAsync <InternalDbServiceException>("Because resource reference is set to null");
                }
            }
        }
Esempio n. 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.ActivityLogs.RemoveRange(await context.ActivityLogs.ToArrayAsync());
                    await context.SaveChangesAsync();
                }

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

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

                    result.Count().Should().Be(0);
                }
            }
        }
Esempio n. 6
0
        public async Task GetWithPaginationAsync__Resource_doesnt_exist__Should_throw_InternalDbServiceException()
        {
            using (var factory = new DbContextFactory())
            {
                using (var context = await factory.CreateContextAsync())
                {
                    // Drop ActivityLogs table.
                    context.Database.ExecuteSqlCommand("DROP TABLE [ActivityLogs]");
                }

                using (var context = await factory.CreateContextAsync())
                {
                    var service = new ActivityLogDbService(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 ActivityLog. " +
                                                                                         "NOTE Excaption actually is type of 'SqLiteError' only if database provider is SQLite.");
                }
            }
        }
Esempio n. 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.ActivityLogs.AddAsync(new ActivityLog { Id = i.ToString() });
                    }
                    await context.SaveChangesAsync();
                }

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

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

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