def test_can_withdraw_money(self): account = BankAccount() account.open() account.deposit(100) account.withdraw(50) self.assertEqual(account.get_balance(), 50)
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_reopened_account_does_not_retain_balance(self): account = BankAccount() account.open() account.deposit(50) account.close() account.open() self.assertEqual(account.get_balance(), 0)
def test_deposit_into_closed_account(self): account = BankAccount() account.open() account.close() with self.assertRaisesWithMessage(ValueError): account.deposit(50)
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)
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)
def test_cannot_deposit_negative(self): account = BankAccount() account.open() with self.assertRaises(ValueError) as err: account.deposit(-50) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "amount must be greater than 0")
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 test_repr(self): account = BankAccount() self.assertIn('BankAccount(', repr(account)) self.assertIn('balance=0', repr(account)) self.assertNotIn('balance=200', repr(account)) account.deposit(200) self.assertIn('balance=200', repr(account)) self.assertNotIn('balance=0', repr(account))
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 AccountInterestBTestCase(unittest.TestCase): def setUp(self): self.account = BankAccount(32, 45) def test_get_default_interests(self): self.account.deposit(100) self.account.getInterests() self.assertEqual(self.account.getInterests(), 100 + 0.02 * 100)
def test_deposit_into_closed_account(self): account = BankAccount() account.open() account.close() with self.assertRaises(ValueError) as err: account.deposit(50) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "account not open")
def test_cannot_withdraw_more_than_deposited(self): account = BankAccount() account.open() account.deposit(25) with self.assertRaises(ValueError) as err: account.withdraw(50) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "amount must be less than balance")
class TestBankAccount(unittest.TestCase): def setUp(self): self.account = BankAccount("Pesho", 0, "BGN") def test_str(self): self.assertEqual(str(self.account), "Bank account for Pesho with balance of 0BGN") def test_int(self): self.assertEqual(int(self.account), 0) def test_depsit(self): self.account.deposit(50) self.assertEqual(int(self.account), 50) def test_deposit_with_negative(self): self.assertRaises(ValueError, self.account.deposit, -50) def test_balance(self): self.account.deposit(150) self.assertEqual(self.account.balance(), 150) def test_withdraw(self): self.account.deposit(1000) self.account.withdraw(600) self.assertEqual(self.account.balance(), 400) def test_withdraw_with_too_much(self): self.account.deposit(200) self.account.withdraw(500) self.assertEqual(self.account.balance(), 200) def test_transfer_to(self): account_2 = BankAccount("Gosho", 1000, "BGN") account_2.transfer_to(self.account, 600) self.assertEqual(account_2.balance(), 400) self.assertEqual(self.account.balance(), 600) def test_history(self): self.account.deposit(1000) self.account.balance() str(self.account) int(self.account) self.account.history() self.account.withdraw(500) self.account.balance() self.account.withdraw(1000) self.account.balance() history = self.account.history() expected = [ 'Account was created', 'Deposited 1000BGN', 'Balance check -> 1000BGN', '__int__ check -> 1000BGN', '500BGN was withdrawed', 'Balance check -> 500BGN', 'Withdraw for 1000BGN failed.', 'Balance check -> 500BGN' ] self.assertEqual(history, expected)
class AccountBalanceTestCase(unittest.TestCase): def setUp(self): self.account_yangu = BankAccount() def test_balance(self): self.assertEqual(self.account_yangu.balance, 3000, msg='Balance invalid') def test_deposit(self): self.account_yangu.deposit(4000) self.assertEqual(self.account_yangu.balance, 7000, msg='some error occured')
class TestBankAccount(unittest.TestCase): def setUp(self): self.account = BankAccount("Rado", 0, "$") def test_init(self): self.assertEqual( str(self.account), 'Bank account for Rado with balance of 0$') def test_deposit(self): self.account.deposit(1000) self.assertEqual(self.account.balance(), 1000) def test_int(self): self.account.deposit(1000) self.assertEqual(int(self.account), self.account.balance()) def test_withdrow(self): self.account.deposit(1000) self.account.withdrow(1000) self.assertEqual(int(self.account), 0) def test_history(self): self.account.deposit(1000) self.assertEqual(self.account.history(), [ 'Account was created', 'Deposited 1000$']) def test_transfer(self): account2 = BankAccount("Bat Georgi", 100, "$") account2.transfer_to(self.account, 100) self.assertEqual(account2.balance(), 0) self.assertEqual(self.account.balance(), 100)
class BankAccountTestCase(unittest.TestCase): def setUp(self): self.my_account = BankAccount() def test_deposit(self): self.my_account.deposit(3000) self.assertEqual(self.my_account.balance, 5000, msg="Inacurate method") def test_withdraw(self): self.my_account.withdraw(1000) self.assertEqual(self.my_account.balance, 1000, msg="Invalid method") def test_invalid_transaction(self): self.assertEqual(self.my_account.withdraw(2500), "invalid transaction!", msg='invalid transaction not handled')
class User: def __init__(self, username, email_address): self.name = username self.email = email_address self.account = BankAccount(0.02, 0) def deposit(self, amount): self.account.deposit(amount) return self def withdraw(self, amount): self.account.withdraw(amount) return self def balance(self): return self.account.balance()
def test_bankaccount__history(self): account = BankAccount("Rado", 0, "$") account.deposit(1000) account.balance() int(account) check_history = ['Account was created', 'Deposited 1000$', 'Balance check -> 1000$', '__int__ check -> 1000$'] rado = BankAccount("Rado", 1000, "BGN") ivo = BankAccount("Ivo", 0, "BGN") rado.transfer_to(ivo, 500) rado.balance() ivo.balance() rado_history = ['Account was created', 'Transfer to Ivo for 500BGN', 'Balance check -> 500BGN'] self.assertListEqual(account.history(), check_history) self.assertListEqual(rado.history(), rado_history)
class AccountBalanceTestCase(unittest.TestCase): def setUp(self): self.bank_account = BankAccount() def test_balance(self): result = self.bank_account.balance self.assertEqual(result, 3000, msg="Account Balance Invalid") def test_deposit(self): result = self.bank_account.deposit(4000) self.assertEqual(result, 7000, msg="Deposit method inaccurate") def test_withdraw(self): result = self.bank_account.withdraw(2000) self.assertEqual(result, 1000, msg='Withdraw method broken.') def test_invalid_transaction(self): result = self.bank_account.withdraw(4000) self.assertEqual( result, "Invalid Transaction", msg='Should not be able to withdraw more than the Balance.') self.assertEqual( self.bank_account.balance, 3000, msg= 'If withdraw is greate than the balance, the balance should remain unaffected.' ) def test_subclass(self): self.assertTrue(issubclass(MinimumBalanceAccount, BankAccount), msg="Not a true subclass.")
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)
class TestBankAccount(unittest.TestCase): def setUp(self): self.my_bank_account = BankAccount("Rado", 1000, "$") def test_create_new_bank_account_class(self): self.assertTrue(isinstance(self.my_bank_account, BankAccount)) # testvame dali tazi funkciq vrushta greshka def test_deposit_amount(self): with self.assertRaises(ValueError): self.my_bank_account.deposit(-1) self.my_bank_account.deposit(1000) self.assertEqual(self.my_bank_account.initial_balance, 2000) def test_check_balance(self): current_balance = self.my_bank_account.balance() self.assertEqual(self.my_bank_account.initial_balance, current_balance) def test_withdraw_negative_amount(self): with self.assertRaises(ValueError): self.my_bank_account.withdraw(-50) self.my_bank_account.withdraw(1000) self.assertEqual(self.my_bank_account.initial_balance, 0) def test_transfer_to(self): other_acc = BankAccount("Pesho", 1010, "$") # self.assertEqual(self.my_bank_account.currency, other_acc.currency) self.my_bank_account.transfer_to(other_acc, 500) self.assertEqual(self.my_bank_account.initial_balance, 1000 - 500) self.assertEqual(other_acc.initial_balance, 1010 + 500) result = self.my_bank_account.transfer_to(other_acc, 20) self.assertTrue(result) def test_transfer_negative_amount(self): other_acc = BankAccount("Pesho", 1010, "$") some_acc = BankAccount("Anton", 1000, "lqlq") with self.assertRaises(ValueError): self.my_bank_account.transfer_to(other_acc, -50) self.my_bank_account.transfer_to(other_acc, 1010) self.my_bank_account.transfer_to(some_acc, 40)
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)
class AccountBalanaceTestCase(unittest.TestCase): def setUp(self): self.account_yangu = BankAccount() def test_balance(self): self.assertEqual(self.account_yangu.balance, 3000, msg='Account Balance Invalid.') def test_deposit(self): self.account_yangu.deposit(4000) self.assertEqual(self.account_yangu.balance, 7000, msg='Deposit Method Inaccurate.') def test_withdraw(self): self.account_yangu.withdraw(500) self.assertEqual(self.account_yangu.balance, 2500, msg='Withdraw method Inaccurate.') def test_invalid_transaction(self): self.assertEqual(self.account_yangu.withdraw(6000), "Invalid Transaction", msg='Invalid Transaction.') def test_sub_class(self): self.assertTrue(issubclass(MinimumBalanceAccount, BankAccount), msg='Not True subclass of Balance Account.')
class AccountBalanceTestCase(unittest.TestCase): def setUp(self): self.account_yangu = BankAccount() def test_balance(self): self.assertEqual(self.account_yangu.balance, 3000, msg='Account balance is invalid') def test_withdraw(self): self.account_yangu.withdraw(500) self.assertEqual(self.account_yangu.balance, 2500, msg='Withdraw method is invalid') def test_deposit(self): self.account_yangu.deposit(5000) self.assertEqual(self.account_yangu.balance, 8000, msg='Deposit method is invalid')
class User: import bank_account def __init__(self, name, email): self.name = name self.email = email self.account = BankAccount(int_rate=0.02, balance=0) # adding the deposit method def make_deposit(self, amount): # takes an argument that is the amount of the deposit self.account.deposit(amount) # the specific user's account increases by the amount of the value received return self def make_withdrawal(self, amount): ## this method decrease the user's balance by the amount specified self.account.withdraw(amount) return self def display_user_balance(self): #- method print the user's name and account balance to the terminal print("User: %s --- Balance: %.2f" % (self.name, self.account.balance)) return self def transfer_money(self, other_user, amount): # - method decrease the user's balance by the amount and add that amount to other other_user's balance self.account.withdraw(amount) other_user.account.deposit(amount) return self
class BankLibrary(object): def set_up(self): self.account = BankAccount() self.printed_text = '' def deposit(self, amount, on, date): self.account.deposit(int(amount), date) def withdraw(self, amount, on, date): self.account.withdraw(int(amount), date) def print_statements(self): operations = self.account.operations() self.printed_text = format_operations(operations) def print_deposits(self): deposits = self.account.deposits() self.printed_text = format_operations(deposits) def get_printed_statements(self): return self.printed_text
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)
class TestBankAccount(unittest.TestCase): def setUp(self): self.bank_account = BankAccount() self.transaction = Mock() def test_balance_at_init_is_zero(self): assert self.bank_account.balance == 0 def test_deposit_adds_money_into_the_account(self): self.bank_account.deposit(self.transaction, 500) assert self.bank_account.balance == 500 self.transaction.credit.assert_called_with(500, 500) def test_withdraw_takes_money_from_the_account(self): self.bank_account.deposit(self.transaction, 1000) self.bank_account.withdraw(self.transaction, 500) assert self.bank_account.balance == 500 self.transaction.debit.assert_called_with(500, 500) def test_trying_to_withdraw_with_no_credit_raises_error(self): with self.assertRaisesRegexp(Exception, 'Not enough credit!'): self.bank_account.withdraw(self.transaction, 1000)
class TestBankAccount(unittest.TestCase): def setUp(self): self.initial_balance = 1000 self.rado = BankAccount("Rado", self.initial_balance, "BGN") def test_bank_account_init(self): self.assertEqual(self.rado.name, "Rado") self.assertEqual(self.rado.balance(), 1000) self.assertEqual(self.rado.currency, 'BGN') self.assertEqual( self.rado.history(), ['Account was created', 'Balance check -> 1000$']) self.assertGreater(self.rado.balance(), -1) def test_if_created_with_negative_balance(self): with self.assertRaises(ValueError): BankAccount("rado", -10, "USD") def test_deposit_working(self): old_deposit = self.rado.balance() sum_to_add = 100 self.rado.deposit(sum_to_add) new_deposit = self.rado.balance() self.assertEqual(new_deposit, old_deposit + sum_to_add) def test_witdrawal_possible_withnegative_ammount(self): self.assertFalse(self.rado.withdraw(200000)) def test_trasfer_with_diff_currencies(self): self.raiko = BankAccount("Raiko", self.initial_balance, "USD") self.assertFalse(self.raiko.transfer_to(self.rado, 333)) def test_history(self): self.rado.deposit(1000) self.rado.balance() self.rado.withdraw(500) self.assertEqual(self.rado.history(), ['Account was created', 'Deposited 1000$', 'Balance check -> 2000$', '500$ was withdrawed'])
def main(): bank_account = BankAccount("Kirkor Kirkorov", 100, "dollars") bank_account.deposit(500) print(bank_account.balance()) bank_account.withdraw(200) print(bank_account.balance()) print(bank_account) print(int(bank_account)) account_1 = BankAccount("Panajot", 500, "euros") account_2 = BankAccount("Panajot", 500, "dollars") print(bank_account == account_1) print(bank_account == account_2) print(bank_account.transfer_to(account_1, 100)) print(bank_account.transfer_to(account_2, 100)) print(bank_account) print(account_1) print(account_2) print(bank_account.history())
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 AccountInterestATestCase(unittest.TestCase): def setUp(self): self.account = BankAccount(1, 2, 50) def smallValue(self): self.account.deposit(500) def mediumValue(self): self.account.deposit(2500) def largeValue(self): self.account.deposit(10000)
class Test(unittest.TestCase): def setUp(self): self.rado = BankAccount("Rado", 1000, "$") def test_bank_account_deposit(self): self.assertEqual(self.rado.deposit(1000), 2000) def test_balance(self): self.assertEqual(self.rado.balance(), 1000) def test_withdraw(self): self.assertEqual(self.rado.withdraw(300), True) def test_str(self): self.assertEqual(str(self.rado), "Bank account for Rado with balance of 1000$") def test_int(self): self.assertEqual(int(self.rado), 1000) def test_transfer_to_account(self): self.ivo = BankAccount("Ivo", 3000, "$") self.assertEqual(self.rado.transfer_to(self.ivo, 20), True)
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 test_cannot_deposit_negative(self): account = BankAccount() account.open() with self.assertRaisesWithMessage(ValueError): account.deposit(-50)
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)
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_()
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)
class BankAccountTest(unittest.TestCase): def setUp(self): self.account = BankAccount() # @unittest.skip def test_newly_opened_account_has_zero_balance(self): self.account.open() self.assertEqual(self.account.get_balance(), 0) # @unittest.skip def test_can_deposit_money(self): self.account.open() self.account.deposit(100) self.assertEqual(self.account.get_balance(), 100) # @unittest.skip 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) # @unittest.skip def test_can_withdraw_money(self): self.account.open() self.account.deposit(100) self.account.withdraw(50) self.assertEqual(self.account.get_balance(), 50) # @unittest.skip 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) # @unittest.skip def test_checking_balance_of_closed_account_throws_error(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.get_balance() # @unittest.skip def test_deposit_into_closed_account(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.deposit(50) # @unittest.skip def test_withdraw_from_closed_account(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.withdraw(50) # @unittest.skip def test_cannot_withdraw_more_than_deposited(self): self.account.open() self.account.deposit(25) with self.assertRaises(ValueError): self.account.withdraw(50) # @unittest.skip def test_cannot_withdraw_negative(self): self.account.open() self.account.deposit(100) with self.assertRaises(ValueError): self.account.withdraw(-50) # @unittest.skip def test_cannot_deposit_negative(self): self.account.open() with self.assertRaises(ValueError): self.account.deposit(-50) # @unittest.skip def test_can_handle_concurrent_transactions(self): self.account.open() self.account.deposit(1000) for _ in range(10): self.adjust_balance_concurrently() # @unittest.skip 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)
class TestBankAccount(unittest.TestCase): def setUp(self): self.account_1 = BankAccount("Rado", 1000, "$") self.account_2 = BankAccount("Mimi", 100, 'BGN') self.account_3 = BankAccount("Gosho", 1000, 'BGN') def test_BankAccount_str_(self): # verify strings verify = 'Bank account for Rado with balance of 0$' self.assertTrue(self.account_1.__str__(), verify) def test_bankaccount_deposit(self): # check if deposit of 1000$ is in __balance self.assertEqual(self.account_1.deposit(1000), self.account_1._BankAccount__balance) def test_bankaccount_balance(self): # check if method balance == property __balance self.assertEqual(self.account_1.balance(), self.account_1._BankAccount__balance) def test_bankaccount__int__(self): # int() == int() True self.assertTrue(int(self.account_1), int()) # int(1000) == 1000 True self.assertEqual(int(self.account_1), self.account_1.balance()) def test_bankaccount__history(self): account = BankAccount("Rado", 0, "$") account.deposit(1000) account.balance() int(account) check_history = ['Account was created', 'Deposited 1000$', 'Balance check -> 1000$', '__int__ check -> 1000$'] rado = BankAccount("Rado", 1000, "BGN") ivo = BankAccount("Ivo", 0, "BGN") rado.transfer_to(ivo, 500) rado.balance() ivo.balance() rado_history = ['Account was created', 'Transfer to Ivo for 500BGN', 'Balance check -> 500BGN'] self.assertListEqual(account.history(), check_history) self.assertListEqual(rado.history(), rado_history) def test_bankaccount_withdraw(self): # check if wihdraw > balance return False self.assertFalse(self.account_1.withdraw(1000), False) # check if withdraw(500) is smaller than balance self.assertLess(self.account_1.withdraw(200), self.account_1.balance()) def test_bankaccount__currency(self): self.assertNotEqual(self.account_1._currency, self.account_2._currency) self.assertEqual(self.account_2._currency, self.account_3._currency) def test_bankaccount_transfer_to(self): self.assertEqual(self.account_2._currency, self.account_3._currency) self.assertLess(self.account_2.withdraw(50), self.account_2._BankAccount__balance) self.assertLessEqual(self.account_3.balance(), self.account_3.deposit(50)) self.assertIsNot(self.account_2._currency, self.account_1._currency)
class BankAccountTest(unittest.TestCase): def setUp(self): self.account = BankAccount("Joro", 10, "BGN") def test_create_bank_account_class(self): self.assertTrue(isinstance(self.account, BankAccount)) def test_is_name_valid_type(self): with self.assertRaises(TypeError): BankAccount(1000, 10, "BGN") def test_is_balance_valid_type(self): with self.assertRaises(TypeError): BankAccount("Joro", "gosho", "BGN") def test_is_currency_valid_type(self): with self.assertRaises(TypeError): BankAccount("Joro", 100, 1000) def test_is_balance_positive_number(self): with self.assertRaises(ValueError): BankAccount("Joro", -10, "BGN") def test_is_balance_private(self): with self.assertRaises(AttributeError): self.account.balance += 10 def test_is_amount_being_deposited(self): old_balance = self.account.get_balance() self.account.deposit(10) new_balance = self.account.get_balance() self.assertEqual(10, new_balance - old_balance) def test_is_withdraw_possible_with_negative_number(self): self.assertFalse(self.account.withdraw(-10)) def test_is_withdraw_possible_if_balance_not_enough(self): self.assertFalse(self.account.withdraw(20)) def test_is_amount_multiple_by_10(self): with self.assertRaises(ValueError): self.account.withdraw(5) def test_account_str_print(self): self.assertEqual(str(self.account), "Bank account for Joro with balance of 10BGN") def test_account_int_return(self): self.assertEqual(int(self.account), 10) def test_is_trasfer_possible_if_accounts_have_different_currencies(self): kiro = BankAccount("Kiro", 50, "$") self.assertFalse(self.account.transfer_to(kiro, 100)) def test_history_of_account_that_is_just_created(self): self.assertEqual(["Account was created"], self.account.history()) def test_history_after_some_actions(self): self.account.deposit(20) self.account.get_balance() int(self.account) self.account.withdraw(20) self.account.get_balance() self.account.withdraw(50) self.assertEqual(['Account was created', 'Deposited 20BGN', 'Balance check -> 30BGN', '__int__ check -> 30BGN', '20BGN was withdrawed', 'Balance check -> 10BGN', 'Withdraw for 50BGN failed.'], self.account.history())
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)
__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")
class BankAccountTest(unittest.TestCase): def setUp(self): self.acc = BankAccount("Rado", 0, "$") def test_creat_new_bank_account(self): self.assertTrue(isinstance(self.acc, BankAccount)) def test_validation_bank_account(self): with self.assertRaises(ValueError): account = BankAccount("", -10, "$") def test_initial_zero_balance_bank_account(self): self.assertEqual(self.acc.balance(), 0) def test_str_dunder_bank_account(self): self.assertEqual(str(self.acc), "Bank account for Rado with balance of 0$") def test_repr_dunder_bank_account(self): self.assertEqual(repr(self.acc), "Rado : 0$") def test_int_dunder_bank_account(self): self.assertEqual(int(self.acc), 0) def test_deposit_bank_account(self): self.acc.deposit(1000) self.assertEqual(int(self.acc), 1000) def test_balance_bank_account(self): self.acc.deposit(1000) self.assertEqual(self.acc.balance(), 1000) def test_deposit_raise_error(self): with self.assertRaises(ValueError): self.acc.deposit(-1111) def test_withdraw_bank_account(self): self.acc.deposit(1000) self.assertTrue(self.acc.withdraw(500)) def test_withdraw_answer_false_bank_account(self): self.acc.deposit(1000) self.assertFalse(self.acc.withdraw(1509)) def test_history_bank_account(self): self.acc.deposit(1000) self.assertEqual(self.acc.history(), ['Account was created', 'Deposited 1000$']) 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))