Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
logger.debug(history)
self.assertEqual(history.count(), 1)
self.assertEqual(history[0].price, 155)
transactions = SuccessfulTransaction.objects.filter(future=future)
self.assertEqual(transactions.count(), 1)
self.assertTrue(self.compare_transactions(
transactions[0], SuccessfulTransaction(
buyer=order_1.creator, seller=user, future=future,
quantity=10, unit_price=155)))
# Wallets.
wallet = Wallet.objects.get(user=user)
wallet_1 = Wallet.objects.get(user=order_1.creator)
key = '%s' % future.pk
self.assertEqual(json.loads(wallet_1.shares_owned)[key], 10)
self.assertEqual(wallet.points, 11000 + (10 * 155))
self.assertEqual(wallet_1.points, 10000 - (10 * 155))
self.assertEqual(wallet.frozen, 11000)
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)))
self.assertTrue(self.compare_transactions(
transactions[1], SuccessfulTransaction(
buyer=user, seller=order_2.creator, future=future,
quantity=3, unit_price=600)))
# Wallets
wallet = Wallet.objects.get(user=user)
wallet_1 = Wallet.objects.get(user=order_1.creator)
wallet_2 = Wallet.objects.get(user=order_2.creator)
key = '%s' % future.pk
self.assertEqual(json.loads(wallet.shares_owned)[key], -2)
self.assertEqual(json.loads(wallet_1.shares_owned)[key], -4)
self.assertEqual(json.loads(wallet_2.shares_owned)[key], -3)
self.assertEqual(json.loads(wallet.shares_owned)['1'], 7) # Unchanged.
self.assertEqual(wallet.points, 20000 - (4 * 325) - (3 * 600))
self.assertEqual(order_1.filled, False)
self.assertEqual(order.filled, True)
self.assertEqual(order_1.quantity, 6)
self.assertEqual(order.filled_by, order_1.creator)
future = Future.objects.get(pk=1)
self.assertEqual(future.last_buy, 155)
self.assertEqual(future.ask, 600)
self.assertEqual(future.bid, 155)
self.assertEqual(future.volume, 4)
history = FutureHistory.objects.filter(future=future)
self.assertEqual(history.count(), 1)
self.assertEqual(history[0].price, 155)
transactions = SuccessfulTransaction.objects.filter(future=future)
self.assertEqual(transactions.count(), 1)
self.assertTrue(self.compare_transactions(
transactions[0], SuccessfulTransaction(
buyer=order_1.creator, seller=user, future=future,
quantity=4, unit_price=155)))
# Wallets
wallet = Wallet.objects.get(user=user)
wallet_1 = Wallet.objects.get(user=order_1.creator)
key = '%s' % future.pk
self.assertEqual(json.loads(wallet.shares_owned)[key], 3)
self.assertEqual(json.loads(wallet_1.shares_owned)[key], 4)
self.assertEqual(json.loads(wallet.shares_owned)['4'], 10) # Unchanged.
self.assertEqual(wallet_1.points, 10000 - (4 * 155))
self.assertEqual(wallet.points, 10000 + (4 * 155))
self.assertEqual(wallet.frozen, 0)
self.assertEqual(wallet_1.frozen, 0)
# 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)))
self.assertTrue(self.compare_transactions(
transactions[1], SuccessfulTransaction(
buyer=user, seller=order_2.creator, future=future,
quantity=8, unit_price=600)))
# Wallets.
wallet = Wallet.objects.get(user=user)
wallet_1 = Wallet.objects.get(user=order_1.creator)
wallet_2 = Wallet.objects.get(user=order_2.creator)
key = '%s' % future.pk
self.assertEqual(json.loads(wallet.shares_owned)[key], 12)
self.assertEqual(wallet.points, 10000 - (4 * 325 + 8 * 600))
self.assertEqual(wallet_1.points, 10000 + (4 * 325))
self.assertEqual(wallet_2.points, 10000 + (8 * 600))
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)))
self.assertTrue(self.compare_transactions(
transactions[1], SuccessfulTransaction(
buyer=user, seller=order_2.creator, future=future,
quantity=3, unit_price=600)))
# Wallets
wallet = Wallet.objects.get(user=user)
wallet_1 = Wallet.objects.get(user=order_1.creator)
wallet_2 = Wallet.objects.get(user=order_2.creator)
key = '%s' % future.pk
self.assertEqual(json.loads(wallet.shares_owned)[key], -2)
self.assertEqual(json.loads(wallet_1.shares_owned)[key], -4)
self.assertEqual(json.loads(wallet_2.shares_owned)[key], -3)
self.assertEqual(json.loads(wallet.shares_owned)['1'], 7) # Unchanged.
self.assertEqual(wallet.points, 20000 - (4 * 325) - (3 * 600))
self.assertEqual(wallet_1.points, 10000 + (4 * 325))
self.assertEqual(wallet_2.points, 10000 + (3 * 600))
self.assertEqual(wallet.frozen, 2000) # The most important assert :P
# Since there wasn't anything pre-frozen, these freezes remain at 0.
self.assertEqual(future.ask, 95) # Lowest sale order, my own
self.assertEqual(future.bid, None) # Highest buy order
self.assertEqual(future.volume, 90)
# History
history = FutureHistory.objects.filter(future=future)
self.assertEqual(history.count(), 2)
self.assertEqual(history[0].price, 275)
self.assertEqual(history[1].price, 100) # Got the highest then lowest.
transactions = SuccessfulTransaction.objects.filter(future=future)
self.assertEqual(transactions.count(), 2)
self.assertTrue(self.compare_transactions(
transactions[0], SuccessfulTransaction(
buyer=order_1.creator, seller=user, future=future,
quantity=15, unit_price=275)))
self.assertTrue(self.compare_transactions(
transactions[1], SuccessfulTransaction(
buyer=order_2.creator, seller=user, future=future,
quantity=75, unit_price=100)))
# Wallets
wallet = Wallet.objects.get(user=user)
wallet_1 = Wallet.objects.get(user=order_1.creator)
wallet_2 = Wallet.objects.get(user=order_2.creator)
key = '%s' % future.pk
self.assertEqual(json.loads(wallet.shares_owned)[key], 5)
self.assertEqual(json.loads(wallet_1.shares_owned)[key], 15)
self.assertEqual(json.loads(wallet_2.shares_owned)[key], 75)
self.assertEqual(json.loads(wallet.shares_owned)['1'], 7) # Unchanged.
self.assertEqual(wallet.points, 10000 + (15 * 275) + (75 * 100))
self.assertEqual(wallet_1.points, 10000 - (15 * 275))
self.assertEqual(wallet_2.points, 10000 - (75 * 100))
self.assertEqual(wallet.frozen, 0)
self.assertEqual(wallet_1.frozen, 0)
filled_q = remaining_items - quantity
if order.order_type == Order.ORDER_TYPE_BUY:
# If we're buying make sure we are offering at least the lowest
# sale price.
if open_order.unit_price > order.unit_price:
# We are not offering enough; break out.
return None, None
elif order.order_type == Order.ORDER_TYPE_SELL:
# If we're selling make sure that we don't sell at less than the
# highest buy price.
if open_order.unit_price < order.unit_price:
return None, None
# We can fill the order (either fully or partially!). Set up the
# points & stock transfer.
transaction = SuccessfulTransaction(future=order.future,
quantity=open_order.quantity,
unit_price=open_order.unit_price)
if order.order_type == Order.ORDER_TYPE_BUY:
# Transfer points from order.creator to open_order.creator
transfer = {'buyer': order.creator.pk,
'buyer_order': order.pk,
'seller': open_order.creator.pk,
'seller_order': open_order.pk,
'price': open_order.unit_price,
'points': quantity * open_order.unit_price,
'share_quantity': quantity,
'share_type': order.future.pk}
transaction.buyer = order.creator
transaction.seller = open_order.creator
elif order.order_type == Order.ORDER_TYPE_SELL:
# Transfer points from open_order.creator to order.creator