def send_transactions(poll_id: int, correct_id: int, transactions: List[Transaction], cursor, connection): """ this method takes transactions, puts them into the db and marks the poll as paid out. it also asks you to verify :param poll_id: :param transactions: :param cursor: :param connection: :return: """ total = 0 sys.stdout.write("\n\nThese are the transactions that are going to be written to the db:\n") for idx, transaction in enumerate(transactions): sys.stdout.write("%d) %s\n" % (idx+1, str(transaction))) total += transaction.amount sys.stdout.write("A total of %d coins are going to sent from the poll.\n\n" % total) result = input("Does this look correct to you? ") if result in no: sys.stderr.write("\nUser-initiated abort. Closing..\n\n") sys.exit() elif result in yes: pass else: sys.stderr.write("\nPlease respond with 'yes' or 'no'.\n\n") sys.exit() # write sql to insert these try: insert_query = Transaction.create_list_sql(transactions) print(insert_query) cursor.execute(insert_query) for transaction in transactions: update_user_bank_sql = "UPDATE banks SET balance=balance + %d WHERE id=%d" % (transaction.amount, transaction.to_id) print(update_user_bank_sql) cursor.execute(update_user_bank_sql) update_poll_bank_sql = "UPDATE banks SET balance=0 WHERE id=%d" % transactions[0].from_id print(update_poll_bank_sql) cursor.execute(update_poll_bank_sql) update_poll_sql = "UPDATE polls SET has_paid=true, correct_answer=%d WHERE id=%d" % (correct_id, poll_id) print(update_poll_sql) cursor.execute(update_poll_sql) except Exception as e: connection.rollback() sys.stderr.write("\nCaught an error: %s" % e) sys.stderr.write("\nAborting now, and rolling back all changes..\n\n") sys.exit() # update poll balance, update balance of respective users # update poll to be paid out, along with correct choice id connection.commit() print("bomb")
def test_create_single_list(self): transaction1 = Transaction(self.from_id, self.to_id, self.category, self.amount) all_transactions = [transaction1] result = Transaction.create_list_sql(all_transactions) expected_result = "INSERT INTO transactions (from_entity, to_entity, type, balance) values " \ "(31, 21, 'bet', 50)" self.assertEqual(expected_result, result)