def test_reopened_account_does_not_retain_balance(self): account = BankAccount() account.open() account.deposit(50) account.close() account.open() self.assertEqual(account.get_balance(), 0)
class TestBankAccount(unittest.TestCase): def setUp(self): self.Ivo = BankAccount("Ivo", 1000, "$") self.Rado = BankAccount("Rado", 500, "$") def test_bank_account_init(self): self.assertEqual(self.Ivo._name, "Ivo") self.assertEqual(self.Ivo._balance, 1000) self.assertEqual(self.Ivo._currency, "$") def test_deposit(self): self.Ivo.deposit(200) self.assertEqual(self.Ivo._balance, 1200) def test_balance(self): self.assertEqual(self.Ivo.balance(), 1000) def test_withdraw(self): self.assertEqual(self.Ivo.withdraw(400), True) self.assertEqual(self.Ivo.withdraw(1400), False) def test___str__(self): self.assertEqual(str(self.Ivo), "Bank account for Ivo with balance of 1000$") def test___int__(self): self.assertEqual(int(self.Ivo), 1000)
def test_can_deposit_money_sequentially(self): account = BankAccount() account.open() account.deposit(100) account.deposit(50) self.assertEqual(account.get_balance(), 150)
def test_can_withdraw_money(self): account = BankAccount() account.open() account.deposit(100) account.withdraw(50) self.assertEqual(account.get_balance(), 50)
def test_can_withdraw_money_sequentially(self): account = BankAccount() account.open() account.deposit(100) account.withdraw(20) account.withdraw(80) self.assertEqual(account.get_balance(), 0)
def setUp(self): self.name = 'viki' self.name2 = 'stenly' self.balance = 1000000 self.currency = 'pound' self.account = BankAccount(self.name, self.balance, self.currency) self.account2 = BankAccount(self.name2, self.balance, self.currency)
def test_cannot_withdraw_more_than_deposited(self): account = BankAccount() account.open() account.deposit(25) with self.assertRaises(ValueError): account.withdraw(50)
def test_cannot_withdraw_negative(self): account = BankAccount() account.open() account.deposit(100) with self.assertRaisesWithMessage(ValueError): account.withdraw(-50)
def test_checking_balance_of_closed_account_throws_error(self): account = BankAccount() account.open() account.close() with self.assertRaisesWithMessage(ValueError): account.get_balance()
def test_withdraw_from_closed_account(self): account = BankAccount() account.open() account.close() with self.assertRaisesWithMessage(ValueError): account.withdraw(50)
def test_deposit_into_closed_account(self): account = BankAccount() account.open() account.close() with self.assertRaisesWithMessage(ValueError): account.deposit(50)
def test_can_handle_concurrent_transactions(self): account = BankAccount() account.open() account.deposit(1000) self.adjust_balance_concurrently(account) self.assertEqual(account.get_balance(), 1000)
class TestBankAccount(unittest.TestCase): def setUp(self): self.account = BankAccount("Rado", 1000, "$") def test_account_init(self): self.assertEqual(self.account.name, "Rado") self.assertEqual(self.account.balance, 1000) self.assertEqual(self.account.currency, "$") def test_balance(self): self.assertEqual(self.account.balance, 1000) def test_deposit(self): self.assertEqual(self.account.deposit(50), 1050)
class TestBankAccount(unittest.TestCase): def setUp(self): self.bank_account = BankAccount("Rado", 0, "$") def test_bankaccount_init(self): self.assertEqual(self.bank_account._name, "Rado") self.assertEqual(self.bank_account._balance, 0) self.assertEqual(self.bank_account._currency, "$") def test_bankaccount_str(self): self.assertEqual(str(self.bank_account), "Bank account for Rado with balance of 0$") def test_bankaccount_int(self): self.assertEqual(int(self.bank_account), 0) def test_bankaccount_eq(self): other1 = BankAccount("Martin", 1000, "$") self.assertTrue(self.bank_account._currency == other1._currency) other2 = BankAccount("Martin", 1000, "lv") self.assertFalse(self.bank_account._currency == other2._currency) def test_bankaccount_deposit(self): self.bank_account.deposit(100) self.assertEqual(self.bank_account._balance, 100) def test_bankaccount_balance(self): self.assertEqual(self.bank_account.balance(), '0$') def test_bankaccount_withdraw(self): self.bank_account._balance = 100 self.assertTrue(self.bank_account.withdraw(50)) self.assertFalse(self.bank_account.withdraw(200)) def test_bankaccount_transfer_to(self): other = BankAccount("Martin", 1000, "$") self.bank_account._balance = 100 self.assertTrue(self.bank_account.transfer_to(other, 50)) #self.bank_account.transfer_to(other, 50) self.assertEqual(other._balance, 1050) self.assertEqual(self.bank_account._balance, 50)
def __init__(self, name, email): self.name = name self.email = email self.accounts = [BankAccount(int_rate=0.02, balance=0)]
def test_deposit_and_withdraw_validation(self): mary_account = BankAccount(balance=100) # Can't start with negative balance with self.assertRaises(ValueError): dana_account = BankAccount(balance=-10) dana_account = BankAccount(balance=0) self.assertEqual(dana_account.balance, 0) self.assertEqual(mary_account.balance, 100) # Can't deposit negative amount with self.assertRaises(ValueError): mary_account.deposit(-10) self.assertEqual(mary_account.balance, 100) # Can't withdraw more than we have (no overdrafting) with self.assertRaises(ValueError): mary_account.withdraw(101) self.assertEqual(mary_account.balance, 100) # Can't transfer more than we have with self.assertRaises(ValueError): mary_account.transfer(dana_account, 101) self.assertEqual(mary_account.balance, 100) self.assertEqual(dana_account.balance, 0) # Can't transfer negative amount with self.assertRaises(ValueError): mary_account.transfer(dana_account, -5) # We can transfer everything mary_account.transfer(dana_account, 100) self.assertEqual(mary_account.balance, 0) self.assertEqual(dana_account.balance, 100) # We can transfer partial amounts dana_account.transfer(mary_account, 10) self.assertEqual(mary_account.balance, 10) self.assertEqual(dana_account.balance, 90) # We can't transfer a negative amount, even when both accounts have money with self.assertRaises(ValueError): mary_account.transfer(dana_account, -5) self.assertEqual(mary_account.balance, 10) self.assertEqual(dana_account.balance, 90)
__author__ = 'vikram' from bank_account import BankAccount account = BankAccount() account.deposit(40) print(account) def exclude(pred_func, full_list): exc_list = [] for n in full_list: if not pred_func(n): exc_list.append(n) return exc_list print exclude(lambda x: len(x) > 3, ["red", "blue", "green"]) print exclude(bool, [False, True, False]) def call(some_func, *args): print args return some_func(*args) print call(int) print call(len, "hello")
def setUp(self): self.my_account = BankAccount("Rado", 100, "$")
class TestBankAccount(unittest.TestCase): def setUp(self): self.my_account = BankAccount("Rado", 100, "$") def test_create_new_account_instance(self): # self.assertTrue(isinstance(self.my_account, BankAccount)) self.assertEqual(self.my_account.name, "Rado") self.assertEqual(self.my_account.balance, 100) self.assertEqual(self.my_account.currency, "$") def test_int_cast(self): self.assertEqual(int(self.my_account), 100) def test_str_cast(self): helper_str = "Bank account for Rado with balance of 100$" self.assertEqual(str(self.my_account), helper_str) def testing_the_current_balance(self): self.assertEqual(100, self.my_account.get_balance()) def test_deposit_amount(self): self.my_account.deposit(1000) self.assertEqual(1100, self.my_account.balance) def test_deposit_with_negative_amount(self): with self.assertRaises(ValueError): self.my_account.deposit(-110) self.assertEqual(self.my_account.balance, 100) def test_withdraw_amount(self): self.assertTrue(self.my_account.withdraw(0)) def test_value_error_raises_from_greater_amount(self): self.assertFalse(self.my_account.withdraw(110)) def test_value_error_raises_from_negative_balance(self): with self.assertRaises(ValueError): current_account = BankAccount("Ivo", -10, "$") def test_type_error_raises_from_float_balance(self): with self.assertRaises(TypeError): current_account = BankAccount("Az", 0.25, "$") def test_transfer_to_different_currency(self): your_account = BankAccount("Ivo", 200, "&") with self.assertRaises(ValueError): self.my_account.transfer_to(your_account, 200) self.assertEqual(self.my_account.balance, 1000) self.assertEqual(your_account.balance, 200) def test_transfert_more_money_than_we_have(self): your_account = BankAccount("Ivo", 200, "$") self.assertFalse(your_account.transfer_to(self.my_account, 300)) self.assertEqual(self.my_account.balance, 1000) self.assertEqual(your_account.balance, 200) def test_tranfer_to(self): your_account = BankAccount("Ivo", 200, "$") self.assertTrue(self.my_account.transfer_to(your_account, 50)) self.assertEqual(your_account.balance, 250) self.assertEqual(my_account.balance, 50)
def setUp(self): self.my_account = BankAccount()
def test_newly_opened_account_has_zero_balance(self): account = BankAccount() account.open() self.assertEqual(account.get_balance(), 0)
def test_close_already_closed_account(self): account = BankAccount() with self.assertRaises(ValueError) as err: account.close() self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "account not open")
def test_can_deposit_money(self): account = BankAccount() account.open() account.deposit(100) self.assertEqual(account.get_balance(), 100)
from bank_account import BankAccount # accounts account1 = BankAccount('John', 'Doe') account2 = BankAccount('Jane', 'Doe') account3 = BankAccount('Test', 'User') #account1.deposit(1000) account3.deposit(2000) print(account3.account_detail) account3.withdraw(1000) #print(account1.account_detail) #print() print(account3.account_detail) print() print('Total accounts: {}'.format(BankAccount.accounts))
def setUp(self): self.account = BankAccount("Rado", 1000, "$")
def test_open_already_opened_account(self): account = BankAccount() account.open() with self.assertRaisesWithMessage(ValueError): account.open()
def test_withdraw(self): account = BankAccount(balance=100) account.withdraw(40) self.assertEqual(account.balance, 60)
def test_opening_balance(self): account = BankAccount(balance=100) self.assertEqual(account.balance, 100)
class BankAccountTest(unittest.TestCase): """ The main bank account testing class. Here we define various methods which specifically test our module for bugs and errors. """ def setUp(self): self.account = BankAccount() def test_newly_opened_account_has_zero_balance(self): self.account.open() self.assertEqual(self.account.get_balance(), 0) def test_can_deposit_money(self): self.account.open() self.account.deposit(99) self.assertEqual(self.account.get_balance(), 99) def test_can_deposit_money_sequentially(self): self.account.open() self.account.deposit(20) self.account.deposit(30) self.assertEqual(self.account.get_balance(), 50) def test_can_withdraw_money(self): self.account.open() self.account.deposit(10) self.account.deposit(80) self.account.withdraw(30) self.account.withdraw(20) self.assertEqual(self.account.get_balance(), 40) def test_checking_balance_of_closed_account_throws_error(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.get_balance()
def setUp(self): self.bank_account = BankAccount("Rado", 0, "$")
def __init__(self, username, email_address): self.name = username self.email = email_address self.account = BankAccount(0.02, 0)
def test_cannot_deposit_negative(self): account = BankAccount() account.open() with self.assertRaisesWithMessage(ValueError): account.deposit(-50)
def test_close_already_closed_account(self): account = BankAccount() with self.assertRaisesWithMessage(ValueError): account.close()
def test_transfert_more_money_than_we_have(self): your_account = BankAccount("Ivo", 200, "$") self.assertFalse(your_account.transfer_to(self.my_account, 300)) self.assertEqual(self.my_account.balance, 1000) self.assertEqual(your_account.balance, 200)
class BankAccountTests(unittest.TestCase): def setUp(self): self.account = BankAccount() def test_newly_opened_account_has_zero_balance(self): self.account.open() self.assertEqual(self.account.get_balance(), 0) def test_can_deposit_money(self): self.account.open() self.account.deposit(100) self.assertEqual(self.account.get_balance(), 100) def test_can_deposit_money_sequentially(self): self.account.open() self.account.deposit(100) self.account.deposit(50) self.assertEqual(self.account.get_balance(), 150) def test_can_withdraw_money(self): self.account.open() self.account.deposit(100) self.account.withdraw(50) self.assertEqual(self.account.get_balance(), 50) def test_can_withdraw_money_sequentially(self): self.account.open() self.account.deposit(100) self.account.withdraw(20) self.account.withdraw(80) self.assertEqual(self.account.get_balance(), 0) def test_checking_balance_of_closed_account_throws_error(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.get_balance() def test_deposit_into_closed_account(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.deposit(50) def test_withdraw_from_closed_account(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.withdraw(50) def test_cannot_withdraw_more_than_deposited(self): self.account.open() self.account.deposit(25) with self.assertRaises(ValueError): self.account.withdraw(50) def test_cannot_withdraw_negative(self): self.account.open() self.account.deposit(100) with self.assertRaises(ValueError): self.account.withdraw(-50) def test_cannot_deposit_negative(self): self.account.open() with self.assertRaises(ValueError): self.account.deposit(-50) def test_can_handle_concurrent_transactions(self): self.account.open() self.account.deposit(1000) for _ in range(10): self.adjust_balance_concurrently() def adjust_balance_concurrently(self): def transact(): self.account.deposit(5) time.sleep(0.001) self.account.withdraw(5) # Greatly improve the chance of an operation being interrupted # by thread switch, thus testing synchronization effectively try: sys.setswitchinterval(1e-12) except AttributeError: # For Python 2 compatibility sys.setcheckinterval(1) threads = [] for _ in range(1000): t = threading.Thread(target=transact) threads.append(t) t.start() for thread in threads: thread.join() self.assertEqual(self.account.get_balance(), 1000)
def test_new_account_balance_default(self): account = BankAccount() self.assertEqual(account.balance, 0)
def setUp(self): self.account = BankAccount()
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 setUp(self): self.acc = BankAccount("Sasho", 100, "dollars") self.acc2 = BankAccount("Misho", 200, "dollars")
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)
from bank_account import BankAccount, SpecialBankAccount from employee import Employee if __name__ == '__main__': ba = BankAccount('ING', 100) print(ba.bank_name) print(ba.balance) ba.deposit(100) ba.withdraw(150) print(ba.balance) try: ba.withdraw(100) except Exception as ex: print(ex) print(ba.balance) e = Employee('Alin', ba) print(e.name.upper()) print(e.bank_account.balance) print(e.salary) e.salary = 800 print(e.salary) e.receive_salary() print(e.bank_account.balance) sba = SpecialBankAccount('BCR', 100, 200)
def test_deposit(self): account = BankAccount() account.deposit(100) self.assertEqual(account.balance, 100)
def test_transfer_to_bank_account(self): rado = BankAccount("Rado", 1000, "BGN") ivo = BankAccount("Ivo", 0, "BGN") self.assertTrue(rado.transfer_to(ivo, 500))
def test_raise_type_error_bank_account(self): rado = BankAccount("Rado", 1000, "BGN") ivo = BankAccount("Ivo", 0, "BGN") self.assertRaises(not(rado.transfer_to(ivo, 500), TypeError))
class BankAccountTest(unittest.TestCase): def setUp(self): self.account = BankAccount() def test_newly_opened_account_has_zero_balance(self): self.account.open() self.assertEqual(self.account.get_balance(), 0) def test_can_deposit_money(self): self.account.open() self.account.deposit(100) self.assertEqual(self.account.get_balance(), 100) def test_can_deposit_money_sequentially(self): self.account.open() self.account.deposit(100) self.account.deposit(50) self.assertEqual(self.account.get_balance(), 150) def test_can_withdraw_money(self): self.account.open() self.account.deposit(100) self.account.withdraw(50) self.assertEqual(self.account.get_balance(), 50) def test_can_withdraw_money_sequentially(self): self.account.open() self.account.deposit(100) self.account.withdraw(20) self.account.withdraw(80) self.assertEqual(self.account.get_balance(), 0) def test_checking_balance_of_closed_account_throws_error(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.get_balance() def test_deposit_into_closed_account(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.deposit(50) def test_withdraw_from_closed_account(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.withdraw(50) def test_cannot_withdraw_more_than_deposited(self): self.account.open() self.account.deposit(25) with self.assertRaises(ValueError): self.account.withdraw(50) def test_cannot_withdraw_negative(self): self.account.open() self.account.deposit(100) with self.assertRaises(ValueError): self.account.withdraw(-50) def test_cannot_deposit_negative(self): self.account.open() with self.assertRaises(ValueError): self.account.deposit(-50) def test_can_handle_concurrent_transactions(self): self.account.open() self.account.deposit(1000) for _ in range(10): self.adjust_balance_concurrently() def adjust_balance_concurrently(self): def transact(): self.account.deposit(5) time.sleep(0.001) self.account.withdraw(5) # Greatly improve the chance of an operation being interrupted # by thread switch, thus testing synchronization effectively try: sys.setswitchinterval(1e-12) except AttributeError: # For Python 2 compatibility sys.setcheckinterval(1) threads = [] for _ in range(1000): t = threading.Thread(target=transact) threads.append(t) t.start() for thread in threads: thread.join() self.assertEqual(self.account.get_balance(), 1000)
def setUp(self): self.account_yangu = BankAccount()
class TestBankAccount(unittest.TestCase): def setUp(self): self.acc = BankAccount("Sasho", 100, "dollars") self.acc2 = BankAccount("Misho", 200, "dollars") def test_init(self): self.assertEqual(self.acc.get_name(), "Sasho") self.assertEqual(self.acc.get_balance(), 100) self.assertEqual(self.acc.get_currency(), "dollars") def test_deposits(self): self.acc.deposit(200) self.assertEqual(self.acc.get_balance(), 300) def test_withdraw(self): self.acc.withdraw(100) self.assertEqual(self.acc.get_balance(), 0) def test_transfer(self): self.acc.transfer_to(self.acc2, 100) self.assertEqual(self.acc2.get_balance(), 300) def test_history(self): arr2 = self.acc.history(self.acc2, 50) arr = ["Account was created", "Balance : "+str(self.acc.get_balance()), "Sasho transfered 50 dollars to Misho", "Balance : 100"] self.assertEqual(arr, arr2)
from bank_account import BankAccount main_account = BankAccount("DE123", "Marko") savings_account = BankAccount("DE4432", "Marko") main_account.booking(2000, "€", "Salary") main_account.booking(-15.50, "€", "Amazon Book") print( f"Owner: {main_account.owner_name}, IBAN: {main_account.iban}, Bookings: {main_account.bookings}" ) print(f"Balance: {main_account.get_balance()}")
class TestBankAccount(unittest.TestCase): def setUp(self): self.name = 'viki' self.name2 = 'stenly' self.balance = 1000000 self.currency = 'pound' self.account = BankAccount(self.name, self.balance, self.currency) self.account2 = BankAccount(self.name2, self.balance, self.currency) def test_account_init(self): self.assertEqual(self.account.name, self.name) self.assertEqual(self.account.balance, self.balance) self.assertEqual(self.account.currency, self.currency) def test_account_str(self): self.assertEqual( str(self.account), "Bank account for {} with balance of {}{}".format( self.name, self.balance, self.currency ) ) def test_account_int(self): self.assertEqual(int(self.account), self.balance) def test_get_balance(self): self.assertEqual(self.account.balance, self.account.get_balance()) def test_account_deposit(self): deposit_amount = 10000000 self.account.deposit(deposit_amount) self.assertEqual(self.account.balance, self.balance + deposit_amount) def test_account_withdraw_success(self): initial_balance = self.account.balance withdraw_amount = 1 result = self.account.withdraw(withdraw_amount) self.assertTrue(result) self.assertEqual(initial_balance - withdraw_amount, self.account.balance) def test_account_withdraw_fail(self): initial_balance = self.account.balance withdraw_amount = 10000000000 result = self.account.withdraw(withdraw_amount) self.assertFalse(result) self.assertEqual(initial_balance, self.account.balance) def test_account_transfer_to_success(self): old_balance1 = self.account.balance old_balance2 = self.account2.balance transfer_amount = 10000 result = self.account.transfer_to(self.account2, transfer_amount) self.assertTrue(result) self.assertEqual(old_balance1 - transfer_amount, self.account.balance) self.assertEqual(old_balance2 + transfer_amount, self.account2.balance) def test_account_transfer_to_fails_currency(self): self.account2.currency = 'BGN' transfer_amount = 10000 result = self.account.transfer_to(self.account2, transfer_amount) self.assertFalse(result) def test_account_transfer_to_fails_amount(self): self.account.balance = 1 transfer_amount = 10000 result = self.account.transfer_to(self.account2, transfer_amount) self.assertFalse(result)
@property def phone(self) -> str: return self._phone @phone.setter def phone(self, phone: str): self._phone = phone if __name__ == "__main__": p1 = Person("Jake Kelly", "*****@*****.**", "12341234") p2 = Person("Cooper Kelly", "*****@*****.**", "34563456") p3 = Person("Sally Kelly", "*****@*****.**", "56785678") acc1 = BankAccount() acc2 = BankAccount() acc1.transactions.append(100) acc1.transactions.append(100) acc1.transactions.append(-50) print(sum(acc1.transactions)) acc2.transactions.append(200) acc2.transactions.append(500) acc2.transactions.append(-50) # for p in [p1, p2, p3]: # print(f"{p.name}, {p.phone} {p.email}") # p2.email = "*****@*****.**"
from bank_account import BankAccount my_account = BankAccount('savings', 100.0) my_account.print_() my_account.deposit(50.0) my_account.print_() my_account.withdraw(15.0) my_account.print_()
def set_up(): """ method to set the bank_account""" bank_account = BankAccount("Boom", 1000) return bank_account