def test_fetch_weekly_historical_stats_ignores_second_service(notify_db, notify_db_session, service_factory):
    template_1 = service_factory.get("1").templates[0]
    template_2 = service_factory.get("2").templates[0]

    notification_history = functools.partial(create_notification_history, notify_db, notify_db_session)
    last_sunday = notification_history(template_1, created_at=datetime(2016, 7, 24, 23, 59))
    last_monday_morning = notification_history(template_2, created_at=datetime(2016, 7, 25, 0, 0))

    with freeze_time("Wed 27th July 2016"):
        ret = dao_fetch_weekly_historical_stats_for_service(template_1.service_id)

    assert len(ret) == 1
    assert ret[0].week_start == datetime(2016, 7, 18)
    assert ret[0].count == 1
def test_fetch_weekly_historical_stats_separates_types(
    notify_db, notify_db_session, sample_template, sample_email_template
):
    notification_history = functools.partial(
        create_notification_history, notify_db, notify_db_session, created_at=datetime(2016, 7, 25)
    )

    notification_history(sample_template)
    notification_history(sample_email_template)

    with freeze_time("Wed 27th July 2016"):
        ret = dao_fetch_weekly_historical_stats_for_service(sample_template.service_id)

    assert len(ret) == 2
    assert ret[0].week_start == datetime(2016, 7, 25)
    assert ret[0].count == 1
    assert ret[0].notification_type == "email"
    assert ret[1].week_start == datetime(2016, 7, 25)
    assert ret[1].count == 1
    assert ret[1].notification_type == "sms"
def test_fetch_weekly_historical_stats_separates_weeks(notify_db, notify_db_session, sample_template):
    notification_history = functools.partial(create_notification_history, notify_db, notify_db_session, sample_template)
    week_53_last_yr = notification_history(created_at=datetime(2016, 1, 1))
    week_1_last_yr = notification_history(created_at=datetime(2016, 1, 5))
    last_sunday = notification_history(created_at=datetime(2016, 7, 24, 23, 59))
    last_monday_morning = notification_history(created_at=datetime(2016, 7, 25, 0, 0))
    last_monday_evening = notification_history(created_at=datetime(2016, 7, 25, 23, 59))

    with freeze_time("Wed 27th July 2016"):
        today = notification_history(created_at=datetime.now(), status="delivered")
        ret = dao_fetch_weekly_historical_stats_for_service(sample_template.service_id)

    assert [(row.week_start, row.status) for row in ret] == [
        (datetime(2015, 12, 28), "created"),
        (datetime(2016, 1, 4), "created"),
        (datetime(2016, 7, 18), "created"),
        (datetime(2016, 7, 25), "created"),
        (datetime(2016, 7, 25), "delivered"),
    ]
    assert ret[-2].count == 2
    assert ret[-1].count == 1