def test_make_payment(self): account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(0, account.balance) balance = self.synchronize(statistics.get_per_day(self.connection)).balance transaction = self.synchronize(payments.start_transaction(self.connection, self.user_id, 10, merchant_id="test")) self.assertGreater(transaction.id, 0) transaction = self.synchronize(payments.get_one(self.connection, transaction.id)) self.assertEqual(constants.PAYMENT_STATUS_IN_PROCESS, transaction.status) self.assertEqual('test', transaction.merchant_id) record = self.synchronize(journal.get_all(self.connection, self.user_id))[0] self.assertEqual(constants.JOURNAL_STATUS_IN_PROCESS, record.status) active_payments = self.synchronize(payments.get_all(self.connection)) self.assertEqual(transaction.id, active_payments[-1].id) self.assertEqual(constants.PAYMENT_STATUS_IN_PROCESS, active_payments[-1].status) with self.assertRaises(payments.exceptions.Forbidden): self.synchronize(payments.finish_transaction(self.connection, transaction.id, imi_amount=11, status=0)) self.synchronize(payments.finish_transaction(self.connection, transaction.id, imi_amount=10, status=0)) balance_after = self.synchronize(statistics.get_per_day(self.connection)).balance self.assertEqual(balance.total + 10, balance_after.total) self.assertEqual(balance.debit, balance_after.debit) account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(10, account.balance) record = self.synchronize(journal.get_all(self.connection, self.user_id))[0] self.assertEqual(constants.BANK_USER_ID, record.other_user_id) self.assertEqual(journal.ObjectClass.payments.value, record.object_class) self.assertEqual(transaction.id, record.object_id) self.assertEqual(10, record.amount) self.assertEqual(constants.JOURNAL_STATUS_SUCCESS, record.status)
def test_get_all(self): records = self.synchronize( journal.get_all(self.connection, self.user_id, None, 1)) self.assertEqual(1, len(records)) last_id = records[-1].id records = self.synchronize( journal.get_all(self.connection, self.user_id, last_id)) self.assertEqual(5, len(records)) self.assertLess(records[-1].id, last_id)
def test_withdraw_money(self): stats1 = self.synchronize(statistics.get_per_day(self.connection)) self.add_money_for(self.user_id, 100) account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(100, account.balance) stats2 = self.synchronize(statistics.get_per_day(self.connection)) withdraw_record = self.synchronize(withdraw_queue.put( self.connection, self.user_id, 50, '{"wallet": "test wallet"}' )) records = self.synchronize(journal.get_all(self.connection, self.user_id)) # withdraw self.assertEqual(constants.BANK_USER_ID, records[0].other_user_id) self.assertEqual(journal.ObjectClass.withdraw.value, records[0].object_class) self.assertEqual(withdraw_record.id, records[0].object_id) self.assertEqual(-50, records[0].amount) self.assertEqual(constants.JOURNAL_STATUS_IN_PROCESS, records[0].status) stats3 = self.synchronize(statistics.get_per_day(self.connection)) self.assertEqual(stats2.withdraw_queue.size + 1, stats3.withdraw_queue.size) self.assertEqual(stats1.balance.total + 100, stats2.balance.total) self.assertEqual(stats1.balance.debit, stats2.balance.debit) self.assertEqual(stats2.balance.total, stats3.balance.total) self.assertEqual(stats2.balance.debit, stats3.balance.debit) self.assertEqual(stats2.balance.credit + 50, stats3.balance.credit) account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(50, account.balance) self.assertEqual(50, account.frozen) withdraw_record = self.synchronize(withdraw_queue.get_all(self.connection))[-1] self.synchronize(withdraw_queue.complete(self.connection, withdraw_record.id)) records = self.synchronize(journal.get_all(self.connection, self.user_id)) # withdraw self.assertEqual(withdraw_record.id, records[0].object_id) self.assertEqual(-50, records[0].amount) self.assertEqual(constants.BANK_USER_ID, records[0].other_user_id) self.assertEqual(journal.ObjectClass.withdraw.value, records[0].object_class) self.assertEqual(constants.JOURNAL_STATUS_SUCCESS, records[0].status) stats4 = self.synchronize(statistics.get_per_day(self.connection)) self.assertEqual(stats3.withdraw_queue.size - 1, stats4.withdraw_queue.size) self.assertEqual(stats3.balance.total - 50, stats4.balance.total) self.assertEqual(stats3.balance.credit - 50, stats4.balance.credit) self.assertEqual( constants.WITHDRAW_STATUS_SUCCESS, self.synchronize(withdraw_queue.get_one(self.connection, withdraw_record.id)).status ) withdraw_records = self.synchronize(withdraw_queue.get_all(self.connection)) self.assertNotIn(withdraw_record.id, {x.id for x in withdraw_records})
def test_no_payment_if_transaction_fail(self): account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(0, account.balance) transaction = self.synchronize(payments.start_transaction(self.connection, self.user_id, 10)) self.synchronize(payments.finish_transaction(self.connection, transaction.id, 10, 1)) account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(0, account.balance) record = self.synchronize(journal.get_all(self.connection, self.user_id))[0] self.assertEqual(constants.JOURNAL_STATUS_ERROR, record.status)
def test_payments_rollback(self): transaction = self.synchronize(payments.start_transaction(self.connection, self.user_id, 100)) self.synchronize(payments.finish_transaction(self.connection, transaction.id, 100, constants.PAYMENT_STATUS_SUCCESS)) journal_record = self.synchronize(journal.get_all(self.connection, self.user_id))[0] result = self.synchronize(payments.rollback(self.connection, transaction.id)) self.assertEqual(journal_record.id, result.journal.id) self.assertEqual( constants.PAYMENT_STATUS_ROLLBACK, self.synchronize(payments.get_one(self.connection, transaction.id)).status ) with self.assertRaises(payments.exceptions.InternalError): self.synchronize(payments.rollback(self.connection, transaction.id))
def test_withdraw_round_algorithm(self, ): self.add_money_for(self.user_id, 200) for amount in (54, 55): withdraw_record = self.synchronize(withdraw_queue.put( self.connection, self.user_id, amount, '{"wallet": "test wallet"}' )) records = self.synchronize(journal.get_all(self.connection, self.user_id)) # withdraw self.assertEqual(constants.BANK_USER_ID, records[0].other_user_id) self.assertEqual(journal.ObjectClass.withdraw.value, records[0].object_class) self.assertEqual(withdraw_record.id, records[0].object_id) self.assertEqual(-amount, records[0].amount) self.assertEqual(constants.JOURNAL_STATUS_IN_PROCESS, records[0].status)
def test_rollback_active(self): self.add_money_for(self.user_id, 100) request = self.synchronize(withdraw_queue.put(self.connection, self.user_id, 20, '{}')) self.synchronize(withdraw_queue.rollback(self.connection, request.id)) record = self.synchronize(journal.get_all(self.connection, self.user_id))[0] self.assertEqual(constants.JOURNAL_STATUS_ROLLBACK, record.status) self.assertEqual(journal.ObjectClass.withdraw.value, record.object_class) account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(100, account.balance) self.assertEqual(0, account.frozen) request = self.synchronize(withdraw_queue.get_one(self.connection, request.id)) self.assertEqual(constants.WITHDRAW_STATUS_ROLLBACK, request.status)
def test_clean(self): transaction = self.synchronize(payments.start_transaction(self.connection, self.user_id, 10)) self.synchronize(payments.finish_transaction(self.connection, transaction.id, imi_amount=10, status=0)) account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(10, account.balance) self.synchronize(payments.finish_transaction(self.connection, transaction.id, imi_amount=10, status=0)) transaction = self.synchronize(payments.get_one(self.connection, transaction.id)) self.synchronize(payments.clean(self.connection, -1)) with self.assertRaises(payments.exceptions.NotFound): self.synchronize(payments.get_one(self.connection, transaction.id)) records = self.synchronize(journal.get_all(self.connection, self.user_id)) obj_ids = {x.object_id for x in records if x.object_class == journal.ObjectClass.payments.value} self.assertNotIn(transaction.id, obj_ids)
def test_no_double_payment_for_one_transaction(self): account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(0, account.balance) transaction = self.synchronize(payments.start_transaction(self.connection, self.user_id, 10)) self.synchronize(payments.finish_transaction(self.connection, transaction.id, 10, 0)) account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(10, account.balance) self.synchronize(payments.finish_transaction(self.connection, transaction.id, 10, 0)) account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(10, account.balance) records = self.synchronize(journal.get_all(self.connection, self.user_id)) self.assertEqual(2, len(records))
def test_clean(self): self.add_money_for(self.user_id, 100) account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual(100, account.balance) record = self.synchronize(withdraw_queue.put( self.connection, self.user_id, 50, '{"wallet": "test wallet"}' )) self.synchronize(withdraw_queue.complete(self.connection, record.id)) self.synchronize(withdraw_queue.clean(self.connection, -1)) with self.assertRaises(withdraw_queue.exceptions.NotFound): self.synchronize(withdraw_queue.get_one(self.connection, record.id)) records = self.synchronize(journal.get_all(self.connection, self.user_id)) obj_ids = {x.object_id for x in records if x.object_class == journal.ObjectClass.withdraw.value} self.assertNotIn(record.id, obj_ids)
def test_rollback_one_payment(self): stats_before = self.synchronize(statistics.get_per_day( self.connection)) payment = self.add_money_for(self.user_id, 50) result = self.synchronize( actions.rollback_payment(self.connection, payment.id)) self.assertEqual(1, len(result)) self.assertEqual({'actual': 50, 'total': 50}, result[self.user_id]) payment = self.synchronize( payments.get_one(self.connection, payment.id)) self.assertEqual(constants.PAYMENT_STATUS_ROLLBACK, payment.status) log = self.synchronize(journal.get_all(self.connection, self.user_id)) self.assertEqual(constants.JOURNAL_STATUS_ROLLBACK, log[0].status) account = self.synchronize(accounts.get(self.connection, self.user_id)) self.assertEqual((0, 0), (account.balance, account.frozen)) stats_after = self.synchronize(statistics.get_per_day(self.connection)) self.assertEqual(stats_before.balance, stats_after.balance)