def transfer(): if not request.json: return make_error(400, "Missing required parameters") # Parse Input json = request.get_json() if 'senderAccount' not in json or type(json['senderAccount']) != str: current_app.logger.info(f"'senderAccount' not found in {json}") return make_error( 404, f"Missing 'senderAccount'. Expected 'str' got {type(json.get('senderAccount'))}" ) if 'receiverAccount' not in json or type(json['receiverAccount']) != str: current_app.logger.info(f"'receiverAccount' not found in {json}") return make_error( 404, f"Missing 'receiverAccount'. Expected 'str' got {type(json.get('receiverAccount'))}" ) if 'amount' not in json or type(json['amount']) != str: current_app.logger.info( f"'amount' not found in {json} or type of 'amount' != str: {type(json.get('amount'))}" ) return make_error( 404, f"Missing 'amount'. Expected 'str' got {type(json.get('amount'))}") s_an = str(json['senderAccount']) r_an = str(json['receiverAccount']) amount = float(json['amount']) # Retrieve Resources s = Account.query.filter_by(account_number=s_an).first() if not s: return make_error(404, f"Couldn't find account: {s_an}") r = Account.query.filter_by(account_number=r_an).first() if not r: return make_error(404, f"Couldn't find account: {r_an}") # Check sufficient balance if s.balance - amount < 0: return make_error(403, "Insufficient balance") # Generate transactions s_transaction = Transaction(account_id=s.id, amount=-amount) db.session.add(s_transaction) r_transaction = Transaction(account_id=r.id, amount=amount) db.session.add(r_transaction) # Perform operations s.balance -= amount db.session.add(s) r.balance += amount db.session.add(r) # Commit results db.session.commit() return {'result': True}
def test_get_customer_info_data(self, test_client, url, data): customer = Customer(name="Monty", surname="Python") db.session.add(customer) db.session.commit() c = Customer.query.first() account = Account(account_number=1234, balance=100, customer_id=c.id) db.session.add(account) db.session.commit() a = Account.query.first() t1 = Transaction(amount=-20, time=datetime(2020, 4, 20, 21), account_id=a.id) t2 = Transaction(amount=50, time=datetime(2020, 4, 19, 15), account_id=a.id) db.session.add_all([t1, t2]) db.session.commit() response = test_client.get(url, json=data) assert response.status_code == 200 json = response.get_json() assert json['customer'] == { 'name': "Monty", 'surname': "Python", 'accounts': [{ 'account_number': 1234, 'balance': 100.0, 'customer_id': c.id, 'transactions': [ { 'amount': -20.0, 'account_id': 1, 'time': datetime(2020, 4, 20, 21).isoformat(), }, { 'amount': 50, 'account_id': 1, 'time': datetime(2020, 4, 19, 15).isoformat(), }, ] }] }
def test_basic(self, test_client): new_trans = Transaction().import_data({'account_id': 1, 'amount': 50}) db.session.add(new_trans) db.session.commit() added_trans = Transaction.query.first() assert added_trans == new_trans transaction_Data = added_trans.export_data() assert transaction_Data == {'account_id': 1, 'amount': 50.0}
def test_transactions_on_account(self, test_client): a = Account(account_number='12345', balance=50, customer_id=1) db.session.add(a) db.session.commit() a = Account.query.first() t = Transaction(amount=25, account_id=a.id) db.session.add(t) db.session.commit() t = Transaction.query.first() a = Account.query.first() assert t.account_id == a.id assert a.transactions.first() == t
def test_repr(self, test_client): new_trans = Transaction().import_data({ 'account_id': 1, 'time': datetime(2020, 4, 19, 15), 'amount': 50 }) db.session.add(new_trans) db.session.commit() t = Transaction.query.first() assert repr( t ) == "<Transaction - t_id: 1 time: 2020-04-19 15:00:00 account_id: 1 amount: 50.0>"
def test_deposit(self): """deposit: check if no exception is raised when deposit transaction is okay. """ value = Decimal("20.00") account = self.create_account(balance=value) transaction = Transaction( value=Decimal(value), transaction_type=TransactionTypeEnum.withdraw, account=account) deposit(transaction) self.assertEqual(account.balance, Decimal("40.00"))
def test_transactions(): transactions = [{ 'account_id': 1, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': 20 }, { 'account_id': 2, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': -20 }] return [ Transaction().import_data(transaction) for transaction in transactions ]
def open_account(): if not request.json and not request.args: return make_error(400, "Missing required parameters") c_id = request.args.get('customerID') or request.json.get('customerID') if not c_id or type(c_id) != str: return make_error(404, f"Missing 'customerID'. Expected 'str' got {type(c_id)}") amount = request.args.get('initialCredit') or request.json.get('initialCredit') if not amount: amount = 0 # Find customer with 'customerID' c = Customer.query.get_or_404(c_id) # Create new account try: a = create_account(c) except ValueError as v_err: return make_error(403, str(v_err)) db.session.add(a) db.session.commit() a = Account.query.filter_by(account_number=a.account_number).first() current_app.logger.info(f"Account created: {a}") # If initialCredit is zero - return response with new account details if float(amount) == 0.0: return { 'result': True, 'account': marshal(a, account_fields) } # Generate Transaction t = Transaction(account_id=a.id, amount=amount) db.session.add(t) # Update Account Balance a.balance = amount db.session.add(a) # Commit results db.session.commit() return { 'result': True, 'account': marshal(a, account_fields) }
def test_validate_funds(self): """ validate_funds: check if no exception is raised when transaction value is not greater than the available balance. """ # given account = self.create_account( balance="20.00", daily_withdraw_limit=Decimal("20.00") ) transaction = Transaction( value=Decimal("10.00"), transaction_type=TransactionTypeEnum.withdraw, account=account ) # when response = validate_funds(transaction) # then self.assertIsNone(response)
def test_validate_funds_insufficient_funds(self): """ validate_funds:check if exception is raised when transaction value is greater than the available balance. """ # given account = self.create_account( balance="10.00", daily_withdraw_limit=Decimal("20.00") ) transaction = Transaction( value=Decimal("15.00"), transaction_type=TransactionTypeEnum.withdraw, account=account ) # when with self.assertRaises(ApiException) as error_context: validate_funds(transaction) # then exception = error_context.exception self.assertEqual(exception.errors, AccountFailures.insufficiente_funds)
def make_order(self, data): return Transaction(**(data or {}))
def test_transaction_import_data(self, test_client): with pytest.raises(ValueError): Transaction().import_data({'account_id': 1}) with pytest.raises(ValueError): Transaction().import_data({})
def lots_of_transactions(): transactions = [ { 'account_id': 1, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': 20 }, { 'account_id': 1, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': 21 }, { 'account_id': 1, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': 25 }, { 'account_id': 1, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': 2756 }, { 'account_id': 1, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': 2354 }, { 'account_id': 1, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': 220 }, { 'account_id': 2, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': -20 }, { 'account_id': 2, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': -99.89 }, { 'account_id': 2, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': 13.72 }, { 'account_id': 2, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': -12.54 }, { 'account_id': 2, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': -144 }, { 'account_id': 2, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': 735 }, { 'account_id': 2, 'time': datetime(2020, 4, 19, 15, 0, 0), 'amount': -35 }, ] return [ Transaction().import_data(transaction) for transaction in transactions ]