def withdraw(account_number): data = BANK_INSTANCE.get_account(account_number) balance = None acc = None if data is None: flash("Bank account doesn't exists", category="error") else: acc = Account(**data) balance = acc.balance if request.method == 'POST' and acc is not None: withdraw_amount = request.form.get('withdraw_number', default=None) try: acc.withdraw_funds(withdraw_amount) BANK_INSTANCE.add_account(acc) except (ValueError, TypeError) as ex: flash("Invalid data type or value for the withdrawal amount", category="error") balance = acc.balance print(balance) return render_template('withdraw.html', account_number=account_number, balance=balance)
def test_account_object_returns_current_balance(self): account = Account('001', 50.0) self.assertEqual(account.account_number, "001") self.assertEqual(account.balance, 50.0) account = Account('001', '50.0') self.assertEqual(account.account_number, "001") self.assertEqual(account.balance, 50.0)
def test_print_accounts(self): with patch('builtins.print') as mocked_print: app_cli.current_bank.add_account(Account('001', 50.0)) app_cli.current_bank.add_account(Account('002', 150.0)) app_cli.print_accounts() # We can even assert that our mocked method was called with the right parameters self.assertIn(call("- {'account_number': '001', 'balance': 50.0}"), mocked_print.call_args_list) self.assertIn( call("- {'account_number': '002', 'balance': 150.0}"), mocked_print.call_args_list)
def test_withdraw_funds_prints_new_balance_before_done(self): with patch('builtins.input', return_value=30.0) as mocked_input: with patch('builtins.print') as mocked_print: app_cli.current_account = Account('001', 50.0) app_cli.ask_withdraw_funds() self.assertEqual(20, app_cli.current_account.balance) mocked_input.assert_called_once_with( "Enter the money amount to withdraw: ") mocked_print.assert_called_once_with( 'The new balance for account 001 is 20.0')
def test_withdraw_funds_catches_value_error_prints_error_message(self): with patch('builtins.input', return_value='not valid number') as mocked_input: with patch('builtins.print') as mocked_print: app_cli.current_account = Account('001', 50.0) app_cli.ask_withdraw_funds() self.assertEqual(50, app_cli.current_account.balance) mocked_input.assert_called_once_with( "Enter the money amount to withdraw: ") mocked_print.assert_called_once_with( 'The amount to withdrawal is not a valid number')
def ask_create_bank_account(): account_number = input("Enter your bank account number: ") try: balance = float(input("Enter your bank account balance: ")) except ValueError: print('The amount for initial balance is not a valid number') return None account = Account(account_number, balance) current_bank.add_account(account) global current_account current_account = account return account
def home(): balance = None if request.method == 'POST': acc_number = request.form.get('account_number', default=None) data = BANK_INSTANCE.get_account(acc_number) if data is None: flash("Bank account doesn't exists", category="error") else: acc = Account(**data) balance = acc.balance return render_template('index.html', balance=balance)
def deposit(account_number): data = BANK_INSTANCE.get_account(account_number) balance = None acc = None if data is None: flash("Bank account doesn't exists", category="error") else: acc = Account(**data) balance = acc.balance if request.method == 'POST' and acc is not None: deposit_amount = request.form.get('deposit_number', default=None) try: acc.deposit_funds(deposit_amount) BANK_INSTANCE.add_account(acc) except (TypeError, ValueError): flash("Invalid data type or value for the deposit amount", category="error") balance = acc.balance return render_template('deposit.html', account_number=account_number, balance=balance)
class AccountTest(unittest.TestCase): def setUp(self): self.account = Account('001', 50.0) def test_init_fails_wrong_account_number_data_type(self): self.assertRaises(TypeError, Account, 33, 50.0) def test_init_fails_wrong_account_balance_data_value_or_type(self): self.assertRaises(ValueError, Account, '001', 'pepito') self.assertRaises(TypeError, Account, '001', dict()) def test_account_object_returns_current_balance(self): account = Account('001', 50.0) self.assertEqual(account.account_number, "001") self.assertEqual(account.balance, 50.0) account = Account('001', '50.0') self.assertEqual(account.account_number, "001") self.assertEqual(account.balance, 50.0) def test_json(self): self.assertDictEqual({ 'account_number': '001', 'balance': 50.0 }, self.account.json()) def test_account_representation(self): self.assertEqual(self.account.__repr__(), "Account(id=001, balance=50.0)") def test_deposit_funds_with_right_amount(self): self.assertEqual(100.34, self.account.deposit_funds(50.34)) self.assertEqual(150.68, self.account.deposit_funds('50.34')) def test_deposit_funds_fails_with_wrong_amount_raises_exception(self): self.assertRaises(ValueError, self.account.deposit_funds, 'pepito') def test_withdraw_funds_with_right_amount(self): self.assertEqual(0.0, self.account.withdraw_funds(50.0)) self.account.balance = 50.0 self.assertEqual(30.0, self.account.withdraw_funds('20.0')) def test_withdraw_funds_fails_with_wrong_amount_raises_exception(self): self.assertRaises(ValueError, self.account.withdraw_funds, 'pepito') def test_withdraw_funds_raises_exception_with_maximum_allowed(self): self.assertRaises(ValueError, self.account.withdraw_funds, 601.0) def test_withdraw_funds_raises_exception_when_not_enough_funds(self): self.assertEqual(50.0, self.account.balance) self.assertRaises(ValueError, self.account.withdraw_funds, 600.0)
def test_get_account_balance_given_its_number(self): account_1 = Account('001', 50.00) self.bank.add_account(account_1) self.assertEqual(50.00, self.bank.get_account_balance('001'))
def test_retrieve_balance(): with patch('builtins.print') as mocked_print: app_cli.current_account = Account('001', 50.0) app_cli.retrieve_account_balance() mocked_print.assert_called_once_with(50.00)
def setUp(self): self.account = Account('001', 50.0)
def test_bank_representation(self): self.assertEqual("Bank(name=Randolph, accounts=0)", self.bank.__repr__()) self.bank.add_account(Account('001', 50.00)) self.assertEqual("Bank(name=Randolph, accounts=1)", self.bank.__repr__())
def i_create_account_with_balance_of_group1(context, account_number, balance): account = Account(account_number, float(balance)) BANK_INSTANCE.add_account(account)
def create_app(test_config=None): # Find the correct template folder when running from a different location tmpl_dir = os.path.join(os.path.dirname(__file__), 'templates') # Creating our flask app and configure it through its config dictionary app = Flask(__name__, template_folder=tmpl_dir, instance_relative_config=True) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass app.config.from_mapping( SECRET_KEY=os.urandom(16), # DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) if test_config is None: # load the instance config, if it exists, when not testing app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.from_mapping(test_config) BANK_INSTANCE.add_account(Account('001', 50.00)) @app.route('/hello', methods=('GET', )) def hello(): return 'Hello, World!' @app.route('/home', methods=('GET', 'POST')) def home(): balance = None if request.method == 'POST': acc_number = request.form.get('account_number', default=None) data = BANK_INSTANCE.get_account(acc_number) if data is None: flash("Bank account doesn't exists", category="error") else: acc = Account(**data) balance = acc.balance return render_template('index.html', balance=balance) @app.route('/deposit/<string:account_number>', methods=('GET', 'POST')) def deposit(account_number): data = BANK_INSTANCE.get_account(account_number) balance = None acc = None if data is None: flash("Bank account doesn't exists", category="error") else: acc = Account(**data) balance = acc.balance if request.method == 'POST' and acc is not None: deposit_amount = request.form.get('deposit_number', default=None) try: acc.deposit_funds(deposit_amount) BANK_INSTANCE.add_account(acc) except (TypeError, ValueError): flash("Invalid data type or value for the deposit amount", category="error") balance = acc.balance return render_template('deposit.html', account_number=account_number, balance=balance) @app.route('/withdraw/<string:account_number>', methods=('GET', 'POST')) def withdraw(account_number): data = BANK_INSTANCE.get_account(account_number) balance = None acc = None if data is None: flash("Bank account doesn't exists", category="error") else: acc = Account(**data) balance = acc.balance if request.method == 'POST' and acc is not None: withdraw_amount = request.form.get('withdraw_number', default=None) try: acc.withdraw_funds(withdraw_amount) BANK_INSTANCE.add_account(acc) except (ValueError, TypeError) as ex: flash("Invalid data type or value for the withdrawal amount", category="error") balance = acc.balance print(balance) return render_template('withdraw.html', account_number=account_number, balance=balance) return app
def test_get_account_given_its_number(self): account_data = {"account_number": "001", "balance": 50} account_1 = Account('001', 50.00) self.bank.add_account(account_1) self.assertEqual(account_data, self.bank.get_account('001'))
def test_add_account(self): account_1 = Account('001', 50.00) account_2 = Account('002', 100.00) self.bank.add_account(account_1) self.bank.add_account(account_2) self.assertEqual(2, len(self.bank.accounts))