def test_should_delete_notification_and_notification_history_for_id(notify_db, notify_db_session, sample_template): data = _notification_json(sample_template) notification = Notification(**data) dao_create_notification(notification) assert Notification.query.count() == 1 assert NotificationHistory.query.count() == 1 dao_delete_notifications_and_history_by_id(notification.id) assert Notification.query.count() == 0 assert NotificationHistory.query.count() == 0
def test_should_delete_no_notifications_or_notification_historys_if_no_matching_ids( notify_db, notify_db_session, sample_template ): id_1 = uuid.uuid4() id_2 = uuid.uuid4() data_1 = _notification_json(sample_template, id=id_1) notification_1 = Notification(**data_1) dao_create_notification(notification_1) assert Notification.query.count() == 1 assert NotificationHistory.query.count() == 1 dao_delete_notifications_and_history_by_id(id_2) assert Notification.query.count() == 1 assert NotificationHistory.query.count() == 1
def send_notification_to_queue(notification, research_mode, queue=None): if research_mode or notification.key_type == KEY_TYPE_TEST: queue = QueueNames.RESEARCH_MODE if notification.notification_type == SMS_TYPE: if not queue: queue = QueueNames.SEND_SMS deliver_task = provider_tasks.deliver_sms if notification.notification_type == EMAIL_TYPE: if not queue: queue = QueueNames.SEND_EMAIL deliver_task = provider_tasks.deliver_email try: deliver_task.apply_async([str(notification.id)], queue=queue) except Exception: dao_delete_notifications_and_history_by_id(notification.id) raise current_app.logger.debug("{} {} sent to the {} queue for delivery".format( notification.notification_type, notification.id, queue))
def test_should_delete_only_notification_and_notification_history_with_id(notify_db, notify_db_session, sample_template): id_1 = uuid.uuid4() id_2 = uuid.uuid4() data_1 = _notification_json(sample_template, id=id_1) data_2 = _notification_json(sample_template, id=id_2) notification_1 = Notification(**data_1) notification_2 = Notification(**data_2) dao_create_notification(notification_1) dao_create_notification(notification_2) assert Notification.query.count() == 2 assert NotificationHistory.query.count() == 2 dao_delete_notifications_and_history_by_id(notification_1.id) assert Notification.query.count() == 1 assert NotificationHistory.query.count() == 1 assert Notification.query.first().id == notification_2.id assert NotificationHistory.query.first().id == notification_2.id
def send_notification_to_queue(notification, research_mode, queue=None): if research_mode or notification.key_type == KEY_TYPE_TEST: queue = 'research-mode' elif not queue: if notification.notification_type == SMS_TYPE: queue = 'send-sms' if notification.notification_type == EMAIL_TYPE: queue = 'send-email' if notification.notification_type == SMS_TYPE: deliver_task = provider_tasks.deliver_sms if notification.notification_type == EMAIL_TYPE: deliver_task = provider_tasks.deliver_email try: deliver_task.apply_async([str(notification.id)], queue=queue) except Exception as e: current_app.logger.exception(e) dao_delete_notifications_and_history_by_id(notification.id) raise SendNotificationToQueueError() current_app.logger.info( "{} {} created at {}".format(notification.notification_type, notification.id, notification.created_at) )