public async Task TestWriteBooksAsyncPriceIncreasesAndTitleUniqueOk()
        {
            //SETUP
            var sqlOptions = SqliteInMemory.CreateOptions <SqlDbContext>();

            using (var sqlContext = new SqlDbContext(sqlOptions))
            {
                sqlContext.Database.EnsureCreated();

                var filepath = TestData.GetFilePath("10ManningBooks.json");

                var generator = new BookGenerator(sqlOptions, null);

                //ATTEMPT
                await generator.WriteBooksAsync(filepath, 25, true, default(CancellationToken));

                //VERIFY
                var lastPrice = 0m;
                foreach (var book in sqlContext.Books.ToList().OrderBy(x => x.OrgPrice))
                {
                    book.OrgPrice.ShouldEqual(lastPrice + 1);
                    lastPrice = book.OrgPrice;
                    _output.WriteLine($"Title {book.Title}, ActualPrice = {book.ActualPrice}, {book.PromotionalText}");
                }
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Books(int totalBooksNeeded, bool wipeDatabase, CancellationToken cancellationToken,
                                                [FromServices] SqlDbContext sqlContext,
                                                [FromServices] BookGenerator generator,
                                                [FromServices] IWebHostEnvironment env)
        {
            if (totalBooksNeeded == 0)
            {
                return(View((object)"Error: should contain the number of books to generate."));
            }

            NoSqlDbContext noSqlContext = (NoSqlDbContext)HttpContext.RequestServices.GetService(typeof(NoSqlDbContext));

            if (wipeDatabase)
            {
                sqlContext.DevelopmentWipeCreated(noSqlContext);
            }

            var filepath = Path.Combine(env.WebRootPath, SetupHelpers.SeedFileSubDirectory,
                                        SetupHelpers.TemplateFileName);
            await generator.WriteBooksAsync(filepath, totalBooksNeeded, true, cancellationToken);

            SetupTraceInfo();

            return
                (View((object)((cancellationToken.IsCancellationRequested ? "Cancelled" : "Successful") +
                               $" generate. Num books in database = {sqlContext.Books.Count()}.")));
        }
        public async Task TestWriteBooksAsyncCorrectAmountOk(int numBooks)
        {
            //SETUP
            var noSqlOptions = this.GetCosmosDbToEmulatorOptions <NoSqlDbContext>();
            var sqlOptions   = SqliteInMemory.CreateOptions <SqlDbContext>();

            using (var noSqlContext = new NoSqlDbContext(noSqlOptions))
                using (var sqlContext = new SqlDbContext(sqlOptions, new NoSqlBookUpdater(noSqlContext)))
                {
                    sqlContext.Database.EnsureCreated();
                    await noSqlContext.Database.EnsureDeletedAsync();

                    await noSqlContext.Database.EnsureCreatedAsync();

                    var filepath = TestData.GetFilePath("10ManningBooks.json");

                    var generator = new BookGenerator(sqlOptions, noSqlOptions);

                    //ATTEMPT
                    await generator.WriteBooksAsync(filepath, numBooks, true, default(CancellationToken));

                    //VERIFY
                    sqlContext.Books.Count().ShouldEqual(numBooks);
                    noSqlContext.Books.Select(_ => 1).AsEnumerable().Count().ShouldEqual(numBooks);
                }
        }
Beispiel #4
0
        public async Task TestWriteBooksAsyncCheckReviews()
        {
            //SETUP
            var fileDir = Path.Combine(TestData.GetTestDataDir());
            var options = SqliteInMemory.CreateOptions <BookDbContext>();

            options.TurnOffDispose();
            using var context = new BookDbContext(options);
            context.Database.EnsureCreated();
            await context.SeedDatabaseWithBooksAsync(fileDir);

            context.ChangeTracker.Clear();

            //ATTEMPT
            var serviceProvider = BuildServiceProvider(options);
            var generator       = new BookGenerator(serviceProvider);

            await generator.WriteBooksAsync(fileDir, false, 20, true, default);

            //VERIFY
            foreach (var book in context.Books)
            {
                _output.WriteLine(book.ToString());
            }

            var books = context.Books.Include(x => x.Reviews).ToList();

            books.Count(x => x.Reviews.Count > 0).ShouldEqual(13);
            //Can't get exact review value compare
            books.All(x => x.ReviewsCount == x.Reviews.Count).ShouldBeTrue();
            books.Where(x => x.ReviewsCount > 0)
            .All(x => x.ReviewsAverageVotes == x.Reviews.Average(y => y.NumStars)).ShouldBeTrue();
            options.ManualDispose();
        }
Beispiel #5
0
        public async Task TestWriteBooksAsyncWipeDatabaseOk()
        {
            //SETUP
            var fileDir = Path.Combine(TestData.GetTestDataDir());
            var options = this.CreateUniqueClassOptions <BookDbContext>();

            using var context = new BookDbContext(options);
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
            await context.SeedDatabaseWithBooksAsync(fileDir);

            context.ChangeTracker.Clear();

            var serviceProvider = BuildServiceProvider(options);
            var generator       = new BookGenerator(serviceProvider);

            //ATTEMPT
            await generator.WriteBooksAsync(fileDir, true, 10, true, default);

            //VERIFY
            context.Books.Count().ShouldEqual(10);
            context.Books
            .Include(x => x.Tags)
            .Count(x => x.Tags.Select(y => y.TagId).Contains("Manning books"))
            .ShouldEqual(6);
        }
        public async Task TestWriteBooksAsyncCancelOk()
        {
            //SETUP
            var noSqlOptions = this.GetCosmosDbToEmulatorOptions <NoSqlDbContext>();
            var sqlOptions   = SqliteInMemory.CreateOptions <SqlDbContext>();

            using (var sqlContext = new SqlDbContext(sqlOptions, null))
            {
                sqlContext.Database.EnsureCreated();

                var filepath = TestData.GetFilePath("10ManningBooks.json");

                var generator = new BookGenerator(sqlOptions, noSqlOptions);

                //ATTEMPT
                await generator.WriteBooksAsync(filepath, 8, true, default(CancellationToken));

                await generator.WriteBooksAsync(filepath, 12, true, new CancellationToken(true));

                //VERIFY
                sqlContext.Books.Count().ShouldEqual(8);
            }
        }
Beispiel #7
0
        public async Task TestWriteBooksAsyncNoDataCausesNewDbOk()
        {
            //SETUP
            await ResetDatabasesAsync();

            var fileDir   = Path.Combine(TestData.GetTestDataDir());
            var generator = new BookGenerator(_serviceProvider);

            //ATTEMPT
            await generator.WriteBooksAsync(fileDir, false, 1, true, default);

            //VERIFY
            var cosmosBooks = _cosmosContext.Books.ToList();

            cosmosBooks.Count().ShouldEqual(6);
            cosmosBooks.All(x => x.Tags.Select(y => y.TagId).Contains("Manning books")).ShouldBeTrue();
        }
        public async Task TestWriteBooksAsyncPromotionValueOk()
        {
            //SETUP
            var sqlOptions = SqliteInMemory.CreateOptions <SqlDbContext>();

            using (var sqlContext = new SqlDbContext(sqlOptions))
            {
                sqlContext.Database.EnsureCreated();

                var filepath = TestData.GetFilePath("10ManningBooks.json");

                var generator = new BookGenerator(sqlOptions, null);

                //ATTEMPT
                await generator.WriteBooksAsync(filepath, 10, true, default(CancellationToken));

                //VERIFY
                var booksWithPromo = sqlContext.Books.Where(x => x.PromotionalText != null).ToList();
                booksWithPromo.ForEach(x => x.ActualPrice.ShouldEqual(x.OrgPrice * 0.5m));
            }
        }
Beispiel #9
0
        public async Task TestWriteBooksAsyncCheckReviews()
        {
            //SETUP
            await ResetDatabasesAsync();

            var fileDir   = Path.Combine(TestData.GetTestDataDir());
            var generator = new BookGenerator(_serviceProvider);

            //ATTEMPT
            await generator.WriteBooksAsync(fileDir, false, 20, true, default);

            //VERIFY
            foreach (var book in _cosmosContext.Books)
            {
                _output.WriteLine(book.ToString());
            }

            var cosmosBooks = _cosmosContext.Books.ToList();

            cosmosBooks.Count(x => x.ReviewsCount > 0).ShouldEqual(13);
            //Can't get exact review value compare
        }
Beispiel #10
0
        public async Task TestWriteBooksAsyncCheckAddUpdateDates()
        {
            //SETUP
            var fileDir = Path.Combine(TestData.GetTestDataDir());
            var options = SqliteInMemory.CreateOptions <BookDbContext>();

            options.TurnOffDispose();
            var timeNow = DateTime.UtcNow;

            var eventConfig = new GenericEventRunnerConfig
            {
                NotUsingDuringSaveHandlers = true
            };

            eventConfig.AddActionToRunAfterDetectChanges <BookDbContext>(localContext =>
                                                                         localContext.ChangeChecker());
            using var context = options.CreateDbWithDiForHandlers <BookDbContext, ReviewAddedHandler>(null, eventConfig);
            context.Database.EnsureCreated();
            await context.SeedDatabaseWithBooksAsync(fileDir); //The LastUpdatedUtc is set via events

            context.ChangeTracker.Clear();

            //ATTEMPT
            var serviceProvider = BuildServiceProvider(options);

            var generator = new BookGenerator(serviceProvider);
            await generator.WriteBooksAsync(fileDir, false, 20, true, default);

            //VERIFY
            var books = context.Books
                        .Include(x => x.Reviews)
                        .Include(x => x.AuthorsLink).ToList();

            books.All(x => x.LastUpdatedUtc >= timeNow).ShouldBeTrue();
            books.SelectMany(x => x.Reviews).All(x => x.LastUpdatedUtc >= timeNow).ShouldBeTrue();
            books.SelectMany(x => x.AuthorsLink).All(x => x.LastUpdatedUtc >= timeNow).ShouldBeTrue();
            options.ManualDispose();
        }
        public async Task TestWriteBooksAsyncFillsInCacheValuesOk()
        {
            //SETUP
            var sqlOptions = SqliteInMemory.CreateOptions <SqlDbContext>();

            using (var sqlContext = new SqlDbContext(sqlOptions))
            {
                sqlContext.Database.EnsureCreated();

                var filepath = TestData.GetFilePath("10ManningBooks.json");

                var generator = new BookGenerator(sqlOptions, null);

                //ATTEMPT
                await generator.WriteBooksAsync(filepath, 10, true, default(CancellationToken));

                //VERIFY
                var booksWithReviews = sqlContext.Books.Include(x => x.Reviews).ToList();
                booksWithReviews.Count().ShouldEqual(10);
                booksWithReviews.ForEach(x => x.ReviewsCount.ShouldEqual(x.Reviews.Count()));
                booksWithReviews.ForEach(x => x.AuthorsOrdered.ShouldNotBeNull());
            }
        }
Beispiel #12
0
        public async Task TestWriteBooksAsyncAskNumberOk(int totalBooks)
        {
            //SETUP
            var fileDir = Path.Combine(TestData.GetTestDataDir());
            var options = SqliteInMemory.CreateOptions <BookDbContext>();

            options.TurnOffDispose();
            using var context = new BookDbContext(options);
            context.Database.EnsureCreated();
            await context.SeedDatabaseWithBooksAsync(fileDir);

            context.ChangeTracker.Clear();

            //ATTEMPT
            var serviceProvider = BuildServiceProvider(options);
            var generator       = new BookGenerator(serviceProvider);

            await generator.WriteBooksAsync(fileDir, false, totalBooks, true, default);

            //VERIFY
            context.Books.Count().ShouldEqual(totalBooks);
            options.ManualDispose();
        }
Beispiel #13
0
        public async Task TestWriteBooksAsyncNoDataCausesNewDbOk()
        {
            //SETUP
            var options = this.CreateUniqueClassOptions <BookDbContext>();

            using var context = new BookDbContext(options);
            context.Database.EnsureClean();

            var fileDir         = Path.Combine(TestData.GetTestDataDir());
            var serviceProvider = BuildServiceProvider(options);
            var generator       = new BookGenerator(serviceProvider);

            //ATTEMPT
            await generator.WriteBooksAsync(fileDir, false, 1, true, default);

            //VERIFY
            context.Books.Count().ShouldEqual(6);
            context.Authors.Count().ShouldEqual(8);
            context.Tags.Count().ShouldEqual(6);
            context.Books
            .Include(x => x.Tags)
            .Count(x => x.Tags.Select(y => y.TagId).Contains("Manning books"))
            .ShouldEqual(6);
        }