2022-11-25 22:15:27 +00:00
|
|
|
from datetime import datetime, time
|
|
|
|
|
2022-11-25 19:28:21 +00:00
|
|
|
from django.test import TestCase
|
2022-11-25 22:15:27 +00:00
|
|
|
|
2022-11-25 19:28:21 +00:00
|
|
|
from core.models import TradingTime, User
|
2022-11-25 22:15:27 +00:00
|
|
|
|
2022-11-25 19:28:21 +00:00
|
|
|
|
|
|
|
class MarketTestCase(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# Create a test user
|
|
|
|
self.user = User.objects.create_user(
|
|
|
|
username="testuser",
|
|
|
|
email="test@example.com",
|
|
|
|
)
|
|
|
|
self.time_9 = time(9, 0, 0)
|
|
|
|
self.time_11 = time(11, 0, 0)
|
|
|
|
self.time_14 = time(14, 0, 0)
|
|
|
|
self.time_16 = time(16, 0, 0)
|
|
|
|
|
|
|
|
def test_time_within_range(self):
|
|
|
|
"""
|
|
|
|
Check the method of TradingTime model works as expected.
|
|
|
|
We check two dates, one that is within the range and one that is not.
|
|
|
|
"""
|
|
|
|
test_time = datetime(2022, 11, 28, 10, 0, 0)
|
|
|
|
|
|
|
|
trading_time = TradingTime.objects.create(
|
2022-11-25 22:15:27 +00:00
|
|
|
user=self.user,
|
|
|
|
start_day=1,
|
|
|
|
end_day=1,
|
|
|
|
start_time=self.time_9,
|
|
|
|
end_time=self.time_11,
|
2022-11-25 19:28:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertTrue(trading_time.within_range(test_time))
|
|
|
|
|
|
|
|
def test_time_within_range_fail(self):
|
|
|
|
"""
|
|
|
|
Check the method of TradingTime model works as expected.
|
|
|
|
We check two dates, one that is within the range and one that is not.
|
|
|
|
"""
|
|
|
|
test_time = datetime(2022, 11, 28, 10, 0, 0)
|
|
|
|
trading_time = TradingTime.objects.create(
|
2022-11-25 22:15:27 +00:00
|
|
|
user=self.user,
|
|
|
|
start_day=1,
|
|
|
|
end_day=1,
|
|
|
|
start_time=self.time_14,
|
|
|
|
end_time=self.time_16,
|
2022-11-25 19:28:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertFalse(trading_time.within_range(test_time))
|
|
|
|
|
|
|
|
def test_time_within_range_across_days(self):
|
|
|
|
test_time = datetime(2022, 11, 28, 20, 0, 0)
|
|
|
|
|
|
|
|
trading_time = TradingTime.objects.create(
|
2022-11-25 22:15:27 +00:00
|
|
|
user=self.user,
|
|
|
|
start_day=1, # Monday
|
|
|
|
end_day=6, # Saturday
|
|
|
|
start_time=self.time_9,
|
|
|
|
end_time=self.time_11,
|
2022-11-25 19:28:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertTrue(trading_time.within_range(test_time))
|
|
|
|
|
|
|
|
def test_time_within_range_across_days_fail(self):
|
|
|
|
# Set test time Monday, 28 November, 2022, 13:00:00
|
|
|
|
test_time = datetime(2022, 11, 28, 13, 0, 0)
|
|
|
|
trading_time = TradingTime.objects.create(
|
2022-11-25 22:15:27 +00:00
|
|
|
user=self.user,
|
|
|
|
start_day=1,
|
|
|
|
end_day=6,
|
|
|
|
start_time=self.time_14,
|
|
|
|
end_time=self.time_16,
|
2022-11-25 19:28:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertFalse(trading_time.within_range(test_time))
|
|
|
|
|
|
|
|
def test_time_within_range_day_mismatch(self):
|
|
|
|
"""
|
|
|
|
Check that the day of the week is taken into account.
|
|
|
|
"""
|
|
|
|
test_time = datetime(2022, 11, 29, 8, 0, 0)
|
|
|
|
|
|
|
|
trading_time = TradingTime.objects.create(
|
2022-11-25 22:15:27 +00:00
|
|
|
user=self.user,
|
|
|
|
start_day=2, # Tuesday
|
|
|
|
end_day=6,
|
|
|
|
start_time=self.time_9,
|
|
|
|
end_time=self.time_11,
|
2022-11-25 19:28:21 +00:00
|
|
|
)
|
|
|
|
|
2022-11-25 22:15:27 +00:00
|
|
|
self.assertFalse(trading_time.within_range(test_time))
|