mirror of
https://github.com/RoboSats/robosats.git
synced 2025-01-31 02:21:35 +00:00
Add order expiry tests
This commit is contained in:
parent
14340fd64b
commit
62ef86f1b4
@ -8,14 +8,7 @@ from api.models import Order
|
|||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
help = "Follows all active hold invoices"
|
help = "Follows all active orders and make them expire if needed."
|
||||||
|
|
||||||
# def add_arguments(self, parser):
|
|
||||||
# parser.add_argument('debug', nargs='+', type=boolean)
|
|
||||||
|
|
||||||
def clean_orders(self, *args, **options):
|
|
||||||
"""Continuously checks order expiration times for 1 hour. If order
|
|
||||||
has expires, it calls the logics module for expiration handling."""
|
|
||||||
|
|
||||||
do_nothing = [
|
do_nothing = [
|
||||||
Order.Status.UCA,
|
Order.Status.UCA,
|
||||||
@ -30,10 +23,11 @@ class Command(BaseCommand):
|
|||||||
Order.Status.WFR,
|
Order.Status.WFR,
|
||||||
]
|
]
|
||||||
|
|
||||||
while True:
|
def clean_orders(self):
|
||||||
time.sleep(5)
|
"""Continuously checks order expiration times. If order
|
||||||
|
has expires, it calls the logics module for expiration handling."""
|
||||||
|
|
||||||
queryset = Order.objects.exclude(status__in=do_nothing)
|
queryset = Order.objects.exclude(status__in=self.do_nothing)
|
||||||
queryset = queryset.filter(
|
queryset = queryset.filter(
|
||||||
expires_at__lt=timezone.now()
|
expires_at__lt=timezone.now()
|
||||||
) # expires at lower than now
|
) # expires at lower than now
|
||||||
@ -69,7 +63,10 @@ class Command(BaseCommand):
|
|||||||
"""Never mind database locked error, keep going, print them out.
|
"""Never mind database locked error, keep going, print them out.
|
||||||
Not an issue with PostgresQL"""
|
Not an issue with PostgresQL"""
|
||||||
try:
|
try:
|
||||||
|
while True:
|
||||||
self.clean_orders()
|
self.clean_orders()
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if "database is locked" in str(e):
|
if "database is locked" in str(e):
|
||||||
self.stdout.write("database is locked")
|
self.stdout.write("database is locked")
|
||||||
|
@ -7,19 +7,20 @@ from decouple import config
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
from api.management.commands.clean_orders import Command as CleanOrders
|
||||||
from api.management.commands.follow_invoices import Command as FollowInvoices
|
from api.management.commands.follow_invoices import Command as FollowInvoices
|
||||||
from api.models import Currency, Order
|
from api.models import Currency, Order
|
||||||
from api.tasks import cache_market, follow_send_payment
|
from api.tasks import cache_market, follow_send_payment
|
||||||
from control.models import BalanceLog
|
from control.models import BalanceLog
|
||||||
from control.tasks import compute_node_balance, do_accounting
|
from control.tasks import compute_node_balance, do_accounting
|
||||||
from tests.node_utils import (
|
from tests.test_api import BaseAPITestCase
|
||||||
|
from tests.utils.node import (
|
||||||
add_invoice,
|
add_invoice,
|
||||||
create_address,
|
create_address,
|
||||||
pay_invoice,
|
pay_invoice,
|
||||||
set_up_regtest_network,
|
set_up_regtest_network,
|
||||||
)
|
)
|
||||||
from tests.pgp_utils import sign_message
|
from tests.utils.pgp import sign_message
|
||||||
from tests.test_api import BaseAPITestCase
|
|
||||||
|
|
||||||
|
|
||||||
def read_file(file_path):
|
def read_file(file_path):
|
||||||
@ -358,8 +359,13 @@ class TradeTest(BaseAPITestCase):
|
|||||||
|
|
||||||
def follow_hold_invoices(self):
|
def follow_hold_invoices(self):
|
||||||
# A background thread checks every 5 second the status of invoices. We invoke directly during test.
|
# A background thread checks every 5 second the status of invoices. We invoke directly during test.
|
||||||
follow_invoices = FollowInvoices()
|
follower = FollowInvoices()
|
||||||
follow_invoices.follow_hold_invoices()
|
follower.follow_hold_invoices()
|
||||||
|
|
||||||
|
def clean_orders(self):
|
||||||
|
# A background thread checks every 5 second order expirations. We invoke directly during test.
|
||||||
|
cleaner = CleanOrders()
|
||||||
|
cleaner.clean_orders()
|
||||||
|
|
||||||
def send_payments(self):
|
def send_payments(self):
|
||||||
# A background thread checks every 5 second whether there are outgoing payments. We invoke directly during test.
|
# A background thread checks every 5 second whether there are outgoing payments. We invoke directly during test.
|
||||||
@ -906,6 +912,130 @@ class TradeTest(BaseAPITestCase):
|
|||||||
data["bad_request"], "This order has been cancelled collaborativelly"
|
data["bad_request"], "This order has been cancelled collaborativelly"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_created_order_expires(self):
|
||||||
|
"""
|
||||||
|
Tests the expiration of a public order
|
||||||
|
"""
|
||||||
|
maker_form = self.maker_form_buy_with_range
|
||||||
|
response = self.make_order(maker_form)
|
||||||
|
|
||||||
|
# Change order expiry to now
|
||||||
|
order = Order.objects.get(id=response.json()["id"])
|
||||||
|
order.expires_at = datetime.now()
|
||||||
|
order.save()
|
||||||
|
|
||||||
|
# Make orders expire
|
||||||
|
self.clean_orders()
|
||||||
|
|
||||||
|
response = self.get_order(response.json()["id"])
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertResponse(response)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
data["status"],
|
||||||
|
Order.Status.EXP,
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
data["expiry_message"],
|
||||||
|
Order.ExpiryReasons(Order.ExpiryReasons.NMBOND).label,
|
||||||
|
)
|
||||||
|
self.assertEqual(data["expiry_reason"], Order.ExpiryReasons.NMBOND)
|
||||||
|
|
||||||
|
def test_public_order_expires(self):
|
||||||
|
"""
|
||||||
|
Tests the expiration of a public order
|
||||||
|
"""
|
||||||
|
maker_form = self.maker_form_buy_with_range
|
||||||
|
response = self.make_and_publish_order(maker_form)
|
||||||
|
|
||||||
|
# Change order expiry to now
|
||||||
|
order = Order.objects.get(id=response.json()["id"])
|
||||||
|
order.expires_at = datetime.now()
|
||||||
|
order.save()
|
||||||
|
|
||||||
|
# Make orders expire
|
||||||
|
self.clean_orders()
|
||||||
|
|
||||||
|
response = self.get_order(response.json()["id"])
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertResponse(response)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
data["status"],
|
||||||
|
Order.Status.EXP,
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
data["expiry_message"],
|
||||||
|
Order.ExpiryReasons(Order.ExpiryReasons.NTAKEN).label,
|
||||||
|
)
|
||||||
|
self.assertEqual(data["expiry_reason"], Order.ExpiryReasons.NTAKEN)
|
||||||
|
|
||||||
|
def test_taken_order_expires(self):
|
||||||
|
"""
|
||||||
|
Tests the expiration of a public order
|
||||||
|
"""
|
||||||
|
maker_form = self.maker_form_buy_with_range
|
||||||
|
response = self.make_and_lock_contract(maker_form)
|
||||||
|
|
||||||
|
# Change order expiry to now
|
||||||
|
order = Order.objects.get(id=response.json()["id"])
|
||||||
|
order.expires_at = datetime.now()
|
||||||
|
order.save()
|
||||||
|
|
||||||
|
# Make orders expire
|
||||||
|
self.clean_orders()
|
||||||
|
|
||||||
|
response = self.get_order(response.json()["id"])
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertResponse(response)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
data["status"],
|
||||||
|
Order.Status.EXP,
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
data["expiry_message"],
|
||||||
|
Order.ExpiryReasons(Order.ExpiryReasons.NESINV).label,
|
||||||
|
)
|
||||||
|
self.assertEqual(data["expiry_reason"], Order.ExpiryReasons.NESINV)
|
||||||
|
|
||||||
|
def test_escrow_locked_expires(self):
|
||||||
|
"""
|
||||||
|
Tests the expiration of a public order
|
||||||
|
"""
|
||||||
|
maker_form = self.maker_form_buy_with_range
|
||||||
|
response = self.trade_to_locked_escrow(maker_form)
|
||||||
|
|
||||||
|
# Change order expiry to now
|
||||||
|
order = Order.objects.get(id=response.json()["id"])
|
||||||
|
order.expires_at = datetime.now()
|
||||||
|
order.save()
|
||||||
|
|
||||||
|
# Make orders expire
|
||||||
|
self.clean_orders()
|
||||||
|
|
||||||
|
response = self.get_order(response.json()["id"])
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertResponse(response)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
data["status"],
|
||||||
|
Order.Status.EXP,
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
data["expiry_message"],
|
||||||
|
Order.ExpiryReasons(Order.ExpiryReasons.NINVOI).label,
|
||||||
|
)
|
||||||
|
self.assertEqual(data["expiry_reason"], Order.ExpiryReasons.NINVOI)
|
||||||
|
|
||||||
def test_ticks(self):
|
def test_ticks(self):
|
||||||
"""
|
"""
|
||||||
Tests the historical ticks serving endpoint after creating a contract
|
Tests the historical ticks serving endpoint after creating a contract
|
||||||
|
0
tests/utils/__init__.py
Normal file
0
tests/utils/__init__.py
Normal file
Loading…
Reference in New Issue
Block a user