class TestSaving(unittest.TestCase): """ Tests SavingsAccount Class """ #create global variables def setUp(self): self.savings_account = SavingsAccount() #test the account is a child of the bank account class def test_create_savings_account(self): self.assertTrue(self.savings_account, BankAccount) #test add person def test_add_person(self): self.assertEqual(self.savings_account.add_person("Adams Kariuki"), "Person Successfully Added") #test add invalid person def test add_invalid_person(self): error = "Invalid Account Name" self.assertEqual(self.savings_account.add_person(""), error) self.assertEqual(self.savings_account.add_person("A@@ "), error) self.assertEqual(self.savings_account.add_person(68768), error) #test successful deposit def test_savings_deposit(self): self.savings_account.deposit(5000) self.assertEqual(self.savings_account.balance, 5000) #test invalid deposit amounts def test_saving_deposit_invalid(self): deposit_error_message = "Invalid Deposit amount" self.assertEqual(self.savings_account.deposit(-100), deposit_error_message) self.assertEqual(self.savings_account.deposit('ak122'), deposit_error_message) self.assertEqual(self.savings_account.deposit(0), deposit_error_message) self.assertEqual(self.savings_account.balance, 0) #test successful withdraw def test_savings_withdraw(self): self.savings_account.deposit(5000) self.savings_account.withdraw(3000) self.assertEqual(self.savings_account.balance, 2000) #test invalid withdraw amounts def test_savings_withdraw_invalid(self): withdraw_error_message = "Invalid Withdraw Amount" self.assertEqual(self.savings_account.withdraw('lk2000'), withdraw_error_message) self.assertEqual(self.savings_account.withdraw(0), withdraw_error_message) self.assertEqual(self.savings_account.withdraw(-100), withdraw_error_message) self.assertEqual(self.savings_account.balance, 0) #test account overdrafts def test_savings_overdraft(self): self.savings_account.withdraw(9000) self.assertGreaterEqual(self.savings_account.balance, -3000, msg='Overdraft Exceeded. Overdraft Limit is 3000 KES')
class TestBankingMethods: '''Testing module banking''' @pytest.fixture(scope='function', autouse=True) def setup_class(self): '''Setting up''' self.addr1 = Address('700 College Dr', 'Decorah', 'IA', '52101') self.addr2 = Address('1000 5th Ave', 'New York', 'NY', '10028') self.addr3 = Address('700 College Dr', 'Decorah', 'IA', '52101') self.customer1 = Customer('John Doe', '1861-09-01', self.addr1) self.customer2 = Customer('Jane Doe', '1861-09-02', self.addr1) self.check_acc = CheckingAccount(self.customer1, 15.00, 100.00) self.save_acc = SavingsAccount(self.customer2, 3.5, 200.00) def test_address(self): '''Testing address properties''' assert self.addr1.street == '700 College Dr' assert self.addr1.city == 'Decorah' assert self.addr1.state == 'IA' assert self.addr1.zip == '52101' def test_address_str(self, capsys): '''Testing address.__str__ method''' print(self.addr1) out, err = capsys.readouterr() assert out.strip() == ('700 College Dr\nDecorah, IA 52101') def test_address_eq(self): '''Testing address.__eq__ method''' assert self.addr1 != self.addr2 assert self.addr1 is not self.addr3 assert self.addr1 == self.addr3 def test_customer(self): '''Testing customer properties''' assert self.customer1.name == 'John Doe' assert self.customer1.dob == '1861-09-01' assert self.customer1.address == Address('700 College Dr', 'Decorah', 'IA', '52101') def test_customer_str(self, capsys): '''Testing customer.__str__ method''' print(self.customer1) out, err = capsys.readouterr() assert out.strip() == ('John Doe (1861-09-01)\n' + '700 College Dr\nDecorah, IA 52101') def test_customer_move(self, capsys): '''Testing customer.move method''' self.customer1.move(self.addr2) assert self.customer1.address == Address('1000 5th Ave', 'New York', 'NY', '10028') print(self.customer1) out, err = capsys.readouterr() assert out.strip() == ('John Doe (1861-09-01)\n' + '1000 5th Ave\n' + 'New York, NY 10028') #Raises TypeError -- Account is abstract, cannot be instantiated def test_abstract_class_Account(self): with pytest.raises(TypeError) as excinfo: self.acc = Account(self.customer1, 500) exception_msg = excinfo.value.args[0] assert exception_msg == 'Can\'t instantiate abstract class Account with abstract methods __init__' def test_account(self): '''Testing account properties''' assert self.check_acc.owner == self.customer1 assert self.save_acc.owner == self.customer2 assert self.check_acc.balance == pytest.approx(100, 0.01) assert self.save_acc.balance == pytest.approx(200, 0.01) def test_account_deposit(self): '''Testing account.deposit method''' self.check_acc.deposit(60) assert self.check_acc.balance == pytest.approx(160, 0.01) self.save_acc.deposit(60) assert self.save_acc.balance == pytest.approx(260, 0.01) def test_account_deposit_error(self): with pytest.raises(ValueError) as excinfo: self.check_acc.deposit(-10) exception_msg = excinfo.value.args[0] assert exception_msg == 'Must deposit positive amount' def test_account_close(self): '''Testing account.close method''' self.check_acc.close() assert self.check_acc.balance == pytest.approx(0, 0.01) self.save_acc.close() assert self.save_acc.balance == pytest.approx(0, 0.01) def test_checking_process_check(self): '''Testing check processing method''' assert self.check_acc.balance == pytest.approx(100, 0.01) self.check_acc.process_check(30) assert self.check_acc.balance == pytest.approx(70, 0.01) self.check_acc.process_check(80) assert self.check_acc.balance == pytest.approx(55, 0.01) def test_checking_str(self, capsys): '''Testing checking.__str__ method''' print(self.check_acc) out, err = capsys.readouterr() assert out.strip() == ('Checking account\n' + 'Owner: John Doe (1861-09-01)\n' + '700 College Dr\n' + 'Decorah, IA 52101\n' + 'Balance: 100.00') def test_savings_yield_interest(self): '''Testing check processing method''' assert self.save_acc.balance == pytest.approx(200.00, 0.01) self.save_acc.yield_interest() assert self.save_acc.balance == pytest.approx(207.00, 0.01) def test_savings_str(self, capsys): '''Testing savings.__str__ method''' print(self.save_acc) out, err = capsys.readouterr() assert out.strip() == ('Savings account\n' + 'Owner: Jane Doe (1861-09-02)\n' + '700 College Dr\n' + 'Decorah, IA 52101\n' + 'Balance: 200.00')
from bank import SavingsAccount acc1 = SavingsAccount("VD", 1000, 10000, 1000) acc1.show_balance() acc1.deposit(5000) acc1.show_balance() acc1.withdraw(8000) acc1.show_balance() acc1.withdraw(6000) acc1.show_balance()