Beispiel #1
0
        /// <summary>
        /// Saves books from the raw test data file.
        /// </summary>
        /// <param name="books"></param>
        /// <returns></returns>
        public static DbContextOptions <BookStoreContext> SeedWith(this DbContextOptions <BookStoreContext> source, string filePath)
        {
            var loadedBooks = BookJsonLoader.LoadBooks(filePath);

            source.SeedWith(loadedBooks);
            return(source);
        }
Beispiel #2
0
        public void TestBookLoadOk()
        {
            //SETUP
            const string searchFile  = "JsonBooks01*.json";
            var          testDataDir = TestFileHelpers.GetTestDataFileDirectory();

            //ATTEMPT
            var books = BookJsonLoader.LoadBooks(testDataDir, searchFile);

            //VERIFY
            books.Count().ShouldEqual(4);
        }
Beispiel #3
0
        public void TestBookLoadBuildReviewsOk()
        {
            //SETUP
            const string searchFile  = "JsonBooks01*.json";
            var          testDataDir = TestData.GetTestDataDir();

            //ATTEMPT
            var books = BookJsonLoader.LoadBooks(testDataDir, searchFile);

            //VERIFY
            var expectedAveVotes = new[] { 5.0, 3.0, 4.0, 4.5 };

            books.Select(x => x.Reviews.Average(y => y.NumStars)).ShouldEqual(expectedAveVotes);
        }
        public void TestLoadBooksOk()
        {
            //SETUP
            var testsDir = TestData.GetCallingAssemblyTopLevelDir();
            var dataDir  = Path.GetFullPath($"{testsDir}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}" +
                                            $"{nameof(ExampleWebApp)}{Path.DirectorySeparatorChar}wwwroot{Path.DirectorySeparatorChar}{DbSetupHelpers.SeedFileSubDirectory}");

            //ATTEMPT
            var books = BookJsonLoader.LoadBooks(dataDir,
                                                 DbSetupHelpers.SeedDataSearchName).ToList();

            //VERIFY
            books.Count.ShouldEqual(53);
            books.All(x => x.ActualPrice != 0).ShouldBeTrue();
        }
Beispiel #5
0
        public void TestBookLoadTagsOk()
        {
            //SETUP
            const string searchFile  = "JsonBooks01*.json";
            var          testDataDir = TestData.GetTestDataDir();

            //ATTEMPT
            var books = BookJsonLoader.LoadBooks(testDataDir, searchFile).ToList();

            //VERIFY
            books[0].Tags.Select(x => x.TagId).ShouldEqual(new[] { "Web" });
            books[1].Tags.Select(x => x.TagId).ShouldEqual(new [] { "Web" });
            books[2].Tags.Select(x => x.TagId).ShouldEqual(new[] { "Android" });
            books[3].Tags.Select(x => x.TagId).ShouldEqual(new[] { "Microsoft .NET", "Web" });
            books.SelectMany(x => x.Tags).Distinct().Count().ShouldEqual(3);
        }
Beispiel #6
0
        public static async Task <int> SeedDatabaseIfNoBooksAsync(this EfCoreContext context, string dataDirectory)
        {
            var numBooks = context.Books.Count();

            if (numBooks == 0)
            {
                //the database is empty so we fill it from a json file
                var books = BookJsonLoader.LoadBooks(Path.Combine(dataDirectory, SeedFileSubDirectory),
                                                     SeedDataSearchName).ToList();
                context.Books.AddRange(books);
                await context.SaveChangesAsync();

                //We add this separately so that it has the highest Id. That will make it appear at the top of the default list
                context.Books.Add(SpecialBook.CreateSpecialBook());
                await context.SaveChangesAsync();

                numBooks = books.Count + 1;
            }

            return(numBooks);
        }
Beispiel #7
0
        public static int SeedDatabase(this DataContext context, string dataDirectory)
        {
            if (!(context.GetService <IDatabaseCreator>() as RelationalDatabaseCreator).Exists())
            {
                throw new InvalidOperationException("The database does not exist. If you are using Migrations then run PMC command update-database to create it");
            }

            var numBooks = context.Books.Count();

            if (numBooks == 0)
            {
                //the database is emply so we fill it from a json file
                var books = BookJsonLoader.LoadBooks(Path.Combine(dataDirectory, SeedFileSubDirectory), SeedDataSearchName).ToList();
                context.Books.AddRange(books);
                context.SaveChanges();
                //We add this separately so that it has the highest Id. That will make it appear at the top of the default list
                context.Books.Add(SpecialBook.CreateSpecialBook());
                context.SaveChanges();
                numBooks = books.Count + 1;
            }

            return(numBooks);
        }