def test_EventDAO_compose_alert_message_days(self, MockEventDAO, MockMessageApi): name = "test_event" interval = 86400 time_diff = 172800 bo = EventBO(MockEventDAO, MockMessageApi) output = bo.compose_alert_message(name, time_diff, interval) self.assertEqual(output, "離上一次\"test_event\"已經2天了!")
def test_EventBO_handle_remove_command(self, MockEventDAO): options = {"name": "test_event"} user = "******" MockEventDAO.remove_event.return_value = True bo = EventBO(MockEventDAO) result = bo.handle_remove_command(user, options) self.assertTrue(result)
def test_EventDAO_compose_alert_message_days(self, MockEventDAO, MockMessageApi): name = "test_event" interval = 86400 time_diff = 172800 bo = EventBO(MockEventDAO, MockMessageApi) output = bo.compose_alert_message(name, time_diff, interval) self.assertEqual( Util.compose_how_long_from_last_time_string(name, 2, "天"), output)
def test_EventBO_handle_add_command_without_alarm_time(self, MockEventDAO): options = {"name": "test_event", "interval": 86400, "alarm_time": Event.DEFAULT_ALARM_TIME} user = "******" MockEventDAO.has_event.return_value = False MockEventDAO.add_event.return_value = True bo = EventBO(MockEventDAO) result = bo.handle_add_command(user, options) self.assertTrue(result)
def test_EventBO_send_notification_should_not_send(self, MockEventDAO, MockMessageApi): MockEventDAO.query_all_events.return_value = [ {"created_time": 10000, "name": "test_target", "interval": 100, "target": "test_target"} ] bo = EventBO(MockEventDAO, MockMessageApi) bo.send_notification(current_time=10001) MockMessageApi.send_reset_confirm_message.assert_not_called() MockEventDAO.update_last_notified_time.assert_not_called()
def test_EventBO_handle_list_command(self, MockEventDAO, MockMessageApi): options = {} user = "******" MockEventDAO.query_events_by_target.return_value = [ { "created_time": 1503061200, "last_notified_time": 1484123123, "name": "test_target", "interval": 86400} ] bo = EventBO(MockEventDAO, MockMessageApi) bo.handle_list_command(user, options)
def test_EventBO_handle_reset_command(self, MockEventDAO): options = {"name": "test_event"} user = "******" MockEventDAO.query_event_by_target_and_name.return_value = \ {"target": "test target", "name": "test event", "last_notified_time": 0} MockEventDAO.reset_event.return_value = True bo = EventBO(MockEventDAO) result = bo.handle_reset_command(user, options) self.assertTrue(result)
def handle_message(event): user_id = event.source.sender_id source_type = event.source.type print(user_id) text = event.message.text bo = EventBO() if text.startswith("/add"): result = bo.handle_add_command(user_id, command_parser(text)) elif text.startswith("/remove"): result = bo.handle_remove_command(user_id, command_parser(text)) elif text.startswith("/reset"): result = bo.handle_reset_command(user_id, command_parser(text)) elif text.startswith("/list"): bo.handle_list_command(user_id, command_parser(text)) elif text.startswith("/help"): send_text_message(user_id, MESSAGE_HELP) elif text.startswith("/test"): send_reset_confirm_message(user_id, "test_event_desc") elif text.startswith("/do_nothing"): pass else: if source_type == "user": send_text_message(user_id, MESSAGE_ERROR) else: return if "result" in locals(): if result: send_text_message(user_id, MESSAGE_OK) else: send_text_message(user_id, MESSAGE_ERROR)
def handle_message(event): user_id = event.source.sender_id source_type = event.source.type reply_token = event.reply_token if isinstance(event.message, TextMessage): text = event.message.text else: import datetime print(datetime.datetime.now()) print(event.message) return message_api = MessageApi(user_id, reply_token) bo = EventBO() bo.set_message_api(message_api) if text.startswith("/add"): result = bo.handle_add_command(user_id, command_parser(text)) elif text.startswith("/remove"): result = bo.handle_remove_command(user_id, command_parser(text)) elif text.startswith("/reset"): result = bo.handle_reset_command(user_id, command_parser(text)) elif text.startswith("/list"): bo.handle_list_command(user_id, command_parser(text)) elif text.startswith("/help"): message_api.reply_text_message(MESSAGE_HELP) elif text.startswith("/do_nothing"): pass else: if source_type == "user": reply_text_message(reply_token, MESSAGE_ERROR) else: return if "result" in locals(): if result: reply_text_message(reply_token, MESSAGE_OK) else: reply_text_message(reply_token, MESSAGE_ERROR)
def list_event(): bo = EventBO() bo.handle_list_command(user_id)
# -*- coding: utf-8 -*- import os import logging from apscheduler.schedulers.blocking import BlockingScheduler from message import send_text_message from bo import EventBO bo = EventBO() # sched = BlockingScheduler() # @sched.scheduled_job('cron', minute="*") # def scheduled_job(): # bo.send_notification() # sched.start() bo.send_notification()