public void TestFaqFilledList()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            context.FaqCategory.AddRange(
                new FaqCategory()
            {
                CategoryName = "TestA"
            },
                new FaqCategory()
            {
                CategoryName = "TestB"
            }
                );
            context.SaveChanges();

            IFaqRepository repository    = new EFFaqRepository(context);
            var            faqCategories = repository.ListCategories();

            Assert.NotNull(faqCategories);
            Assert.True(faqCategories.Result.Count() == 2);
        }
        public async void TestCategoryListWithItemsOnePageSizeTen()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            context.FaqCategory.AddRange(
                new FaqCategory()
            {
                CategoryName = "TestA"
            },
                new FaqCategory()
            {
                CategoryName = "TestB"
            }
                );

            await context.SaveChangesAsync();

            IFaqRepository repository = new EFFaqRepository(context);

            PaginatedList <FaqCategory> paginatedCategories = await repository.ListCategories(1, 10);

            Assert.NotNull(paginatedCategories);
            Assert.Equal(2, paginatedCategories.Count);
            Assert.Equal(1, paginatedCategories.PageIndex);
            Assert.Equal(10, paginatedCategories.PageSize);
            Assert.Equal(1, paginatedCategories.TotalPages);
            Assert.False(paginatedCategories.HasPreviousPage);
            Assert.False(paginatedCategories.HasNextPage);
        }
        public async void TestSaveNotExistingCategoryWithNotExistingId()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            FaqCategory ExistingCat = new FaqCategory()
            {
                Id           = 1,
                CategoryName = "Test",
            };

            await context.FaqCategory.AddAsync(ExistingCat);

            await context.SaveChangesAsync();

            IFaqRepository repository = new EFFaqRepository(context);

            // Change some values
            FaqCategory catToUpdate = new FaqCategory()
            {
                Id           = 2,
                CategoryName = "New",
            };

            await Assert.ThrowsAsync <DbUpdateConcurrencyException>(() => repository.SaveCategory(catToUpdate));
        }
        public void TestFaqFilledRepository()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            context.FaqCategory.AddRange(
                new FaqCategory()
            {
                CategoryName = "TestA"
            },
                new FaqCategory()
            {
                CategoryName = "TestB"
            },
                new FaqCategory()
            {
                CategoryName = "TestC"
            }
                );
            context.SaveChanges();

            IFaqRepository repository = new EFFaqRepository(context);

            Assert.True(repository.GetCategoriesAndQuestions().Count() == 3);
        }
        public void TestFilledFaqList()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            context.FaqCategory.AddRange(
                new FaqCategory()
            {
                CategoryName = "TestA"
            },
                new FaqCategory()
            {
                CategoryName = "TestB"
            },
                new FaqCategory()
            {
                CategoryName = "TestC"
            }
                );
            context.SaveChanges();

            IFaqRepository repository = new EFFaqRepository(context);
            IFaqManager    manager    = new FaqManager(repository);

            var target = new FaqController(manager);
            var result = target.Index(1, 10).Result as ViewResult;
            var model  = result.Model as IEnumerable <FaqCategory>;

            Assert.NotNull(model);
            Assert.True(model.Count() == 3);
        }
        public async void TestSaveUpdatedCategory()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            FaqCategory catToUpdate = new FaqCategory()
            {
                Id           = 1,
                CategoryName = "Test",
            };

            await context.FaqCategory.AddAsync(catToUpdate);

            await context.SaveChangesAsync();

            IFaqRepository repository = new EFFaqRepository(context);

            // Change some values
            catToUpdate.CategoryName = "New";

            await repository.SaveCategory(catToUpdate);

            // Check if the item was updated
            FaqCategory updatedCat = await context.FaqCategory.FindAsync((long)1);

            Assert.NotNull(updatedCat);
            Assert.Equal("New", updatedCat.CategoryName);
        }
        public async void TestSaveNewCategory()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            context.FaqCategory.AddRange(
                new FaqCategory()
            {
                CategoryName = "Test"
            }
                );

            await context.SaveChangesAsync();

            IFaqRepository repository = new EFFaqRepository(context);

            FaqCategory categoryToCreate = new FaqCategory()
            {
                CategoryName = "Test"
            };

            await repository.SaveCategory(categoryToCreate);

            // Check if the item was created
            FaqCategory foundCat = await context.FaqCategory.FirstOrDefaultAsync(x => x.CategoryName == "Test");

            Assert.NotNull(foundCat);
            Assert.Equal("Test", foundCat.CategoryName);
        }
        public void TestFaqEmptyRepository()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context    = new HomeMyDayDbContext(optionsBuilder.Options);
            IFaqRepository     repository = new EFFaqRepository(context);

            Assert.Empty(repository.GetCategoriesAndQuestions());
        }
        public void TestFaqEmptyList()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context    = new HomeMyDayDbContext(optionsBuilder.Options);
            IFaqRepository     repository = new EFFaqRepository(context);

            var faqCategories = repository.ListCategories();

            Assert.True(!faqCategories.Result.Any());
        }
        public void TestEmptyFaqList()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context    = new HomeMyDayDbContext(optionsBuilder.Options);
            IFaqRepository     repository = new EFFaqRepository(context);
            IFaqManager        manager    = new FaqManager(repository);

            var target = new FaqController(manager);
            var result = target.Index(1, 10).Result as ViewResult;
            var model  = result.Model as IEnumerable <FaqCategory>;

            Assert.NotNull(model);
            Assert.True(!model.Any());
        }
        public async void TestQuestionListWithItemsPageBelowZeroPageSizeBelowZero()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            context.FaqCategory.AddRange(
                new FaqCategory()
            {
                Id           = 1,
                CategoryName = "TestA",
                Questions    = new System.Collections.Generic.List <FaqQuestion>()
                {
                    new FaqQuestion()
                    {
                        Question = "Question 1",
                        Answer   = "Answer 1"
                    },
                    new FaqQuestion()
                    {
                        Question = "Question 2",
                        Answer   = "Answer 2"
                    }
                }
            },
                new FaqCategory()
            {
                Id = 2, CategoryName = "TestB"
            }
                );

            await context.SaveChangesAsync();

            IFaqRepository repository = new EFFaqRepository(context);

            PaginatedList <FaqQuestion> paginatedQuestions = await repository.ListQuestions(1, -8, -10);

            Assert.NotNull(paginatedQuestions);
            Assert.Equal(2, paginatedQuestions.Count);
            Assert.Equal(1, paginatedQuestions.PageIndex);
            Assert.Equal(10, paginatedQuestions.PageSize);
            Assert.Equal(1, paginatedQuestions.TotalPages);
            Assert.False(paginatedQuestions.HasPreviousPage);
            Assert.False(paginatedQuestions.HasNextPage);
        }
        public async void TestEmptyListPageBelowZeroPageSizeBelowZero()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            IFaqRepository repository = new EFFaqRepository(context);

            PaginatedList <FaqCategory> paginatedCategories = await repository.ListCategories(-5, -10);

            Assert.NotNull(paginatedCategories);
            Assert.Empty(paginatedCategories);
            Assert.Equal(1, paginatedCategories.PageIndex);
            Assert.Equal(10, paginatedCategories.PageSize);
            Assert.Equal(1, paginatedCategories.TotalPages);
            Assert.False(paginatedCategories.HasPreviousPage);
            Assert.False(paginatedCategories.HasNextPage);
        }
        public async void TestDeleteIdEqualsZero()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            context.FaqCategory.AddRange(
                new FaqCategory()
            {
                CategoryName = "Test"
            }
                );

            await context.SaveChangesAsync();

            IFaqRepository repository = new EFFaqRepository(context);

            await Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => repository.DeleteCategory(0));
        }
        public async void TestQuestionListWithIdBelowZero()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            context.FaqCategory.AddRange(
                new FaqCategory()
            {
                Id           = 1,
                CategoryName = "TestA",
                Questions    = new System.Collections.Generic.List <FaqQuestion>()
                {
                    new FaqQuestion()
                    {
                        Question = "Question 1",
                        Answer   = "Answer 1"
                    },
                    new FaqQuestion()
                    {
                        Question = "Question 2",
                        Answer   = "Answer 2"
                    }
                }
            },
                new FaqCategory()
            {
                Id = 2, CategoryName = "TestB"
            }
                );

            await context.SaveChangesAsync();

            IFaqRepository repository = new EFFaqRepository(context);

            await Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => repository.ListQuestions(-5, 1, 10));
        }
        public async void TestDeleteExistingCategory()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options);

            context.FaqCategory.AddRange(
                new FaqCategory()
            {
                Id = 1, CategoryName = "Test"
            }
                );

            await context.SaveChangesAsync();

            IFaqRepository repository = new EFFaqRepository(context);

            await repository.DeleteCategory(1);

            FaqCategory deletedCat = await context.FaqCategory.FindAsync((long)1);

            Assert.Null(deletedCat);
        }