def test_floats_usd(self): value = Currency.us_dollars("99999999.99123459", "30.12342") account1 = self.user1_account account2 = self.user2_account transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : value}) transaction.save() transaction.apply() account1 = Account.objects.get_one({'_id' : account1._id}) account2 = Account.objects.get_one({'_id' : account2._id}) self.assertEqual(account1.balance.amount, -value.amount * value.rate) self.assertEqual(account2.balance.amount, value.amount * value.rate) new_value = Currency.russian_roubles("-10.99122459") value += new_value transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : new_value}) transaction.save() transaction.apply() account1 = Account.objects.get_one({'_id' : account1._id}) account2 = Account.objects.get_one({'_id' : account2._id}) self.assertEqual(account1.balance, -value) self.assertEqual(account2.balance, value)
def test_currency_exchange_sum_one(self): self.assertEqual( CurrencyExchangeManager.sum([Currency.us_dollars(2, 3)]), Currency.russian_roubles(60)) self.assertEqual( CurrencyExchangeManager.sum([Currency.us_dollars(2, 3)], 'USD'), Currency.us_dollars(2, 3))
def test_currency_exchange_reduce(self): two_dollars = Currency.us_dollars(2, 3) self.assertEqual(CurrencyExchangeManager.reduce(two_dollars), Currency.russian_roubles(60)) self.assertNotEqual(CurrencyExchangeManager.reduce(two_dollars, 'USD'), Currency.russian_roubles(60)) self.assertNotEqual( CurrencyExchangeManager.reduce(two_dollars, 'USD'), Currency.us_dollars(2, CurrencyExchangeManager.exchange_rates['USD'])) self.assertEqual(CurrencyExchangeManager.reduce(two_dollars, 'USD'), Currency.us_dollars(2, 3))
def test_cron_transaction_repair(self): value = Currency.us_dollars("99999999.99123459", "30.12342") account1 = self.user1_account account2 = self.user2_account transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : value}) transaction.save() amount_rub = transaction.amount.db_value() Transaction.objects.update({'_id': transaction._id}, {'$set': {'state': Transaction.STATE_PENDING}}) Account.objects.update({'_id': account1._id, 'pending_transactions': {'$ne': transaction._id}}, {'$inc': {'balance': -amount_rub}, '$push': {'pending_transactions': transaction._id}}) transaction2 = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : -value}) transaction2.save() amount_rub2 = transaction2.amount.db_value() Transaction.objects.update({'_id': transaction2._id}, {'$set': {'state': Transaction.STATE_PENDING}}) Account.objects.update({'_id': account1._id, 'pending_transactions': {'$ne': transaction2._id}}, {'$inc': {'balance': -amount_rub2}, '$push': {'pending_transactions': transaction2._id}}) bad_transactions = Transaction.objects.get({'state' : {'$nin' : [Transaction.STATE_INITIAL, Transaction.STATE_DONE]}}) self.assertTrue(len(bad_transactions) > 0) task = RepairTransactionsTask() task.execute() bad_transactions = Transaction.objects.get({'state' : {'$nin' : [Transaction.STATE_INITIAL, Transaction.STATE_DONE]}}) self.assertEquals(len(bad_transactions), 0)
def test_complete_after_committed3(self): value = Currency.us_dollars("99999999.99123459", "30.12342") account1 = self.user1_account account2 = self.user2_account transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : value}) transaction.save() # partial transaction.apply() - up to committed amount_rub = transaction.amount.db_value() Transaction.objects.update({'_id': transaction._id}, {'$set': {'state': Transaction.STATE_PENDING}}) Account.objects.update({'_id': account1._id, 'pending_transactions': {'$ne': transaction._id}}, {'$inc': {'balance': -amount_rub}, '$push': {'pending_transactions': transaction._id}}) Account.objects.update({'_id': account2._id, 'pending_transactions': {'$ne': transaction._id}}, {'$inc': {'balance': amount_rub}, '$push': {'pending_transactions': transaction._id}}) Transaction.objects.update({'_id': transaction._id}, {'$set': {'state': Transaction.STATE_COMMITTED}}) Account.objects.update({'_id': account1._id}, {'$pull': {'pending_transactions': transaction._id}}) Account.objects.update({'_id': account2._id}, {'$pull': {'pending_transactions': transaction._id}}) transaction = Transaction.objects.get_one({'_id' : transaction._id}) transaction.complete() account1 = Account.objects.get_one({'_id' : account1._id}) account2 = Account.objects.get_one({'_id' : account2._id}) trans = Transaction.objects.get_one({'_id' : transaction._id}) self.assertEqual(trans.state, Transaction.STATE_DONE) self.assertEqual(account1.balance.amount, -value.amount * value.rate) self.assertEqual(account2.balance.amount, value.amount * value.rate)
def test_currency_ranges(self): Currency.russian_roubles("9999999999.99123459") self.assertRaises(MaxCurrencyException, Currency.russian_roubles, "19999999999.99123459") self.assertEqual(Currency.russian_roubles("9999999999.99123459").db_value(), 999999999999123459) self.assertEqual(Currency.russian_roubles("9999999999.99123459123").db_value(), 999999999999123459) self.assertEqual(Currency.euro("999999999.991234591", 10).db_value(), 999999999991234591) self.assertEqual(Currency.us_dollars("9111111.991234591", 1058).db_value(), 963955648672619728) self.assertEqual(Currency.us_dollars("9111111.991234591123123123", 1058).db_value(), 963955648672619741) self.assertEqual(Currency.euro("0.000000000001", 120).db_value(), 0) self.assertEqual(Currency.euro("0.0000000001", 100).db_value(), 1)
def test_init(self): two_roubles = Currency({'code': 'RUB', 'rate': 1, 'amount': 2}) self.assertEqual(two_roubles.code, 'RUB') self.assertIsInstance(two_roubles.amount, Decimal) self.assertEqual(two_roubles.amount, Decimal(2)) self.assertIsInstance(two_roubles.rate, Decimal) self.assertEqual(two_roubles.rate, Decimal(1))
def test_roubles(self): one_rouble = Currency.russian_roubles(1) self.assertEqual(one_rouble.code, 'RUB') self.assertIsInstance(one_rouble.amount, Decimal) self.assertEqual(one_rouble.amount, Decimal(1)) self.assertIsInstance(one_rouble.rate, Decimal) self.assertEqual(one_rouble.rate, Decimal(1))
def test_usd(self): five_dollars = Currency.us_dollars(5, Decimal("1.5")) self.assertEqual(five_dollars.code, 'USD') self.assertIsInstance(five_dollars.amount, Decimal) self.assertEqual(five_dollars.amount, Decimal(5)) self.assertIsInstance(five_dollars.rate, Decimal) self.assertEqual(five_dollars.rate, Decimal("1.5"))
def test_money_transfer(self): value = Currency.russian_roubles("123.43") account1 = self.user1_account account2 = self.user2_account transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : value}) transaction.save() transaction.apply() account1 = Account.objects.get_one({'_id' : account1._id}) account2 = Account.objects.get_one({'_id' : account2._id}) self.assertEqual(account1.balance, -value) self.assertEqual(account2.balance, value)
def test_currency_exchange_sum_two(self): self.assertEqual( CurrencyExchangeManager.sum( [Currency.us_dollars(2, 3), Currency.us_dollars(3, 5)]), Currency.russian_roubles(150)) self.assertEqual( CurrencyExchangeManager.sum( [Currency.us_dollars(2, 3), Currency.us_dollars(3, 5)], 'USD'), Currency.us_dollars(5, CurrencyExchangeManager.exchange_rates['USD']))
def test_complete_after_pending1(self): value = Currency.us_dollars("99999999.99123459", "30.12342") account1 = self.user1_account account2 = self.user2_account transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : value}) transaction.save() # partial transaction.apply() - up to pending Transaction.objects.update({'_id': transaction._id}, {'$set': {'state': Transaction.STATE_PENDING}}) transaction = Transaction.objects.get_one({'_id' : transaction._id}) transaction.complete() account1 = Account.objects.get_one({'_id' : account1._id}) account2 = Account.objects.get_one({'_id' : account2._id}) trans = Transaction.objects.get_one({'_id' : transaction._id}) self.assertEqual(trans.state, Transaction.STATE_DONE) self.assertEqual(account1.balance.amount, -value.amount * value.rate) self.assertEqual(account2.balance.amount, value.amount * value.rate)
def test_neg(self): five_dollars = Currency.us_dollars(5, Decimal("1.5")) self.assertEqual(five_dollars.amount, Decimal(5)) self.assertEqual((-five_dollars).amount, Decimal(-5))
def test_currency_exchange_sum_one(self): self.assertEqual(CurrencyExchangeManager.sum([Currency.us_dollars(2, 3)]), Currency.russian_roubles(60)) self.assertEqual(CurrencyExchangeManager.sum([Currency.us_dollars(2, 3)], 'USD'), Currency.us_dollars(2, 3))
def test_calculate_diff_same_currency(self): two_dollars = Currency.us_dollars(2, 3) seven_dollars = Currency.us_dollars(7, 4) self.assertEqual(seven_dollars - two_dollars, Currency.russian_roubles(22))
def test_calculate_sum_same_currency(self): two_dollars = Currency.us_dollars(2, 3) seven_dollars = Currency.us_dollars(7, 4) self.assertEqual(two_dollars + seven_dollars, Currency.russian_roubles(34))
def test_calculate_diff_diff_currency(self): two_dollars = Currency.us_dollars(2, 3) five_roubles = Currency.russian_roubles(5) self.assertEqual(two_dollars - five_roubles, Currency.russian_roubles(1))
def test_currency_exchange_reduce(self): two_dollars = Currency.us_dollars(2, 3) self.assertEqual(CurrencyExchangeManager.reduce(two_dollars), Currency.russian_roubles(60)) self.assertNotEqual(CurrencyExchangeManager.reduce(two_dollars, 'USD'), Currency.russian_roubles(60)) self.assertNotEqual(CurrencyExchangeManager.reduce(two_dollars, 'USD'), Currency.us_dollars(2, CurrencyExchangeManager.exchange_rates['USD'])) self.assertEqual(CurrencyExchangeManager.reduce(two_dollars, 'USD'), Currency.us_dollars(2, 3))
def test_currency_exchange_sum_list(self): self.assertEqual( CurrencyExchangeManager.sum([ Currency.us_dollars(2, 3), Currency.us_dollars(3, 5), Currency.russian_roubles(60) ], 'USD'), Currency.us_dollars(7, CurrencyExchangeManager.exchange_rates['USD'])) self.assertEqual( CurrencyExchangeManager.sum([ Currency.us_dollars(2, 3), Currency.us_dollars(3, 5), Currency.russian_roubles(60) ]), Currency.russian_roubles(210)) self.assertEqual( CurrencyExchangeManager.sum([ Currency.us_dollars(2, 3), Currency.us_dollars(3, 5), Currency.russian_roubles(60), Currency.euro(4, 90) ], 'EUR'), Currency.euro(Decimal('8.66666666666666667'), CurrencyExchangeManager.exchange_rates['EUR']))
def test_currency_exchange_sum_list(self): self.assertEqual(CurrencyExchangeManager.sum([Currency.us_dollars(2, 3), Currency.us_dollars(3, 5), Currency.russian_roubles(60)], 'USD'), Currency.us_dollars(7, CurrencyExchangeManager.exchange_rates['USD'])) self.assertEqual(CurrencyExchangeManager.sum([Currency.us_dollars(2, 3), Currency.us_dollars(3, 5), Currency.russian_roubles(60)]), Currency.russian_roubles(210)) self.assertEqual(CurrencyExchangeManager.sum([Currency.us_dollars(2, 3), Currency.us_dollars(3, 5), Currency.russian_roubles(60), Currency.euro(4, 90)], 'EUR'), Currency.euro(Decimal('8.66666666666666667'), CurrencyExchangeManager.exchange_rates['EUR']))
def test_currency_exchange_sum_two(self): self.assertEqual(CurrencyExchangeManager.sum([Currency.us_dollars(2, 3), Currency.us_dollars(3, 5)]), Currency.russian_roubles(150)) self.assertEqual(CurrencyExchangeManager.sum([Currency.us_dollars(2, 3), Currency.us_dollars(3, 5)], 'USD'), Currency.us_dollars(5, CurrencyExchangeManager.exchange_rates['USD']))