Exemplo n.º 1
0
        public void CanCreateLoan()
        {
            var helper = Substitute.For<ILoanHelper>();
            var book = Substitute.For<IBook>();
            var member = Substitute.For<IMember>();

            var loanDao = new LoanDao(helper);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            // Adds the member to a collection of members and returns new member.
            Assert.Equal(0, loanDao.LoanList.Count);

            // Tell the mock what to return when it is called.
            helper.MakeLoan(book, member, borrowDate, dueDate).Returns(new Loan(book, member, borrowDate, dueDate));

            var result = loanDao.CreateLoan(member, book, borrowDate, dueDate);

            // Assert that the mock's MakeLoan method was called.
            helper.Received().MakeLoan(book, member, borrowDate, dueDate);

            Assert.NotNull(result);
            Assert.Equal(book, result.Book);
            Assert.Equal(member, result.Borrower);

            // We don't want to store until we commit the loan.
        }
Exemplo n.º 2
0
        public void CanCommitLoan()
        {
            var helper = Substitute.For<ILoanHelper>();
            var book = Substitute.For<IBook>();
            var member = Substitute.For<IMember>();

            var loanDao = new LoanDao(helper);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            // Adds the member to a collection of members and returns new member.
            Assert.Equal(0, loanDao.LoanList.Count);

            // Tell the mock what to return when it is called.
            helper.MakeLoan(book, member, borrowDate, dueDate).Returns(Substitute.For<Loan>(book, member, borrowDate, dueDate));

            var loan = loanDao.CreateLoan(member, book, borrowDate, dueDate);

            // Assert that the mock's MakeLoan method was called.
            helper.Received().MakeLoan(book, member, borrowDate, dueDate);

            loanDao.CommitLoan(loan);

            Assert.NotEqual(0, loan.ID);

            Assert.Equal(1, loanDao.LoanList.Count);

            var stored = loanDao.LoanList[0];

            Assert.Equal(loan, stored);
        }
        public void CanCreateLoan()
        {
            ILoanHelper loanHelper = new LoanHelper();
            ILoanDAO loanDao = new LoanDao(loanHelper);

            IMemberHelper memberHelper = new MemberHelper();
            IMemberDAO memberDao = new MemberDao(memberHelper);

            IBookHelper bookHelper = new BookHelper();
            IBookDAO bookDao = new BookDao(bookHelper);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var member = memberDao.AddMember("Jim", "Tulip", "csu phone", "*****@*****.**");

            var book = bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var loan = loanDao.CreateLoan(member, book, borrowDate, dueDate);

            Assert.NotNull(loan);

            Assert.Empty(loanDao.LoanList);

            Assert.Equal(book, loan.Book);
            Assert.Equal(member, loan.Borrower);

            Assert.Equal(0, loan.ID);
        }
        public void RunScenario()
        {
            var bookDao = new BookDao(new BookHelper());
               var loanDao = new LoanDao(new LoanHelper());
               var memberDao = new MemberDao(new MemberHelper());

            var display = new MainWindow();
            var reader = new CardReader();
            var scanner = new Scanner();
            var printer = new Printer();

            var controller = new BorrowController(display, reader, scanner, printer,
                bookDao, loanDao, memberDao);

            // Test pre-conditions
            Assert.NotNull(controller);

            Assert.NotNull(bookDao);
            Assert.NotNull(loanDao);
            Assert.NotNull(memberDao);
            Assert.NotNull(display);
            Assert.NotNull(reader);
            Assert.NotNull(scanner);
            Assert.NotNull(printer);

            Assert.Equal(controller._bookDAO, bookDao);
            Assert.Equal(controller._loanDAO, loanDao);
            Assert.Equal(controller._memberDAO, memberDao);
            Assert.Equal(controller._display, display);
            Assert.Equal(controller._reader, reader);
            Assert.Equal(controller._scanner, scanner);
            Assert.Equal(controller._printer, printer);

            Assert.Equal(controller._reader.Listener, controller);
            Assert.Equal(controller._scanner.Listener, controller);

            Assert.Equal(EBorrowState.CREATED, controller._state);

            // Run use case
            controller.initialise();

            // Test post-conditions
            // Borrow book UI Displayed
            Assert.True(display.Display.IsEnabled);

            var borrowCtrl = ((BorrowControl) display.Display);
            var swipeCtrl = borrowCtrl._controlDict.Single(c => c.Value is SwipeCardControl).Value as SwipeCardControl;

            Assert.NotNull(swipeCtrl);
            Assert.True(swipeCtrl.IsEnabled);
            Assert.True(swipeCtrl.cancelButton.IsEnabled);

            Assert.True(reader.Enabled);
            Assert.True(!scanner.Enabled);
            Assert.Equal(EBorrowState.INITIALIZED, controller._state);
        }
        public void CanGetLoanByBookTitle()
        {
            ILoanHelper loanHelper = new LoanHelper();
            ILoanDAO loanDao = new LoanDao(loanHelper);

            IMemberHelper memberHelper = new MemberHelper();
            IMemberDAO memberDao = new MemberDao(memberHelper);

            IBookHelper bookHelper = new BookHelper();
            IBookDAO bookDao = new BookDao(bookHelper);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var member = memberDao.AddMember("Jim", "Tulip", "csu phone", "*****@*****.**");

            var book = bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var loan = loanDao.CreateLoan(member, book, borrowDate, dueDate);

            loanDao.CommitLoan(loan);

            for (int i = 0; i < 10; i++)
            {
                var m = memberDao.AddMember("Test", "Test", "Test Phone", "Test Email");
                var b = bookDao.AddBook("Test", "Test", "Test");

                var l = loanDao.CreateLoan(m, b, borrowDate, dueDate);

                loanDao.CommitLoan(l);
            }

            var result = loanDao.FindLoansByBookTitle(book.Title);

            var single = result.Single();

            Assert.Equal(loan, single);
        }
Exemplo n.º 6
0
        public void CanCreateLoanDao()
        {
            var helper = Substitute.For<ILoanHelper>();

            var loanDao = new LoanDao(helper);

            Assert.NotNull(loanDao);
        }
Exemplo n.º 7
0
        public void LoanDaoImplementsILoanDAOInterface()
        {
            var helper = Substitute.For<ILoanHelper>();

            var loanDao = new LoanDao(helper);

            Assert.IsAssignableFrom<ILoanDAO>(loanDao);

            var typedMember = loanDao as ILoanDAO;

            Assert.NotNull(typedMember);
        }
Exemplo n.º 8
0
        public void GetMemberByIdReturnsNullIfNotFound()
        {
            var helper = Substitute.For<ILoanHelper>();

            var loanDao = new LoanDao(helper);

            loanDao.LoanList = new List<ILoan>
            {
                Substitute.For<ILoan>(),
                Substitute.For<ILoan>(),
            };

            var loan = loanDao.GetLoanByID(2);

            Assert.Null(loan);
        }
Exemplo n.º 9
0
        public void CreateLoanThrowsNullWhenBookOrMemberIsNull()
        {
            var helper = Substitute.For<ILoanHelper>();
            var book = Substitute.For<IBook>();
            var member = Substitute.For<IMember>();

            var loanDao = new LoanDao(helper);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var ex1 = Assert.Throws<ArgumentException>(() =>
            {
                var result = loanDao.CreateLoan(null, book, borrowDate, dueDate);
            });

            Assert.Equal("A Member must be provided to create a loan", ex1.Message);

            var ex2 = Assert.Throws<ArgumentException>(() =>
            {
                var result = loanDao.CreateLoan(member, null, borrowDate, dueDate);
            });

            Assert.Equal("A Book must be provided to create a loan", ex2.Message);
        }
Exemplo n.º 10
0
        public void CreateLoanDaoThrowsExceptionOnNullParameter()
        {
            var ex = Assert.Throws<ArgumentException>(() => { var loanDao = new LoanDao(null); });

            Assert.Equal("Helper must be provided when creating LoanDao", ex.Message);
        }
        public void CanUpdateOverdueStatus()
        {
            ILoanHelper loanHelper = new LoanHelper();
            ILoanDAO loanDao = new LoanDao(loanHelper);

            IMemberHelper memberHelper = new MemberHelper();
            IMemberDAO memberDao = new MemberDao(memberHelper);

            IBookHelper bookHelper = new BookHelper();
            IBookDAO bookDao = new BookDao(bookHelper);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var member = memberDao.AddMember("Jim", "Tulip", "csu phone", "*****@*****.**");

            var book = bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var loan = loanDao.CreateLoan(member, book, borrowDate, dueDate);

            loanDao.CommitLoan(loan);

            Assert.Equal(LoanState.CURRENT, loan.State);

            loanDao.UpdateOverDueStatus(DateTime.Today.AddMonths(1));

            Assert.Equal(LoanState.OVERDUE, loan.State);
        }
Exemplo n.º 12
0
        public void CanGetLoansByBorrower()
        {
            var helper = Substitute.For<ILoanHelper>();

            var loanDao = new LoanDao(helper);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var borrower = Substitute.For<Member>("Borris", "Natasha", "phone", "email", 1);

            var storedLoan = new Loan(Substitute.For<IBook>(), borrower, borrowDate, dueDate);

            loanDao.LoanList = new List<ILoan>
            {
                Substitute.For<ILoan>(),
                Substitute.For<ILoan>(),
                storedLoan
            };

            var loans = loanDao.FindLoansByBorrower(borrower);

            Assert.NotNull(loans);
            Assert.Equal(storedLoan, loans[0]);
        }
Exemplo n.º 13
0
        public void CanGetLoansByBookTitle()
        {
            var helper = Substitute.For<ILoanHelper>();

            var loanDao = new LoanDao(helper);

            var book = Substitute.For <Book>("author", "testing", "call", 1);
            var member = Substitute.For<IMember>();

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var storedLoan = Substitute.For<Loan>(book, member, borrowDate, dueDate);

            loanDao.LoanList = new List<ILoan>
            {
                Substitute.For<Loan>(Substitute.For<Book>("author", "title", "call", 1), Substitute.For<IMember>(), borrowDate, dueDate),
                Substitute.For<Loan>(Substitute.For<Book>("author", "title", "call", 1), Substitute.For<IMember>(), borrowDate, dueDate),
                storedLoan
            };

            var loans = loanDao.FindLoansByBookTitle("testing");

            Assert.NotNull(loans);
            Assert.Equal(storedLoan, loans[0]);
        }
        public void GetLoanByLoanIdReturnsNullIfNotFound()
        {
            ILoanHelper loanHelper = new LoanHelper();
            ILoanDAO loanDao = new LoanDao(loanHelper);

            IMemberHelper memberHelper = new MemberHelper();
            IMemberDAO memberDao = new MemberDao(memberHelper);

            IBookHelper bookHelper = new BookHelper();
            IBookDAO bookDao = new BookDao(bookHelper);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var member = memberDao.AddMember("Jim", "Tulip", "csu phone", "*****@*****.**");

            var book = bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var loan = loanDao.CreateLoan(member, book, borrowDate, dueDate);

            loanDao.CommitLoan(loan);

            for (int i = 0; i < 10; i++)
            {
                var m = memberDao.AddMember("Test", "Test", "Test Phone", "Test Email");
                var b = bookDao.AddBook("Test", "Test", "Test");

                var l = loanDao.CreateLoan(m, b, borrowDate, dueDate);

                loanDao.CommitLoan(l);
            }

            var result = loanDao.GetLoanByID(1000);

            Assert.Null(result);
        }
Exemplo n.º 15
0
        public void CanGetLoanById()
        {
            var helper = Substitute.For<ILoanHelper>();

            var loanDao = new LoanDao(helper);

            var book = Substitute.For<IBook>();
            var member = Substitute.For<IMember>();

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            helper.MakeLoan(book, member, borrowDate, dueDate)
                .Returns(Substitute.For<Loan>(book, member, borrowDate, dueDate));

            // Commit one we can test.
            var loan = loanDao.CreateLoan(member, book, borrowDate, dueDate);
            loanDao.CommitLoan(loan);

            helper.Received().MakeLoan(book, member, borrowDate, dueDate);

            var max = loanDao.LoanList.Max(l => l.ID);

            var result = loanDao.GetLoanByID(max);

            Assert.NotNull(loan);

            Assert.Equal(loan.ID, result.ID);
            Assert.Equal(loan, result);
        }
Exemplo n.º 16
0
        public void CanUpdateOverdueStatus()
        {
            var helper = Substitute.For<ILoanHelper>();
            var book = Substitute.For<IBook>();
            var member = Substitute.For<IMember>();

            var loanDao = new LoanDao(helper);

            var borrowDate = DateTime.Today.AddMonths(-1);
            var dueDate = DateTime.Today.AddMonths(-1).AddDays(7);

            // Adds the member to a collection of members and returns new member.
            Assert.Equal(0, loanDao.LoanList.Count);

            // Tell the mock what to return when it is called.
            helper.MakeLoan(book, member, borrowDate, dueDate).Returns(Substitute.For<Loan>(book, member, borrowDate, dueDate));

            var loan = loanDao.CreateLoan(member, book, borrowDate, dueDate);

            // Assert that the mock's MakeLoan method was called.
            helper.Received().MakeLoan(book, member, borrowDate, dueDate);

            loanDao.CommitLoan(loan);

            // Store two more for iteration
            loanDao.CommitLoan(Substitute.For<ILoan>());
            loanDao.CommitLoan(Substitute.For<ILoan>());

            loanDao.UpdateOverDueStatus(DateTime.Today);

            Assert.Contains(loan, loanDao.LoanList);

            Assert.Equal(LoanState.OVERDUE, loan.State);
        }
        public void CanGetOverdueLoans()
        {
            ILoanHelper loanHelper = new LoanHelper();
            ILoanDAO loanDao = new LoanDao(loanHelper);

            IMemberHelper memberHelper = new MemberHelper();
            IMemberDAO memberDao = new MemberDao(memberHelper);

            IBookHelper bookHelper = new BookHelper();
            IBookDAO bookDao = new BookDao(bookHelper);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var member = memberDao.AddMember("Jim", "Tulip", "csu phone", "*****@*****.**");

            var book = bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var loan = loanDao.CreateLoan(member, book, borrowDate, dueDate);

            loanDao.CommitLoan(loan);

            Assert.Equal(LoanState.CURRENT, loan.State);

            loanDao.UpdateOverDueStatus(DateTime.Today.AddMonths(1));

            Assert.Equal(LoanState.OVERDUE, loan.State);

            for (int i = 0; i < 10; i++)
            {
                var m = memberDao.AddMember("Test", "Test", "Test Phone", "Test Email");
                var b = bookDao.AddBook("Test", "Test", "Test");

                var l = loanDao.CreateLoan(m, b, borrowDate, dueDate);

                loanDao.CommitLoan(l);
            }

            var overdue = loanDao.FindOverDueLoans();

            Assert.Equal(1, overdue.Count);
            Assert.Equal(loan, overdue[0]);
        }