mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
Removed unused endpoint and dao methods.
This commit is contained in:
@@ -1,34 +1,20 @@
|
||||
import pytest
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
from freezegun import freeze_time
|
||||
|
||||
from flask import current_app
|
||||
from freezegun import freeze_time
|
||||
|
||||
from app.dao.date_util import get_financial_year
|
||||
from app.dao.notification_usage_dao import (
|
||||
get_rates_for_daterange,
|
||||
get_yearly_billing_data,
|
||||
get_billing_data_for_month,
|
||||
get_monthly_billing_data,
|
||||
get_total_billable_units_for_sent_sms_notifications_in_date_range,
|
||||
discover_rate_bounds_for_billing_query
|
||||
get_monthly_billing_data
|
||||
)
|
||||
from app.models import (
|
||||
Rate,
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
||||
NOTIFICATION_STATUS_TYPES_NON_BILLABLE,
|
||||
SMS_TYPE,
|
||||
)
|
||||
from tests.app.conftest import (
|
||||
sample_notification,
|
||||
sample_email_template,
|
||||
sample_letter_template,
|
||||
sample_service
|
||||
)
|
||||
from tests.app.db import create_notification, create_rate
|
||||
from tests.conftest import set_config
|
||||
|
||||
|
||||
def test_get_rates_for_daterange(notify_db, notify_db_session):
|
||||
@@ -285,452 +271,6 @@ def set_up_rate(notify_db, start_date, value):
|
||||
notify_db.session.add(rate)
|
||||
|
||||
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_returns_total_billable_units_for_sms_notifications(notify_db, notify_db_session, sample_service):
|
||||
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 0.016)
|
||||
|
||||
sample_notification(
|
||||
notify_db, notify_db_session, service=sample_service, billable_units=1, status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db, notify_db_session, service=sample_service, billable_units=2, status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db, notify_db_session, service=sample_service, billable_units=3, status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db, notify_db_session, service=sample_service, billable_units=4, status=NOTIFICATION_DELIVERED)
|
||||
|
||||
start = datetime.utcnow() - timedelta(minutes=10)
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||
start, end, sample_service.id)[0] == 10
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||
start, end, sample_service.id)[1] == 0.16
|
||||
|
||||
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notifications(
|
||||
notify_db, notify_db_session, sample_service
|
||||
):
|
||||
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||
|
||||
sample_notification(
|
||||
notify_db, notify_db_session, service=sample_service, rate_multiplier=1.0, status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db, notify_db_session, service=sample_service, rate_multiplier=2.0, status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db, notify_db_session, service=sample_service, rate_multiplier=5.0, status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db, notify_db_session, service=sample_service, rate_multiplier=10.0, status=NOTIFICATION_DELIVERED)
|
||||
|
||||
start = datetime.utcnow() - timedelta(minutes=10)
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[0] == 18
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 45
|
||||
|
||||
|
||||
def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notifications_for_several_rates(
|
||||
notify_db, notify_db_session, sample_service
|
||||
):
|
||||
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2)
|
||||
set_up_rate(notify_db, datetime(2016, 10, 1), 4)
|
||||
set_up_rate(notify_db, datetime(2017, 1, 1), 6)
|
||||
|
||||
eligble_rate_1 = datetime(2016, 2, 1)
|
||||
eligble_rate_2 = datetime(2016, 11, 1)
|
||||
eligble_rate_3 = datetime(2017, 2, 1)
|
||||
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
rate_multiplier=1.0,
|
||||
status=NOTIFICATION_DELIVERED,
|
||||
created_at=eligble_rate_1)
|
||||
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
rate_multiplier=2.0,
|
||||
status=NOTIFICATION_DELIVERED,
|
||||
created_at=eligble_rate_2)
|
||||
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
rate_multiplier=5.0,
|
||||
status=NOTIFICATION_DELIVERED,
|
||||
created_at=eligble_rate_3)
|
||||
|
||||
start = datetime(2016, 1, 1)
|
||||
end = datetime(2018, 1, 1)
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[0] == 8
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 40
|
||||
|
||||
|
||||
def test_returns_total_billable_units_for_sms_notifications_for_several_rates_where_dates_match_rate_boundary(
|
||||
notify_db, notify_db_session, sample_service
|
||||
):
|
||||
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2)
|
||||
set_up_rate(notify_db, datetime(2016, 10, 1), 4)
|
||||
set_up_rate(notify_db, datetime(2017, 1, 1), 6)
|
||||
|
||||
eligble_rate_1_start = datetime(2016, 1, 1, 0, 0, 0, 0)
|
||||
eligble_rate_1_end = datetime(2016, 9, 30, 23, 59, 59, 999)
|
||||
eligble_rate_2_start = datetime(2016, 10, 1, 0, 0, 0, 0)
|
||||
eligble_rate_2_end = datetime(2016, 12, 31, 23, 59, 59, 999)
|
||||
eligble_rate_3_start = datetime(2017, 1, 1, 0, 0, 0, 0)
|
||||
eligble_rate_3_whenever = datetime(2017, 12, 12, 0, 0, 0, 0)
|
||||
|
||||
def make_notification(created_at):
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
rate_multiplier=1.0,
|
||||
status=NOTIFICATION_DELIVERED,
|
||||
created_at=created_at)
|
||||
|
||||
make_notification(eligble_rate_1_start)
|
||||
make_notification(eligble_rate_1_end)
|
||||
make_notification(eligble_rate_2_start)
|
||||
make_notification(eligble_rate_2_end)
|
||||
make_notification(eligble_rate_3_start)
|
||||
make_notification(eligble_rate_3_whenever)
|
||||
|
||||
start = datetime(2016, 1, 1)
|
||||
end = datetime(2018, 1, 1)
|
||||
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||
start, end, sample_service.id)[0] == 6
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||
start, end, sample_service.id)[1] == 24.0
|
||||
|
||||
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_returns_total_billable_units_for_sms_notifications_ignoring_letters_and_emails(
|
||||
notify_db, notify_db_session, sample_service
|
||||
):
|
||||
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||
|
||||
email_template = sample_email_template(notify_db, notify_db_session, service=sample_service)
|
||||
letter_template = sample_letter_template(sample_service)
|
||||
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
billable_units=2,
|
||||
status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template=email_template,
|
||||
service=sample_service,
|
||||
billable_units=2,
|
||||
status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template=letter_template,
|
||||
service=sample_service,
|
||||
billable_units=2,
|
||||
status=NOTIFICATION_DELIVERED
|
||||
)
|
||||
|
||||
start = datetime.utcnow() - timedelta(minutes=10)
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[0] == 2
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 5
|
||||
|
||||
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_returns_total_billable_units_for_sms_notifications_for_only_requested_service(
|
||||
notify_db, notify_db_session
|
||||
):
|
||||
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||
|
||||
service_1 = sample_service(notify_db, notify_db_session, service_name=str(uuid.uuid4()))
|
||||
service_2 = sample_service(notify_db, notify_db_session, service_name=str(uuid.uuid4()))
|
||||
service_3 = sample_service(notify_db, notify_db_session, service_name=str(uuid.uuid4()))
|
||||
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=service_1,
|
||||
billable_units=2,
|
||||
status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=service_2,
|
||||
billable_units=2,
|
||||
status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=service_3,
|
||||
billable_units=2,
|
||||
status=NOTIFICATION_DELIVERED
|
||||
)
|
||||
|
||||
start = datetime.utcnow() - timedelta(minutes=10)
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, service_1.id)[0] == 2
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, service_1.id)[1] == 5
|
||||
|
||||
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_returns_total_billable_units_for_sms_notifications_handling_null_values(
|
||||
notify_db, notify_db_session, sample_service
|
||||
):
|
||||
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
billable_units=2,
|
||||
rate_multiplier=None,
|
||||
status=NOTIFICATION_DELIVERED)
|
||||
|
||||
start = datetime.utcnow() - timedelta(minutes=10)
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[0] == 2
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 5
|
||||
|
||||
|
||||
@pytest.mark.parametrize('billable_units, states', ([
|
||||
(len(NOTIFICATION_STATUS_TYPES_BILLABLE), NOTIFICATION_STATUS_TYPES_BILLABLE),
|
||||
(0, NOTIFICATION_STATUS_TYPES_NON_BILLABLE)
|
||||
]))
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_ignores_non_billable_states_when_returning_billable_units_for_sms_notifications(
|
||||
notify_db, notify_db_session, sample_service, billable_units, states
|
||||
):
|
||||
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||
|
||||
for state in states:
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
billable_units=1,
|
||||
rate_multiplier=None,
|
||||
status=state)
|
||||
|
||||
start = datetime.utcnow() - timedelta(minutes=10)
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||
start, end, sample_service.id
|
||||
)[0] == billable_units
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||
start, end, sample_service.id
|
||||
)[1] == billable_units * 2.5
|
||||
|
||||
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_restricts_to_time_period_when_returning_billable_units_for_sms_notifications(
|
||||
notify_db, notify_db_session, sample_service
|
||||
):
|
||||
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
billable_units=1,
|
||||
rate_multiplier=1.0,
|
||||
created_at=datetime.utcnow() - timedelta(minutes=100),
|
||||
status=NOTIFICATION_DELIVERED)
|
||||
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
billable_units=1,
|
||||
rate_multiplier=1.0,
|
||||
created_at=datetime.utcnow() - timedelta(minutes=5),
|
||||
status=NOTIFICATION_DELIVERED)
|
||||
|
||||
start = datetime.utcnow() - timedelta(minutes=10)
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||
start, end, sample_service.id)[0] == 1
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||
start, end, sample_service.id)[1] == 2.5
|
||||
|
||||
|
||||
def test_returns_zero_if_no_matching_rows_when_returning_billable_units_for_sms_notifications(
|
||||
notify_db, notify_db_session, sample_service
|
||||
):
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||
|
||||
start = datetime.utcnow() - timedelta(minutes=10)
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[0] == 0
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 0.0
|
||||
|
||||
|
||||
def test_should_calculate_rate_boundaries_for_billing_query_for_single_relevant_rate(notify_db, notify_db_session):
|
||||
start_date, end_date = get_financial_year(2016)
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 0.016)
|
||||
rate_boundaries = discover_rate_bounds_for_billing_query(start_date, end_date)
|
||||
assert len(rate_boundaries) == 1
|
||||
assert rate_boundaries[0]['start_date'] == start_date
|
||||
assert rate_boundaries[0]['end_date'] == end_date
|
||||
assert rate_boundaries[0]['rate'] == 0.016
|
||||
|
||||
|
||||
def test_should_calculate_rate_boundaries_for_billing_query_for_two_relevant_rates(notify_db, notify_db_session):
|
||||
start_date, end_date = get_financial_year(2016)
|
||||
|
||||
rate_1_valid_from = datetime(2016, 1, 1)
|
||||
rate_2_valid_from = datetime(2017, 1, 1)
|
||||
|
||||
set_up_rate(notify_db, rate_1_valid_from, 0.02)
|
||||
set_up_rate(notify_db, rate_2_valid_from, 0.04)
|
||||
rate_boundaries = discover_rate_bounds_for_billing_query(start_date, end_date)
|
||||
assert len(rate_boundaries) == 2
|
||||
assert rate_boundaries[0]['start_date'] == start_date
|
||||
assert rate_boundaries[0]['end_date'] == rate_2_valid_from
|
||||
assert rate_boundaries[0]['rate'] == 0.02
|
||||
|
||||
assert rate_boundaries[1]['start_date'] == rate_2_valid_from
|
||||
assert rate_boundaries[1]['end_date'] == end_date
|
||||
assert rate_boundaries[1]['rate'] == 0.04
|
||||
|
||||
|
||||
def test_should_calculate_rate_boundaries_for_billing_query_for_three_relevant_rates(notify_db, notify_db_session):
|
||||
start_date, end_date = get_financial_year(2016)
|
||||
rate_1_valid_from = datetime(2016, 1, 1)
|
||||
rate_2_valid_from = datetime(2017, 1, 1)
|
||||
rate_3_valid_from = datetime(2017, 2, 1)
|
||||
|
||||
set_up_rate(notify_db, rate_1_valid_from, 0.02)
|
||||
set_up_rate(notify_db, rate_2_valid_from, 0.04)
|
||||
set_up_rate(notify_db, rate_3_valid_from, 0.06)
|
||||
rate_boundaries = discover_rate_bounds_for_billing_query(start_date, end_date)
|
||||
assert len(rate_boundaries) == 3
|
||||
|
||||
assert rate_boundaries[0]['start_date'] == start_date
|
||||
assert rate_boundaries[0]['end_date'] == rate_2_valid_from
|
||||
assert rate_boundaries[0]['rate'] == 0.02
|
||||
|
||||
assert rate_boundaries[1]['start_date'] == rate_2_valid_from
|
||||
assert rate_boundaries[1]['end_date'] == rate_3_valid_from
|
||||
assert rate_boundaries[1]['rate'] == 0.04
|
||||
|
||||
assert rate_boundaries[2]['start_date'] == rate_3_valid_from
|
||||
assert rate_boundaries[2]['end_date'] == end_date
|
||||
assert rate_boundaries[2]['rate'] == 0.06
|
||||
|
||||
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_deducts_free_tier_from_bill(
|
||||
notify_db, notify_db_session
|
||||
):
|
||||
start_value = current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
|
||||
try:
|
||||
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 1
|
||||
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||
|
||||
service_1 = sample_service(notify_db, notify_db_session, service_name=str(uuid.uuid4()))
|
||||
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=service_1,
|
||||
billable_units=1,
|
||||
status=NOTIFICATION_DELIVERED)
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=service_1,
|
||||
billable_units=1,
|
||||
status=NOTIFICATION_DELIVERED)
|
||||
|
||||
start = datetime.utcnow() - timedelta(minutes=10)
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, service_1.id)[0] == 2
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, service_1.id)[1] == 2.5
|
||||
finally:
|
||||
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = start_value
|
||||
|
||||
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
@pytest.mark.parametrize(
|
||||
'free_tier, expected_cost',
|
||||
[(0, 24.0), (1, 22.0), (2, 20.0), (3, 16.0), (4, 12.0), (5, 6.0), (6, 0.0)]
|
||||
)
|
||||
def test_deducts_free_tier_from_bill_across_rate_boundaries(
|
||||
notify_db, notify_db_session, sample_service, free_tier, expected_cost
|
||||
):
|
||||
start_value = current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
|
||||
try:
|
||||
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = free_tier
|
||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2)
|
||||
set_up_rate(notify_db, datetime(2016, 10, 1), 4)
|
||||
set_up_rate(notify_db, datetime(2017, 1, 1), 6)
|
||||
|
||||
eligble_rate_1_start = datetime(2016, 1, 1, 0, 0, 0, 0)
|
||||
eligble_rate_1_end = datetime(2016, 9, 30, 23, 59, 59, 999)
|
||||
eligble_rate_2_start = datetime(2016, 10, 1, 0, 0, 0, 0)
|
||||
eligble_rate_2_end = datetime(2016, 12, 31, 23, 59, 59, 999)
|
||||
eligble_rate_3_start = datetime(2017, 1, 1, 0, 0, 0, 0)
|
||||
eligble_rate_3_whenever = datetime(2017, 12, 12, 0, 0, 0, 0)
|
||||
|
||||
def make_notification(created_at):
|
||||
sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
rate_multiplier=1.0,
|
||||
status=NOTIFICATION_DELIVERED,
|
||||
created_at=created_at)
|
||||
|
||||
make_notification(eligble_rate_1_start)
|
||||
make_notification(eligble_rate_1_end)
|
||||
make_notification(eligble_rate_2_start)
|
||||
make_notification(eligble_rate_2_end)
|
||||
make_notification(eligble_rate_3_start)
|
||||
make_notification(eligble_rate_3_whenever)
|
||||
|
||||
start = datetime(2016, 1, 1)
|
||||
end = datetime(2018, 1, 1)
|
||||
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[0] == 6
|
||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||
start, end, sample_service.id
|
||||
)[1] == expected_cost
|
||||
finally:
|
||||
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = start_value
|
||||
|
||||
|
||||
def test_get_yearly_billing_data_for_start_date_before_rate_returns_empty(
|
||||
sample_template
|
||||
):
|
||||
|
||||
@@ -1970,92 +1970,6 @@ def test_update_service_does_not_call_send_notification_when_restricted_not_chan
|
||||
assert not send_notification_mock.called
|
||||
|
||||
|
||||
def test_get_yearly_billing_usage_count_returns_400_if_missing_year(client, sample_service):
|
||||
response = client.get(
|
||||
'/service/{}/yearly-sms-billable-units'.format(sample_service.id),
|
||||
headers=[create_authorization_header()]
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert json.loads(response.get_data(as_text=True)) == {
|
||||
'message': 'No valid year provided', 'result': 'error'
|
||||
}
|
||||
|
||||
|
||||
def test_get_yearly_billing_usage_count_returns_400_if_invalid_year(client, sample_service, mocker):
|
||||
redis_get_mock = mocker.patch('app.service.rest.redis_store.get_all_from_hash', return_value=None)
|
||||
redis_set_mock = mocker.patch('app.service.rest.redis_store.set_hash_and_expire')
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/yearly-sms-billable-units?year=HAHAHAHAH'.format(sample_service.id),
|
||||
headers=[create_authorization_header()]
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert json.loads(response.get_data(as_text=True)) == {
|
||||
'message': 'No valid year provided', 'result': 'error'
|
||||
}
|
||||
redis_get_mock.assert_called_once_with("{}-sms_billable_units".format(str(sample_service.id)))
|
||||
redis_set_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_get_yearly_billing_usage_count_returns_200_if_year_provided(client, sample_service, mocker):
|
||||
redis_get_mock = mocker.patch('app.service.rest.redis_store.get_all_from_hash', return_value=None)
|
||||
redis_set_mock = mocker.patch('app.service.rest.redis_store.set_hash_and_expire')
|
||||
|
||||
start = datetime.utcnow()
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
mock_query = mocker.patch(
|
||||
'app.service.rest.get_total_billable_units_for_sent_sms_notifications_in_date_range', return_value=(100, 200.0)
|
||||
)
|
||||
mock_year = mocker.patch('app.service.rest.get_financial_year', return_value=(start, end))
|
||||
response = client.get(
|
||||
'/service/{}/yearly-sms-billable-units?year=2016'.format(sample_service.id),
|
||||
headers=[create_authorization_header()]
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.get_data(as_text=True)) == {
|
||||
'billable_sms_units': 100,
|
||||
'total_cost': 200.0
|
||||
}
|
||||
mock_query.assert_called_once_with(start, end, sample_service.id)
|
||||
mock_year.assert_called_once_with(2016)
|
||||
redis_get_mock.assert_called_once_with("{}-sms_billable_units".format(str(sample_service.id)))
|
||||
redis_set_mock.assert_called_once_with(
|
||||
"{}-sms_billable_units".format(str(sample_service.id)),
|
||||
{'billable_units': 100, 'total_cost': 200.0},
|
||||
expire_in_seconds=60
|
||||
)
|
||||
|
||||
|
||||
def test_get_yearly_billing_usage_count_returns_from_cache_if_present(client, sample_service, mocker):
|
||||
redis_get_mock = mocker.patch(
|
||||
'app.service.rest.redis_store.get_all_from_hash',
|
||||
return_value={b'total_cost': 100.0, b'billable_units': 50}
|
||||
)
|
||||
redis_set_mock = mocker.patch('app.service.rest.redis_store.set_hash_and_expire')
|
||||
mock_query = mocker.patch(
|
||||
'app.service.rest.get_total_billable_units_for_sent_sms_notifications_in_date_range', return_value=(50, 100.0)
|
||||
)
|
||||
|
||||
start = datetime.utcnow()
|
||||
end = datetime.utcnow() + timedelta(minutes=10)
|
||||
mock_year = mocker.patch('app.service.rest.get_financial_year', return_value=(start, end))
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/yearly-sms-billable-units?year=2016'.format(sample_service.id),
|
||||
headers=[create_authorization_header()]
|
||||
)
|
||||
response.get_data(as_text=True)
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.get_data(as_text=True)) == {
|
||||
'billable_sms_units': 50,
|
||||
'total_cost': 100.0
|
||||
}
|
||||
redis_get_mock.assert_called_once_with("{}-sms_billable_units".format(str(sample_service.id)))
|
||||
mock_year.assert_not_called()
|
||||
mock_query.assert_not_called()
|
||||
redis_set_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_search_for_notification_by_to_field_filters_by_status(client, notify_db, notify_db_session):
|
||||
create_notification = partial(
|
||||
create_sample_notification,
|
||||
|
||||
Reference in New Issue
Block a user