def test_transactions_deposit(self): expected_transactions = [ ('OPEN', 0, 0), ('DEPOSIT', 100, 100), ] account = BankAccount() account.deposit(100) self.assertEqual(account.transactions, expected_transactions)
def test_transactions_withdraw(self): expected_transactions = [ ('OPEN', 100, 100), ('WITHDRAWAL', -40, 60), ] account = BankAccount(balance=100) account.withdraw(40) self.assertEqual(account.transactions, expected_transactions)
def test_dir_does_not_show_balance_attribute(self): account = BankAccount() account.deposit(100) self.assertNotIn('_balance', dir(account)) allowed = { 'accounts', 'balance', 'deposit', 'withdraw', 'transfer', 'transactions', 'name', } | set(dir(type('', (), {})())) self.assertEqual(set(dir(account)) - allowed, set())
def test_transactions_with_transfer(self): mary_transactions = [ ('OPEN', 100, 100), ('WITHDRAWAL', -20, 80), ] dana_transactions = [ ('OPEN', 0, 0), ('DEPOSIT', 20, 20), ] mary_account = BankAccount(balance=100) dana_account = BankAccount(balance=0) mary_account.transfer(dana_account, 20) self.assertEqual(mary_account.transactions, mary_transactions) self.assertEqual(dana_account.transactions, dana_transactions)
def test_transactions_scenario(self): expected_transactions = [ ('OPEN', 0, 0), ('DEPOSIT', 100, 100), ('WITHDRAWAL', -40, 60), ('DEPOSIT', 95, 155), ] account = BankAccount() account.deposit(100) account.withdraw(40) account.deposit(95) self.assertEqual(account.transactions, expected_transactions)
def test_account_comparisons(self): account1 = BankAccount() account2 = BankAccount() self.assertTrue(account1 == account2) self.assertTrue(account1 >= account2) self.assertTrue(account1 <= account2) account1.deposit(100) account2.deposit(10) self.assertTrue(account1 != account2) self.assertTrue(account2 < account1) self.assertTrue(account1 > account2) self.assertTrue(account2 < account1) self.assertTrue(account1 >= account2) self.assertTrue(account2 <= account1)
def test_transactions_open(self): expected_transactions = [ ('OPEN', 100, 100), ] account = BankAccount(balance=100) self.assertEqual(account.transactions, expected_transactions)
def test_transfer(self): mary_account = BankAccount(balance=100) dana_account = BankAccount(balance=0) mary_account.transfer(dana_account, 20) self.assertEqual(mary_account.balance, 80) self.assertEqual(dana_account.balance, 20)
def test_repr(self): account = BankAccount() self.assertEqual(repr(account), 'BankAccount(balance=0)') account.deposit(200) self.assertEqual(repr(account), 'BankAccount(balance=200)')
def test_withdraw(self): account = BankAccount(balance=100) account.withdraw(40) self.assertEqual(account.balance, 60)
def test_deposit(self): account = BankAccount() account.deposit(100) self.assertEqual(account.balance, 100)
def test_opening_balance(self): account = BankAccount(balance=100) self.assertEqual(account.balance, 100)
def test_new_account_balance_default(self): account = BankAccount() self.assertEqual(account.balance, 0)
def test_balance_cannot_be_written(self): account1 = BankAccount() account2 = BankAccount(100) self.assertEqual(account1.balance, 0) with self.assertRaises(Exception): account1.balance = 50 self.assertEqual(account1.balance, 0) self.assertEqual(account2.balance, 100) with self.assertRaises(Exception): account2.balance = 50 self.assertEqual(account2.balance, 100) account1.deposit(100) account2.deposit(10) self.assertEqual(account1.balance, 100) self.assertEqual(account2.balance, 110) with self.assertRaises(Exception): account2.balance = 500 self.assertEqual(account2.balance, 110) account2.transfer(account1, 50) self.assertEqual(account1.balance, 150) self.assertEqual(account2.balance, 60)
def test_truthy_accounts(self): account = BankAccount() self.assertIs(bool(account), False) account.deposit(100) self.assertIs(bool(account), True)
def test_str(self): account = BankAccount() self.assertEqual(str(account), 'Account with balance of $0') account.deposit(200) self.assertEqual(str(account), 'Account with balance of $200')