Exemple #1
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 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);
        }
Exemple #3
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
            }
        }
Exemple #4
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);
            }
        }
        public void TestCreateBookWithExistingAuthorAddedButNotSavedToDatabase()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

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

            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);
        }
Exemple #6
0
        public void TestCreateBookWriteTwiceBad()
        {
            //SETUP
            var inMemDb = new SqliteInMemory();

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

                //ATTEMPT
                context.Add(oneBook);
                context.SaveChanges();
                var state1 = context.Entry(oneBook).State;
                context.Add(oneBook);
                var state2 = context.Entry(oneBook).State;
                var ex     = Assert.Throws <DbUpdateException>(() => context.SaveChanges());

                //VERIFY
                ex.Message.ShouldEqual("An error occurred while updating the entries. See the inner exception for details.");
                state1.ShouldEqual(EntityState.Unchanged);
                state2.ShouldEqual(EntityState.Added);
            }
        }