Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_short_sell(self):
user = User.objects.get(pk=1)
wallet = Wallet.objects.get(user=user)
# Want to short-sell "Nigel Richards".
future = Future.objects.get(pk=1)
wallet.points = 11000
wallet.save()
response = process_order(11, 140, wallet, 'sell', future)
order = Order.objects.all().order_by('-pk')[0]
order_1 = Order.objects.get(pk=1) # Buy 10@155
self.assertEqual(order_1.filled, True)
self.assertEqual(order.filled, False)
self.assertEqual(order.quantity, 1)
self.assertEqual(order_1.filled_by, user)
### Future
future = Future.objects.get(pk=1)
self.assertEqual(future.last_buy, 155)
self.assertEqual(future.ask, 140)
self.assertEqual(future.bid, None)
self.assertEqual(future.volume, 10)
history = FutureHistory.objects.filter(future=future)
def test_order_3(self):
"""
Test second priority (chronologically).
"""
user = User.objects.get(pk=1)
wallet = Wallet.objects.get(user=user)
# Want to buy future "Nigel Richards"
# There's a buy order for 10@155, sell for 12@600, sell for 5@600
future = Future.objects.get(pk=1)
response = process_order(5, 620, wallet, 'buy', future)
# Get my order.
order = Order.objects.all().order_by('-pk')[0]
self.assertEqual(order.filled, True)
self.assertEqual(order.unit_price, 620) # Unchanged
# Get the matching orders.
order_1 = Order.objects.get(pk=10) # The 12 orders
order_2 = Order.objects.get(pk=12) # The 5 orders
self.assertEqual(order_1.filled, False) # Not fully filled
self.assertEqual(order_2.filled, False)
self.assertEqual(order_1.quantity, 7) # Updated quantity.
self.assertEqual(order_2.quantity, 5) # This order never changed.
self.assertEqual(order.filled_by, order_1.creator)
wallet.save()
future = Future.objects.get(pk=4)
# 8@600, 4@325
# I want to buy 7@615
response = process_order(7, 615, wallet, 'buy', future)
order = Order.objects.all().order_by('-pk')[0] # My buy.
order_1 = Order.objects.get(pk=9) # 4@325
order_2 = Order.objects.get(pk=3) # 8@600
self.assertEqual(order_1.filled, True)
self.assertEqual(order_2.filled, False)
self.assertEqual(order.filled, True)
self.assertEqual(order.filled_by, order_2.creator)
self.assertEqual(order_2.quantity, 5) # 3 got bought
self.assertEqual(order_1.filled_by, user)
self.assertEqual(order_2.filled_by, None)
future = Future.objects.get(pk=4)
self.assertEqual(future.last_buy, 600)
self.assertEqual(future.ask, 600) # Lowest sale order
self.assertEqual(future.bid, 275) # Highest buy order
self.assertEqual(future.volume, 7)
# History
history = FutureHistory.objects.filter(future=future)
self.assertEqual(history.count(), 2)
# Lowest then highest.
self.assertEqual(history[0].price, 325)
self.assertEqual(history[1].price, 600)
transactions = SuccessfulTransaction.objects.filter(future=future)
self.assertEqual(transactions.count(), 2)
self.assertTrue(self.compare_transactions(
transactions[0], SuccessfulTransaction(
buyer=user, seller=order_1.creator, future=future,
quantity=4, unit_price=325)))
def test_order_1(self):
user = User.objects.get(pk=8)
# Want to buy future "Jesse Day".
# There is a sell order of 8 for 600, a buy order of 4 for 100,
# a sell order of 4 for 325, a buy order of 4 for 275 in the fixture.
response = process_order(15, 650,
Wallet.objects.get(user=user),
'buy',
Future.objects.get(pk=4))
# Now get the new orders for people.
# First, get the order that was just created by this test user.
order = Order.objects.all().order_by('-pk')[0]
self.assertEqual(order.filled, False)
self.assertEqual(order.quantity, 3) # Should be 15 - (8 + 4)
self.assertEqual(order.unit_price, 650) # Unchanged
# Get the filled orders.
order_1 = Order.objects.get(pk=9) # The 325 S
order_2 = Order.objects.get(pk=3) # The 650 S
self.assertEqual(order_1.filled, True)
self.assertEqual(order_2.filled, True)
self.assertEqual(order_1.filled_by, user)
self.assertEqual(order_2.filled_by, user)
# Fetch new updated future.
future = Future.objects.get(pk=4)
self.assertEqual(future.last_buy, 600)
Future.objects.get(pk=4))
# Now get the new orders for people.
# First, get the order that was just created by this test user.
order = Order.objects.all().order_by('-pk')[0]
self.assertEqual(order.filled, False)
self.assertEqual(order.quantity, 3) # Should be 15 - (8 + 4)
self.assertEqual(order.unit_price, 650) # Unchanged
# Get the filled orders.
order_1 = Order.objects.get(pk=9) # The 325 S
order_2 = Order.objects.get(pk=3) # The 650 S
self.assertEqual(order_1.filled, True)
self.assertEqual(order_2.filled, True)
self.assertEqual(order_1.filled_by, user)
self.assertEqual(order_2.filled_by, user)
# Fetch new updated future.
future = Future.objects.get(pk=4)
self.assertEqual(future.last_buy, 600)
self.assertEqual(future.bid, 650)
self.assertEqual(future.ask, None)
self.assertEqual(future.volume, 12)
history = FutureHistory.objects.filter(future=future)
logger.debug(history)
self.assertEqual(history.count(), 2)
self.assertEqual(history[0].price, 325)
self.assertEqual(history[1].price, 600)
transactions = SuccessfulTransaction.objects.filter(future=future)
self.assertEqual(transactions.count(), 2)
self.assertTrue(self.compare_transactions(
transactions[0], SuccessfulTransaction(
buyer=user, seller=order_1.creator, future=future,
quantity=4, unit_price=325)))