def test_create_nightly_notification_status_triggers_tasks_for_days( notify_api, mocker): mock_celery = mocker.patch( 'app.celery.reporting_tasks.create_nightly_notification_status_for_day' ) create_nightly_notification_status() assert mock_celery.apply_async.call_count == ( (4 * 3) # four days, three notification types + 6 # six more days of just letters ) for process_date, notification_type in itertools.product( ['2019-07-31', '2019-07-30', '2019-07-29', '2019-07-28'], [SMS_TYPE, EMAIL_TYPE, LETTER_TYPE]): mock_celery.apply_async.assert_any_call(kwargs={ 'process_day': process_date, 'notification_type': notification_type }, queue=QueueNames.REPORTING) for process_date in [ '2019-07-27', '2019-07-26', '2019-07-25', '2019-07-24', '2019-07-23', '2019-07-22' ]: mock_celery.apply_async.assert_any_call(kwargs={ 'process_day': process_date, 'notification_type': LETTER_TYPE }, queue=QueueNames.REPORTING)
def test_create_nightly_notification_status_respects_aet(sample_template): create_notification(sample_template, status='delivered', created_at=datetime(2019, 10, 20, 13, 0)) # too new create_notification(sample_template, status='created', created_at=datetime(2019, 10, 20, 12, 59)) create_notification(sample_template, status='created', created_at=datetime(2019, 10, 19, 23, 0)) create_notification(sample_template, status='temporary-failure', created_at=datetime(2019, 10, 19, 22, 59)) # we create records for last four days create_notification(sample_template, status='sending', created_at=datetime(2019, 10, 17, 0, 0)) create_notification(sample_template, status='delivered', created_at=datetime(2019, 3, 16, 23, 59)) # too old create_nightly_notification_status() noti_status = FactNotificationStatus.query.order_by(FactNotificationStatus.aet_date).all() assert len(noti_status) == 3 assert noti_status[0].aet_date == date(2019, 10, 17) assert noti_status[0].notification_status == 'sending' assert noti_status[1].aet_date == date(2019, 10, 20) assert noti_status[1].notification_status == 'created' assert noti_status[2].aet_date == date(2019, 10, 20) assert noti_status[2].notification_status == 'temporary-failure'
def test_create_nightly_notification_status_triggers_tasks_for_days(notify_api, mocker, day_start, expected_kwargs): mock_celery = mocker.patch('app.celery.reporting_tasks.create_nightly_notification_status_for_day') create_nightly_notification_status(day_start) assert mock_celery.apply_async.call_count == 4 for i in range(4): assert mock_celery.apply_async.call_args_list[i][1]['kwargs'] == {'process_day': expected_kwargs[i]}
def test_create_nightly_notification_status_triggers_tasks_for_days( notify_api, mocker, day_start, expected_kwargs): mock_celery = mocker.patch( 'app.celery.reporting_tasks.create_nightly_notification_status_for_day' ) create_nightly_notification_status(day_start) assert mock_celery.apply_async.call_count == 4 * 3 # four days, three notification types for process_date, notification_type in itertools.product( expected_kwargs, ['sms', 'email', 'letter']): mock_celery.apply_async.assert_any_call(kwargs={ 'process_day': process_date, 'notification_type': notification_type }, queue=QueueNames.REPORTING)
def test_create_nightly_notification_status(notify_db_session): first_service = create_service(service_name='First Service') first_template = create_template(service=first_service) second_service = create_service(service_name='second Service') second_template = create_template(service=second_service, template_type='email') third_service = create_service(service_name='third Service') third_template = create_template(service=third_service, template_type='letter') create_notification(template=first_template, status='delivered') create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=1)) create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=2)) create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=4)) create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=5)) create_notification(template=second_template, status='temporary-failure') create_notification(template=second_template, status='temporary-failure', created_at=datetime.utcnow() - timedelta(days=1)) create_notification(template=second_template, status='temporary-failure', created_at=datetime.utcnow() - timedelta(days=2)) create_notification(template=second_template, status='temporary-failure', created_at=datetime.utcnow() - timedelta(days=4)) create_notification(template=second_template, status='temporary-failure', created_at=datetime.utcnow() - timedelta(days=5)) create_notification(template=third_template, status='created') create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=1)) create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=2)) create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=4)) create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=5)) assert len(FactNotificationStatus.query.all()) == 0 create_nightly_notification_status() new_data = FactNotificationStatus.query.order_by( FactNotificationStatus.aet_date, FactNotificationStatus.notification_type ).all() assert len(new_data) == 9 assert str(new_data[0].aet_date) == datetime.strftime(convert_utc_to_aet(datetime.utcnow() - timedelta(days=4)), "%Y-%m-%d") assert str(new_data[3].aet_date) == datetime.strftime(convert_utc_to_aet(datetime.utcnow() - timedelta(days=2)), "%Y-%m-%d") assert str(new_data[6].aet_date) == datetime.strftime(convert_utc_to_aet(datetime.utcnow() - timedelta(days=1)), "%Y-%m-%d")