from datetime import datetime, time from django.test import TestCase from core.models import TradingTime, User 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( user=self.user, start_day=1, end_day=1, start_time=self.time_9, end_time=self.time_11, ) 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( user=self.user, start_day=1, end_day=1, start_time=self.time_14, end_time=self.time_16, ) 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( user=self.user, start_day=1, # Monday end_day=6, # Saturday start_time=self.time_9, end_time=self.time_11, ) 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( user=self.user, start_day=1, end_day=6, start_time=self.time_14, end_time=self.time_16, ) 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( user=self.user, start_day=2, # Tuesday end_day=6, start_time=self.time_9, end_time=self.time_11, ) self.assertFalse(trading_time.within_range(test_time))