public void Test_Lazy_Initialization() { Book book = bookDao.FindById(1); Author author = authorDao.FindById(1); // check if both obtained instances are proxies Assert.IsTrue(book is BookProxy); Assert.IsTrue(author is AuthorProxy); // check if both proxies have uninitialized collections BookProxy bookProxy = (BookProxy)book; AuthorProxy authorProxy = (AuthorProxy)author; Assert.IsFalse(bookProxy.AuthorsAreFetchedOrSet); Assert.IsFalse(authorProxy.BooksAreFetchedOrSet); // initialize collection of authors Console.WriteLine(); foreach (Author a in book.Authors) { Console.WriteLine($"First Name: {a.FirstName}, LastName: {a.LastName}"); } Console.WriteLine(); foreach (Book b in author.Books) { Console.WriteLine($"Id: {b.Id}, Title: {b.Title}"); } Console.WriteLine(); // check if object inside collection is also proxy var authors = new List <Author>(book.Authors); var books = new List <Book>(author.Books); Author a1 = authors[1]; Book b1 = books[0]; Assert.IsTrue(a1 is AuthorProxy); Assert.IsTrue(b1 is BookProxy); authorProxy = (AuthorProxy)a1; bookProxy = (BookProxy)b1; Assert.IsFalse(authorProxy.BooksAreFetchedOrSet); Assert.IsFalse(bookProxy.AuthorsAreFetchedOrSet); }
public void Test_BookDao() { var nonCachedInstanceFetch = new Stopwatch(); var cachedInstanceFetch = new Stopwatch(); // Test cache efficiency nonCachedInstanceFetch.Start(); Book nonCachedBookInstance = BookDao.FindById(1); nonCachedInstanceFetch.Stop(); cachedInstanceFetch.Start(); Book cachedBookInstance = BookDao.FindById(1); cachedInstanceFetch.Stop(); Assert.IsTrue(cachedInstanceFetch.ElapsedTicks < nonCachedInstanceFetch.ElapsedTicks); /* ***************************** */ // Check if returned by findById() method instances // are BookProxy' instance Assert.IsTrue(cachedBookInstance is BookProxy); Assert.IsTrue(nonCachedBookInstance is BookProxy); // *find* methods tests IList <Book> allBooks = BookDao.FindAll(); IList <Book> foundByTitle = BookDao.FindByTitle("Fast Recipes"); IList <Book> foundByTitle2 = BookDao.FindByTitle("Fake Your Death"); IList <Book> foundByTitle3 = BookDao.FindByTitle("True"); IList <Book> foundByRating = BookDao.FindByRating(7.8f); IList <Book> foundBySection = BookDao.FindBySection(BookSection.FICTION); Assert.AreEqual(1, foundByTitle.Count); Assert.AreEqual(1, foundByTitle2.Count); Assert.AreEqual(2, foundByTitle3.Count); Assert.AreEqual(3, foundByRating.Count); Assert.AreEqual(5, foundBySection.Count); Book temp = BookDao.FindById(1); string tempTitle = temp.Title; // test refresh method temp.Title = "New Title"; BookDao.Refresh(temp); Assert.AreEqual(tempTitle, temp.Title); // test update method temp.Title = "New Title"; BookDao.Update(temp); Book temp1 = BookDao.FindById(temp.Id); Assert.AreEqual(temp1.Title, temp.Title); // test save method again temp.Title = "Title*"; BookDao.Save(temp, SaveOption.UPDATE_IF_EXIST); temp1 = BookDao.FindById(temp.Id); Assert.AreEqual(temp1.Title, temp.Title); }