There was a split between a transaction request and an actual transaction. This was kind of annoying because transactions were one-way only, while transaction requests were two-way - which is what I believe most transactions will be using. Tx model has been removed and the responsibilities of it are covered by TxRequest. It simplifies everything surrounding transactions, since we have only one model to deal with instead of two. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
from django.test import TestCase
|
|
from trading.models import User, Commodity, TxRequest, BalanceError
|
|
|
|
|
|
class TestTransactions(TestCase):
|
|
def test_tx_requests(self):
|
|
# Create users
|
|
user1 = User.objects.create_user(username="test1", email="test1@test.test")
|
|
user2 = User.objects.create_user(username="test2", email="test2@test.test")
|
|
|
|
# Create test commodity for user1
|
|
com1 = Commodity(created_by=user1, in_circulation=1000, name="test1")
|
|
com1.save()
|
|
|
|
# Create test commodity for user2
|
|
com2 = Commodity(created_by=user2, in_circulation=1000, name="test2")
|
|
com2.save()
|
|
|
|
# Create TX request
|
|
req1 = TxRequest.open(user1, user2, com1, com2, 10, 10)
|
|
|
|
# Confirm balances
|
|
self.assertEqual(user1.balance_of(com1), 990)
|
|
self.assertEqual(user1.balance_of(com2), 0)
|
|
self.assertEqual(user2.balance_of(com1), 0)
|
|
self.assertEqual(user2.balance_of(com2), 1000)
|
|
|
|
# Accept TX request
|
|
req1.accept()
|
|
|
|
# Confirm balances
|
|
self.assertEqual(user1.balance_of(com1), 990)
|
|
self.assertEqual(user1.balance_of(com2), 10)
|
|
self.assertEqual(user2.balance_of(com1), 10)
|
|
self.assertEqual(user2.balance_of(com2), 990)
|
|
|
|
# Create TX request
|
|
req2 = TxRequest.open(user2, user1, com2, com1, 10, 10)
|
|
|
|
# Confirm balances
|
|
self.assertEqual(user1.balance_of(com1), 990)
|
|
self.assertEqual(user1.balance_of(com2), 10)
|
|
self.assertEqual(user2.balance_of(com1), 10)
|
|
self.assertEqual(user2.balance_of(com2), 980)
|
|
|
|
# Decline TX request
|
|
req2.decline()
|
|
|
|
# Confirm balances
|
|
self.assertEqual(user1.balance_of(com1), 990)
|
|
self.assertEqual(user1.balance_of(com2), 10)
|
|
self.assertEqual(user2.balance_of(com1), 10)
|
|
self.assertEqual(user2.balance_of(com2), 990)
|
|
|
|
# Ensure assertions on already created TXs
|
|
with self.assertRaises(AssertionError):
|
|
req1.accept()
|
|
with self.assertRaises(AssertionError):
|
|
req1.decline()
|
|
with self.assertRaises(AssertionError):
|
|
req2.accept()
|
|
with self.assertRaises(AssertionError):
|
|
req2.decline()
|
|
|
|
# Balance checking
|
|
# Ensure whoever opens the request has the balance available
|
|
with self.assertRaises(BalanceError):
|
|
TxRequest.open(user1, user2, com2, com1, 100, 100)
|
|
|
|
# Ensure whoever tries to accept the request has the balance available
|
|
req3 = TxRequest.open(user1, user2, com2, com1, 10, 100)
|
|
with self.assertRaises(BalanceError):
|
|
req3.accept()
|
|
|
|
# And make sure you can still decline it after failing to accept it.
|
|
req3.decline()
|
|
|