public void TestSaveSurveyNoQuestions()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentCompletedSurveys = context.CompletedSurveys.Count();
                var completedQuestionsDto   = EfTestData.CreateCompletedQuestionsDto();
                var completedSurveyDto      = new CompletedSurveyDto
                {
                    Name   = "Test Completed Survey",
                    CaseNo = 999
                };

                var service = new SaveCompletedSurveysService(context, new MapCompletedQuestionsFromDtoService());
                var result  = service.SaveCompletedSurvey(completedSurveyDto);
                context.SaveChanges();
                result.ShouldNotBeNull();
                result.First().ErrorMessage.ShouldEqual("No Questions have been submitted with this completed survey.");
                context.CompletedSurveys.Count().ShouldEqual(currentCompletedSurveys);
            }
        }
Exemplo n.º 2
0
        public async Task ShouldReturnOkObjectResult()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var controller = new TestController();
                var surveys    = EfTestData.CreateSurveys();

                var mockService = new Mock <IListSurveysService>();
                mockService.Setup(m => m.GetSurveys()).Returns((Task.FromResult((surveys))));

                var result = await controller.GetListOfSurveys(mockService.Object);

                result.ShouldNotBeNull();
                result.ShouldEqual(result as OkObjectResult);

                var r = result as OkObjectResult;

                r.Value.ShouldEqual(surveys);
            }
        }
        public void TestSaveSurveyOk()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentCompletedSurveys = context.CompletedSurveys.Count();
                var completedQuestionsDto   = EfTestData.CreateCompletedQuestionsDto();
                var completedSurveyDto      = new CompletedSurveyDto
                {
                    Name      = "Test Completed Survey",
                    CaseNo    = 999,
                    Questions = completedQuestionsDto
                };

                var service = new SaveCompletedSurveysService(context, new MapCompletedQuestionsFromDtoService());
                var result  = service.SaveCompletedSurvey(completedSurveyDto);
                result.ShouldBeNull();
                context.SaveChanges();
                service.Errors.Count.ShouldEqual(0);
                context.CompletedSurveys.Count().ShouldEqual(currentCompletedSurveys + 1);
            }
        }
        public void ShouldReturnAnErrorMessageWhenNoNameProvided()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentSurveys = context.Surveys.Count();
                var questionGroups = EfTestData.CreateQuestionGroupDtos();

                var surveyDto = new SurveyDto
                {
                    QuestionGroupsDtos = questionGroups,
                };

                var service = new AddSurveyService(context, new MapQuestionsToGroupService());
                var result  = service.AddSurvey(surveyDto);
                result.ShouldNotBeNull();
                context.SaveChanges();
                result.First().ErrorMessage.ShouldEqual("A name has not been provided for this survey.");
                context.Surveys.Count().ShouldEqual(currentSurveys);
            }
        }
Exemplo n.º 5
0
        public void TestCreateBookWithExistingAuthorAddedButNotSavedToDatabase()
        {
            //SETUP
            var inMemDb = new SqliteInMemory();

            using (var context = inMemDb.GetContextWithSetup())
            {
                var oneBook =
                    EfTestData.CreateDummyBookOneAuthor();
                context.Add(oneBook);

                var book = new Book
                {
                    Title       = "Test Book",
                    PublishedOn = DateTime.Today
                };
                book.AuthorsLink = new List <BookAuthor>
                {
                    new BookAuthor
                    {
                        Book   = book,
                        Author = oneBook.AuthorsLink
                                 .First().Author
                    }
                };

                //ATTEMPT
                context.Add(book);
                context.SaveChanges();

                //VERIFY
                context.Books.Count().ShouldEqual(2);
                context.Authors.Count().ShouldEqual(1);
            }
        }
        public void TestSaveSurveyNoName()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentCompletedSurveys = context.CompletedSurveys.Count();
                var completedQuestionsDto   = EfTestData.CreateCompletedQuestionsDto();
                var completedSurveyDto      = new CompletedSurveyDto
                {
                    CaseNo    = 999,
                    Questions = completedQuestionsDto
                };

                var service = new SaveCompletedSurveysService(context, new MapCompletedQuestionsFromDtoService());
                var result  = service.SaveCompletedSurvey(completedSurveyDto);
                context.SaveChanges();
                result.ShouldNotBeNull();
                service.Errors.Count.ShouldEqual(1);
                service.Errors.First().ErrorMessage.ShouldEqual("A survey name has not been supplied.");
                context.CompletedSurveys.Count().ShouldEqual(currentCompletedSurveys);
            }
        }
        public void ShouldAddQuestionGroup()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentQuestionGroups = context.QuestionGroups.Count();
                var questions             = EfTestData.CreateQuestionsDtos();

                var questionGroupsDto = new List <QuestionGroupDto>
                {
                    new QuestionGroupDto
                    {
                        Name      = "Test QuestionGroup Dto",
                        Questions = questions
                    }
                };

                var surveyDto = new SurveyDto
                {
                    Id = 1,
                    QuestionGroupsDtos = questionGroupsDto
                };

                var service = new AddQuestionGroupService(context, new MapQuestionsToGroupService());
                var result  = service.AddQuestionGroup(surveyDto);
                result.ShouldBeNull();
                context.SaveChanges();
                context.QuestionGroups.Count().ShouldEqual(currentQuestionGroups + 1);
            }
        }
        public async Task TestMapBookToDtoAsyncOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                context.Books.AddRange(EfTestData.CreateFourBooks());
                await context.SaveChangesAsync();

                //ATTEMPT
                var result = await context.Books.Select(p =>
                                                        new BookListDto
                {
                    ActualPrice = p.Promotion == null
                            ? p.Price
                            : p.Promotion.NewPrice,
                    ReviewsCount = p.Reviews.Count,
                }).ToListAsync();

                //VERIFY
                result.Count.ShouldEqual(4);
            }
        }
Exemplo n.º 9
0
        public async Task TestMapBookToDtoAsyncOk()
        {
            //SETUP
            var inMemDb = new SqliteInMemory();

            //ATTEMPT
            using (var context = inMemDb.GetContextWithSetup())
            {
                context.Books.AddRange(EfTestData.CreateFourBooks());
                await context.SaveChangesAsync();

                //ATTEMPT
                var result = await context.Books.Select(p =>
                                                        new BookListDto
                {
                    ActualPrice = p.Promotion == null
                            ? p.Price
                            : p.Promotion.NewPrice,
                    ReviewsCount = p.Reviews.Count,
                }).ToListAsync();

                //VERIFY
                result.Count.ShouldEqual(4);
            }
        }
Exemplo n.º 10
0
        public void SetupRestOfDto(BooksFilterBy filterBy, string filterValue, int pageSize,
                                   int expectedPageNum, int expectedNumPages)
        {
            //SETUP
            var       inMemDb  = new SqliteInMemory();
            const int numBooks = 12;

            using (var db = inMemDb.GetContextWithSetup())
            {
                db.Books.AddRange(EfTestData.CreateDummyBooks(numBooks, false));
                db.SaveChanges();

                var sfpDto = new SortFilterPageOptions
                {
                    FilterBy    = BooksFilterBy.ByVotes,
                    FilterValue = "Dummy",
                    PageNum     = 2
                };

                //need to do this to to setup PrevCheckState
                sfpDto.SetupRestOfDto(db.Books);

                //ATTEMPT
                sfpDto.PageNum     = 2;
                sfpDto.FilterBy    = filterBy;
                sfpDto.FilterValue = filterValue;
                sfpDto.PageSize    = pageSize;
                sfpDto.SetupRestOfDto(db.Books);

                //VERIFY
                sfpDto.PageNum.ShouldEqual(expectedPageNum);
                sfpDto.NumPages.ShouldEqual(expectedNumPages);
            }
        }
        public void ShouldReturnAnErrorMessageWhenGroupNoNameProvided()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var currentQuestionGroups = context.QuestionGroups.Count();
                var questions             = EfTestData.CreateQuestionsDtos();

                var questionGroupsDto = new List <QuestionGroupDto>
                {
                    new QuestionGroupDto
                    {
                        Questions = questions
                    }
                };

                var surveyDto = new SurveyDto
                {
                    Id = 1,
                    QuestionGroupsDtos = questionGroupsDto
                };

                var service = new AddQuestionGroupService(context, new MapQuestionsToGroupService());
                var result  = service.AddQuestionGroup(surveyDto);
                result.ShouldNotBeNull();
                context.SaveChanges();
                result.First().ErrorMessage.ShouldEqual("A name is needed when creating a new question group.");
                context.QuestionGroups.Count().ShouldEqual(currentQuestionGroups);
            }
        }
Exemplo n.º 12
0
 public static void SeedDatabase  //#A
     (this EfCoreContext context) //#A
 {
     if (context.Books.Any())
     {
         return;                        //#B
     }
     context.Books.AddRange(            //#C
         EfTestData.CreateFourBooks()); //#C
     context.SaveChanges();             //#C
 }
 public static async Task SeedDatabaseAsync //#A
     (this EfCoreContext context)           //#A
 {
     if (context.Books.Any())
     {
         return;                        //#B
     }
     context.Books.AddRange(            //#C
         EfTestData.CreateFourBooks()); //#C
     await context.SaveChangesAsync();  //#D
 }
        public Ch05_AsyncPerformance(ITestOutputHelper output)
        {
            _output = output;

            _options = this.CreateUniqueClassOptions <EfCoreContext>();
            using (var context = new EfCoreContext(_options))
            {
                context.Database.EnsureClean();
                context.Books.AddRange(EfTestData.CreateDummyBooks(1000, false, false));
                context.SaveChanges();
                _firstBookId = context.Books.First().BookId;
            }
        }
        public void TestCreateTestDataOk()
        {
            //SETUP

            //ATTEMPT
            var books = EfTestData.CreateFourBooks();

            //VERIFY
            books.Count.ShouldEqual(4);
            books.ForEach(x => x.AuthorsLink.Count.ShouldEqual(1));
            books[3].Reviews.Count.ShouldEqual(2);
            books[3].Promotion.ShouldNotBeNull();
        }
Exemplo n.º 16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="createLastInFuture">If true then the last book will be in the future</param>
        /// <param name="promotionPriceForFirstBook">if number it adds a promotion to the first book</param>
        public MockPlaceOrderDbAccess(bool createLastInFuture = false, int?promotionPriceForFirstBook = null)
        {
            var numBooks = createLastInFuture ? DateTime.UtcNow.Year - EfTestData.DummyBookStartDate.Year + 2 : 10;
            var books    = EfTestData.CreateDummyBooks(numBooks, createLastInFuture);

            if (promotionPriceForFirstBook != null)
            {
                books.First().Promotion = new PriceOffer
                {
                    NewPrice        = (int)promotionPriceForFirstBook,
                    PromotionalText = "Unit Test"
                }
            }
            ;
            Books = books.ToImmutableList();
        }
Exemplo n.º 17
0
        public void TestSqliteInMemoryBasicOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();

            var books = EfTestData.CreateFourBooks();

            context.Books.AddRange(books);
            context.SaveChanges();

            //VERIFY
            context.Books.Count().ShouldEqual(4);
            context.Books.Count(p => p.Title.StartsWith("Quantum")).ShouldEqual(1);
        }
Exemplo n.º 18
0
        public void TestSqliteInMemoryBasicOk()
        {
            //SETUP
            var inMemDb = new SqliteInMemory();

            //ATTEMPT
            using (var context = inMemDb.GetContextWithSetup())
            {
                var books = EfTestData.CreateFourBooks();
                context.Books.AddRange(books);
                context.SaveChanges();

                //VERIFY
                context.Books.Count().ShouldEqual(4);
                context.Books.Count(p => p.Title.StartsWith("Quantum")).ShouldEqual(1);
            }
        }
        public void TestCreateBookAddTwice()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();
            var oneBook =
                EfTestData.CreateDummyBookOneAuthor();

            //ATTEMPT
            context.Add(oneBook);
            context.Add(oneBook);
            context.SaveChanges();

            //VERIFY
            context.Books.Count().ShouldEqual(1);
        }
Exemplo n.º 20
0
        public async Task TestReadSqliteAsyncOk()
        {
            //SETUP
            var inMemDb = new SqliteInMemory();

            //ATTEMPT
            using (var context = inMemDb.GetContextWithSetup())
            {
                context.Books.AddRange(EfTestData.CreateFourBooks());
                await context.SaveChangesAsync();

                //ATTEMPT
                var books = await context.Books.ToListAsync();

                //VERIFY
                books.Count.ShouldEqual(4);
                books.Count(p => p.Title.StartsWith("Quantum")).ShouldEqual(1);
            }
        }
Exemplo n.º 21
0
        public void SaveCompletedSurveyOk()
        {
            // SETUP
            var mockDbA = new MockCompletedSurveyDbAccess();
            var service = new Surveys.BizLogic.CompletedSurveys.SaveCompletedSurveyAction(mockDbA);

            var completedQuestions = EfTestData.CreateCompletedQuestions();
            var caseNo             = 999;
            var name = "Completed Survey Test";

            // ATTEMPT
            var result = service.Action(new SaveCompletedSurveyDto(name, caseNo, completedQuestions));

            // VERIFY
            service.Errors.Any().ShouldEqual(false);
            mockDbA.AddedCompletedSurvey.CaseNumber.ShouldEqual(caseNo);
            mockDbA.AddedCompletedSurvey.Name.ShouldEqual(name);
            mockDbA.AddedCompletedSurvey.CompletedQuestions.Count().ShouldEqual(completedQuestions.Count);
        }
        public async Task TestWriteTestDataSqliteOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                var books = EfTestData.CreateFourBooks();
                context.Books.AddRange(books);
                await context.SaveChangesAsync();

                //VERIFY
                context.Books.Count().ShouldEqual(4);
                context.Books.Count(p => p.Title.StartsWith("Quantum")).ShouldEqual(1);
            }
        }
Exemplo n.º 23
0
        public void TestCreateBookAddTwice()
        {
            //SETUP
            var inMemDb = new SqliteInMemory();

            using (var context = inMemDb.GetContextWithSetup())
            {
                var oneBook =
                    EfTestData.CreateDummyBookOneAuthor();

                //ATTEMPT
                context.Add(oneBook);
                context.Add(oneBook);
                context.SaveChanges();

                //VERIFY
                context.Books.Count().ShouldEqual(1);
            }
        }
Exemplo n.º 24
0
        public void TestCreateBookWithExistingAuthorSavedToDatabase()
        {
            //SETUP
            var inMemDb = new SqliteInMemory();

            using (var context = inMemDb.GetContextWithSetup())
            {
                var oneBook =
                    EfTestData.CreateDummyBookOneAuthor(); //#A
                context.Add(oneBook);                      //#A
                context.SaveChanges();                     //#A

                var book = new Book                        //#B
                {                                          //#B
                    Title       = "Test Book",             //#B
                    PublishedOn = DateTime.Today           //#B
                };                                         //#B
                book.AuthorsLink = new List <BookAuthor>   //#C
                {                                          //#C
                    new BookAuthor                         //#C
                    {                                      //#C
                        Book   = book,                     //#C
                        Author = oneBook.AuthorsLink       //#C
                                 .First().Author           //#C
                    }                                      //#C
                };                                         //#C

                //ATTEMPT
                context.Add(book);                //#D
                context.SaveChanges();            //#D

                /************************************************************
                 #A This method creates dummy books for testing. I create one dummy book with one Author and add it to the empty database
                 #B This creates a book in the same way as the previous example, but sets up its Author
                 #C This adds a AuthorBook linking entry, but it reads in an existing the Author from the first book
                 #D This is the same process: add the new book to the DbContext Books property and call SaveChanges
                 * *********************************************************/

                //VERIFY
                context.Books.Count().ShouldEqual(2);   //#E
                context.Authors.Count().ShouldEqual(1); //#F
            }
        }
Exemplo n.º 25
0
        public void DropdownByDate()
        {
            //SETUP
            const int numBooks = 5;
            var       options  = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.Books.AddRange(EfTestData.CreateDummyBooks(numBooks, true));
                context.SaveChanges();
                var service = new BookFilterDropdownService(context);

                //ATTEMPT
                var dropDown = service.GetFilterDropDownValues(BooksFilterBy.ByPublicationYear);

                //VERIFY
                dropDown.Select(x => x.Value).ToArray().ShouldEqual(new[] { "2014", "2013", "2012", "2011", "2010" });
            }
        }
        public void TestWriteTestDataSqliteInMemoryOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                var books = EfTestData.CreateFourBooks();
                context.Books.AddRange(books);
                context.SaveChanges();

                //VERIFY
                context.Books.Count().ShouldEqual(4);
                context.Books.Count(p =>
                                    p.Title == "Refactoring").ShouldEqual(1);
            }
        }
Exemplo n.º 27
0
        public void DropdownByDate()
        {
            //SETUP
            var inMemDb = new SqliteInMemory();

            const int numBooks = 5;

            using (var db = inMemDb.GetContextWithSetup())
            {
                db.Books.AddRange(EfTestData.CreateDummyBooks(numBooks, true));
                db.SaveChanges();
                var service = new BookFilterDropdownService(db);

                //ATTEMPT
                var dropDown = service.GetFilterDropDownValues(BooksFilterBy.ByPublicationYear);

                //VERIFY
                dropDown.Select(x => x.Value).ToArray().ShouldEqual(new [] { "2014", "2013", "2012", "2011", "2010" });
            }
        }
Exemplo n.º 28
0
        public Ch05_AsyncPerformance(ITestOutputHelper output)
        {
            _output = output;

            var connection     = this.GetUniqueDatabaseConnectionString();
            var optionsBuilder =
                new DbContextOptionsBuilder <EfCoreContext>();

            optionsBuilder.UseSqlServer(connection);
            _options = optionsBuilder.Options;
            using (var context = new EfCoreContext(_options))
            {
                context.Database.EnsureCreated();
                if (!context.Books.Any())
                {
                    context.Books.AddRange(EfTestData.CreateDummyBooks(1000, false, false));
                    context.SaveChanges();
                }
                _firstBookId = context.Books.First().BookId;
            }
        }
Exemplo n.º 29
0
        public void TestBookQueryWithIncludes()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();
            var book4Reviews2Authors = EfTestData.CreateDummyBooks(5).Last();

            context.Add(book4Reviews2Authors);
            context.SaveChanges();

            //ATTEMPT
            var query = context.Books
                        .Include(x => x.Reviews)
                        .Include(x => x.AuthorsLink)
                        .ThenInclude(x => x.Author);

            //VERIFY
            _output.WriteLine(query.ToQueryString());
        }
Exemplo n.º 30
0
        }                                             //#C

        /// <summary>
        ///
        /// </summary>
        /// <param name="createLastInFuture">If true then the last book will be in the future</param>
        /// <param name="promotionPriceForFirstBook">if number it adds a promotion to the first book</param>
        public MockPlaceOrderDbAccess(                            //#D
            bool createLastInFuture        = false,               //#E
            int?promotionPriceForFirstBook = null)                //#F
        {
            var numBooks = createLastInFuture                     //#G
                ? DateTime.UtcNow.Year -                          //#G
                           EfTestData.DummyBookStartDate.Year + 2 //#G
                : 10;                                             //#G
            var books = EfTestData.CreateDummyBooks               //#H
                            (numBooks, createLastInFuture);       //#H

            if (promotionPriceForFirstBook != null)
            {
                books.First().Promotion = new PriceOffer               //#I
                {                                                      //#I
                    NewPrice        = (int)promotionPriceForFirstBook, //#I
                    PromotionalText = "Unit Test"                      //#I
                }
            }
            ;                                                  //#I
            DummyBooks = books.ToImmutableList();
        }