Ejemplo n.º 1
0
def test_create_nightly_billing_update_when_record_exists(
        notify_db,
        notify_db_session,
        sample_service,
        sample_template,
        mocker):

    mocker.patch('app.celery.reporting_tasks.get_rate', side_effect=mocker_get_rate)

    sample_notification(
        notify_db,
        notify_db_session,
        created_at=datetime.now() - timedelta(days=1),
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by=None,
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )

    records = FactBilling.query.all()
    assert len(records) == 0

    create_nightly_billing()
    records = FactBilling.query.order_by(FactBilling.aet_date).all()

    assert len(records) == 1
    assert records[0].aet_date == date(2018, 1, 14)

    # run again, make sure create_nightly_billing updates with no error
    create_nightly_billing()
    assert len(records) == 1
def test_create_nightly_billing_triggers_tasks_for_days(notify_api, mocker, day_start, expected_kwargs):
    mock_celery = mocker.patch('app.celery.reporting_tasks.create_nightly_billing_for_day')
    create_nightly_billing(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]}
Ejemplo n.º 3
0
def test_create_nightly_billing_letter(
        notify_db,
        notify_db_session,
        sample_service,
        sample_letter_template,
        mocker):
    yesterday = datetime.utcnow() - timedelta(days=1)

    mocker.patch('app.celery.reporting_tasks.get_rate', side_effect=mocker_get_rate)

    sample_notification(
        notify_db,
        notify_db_session,
        created_at=yesterday,
        service=sample_service,
        template=sample_letter_template,
        status='delivered',
        sent_by='dvla',
        international=False,
        rate_multiplier=2.0,
        billable_units=2,
    )

    records = FactBilling.query.all()
    assert len(records) == 0

    create_nightly_billing()
    records = FactBilling.query.order_by('rate_multiplier').all()
    assert len(records) == 1
    record = records[0]
    assert record.notification_type == LETTER_TYPE
    assert record.aet_date == datetime.date(convert_utc_to_aet(yesterday))
    assert record.rate == Decimal(2.1)
    assert record.billable_units == 2
    assert record.rate_multiplier == 2.0
Ejemplo n.º 4
0
def test_create_nightly_billing_consolidate_from_3_days_delta(
        notify_db, notify_db_session, sample_service, sample_template, mocker):

    mocker.patch('app.dao.fact_billing_dao.get_rate',
                 side_effect=mocker_get_rate)

    # create records from 11th to 15th
    for i in range(0, 11):
        sample_notification(
            notify_db,
            notify_db_session,
            created_at=datetime.now() - timedelta(days=i),
            service=sample_service,
            template=sample_template,
            status='delivered',
            sent_by=None,
            international=False,
            rate_multiplier=1.0,
            billable_units=1,
        )

    notification = Notification.query.order_by(Notification.created_at).all()
    assert datetime.date(notification[0].created_at) == date(2018, 1, 5)

    records = FactBilling.query.all()
    assert len(records) == 0

    create_nightly_billing()
    records = FactBilling.query.order_by(FactBilling.bst_date).all()

    assert len(records) == 10
    assert records[0].bst_date == date(2018, 1, 5)
    assert records[-1].bst_date == date(2018, 1, 14)
Ejemplo n.º 5
0
def test_create_nightly_billing_letter(notify_db, notify_db_session,
                                       sample_service, sample_letter_template,
                                       mocker):
    yesterday = datetime.now() - timedelta(days=1)

    mocker.patch('app.dao.fact_billing_dao.get_rate',
                 side_effect=mocker_get_rate)

    sample_notification(
        notify_db,
        notify_db_session,
        created_at=yesterday,
        service=sample_service,
        template=sample_letter_template,
        status='delivered',
        sent_by='dvla',
        international=False,
        rate_multiplier=2.0,
        billable_units=2,
    )

    records = FactBilling.query.all()
    assert len(records) == 0
    # Celery expects the arguments to be a string or primitive type.
    yesterday_str = datetime.strftime(yesterday, "%Y-%m-%d")
    create_nightly_billing(yesterday_str)
    records = FactBilling.query.order_by('rate_multiplier').all()
    assert len(records) == 1
    record = records[0]
    assert record.notification_type == LETTER_TYPE
    assert record.bst_date == datetime.date(yesterday)
    assert record.rate == Decimal(2.1)
    assert record.billable_units == 2
    assert record.rate_multiplier == 2.0
Ejemplo n.º 6
0
def test_create_nightly_billing_null_sent_by_sms(notify_db, notify_db_session,
                                                 sample_service,
                                                 sample_template, mocker):
    yesterday = datetime.utcnow() - timedelta(days=1)

    mocker.patch('app.celery.reporting_tasks.get_rate',
                 side_effect=mocker_get_rate)

    sample_notification(
        notify_db,
        notify_db_session,
        created_at=yesterday,
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by=None,
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )

    records = FactBilling.query.all()
    assert len(records) == 0

    create_nightly_billing()
    records = FactBilling.query.all()

    assert len(records) == 1
    record = records[0]
    assert record.aet_date == datetime.date(convert_utc_to_aet(yesterday))
    assert record.rate == Decimal(1.33)
    assert record.billable_units == 1
    assert record.rate_multiplier == 1
    assert record.provider == 'unknown'
Ejemplo n.º 7
0
def test_create_nightly_billing_sms_rate_multiplier(
        notify_db,
        notify_db_session,
        sample_service,
        sample_template,
        mocker,
        second_rate,
        records_num,
        billable_units,
        multiplier):

    yesterday = datetime.utcnow() - timedelta(days=1)

    mocker.patch('app.celery.reporting_tasks.get_rate', side_effect=mocker_get_rate)

    # These are sms notifications
    sample_notification(
        notify_db,
        notify_db_session,
        created_at=yesterday,
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by='mmg',
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )
    sample_notification(
        notify_db,
        notify_db_session,
        created_at=yesterday,
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by='mmg',
        international=False,
        rate_multiplier=second_rate,
        billable_units=1,
    )

    records = FactBilling.query.all()
    assert len(records) == 0

    create_nightly_billing()
    records = FactBilling.query.order_by('rate_multiplier').all()
    assert len(records) == records_num

    for i, record in enumerate(records):
        assert record.aet_date == datetime.date(convert_utc_to_aet(yesterday))
        assert record.rate == Decimal(1.33)
        assert record.billable_units == billable_units
        assert record.rate_multiplier == multiplier[i]
Ejemplo n.º 8
0
def test_create_nightly_billing_use_aet(
        notify_db,
        notify_db_session,
        sample_service,
        sample_template,
        mocker):

    mocker.patch('app.celery.reporting_tasks.get_rate', side_effect=mocker_get_rate)

    sample_notification(
        notify_db,
        notify_db_session,
        created_at=datetime(2018, 10, 6, 14, 30),  # 07/10/2018 00:30:00 AEDT
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by=None,
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )

    sample_notification(
        notify_db,
        notify_db_session,
        created_at=datetime(2018, 10, 7, 13, 30),  # 08/10/2018 00:30:00 AEDT
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by=None,
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )

    notifications = Notification.query.order_by(Notification.created_at).all()
    assert len(notifications) == 2
    records = FactBilling.query.all()
    assert len(records) == 0

    create_nightly_billing()
    records = FactBilling.query.order_by(FactBilling.aet_date).all()

    assert len(records) == 2
    # The first record's aet_date is 06/10/2018. This is because the current
    # time is 2018-10-09T13:30:00 UTC, and 3 days earlier than that is
    # 2018-10-06T13:30:00 UTC which is 06/10/2018 23:30:00 AEST. This falls
    # outside of daylight savings time and so the aet_date is the 6th, not the
    # 7th.
    assert records[0].aet_date == date(2018, 10, 6)
    assert records[-1].aet_date == date(2018, 10, 8)
Ejemplo n.º 9
0
def test_create_nightly_billing_different_sent_by(notify_db, notify_db_session,
                                                  sample_service,
                                                  sample_template,
                                                  sample_email_template,
                                                  mocker):
    yesterday = datetime.now() - timedelta(days=1)

    mocker.patch('app.dao.fact_billing_dao.get_rate',
                 side_effect=mocker_get_rate)

    # These are sms notifications
    sample_notification(
        notify_db,
        notify_db_session,
        created_at=yesterday,
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by='mmg',
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )
    sample_notification(
        notify_db,
        notify_db_session,
        created_at=yesterday,
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by='firetext',
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )

    records = FactBilling.query.all()
    assert len(records) == 0

    # Celery expects the arguments to be a string or primitive type.
    yesterday_str = datetime.strftime(yesterday, "%Y-%m-%d")
    create_nightly_billing(yesterday_str)
    records = FactBilling.query.order_by('rate_multiplier').all()

    assert len(records) == 2
    for i, record in enumerate(records):
        assert record.bst_date == datetime.date(yesterday)
        assert record.rate == Decimal(1.33)
        assert record.billable_units == 1
        assert record.rate_multiplier == 1.0
Ejemplo n.º 10
0
def test_create_nightly_billing_different_templates(
        notify_db, notify_db_session, sample_service, sample_template,
        sample_email_template, mocker):
    yesterday = datetime.utcnow() - timedelta(days=1)

    mocker.patch('app.celery.reporting_tasks.get_rate',
                 side_effect=mocker_get_rate)

    sample_notification(
        notify_db,
        notify_db_session,
        created_at=yesterday,
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by='twilio',
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )
    sample_notification(
        notify_db,
        notify_db_session,
        created_at=yesterday,
        service=sample_service,
        template=sample_email_template,
        status='delivered',
        sent_by='ses',
        international=False,
        rate_multiplier=0,
        billable_units=0,
    )

    records = FactBilling.query.all()
    assert len(records) == 0

    create_nightly_billing()
    records = FactBilling.query.order_by('rate_multiplier').all()

    assert len(records) == 2
    multiplier = [0, 1]
    billable_units = [0, 1]
    rate = [0, Decimal(1.33)]
    for i, record in enumerate(records):
        assert record.aet_date == datetime.date(convert_utc_to_aet(yesterday))
        assert record.rate == rate[i]
        assert record.billable_units == billable_units[i]
        assert record.rate_multiplier == multiplier[i]
Ejemplo n.º 11
0
def test_create_nightly_billing_use_BST(notify_db, notify_db_session,
                                        sample_service, sample_template,
                                        mocker):

    mocker.patch('app.dao.fact_billing_dao.get_rate',
                 side_effect=mocker_get_rate)

    sample_notification(
        notify_db,
        notify_db_session,
        created_at=datetime(2018, 3, 25, 12, 0),
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by=None,
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )

    sample_notification(
        notify_db,
        notify_db_session,
        created_at=datetime(2018, 3, 25, 23, 5),
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by=None,
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )

    notifications = Notification.query.order_by(Notification.created_at).all()
    assert len(notifications) == 2
    records = FactBilling.query.all()
    assert len(records) == 0

    create_nightly_billing()
    records = FactBilling.query.order_by(FactBilling.bst_date).all()

    assert len(records) == 2
    assert records[0].bst_date == date(2018, 3, 25)
    assert records[-1].bst_date == date(2018, 3, 26)
Ejemplo n.º 12
0
def test_create_nightly_billing_null_sent_by_sms(notify_db, notify_db_session,
                                                 sample_service,
                                                 sample_template, mocker):
    yesterday = datetime.now() - timedelta(days=1)

    mocker.patch('app.dao.fact_billing_dao.get_rate',
                 side_effect=mocker_get_rate)

    sample_notification(
        notify_db,
        notify_db_session,
        created_at=yesterday,
        service=sample_service,
        template=sample_template,
        status='delivered',
        sent_by=None,
        international=False,
        rate_multiplier=1.0,
        billable_units=1,
    )

    records = FactBilling.query.all()
    assert len(records) == 0

    # Celery expects the arguments to be a string or primitive type.
    yesterday_str = datetime.strftime(yesterday, "%Y-%m-%d")
    create_nightly_billing(yesterday_str)
    records = FactBilling.query.all()

    assert len(records) == 1
    record = records[0]
    assert record.bst_date == datetime.date(yesterday)
    assert record.rate == Decimal(1.33)
    assert record.billable_units == 1
    assert record.rate_multiplier == 1
    assert record.provider == 'unknown'