import unittest from datetime import datetime, timedelta from unittest.mock import Mock, patch from ..sleep import SleepPlugin from edmond.tests.test_plugin import get_plugin_patcher class TestSleepPlugin(unittest.TestCase): def test_is_sleep_time(self): with get_plugin_patcher(SleepPlugin): plugin = SleepPlugin() today = datetime.today() tomorrow = datetime.today() + timedelta(days=1) # Given sleep time from 23:00 to 7:00 plugin.config = {"sleep_time": 23, "wakeup_time": 7} # You should sleep within the sleep range. today_23_00 = today.replace(hour=23, minute=0, second=0) self.assertTrue(plugin.is_sleep_time(today_23_00)) today_23_30 = today.replace(hour=23, minute=30, second=0) self.assertTrue(plugin.is_sleep_time(today_23_30)) tomorrow_00_00 = tomorrow.replace(hour=0, minute=0, second=0) self.assertTrue(plugin.is_sleep_time(tomorrow_00_00)) tomorrow_06_59 = tomorrow.replace(hour=6, minute=59, second=0) self.assertTrue(plugin.is_sleep_time(tomorrow_06_59)) # But not outside the range. today_12_00 = today.replace(hour=12, minute=0, second=0) self.assertFalse(plugin.is_sleep_time(today_12_00)) today_22_59 = today.replace(hour=22, minute=59, second=0) self.assertFalse(plugin.is_sleep_time(today_22_59)) # Given sleep time from 00:00 to 7:00 plugin.config = {"sleep_time": 0, "wakeup_time": 7} # You should sleep within the sleep range. today_00_00 = today.replace(hour=0, minute=0, second=0) self.assertTrue(plugin.is_sleep_time(today_00_00)) today_06_59 = today.replace(hour=6, minute=59, second=0) self.assertTrue(plugin.is_sleep_time(today_06_59)) tomorrow_06_59 = tomorrow.replace(hour=6, minute=59, second=0) self.assertTrue(plugin.is_sleep_time(tomorrow_06_59)) # But not outside the range. today_12_00 = today.replace(hour=12, minute=0, second=0) self.assertFalse(plugin.is_sleep_time(today_12_00)) today_22_59 = today.replace(hour=22, minute=59, second=0) self.assertFalse(plugin.is_sleep_time(today_22_59))