From 74c9ca2bf68218f5d3c77ba655bc16a54cbf4d88 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Thu, 5 Aug 2021 18:32:06 +0100 Subject: [PATCH] Fetch all broadcast messages that are or were transmitted Regardless of channel. Do not include: - broadcasts older than 25.05.2021 - stubbed broadcasts - broadcasts that were not transmitted. So only broadcasting, cancelled and completed make the list; --- app/dao/broadcast_message_dao.py | 11 +++++ tests/app/dao/test_broadcast_message_dao.py | 55 +++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/app/dao/broadcast_message_dao.py b/app/dao/broadcast_message_dao.py index 8adb9b681..003582deb 100644 --- a/app/dao/broadcast_message_dao.py +++ b/app/dao/broadcast_message_dao.py @@ -1,4 +1,6 @@ import uuid +from datetime import datetime +from sqlalchemy import desc from app import db from app.dao.dao_utils import autocommit @@ -9,6 +11,7 @@ from app.models import ( BroadcastProviderMessage, BroadcastProviderMessageNumber, BroadcastProviderMessageStatus, + BroadcastStatusType ) @@ -29,6 +32,14 @@ def dao_get_broadcast_messages_for_service(service_id): ).order_by(BroadcastMessage.created_at) +def dao_get_all_broadcast_messages(): + return BroadcastMessage.query.filter( + BroadcastMessage.starts_at >= datetime(2021, 5, 25, 0, 0, 0), + BroadcastMessage.stubbed == False, # noqa + BroadcastMessage.status.in_(BroadcastStatusType.LIVE_STATUSES) + ).order_by(desc(BroadcastMessage.starts_at)).all() + + def get_earlier_events_for_broadcast_event(broadcast_event_id): """ This is used to build up the references list. diff --git a/tests/app/dao/test_broadcast_message_dao.py b/tests/app/dao/test_broadcast_message_dao.py index 1b0738a0f..8b5a947e0 100644 --- a/tests/app/dao/test_broadcast_message_dao.py +++ b/tests/app/dao/test_broadcast_message_dao.py @@ -1,13 +1,18 @@ from datetime import datetime +from flask import current_app from app.dao.broadcast_message_dao import ( create_broadcast_provider_message, + dao_get_all_broadcast_messages, get_earlier_events_for_broadcast_event, ) +from app.dao.broadcast_service_dao import insert_or_update_service_broadcast_settings from app.models import BROADCAST_TYPE, BroadcastEventMessageType + from tests.app.db import ( create_broadcast_event, create_broadcast_message, + create_service, create_template, ) @@ -65,3 +70,53 @@ def test_create_broadcast_provider_message_creates_in_correct_state(sample_broad assert broadcast_provider_message.broadcast_event_id == broadcast_event.id assert broadcast_provider_message.created_at is not None assert broadcast_provider_message.updated_at is None + + +def test_dao_get_all_broadcast_messages(sample_broadcast_service): + template_1 = create_template(sample_broadcast_service, BROADCAST_TYPE) + # older message, should appear second in list + broadcast_message_1 = create_broadcast_message( + template_1, + starts_at=datetime(2021, 6, 15, 12, 0, 0), + status='cancelled') + + service_2 = create_service( + service_name="broadcast service 2", + service_permissions=[BROADCAST_TYPE] + ) + insert_or_update_service_broadcast_settings(service_2, channel="severe") + + template_2 = create_template(service_2, BROADCAST_TYPE) + # newer message, should appear first in list + broadcast_message_2 = create_broadcast_message( + template_2, + stubbed=False, + status='broadcasting', + starts_at=datetime(2021, 6, 20, 12, 0, 0), + ) + + # broadcast_message_stubbed + create_broadcast_message( + template_2, + stubbed=True, + status='broadcasting', + starts_at=datetime(2021, 6, 15, 12, 0, 0), + ) + # broadcast_message_old + create_broadcast_message( + template_2, + stubbed=False, + status='completed', + starts_at=datetime(2021, 5, 20, 12, 0, 0), + ) + # broadcast_message_rejected + create_broadcast_message( + template_2, + stubbed=False, + status='rejected', + starts_at=datetime(2021, 6, 15, 12, 0, 0), + ) + + broadcast_messages = dao_get_all_broadcast_messages() + assert len(broadcast_messages) == 2 + assert broadcast_messages == [broadcast_message_2, broadcast_message_1]