Ejemplo n.º 1
0
        public void EditBookTest()
        {
            using (var db = new BiblioContext())
            {
                Authors testAuthor = new Authors
                {
                    FirstName = "Aaron A.",
                    LastName  = "Aardvark"
                };
                db.Add(testAuthor);
                db.SaveChanges();
                int   authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId;
                Books testBook = new Books
                {
                    Title         = "A Aardvark Attractiveness Album",
                    AuthorId      = authorId,
                    Isbn10        = "0000000000",
                    Isbn13        = "0000000000000",
                    NumOfCopies   = 1,
                    NumOfPages    = 200,
                    Read          = true,
                    Review        = 5,
                    PublishedDate = "2020",
                    Publisher     = "Aardvark 'ardbacks",
                    Description   = "Aardvark attractiveness alternates accorinding to austerity apparentley, as acknowledged by Aardvark 'ardbacks anyway."
                };
                db.Add(testBook);
                db.SaveChanges();
                _testBiblio.SetSelectedBook(testBook);
            }
            string editDescription = "new description";
            int    editReview      = 4;
            bool   editRead        = false;

            _testBiblio.EditBook("Aaron a.", "Aardvark", "An Aardvark Attractiveness Album", "0000000000", "0000000000000", "Aardvark 'ardbacks", "2020", 200, editDescription, editReview, editRead);

            using (var db = new BiblioContext())
            {
                string newDescription = db.Books.OrderByDescending(b => b.BookId).First().Description;
                int    newReview      = db.Books.OrderByDescending(b => b.BookId).First().Review;
                bool   newRead        = db.Books.OrderByDescending(b => b.BookId).First().Read;

                Assert.AreEqual(editDescription, newDescription);
                Assert.AreEqual(editReview, newReview);
                Assert.AreEqual(editRead, newRead);

                var removeBook   = db.Books.OrderByDescending(b => b.BookId).First();
                var removeAuthor = db.Authors.OrderByDescending(a => a.AuthorId).First();
                db.Remove(removeBook);
                db.SaveChanges();
                db.Remove(removeAuthor);
                db.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        public void OrderByTitleTest()
        {
            using (var db = new BiblioContext())
            {
                Authors testAuthor = new Authors
                {
                    FirstName = "Aaron A.",
                    LastName  = "Aardvark"
                };
                db.Add(testAuthor);
                db.SaveChanges();

                int   authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId;
                Books testBook = new Books
                {
                    Title         = "A Aardvark Attractiveness Album",
                    AuthorId      = authorId,
                    Isbn10        = "0000000000",
                    Isbn13        = "0000000000000",
                    NumOfCopies   = 1,
                    NumOfPages    = 200,
                    Read          = true,
                    Review        = 5,
                    PublishedDate = "2020",
                    Publisher     = "Aardvark 'ardbacks",
                    Description   = "Aardvark attractiveness alternates accorinding to austerity apparentley, as acknowledged by Aardvark 'ardbacks anyway."
                };
                db.Add(testBook);
                db.SaveChanges();
            }

            var result = _testBiblio.GetAllBooksByTitle().First();

            Assert.AreEqual(result.Title, "A Aardvark Attractiveness Album");
            Assert.AreEqual(result.Author.FirstName, "Aaron A.");
            Assert.AreEqual(result.Author.LastName, "Aardvark");

            using (var db = new BiblioContext())
            {
                var removeBook   = db.Books.OrderByDescending(b => b.BookId).First();
                var removeAuthor = db.Authors.OrderByDescending(a => a.AuthorId).First();
                db.Remove(removeBook);
                db.SaveChanges();
                db.Remove(removeAuthor);
                db.SaveChanges();
            }
        }
Ejemplo n.º 3
0
        public void DeleteBookTest()
        {
            Books selectedBook;

            using (var db = new BiblioContext())
            {
                Authors testAuthor = new Authors
                {
                    FirstName = "Aaron A.",
                    LastName  = "Aardvark"
                };
                db.Add(testAuthor);
                db.SaveChanges();
                int   authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId;
                Books testBook = new Books
                {
                    Title         = "A Aardvark Attractiveness Album",
                    AuthorId      = authorId,
                    Isbn10        = "0000000000",
                    Isbn13        = "0000000000000",
                    NumOfCopies   = 1,
                    NumOfPages    = 200,
                    Read          = true,
                    Review        = 5,
                    PublishedDate = "2020",
                    Publisher     = "Aardvark 'ardbacks",
                    Description   = "Aardvark attractiveness alternates accorinding to austerity apparentley, as acknowledged by Aardvark 'ardbacks anyway."
                };
                db.Add(testBook);
                db.SaveChanges();
                selectedBook = testBook;
            }
            _testBiblio.DeleteBook(selectedBook);
            using (var db = new BiblioContext())
            {
                int resultBook   = db.Books.Where(b => b.Title == "A Aardvark Attractiveness Album").Count();
                int resultAuthor = db.Authors.Where(a => a.LastName == "Aardvark" && a.FirstName == "Aaron A.").Count();

                Assert.AreEqual(resultBook, 0);
                Assert.AreEqual(resultAuthor, 1);

                var removeAuthor = db.Authors.OrderByDescending(a => a.AuthorId).First();
                db.SaveChanges();
                db.Remove(removeAuthor);
                db.SaveChanges();
            }
        }
Ejemplo n.º 4
0
        public void AddBookTestExistingBook()
        {
            int preAddTestBooks   = 0;
            int preAddTestAuthor  = 0;
            int preNumCopies      = 0;
            int postAddTestBooks  = 0;
            int postAddTestAuthor = 0;
            int postNumCopies     = 0;

            using (var db = new BiblioContext())
            {
                Authors testAuthor = new Authors
                {
                    FirstName = "Aaron A.",
                    LastName  = "Aardvark"
                };
                db.Add(testAuthor);
                db.SaveChanges();
                int   authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId;
                Books testBook = new Books
                {
                    Title         = "A Aardvark Attractiveness Album",
                    AuthorId      = authorId,
                    Isbn10        = "0000000000",
                    Isbn13        = "0000000000000",
                    NumOfCopies   = 1,
                    NumOfPages    = 200,
                    Read          = true,
                    Review        = 5,
                    PublishedDate = "2020",
                    Publisher     = "Aardvark 'ardbacks",
                    Description   = "Aardvark attractiveness alternates accorinding to austerity apparentley, as acknowledged by Aardvark 'ardbacks anyway."
                };
                db.Add(testBook);
                db.SaveChanges();
                preAddTestBooks  = db.Books.Count();
                preAddTestAuthor = db.Authors.Count();
                preNumCopies     = db.Books.Where(b => b.Title == "A Aardvark Attractiveness Album").First().NumOfCopies;
            }

            _testBiblio.AddBook(AuthorFirst, AuthorLast, Title, Isbn10, Isbn13, Publisher, PublishedDate, NumOfPages, Description, Review, Read);

            using (var db = new BiblioContext())
            {
                postAddTestBooks  = db.Books.Count();
                postAddTestAuthor = db.Authors.Count();
                postNumCopies     = db.Books.Where(b => b.Title == "A Aardvark Attractiveness Album").First().NumOfCopies;
            }
            Assert.AreEqual(preAddTestBooks, postAddTestBooks);
            Assert.AreEqual(preAddTestAuthor, postAddTestAuthor);
            Assert.AreEqual(postNumCopies - 1, preNumCopies);
            using (var db = new BiblioContext())
            {
                var removeBook   = db.Books.OrderByDescending(b => b.BookId).First();
                var removeAuthor = db.Authors.OrderByDescending(a => a.AuthorId).First();
                db.Remove(removeBook);
                db.SaveChanges();
                db.Remove(removeAuthor);
                db.SaveChanges();
            }
        }
        //Method that adds books to the database
        //1)    Checks if the author is in the database
        //2a)   If author is in then author Id is set to the inputted author
        //2b)   If author isn't in then the author is added and author Id is set to the latest one in the database
        //2b.1) If the firstname contains Dr. or Prof. then that is separated and added as author Title.
        //3)    Checks if the book is in the database
        //3a)   If book is in then nothing is added
        //3b)   If book isnt then the book is added with inputted data
        public void AddBook(string authorFirst, string authorLast, string bookTitle, string isbn10 = null, string isbn13 = null, string publisher = null, string publishDate = null, int numOfPages = 0, string description = null, int review = 0, bool read = false)
        {
            using (var db = new BiblioContext())
            {
                int authorId;
                var authorQuery = db.Authors.Where(a => a.FirstName.Contains(authorFirst) && a.LastName.Contains(authorLast)).ToList();
                if (authorQuery.Count() == 0)
                {
                    if (authorFirst.ToUpper().Contains("DR."))
                    {
                        Authors author = new Authors
                        {
                            Title     = "Dr.",
                            FirstName = authorFirst.Remove(0, 4),
                            LastName  = authorLast
                        };
                        db.Add(author);
                        authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId;
                    }
                    else if (authorFirst.ToUpper().Contains("PROF."))
                    {
                        Authors author = new Authors
                        {
                            Title     = "Prof.",
                            FirstName = authorFirst.Remove(0, 6),
                            LastName  = authorLast
                        };
                        db.Add(author);
                        authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId;
                    }
                    else
                    {
                        Authors author = new Authors
                        {
                            Title     = null,
                            FirstName = authorFirst,
                            LastName  = authorLast
                        };
                        db.Add(author);
                    }
                    db.SaveChanges();
                    authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId;
                }

                else
                {
                    authorId = authorQuery.First().AuthorId;
                }
                var bookQuery = db.Books.Where(b => b.Title == bookTitle).ToList();
                if (bookQuery.Count() == 0)
                {
                    Books book = new Books
                    {
                        Title         = bookTitle,
                        Isbn10        = isbn10,
                        Isbn13        = isbn13,
                        Publisher     = publisher,
                        PublishedDate = publishDate,
                        NumOfPages    = numOfPages,
                        NumOfCopies   = 1,
                        Description   = description,
                        Read          = read,
                        AuthorId      = authorId,
                        Review        = review
                    };
                    db.Add(book);
                }
                else
                {
                    bookQuery.First().NumOfCopies++;
                }
                db.SaveChanges();
            }
        }