Exemplo n.º 1
0
        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);
        }