def test_session_params(self): prog = UserProgram(users_id=123) #prog.set_var("wakeup", datetime.time(6,30)) prog.default_wakeup = datetime.time(3, 45) prog.default_window = 29 #prog.default_sleep_time = None prog.save() session = Session(program=prog) self.assertEqual(session.wakeup, time_to_next_datetime(prog.default_wakeup)) self.assertEqual(session.window, prog.default_window) self.assertEqual(session.sleep_time, None) prog.default_window = 24 prog.default_wakeup = datetime.time(3, 23) prog.save() self.assertEqual(session.wakeup, time_to_next_datetime(datetime.time(3, 45))) self.assertEqual(session.window, 29) self.assertEqual(session.sleep_time, None) session.program = prog self.assertEqual(session.wakeup, time_to_next_datetime(datetime.time(3, 45))) self.assertEqual(session.window, 29) self.assertEqual(session.sleep_time, None) prog.default_sleep_time = 300 session = Session(program=prog) self.assertEqual(session.sleep_time, 300) session.save() # test log logging.basicConfig(level=logging.INFO) session.log("UNKNOWN", "test info") session.log("WAKEUP", "test wakeup") session.log("LIGHTS", "test lights") session.log("INFO", "test info") session.log("WARNING", "test warning") session.log("DEBUG", "test debug") session.log("ERROR", "test error") self.assertEqual(session.logs.count(), 7) session.log(123, "last") self.assertEqual(session.logs.all()[0].typ, 123) # test actions arguments session = Session(program=prog, wakeup_action=settings.COMMANDS.keys()[0], lights_action=settings.COMMANDS.keys()[1]) session.save() self.assertEqual(session.wakeup_action, settings.COMMANDS.keys()[0]) self.assertEqual(session.lights_action, settings.COMMANDS.keys()[1]) session = Session(program=prog, sleep_time='123') session.save() self.assertEqual(session.sleep_time, 123) prog.delete()
def test_time_to_next_datetime(self): now = datetime.datetime.now() prev = now - datetime.timedelta(minutes=1) nd = time_to_next_datetime(prev.time()) # past test self.assertTrue(nd > now) self.assertTrue(nd - datetime.timedelta(hours=23, minutes=55) > now) # futur test next = now + datetime.timedelta(minutes=1) nd = time_to_next_datetime(next.time()) self.assertTrue(nd > now) self.assertTrue(nd - datetime.timedelta(minutes=2) < now)
def __init__(self, *args, **kwargs): # copy default values from program if "sleep_time" in kwargs: try: kwargs["sleep_time"] = int(kwargs["sleep_time"]) except ValueError: raise ValueError, "sleep_time is not an integer" if "program" in kwargs: prog = kwargs["program"] if not "wakeup" in kwargs and prog.default_wakeup: kwargs["wakeup"] = time_to_next_datetime(prog.default_wakeup) if not "sleep_time" in kwargs and prog.default_sleep_time: kwargs["sleep_time"] = prog.default_sleep_time if not "window" in kwargs and prog.default_window: kwargs["window"] = prog.default_window if not "lights_action" in kwargs and prog.lights_action: kwargs["lights_action"] = prog.lights_action if not "wakeup_action" in kwargs and prog.wakeup_action: kwargs["wakeup_action"] = prog.wakeup_action if not "wakeup" in kwargs and "sleep_time" in kwargs: kwargs["wakeup"] = datetime.datetime.now() + datetime.timedelta(minutes=kwargs["sleep_time"]) return super(Session, self).__init__(*args, **kwargs)