mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-07 11:18:24 -04:00
Merge pull request #1022 from alphagov/remove-nasty-query-from-dashboard
Remove nasty query from dashboard
This commit is contained in:
@@ -249,6 +249,8 @@ class Config(object):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FREE_SMS_TIER_FRAGMENT_COUNT = 250000
|
||||||
|
|
||||||
|
|
||||||
######################
|
######################
|
||||||
# Config overrides ###
|
# Config overrides ###
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
from flask import current_app
|
||||||
from sqlalchemy import Float, Integer
|
from sqlalchemy import Float, Integer
|
||||||
from sqlalchemy import func, case, cast
|
from sqlalchemy import func, case, cast
|
||||||
from sqlalchemy import literal_column
|
from sqlalchemy import literal_column
|
||||||
@@ -11,7 +12,7 @@ from app.models import (NotificationHistory,
|
|||||||
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
||||||
KEY_TYPE_TEST,
|
KEY_TYPE_TEST,
|
||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
EMAIL_TYPE)
|
EMAIL_TYPE, Service)
|
||||||
from app.statsd_decorators import statsd
|
from app.statsd_decorators import statsd
|
||||||
from app.utils import get_london_month_from_utc_column
|
from app.utils import get_london_month_from_utc_column
|
||||||
|
|
||||||
@@ -158,6 +159,8 @@ def rate_multiplier():
|
|||||||
@statsd(namespace="dao")
|
@statsd(namespace="dao")
|
||||||
def get_total_billable_units_for_sent_sms_notifications_in_date_range(start_date, end_date, service_id):
|
def get_total_billable_units_for_sent_sms_notifications_in_date_range(start_date, end_date, service_id):
|
||||||
|
|
||||||
|
free_sms_limit = Service.free_sms_fragment_limit()
|
||||||
|
|
||||||
billable_units = 0
|
billable_units = 0
|
||||||
total_cost = 0.0
|
total_cost = 0.0
|
||||||
|
|
||||||
@@ -176,9 +179,15 @@ def get_total_billable_units_for_sent_sms_notifications_in_date_range(start_date
|
|||||||
)
|
)
|
||||||
billable_units_by_rate_boundry = result.scalar()
|
billable_units_by_rate_boundry = result.scalar()
|
||||||
if billable_units_by_rate_boundry:
|
if billable_units_by_rate_boundry:
|
||||||
billable_units += int(billable_units_by_rate_boundry)
|
int_billable_units_by_rate_boundry = int(billable_units_by_rate_boundry)
|
||||||
total_cost += int(billable_units_by_rate_boundry) * rate_boundary['rate']
|
if billable_units >= free_sms_limit:
|
||||||
|
total_cost += int_billable_units_by_rate_boundry * rate_boundary['rate']
|
||||||
|
elif billable_units + int_billable_units_by_rate_boundry > free_sms_limit:
|
||||||
|
remaining_free_allowance = abs(free_sms_limit - billable_units)
|
||||||
|
total_cost += ((int_billable_units_by_rate_boundry - remaining_free_allowance) * rate_boundary['rate'])
|
||||||
|
else:
|
||||||
|
total_cost += 0
|
||||||
|
billable_units += int_billable_units_by_rate_boundry
|
||||||
return billable_units, total_cost
|
return billable_units, total_cost
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -217,6 +217,10 @@ class Service(db.Model, Versioned):
|
|||||||
self.can_send_letters = LETTER_TYPE in [p.permission for p in self.permissions]
|
self.can_send_letters = LETTER_TYPE in [p.permission for p in self.permissions]
|
||||||
self.can_send_international_sms = INTERNATIONAL_SMS_TYPE in [p.permission for p in self.permissions]
|
self.can_send_international_sms = INTERNATIONAL_SMS_TYPE in [p.permission for p in self.permissions]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def free_sms_fragment_limit():
|
||||||
|
return current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT']
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, data):
|
def from_json(cls, data):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -25,9 +25,8 @@ from notifications_utils.recipients import (
|
|||||||
|
|
||||||
from app import ma
|
from app import ma
|
||||||
from app import models
|
from app import models
|
||||||
from app.models import ServicePermission, INTERNATIONAL_SMS_TYPE, SMS_TYPE, LETTER_TYPE, EMAIL_TYPE
|
from app.models import ServicePermission, INTERNATIONAL_SMS_TYPE, LETTER_TYPE
|
||||||
from app.dao.permissions_dao import permission_dao
|
from app.dao.permissions_dao import permission_dao
|
||||||
from app.dao.service_permissions_dao import dao_fetch_service_permissions
|
|
||||||
from app.utils import get_template_instance
|
from app.utils import get_template_instance
|
||||||
|
|
||||||
|
|
||||||
@@ -176,6 +175,7 @@ class ProviderDetailsHistorySchema(BaseSchema):
|
|||||||
|
|
||||||
class ServiceSchema(BaseSchema):
|
class ServiceSchema(BaseSchema):
|
||||||
|
|
||||||
|
free_sms_fragment_limit = fields.Method(method_name='get_free_sms_fragment_limit')
|
||||||
created_by = field_for(models.Service, 'created_by', required=True)
|
created_by = field_for(models.Service, 'created_by', required=True)
|
||||||
organisation = field_for(models.Service, 'organisation')
|
organisation = field_for(models.Service, 'organisation')
|
||||||
branding = field_for(models.Service, 'branding')
|
branding = field_for(models.Service, 'branding')
|
||||||
@@ -183,11 +183,15 @@ class ServiceSchema(BaseSchema):
|
|||||||
permissions = fields.Method("service_permissions")
|
permissions = fields.Method("service_permissions")
|
||||||
override_flag = False
|
override_flag = False
|
||||||
|
|
||||||
|
def get_free_sms_fragment_limit(selfs, service):
|
||||||
|
return service.free_sms_fragment_limit()
|
||||||
|
|
||||||
def service_permissions(self, service):
|
def service_permissions(self, service):
|
||||||
return [p.permission for p in service.permissions]
|
return [p.permission for p in service.permissions]
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Service
|
model = models.Service
|
||||||
|
dump_only = ['free_sms_fragment_limit']
|
||||||
exclude = (
|
exclude = (
|
||||||
'updated_at',
|
'updated_at',
|
||||||
'created_at',
|
'created_at',
|
||||||
@@ -256,6 +260,11 @@ class ServiceSchema(BaseSchema):
|
|||||||
class DetailedServiceSchema(BaseSchema):
|
class DetailedServiceSchema(BaseSchema):
|
||||||
statistics = fields.Dict()
|
statistics = fields.Dict()
|
||||||
|
|
||||||
|
free_sms_fragment_limit = fields.Method(method_name='get_free_sms_fragment_limit')
|
||||||
|
|
||||||
|
def get_free_sms_fragment_limit(selfs, service):
|
||||||
|
return service.free_sms_fragment_limit()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Service
|
model = models.Service
|
||||||
exclude = (
|
exclude = (
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import uuid
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from flask import current_app
|
||||||
|
|
||||||
from app.dao.date_util import get_financial_year
|
from app.dao.date_util import get_financial_year
|
||||||
from app.dao.notification_usage_dao import (
|
from app.dao.notification_usage_dao import (
|
||||||
@@ -15,12 +16,13 @@ from app.models import (
|
|||||||
Rate,
|
Rate,
|
||||||
NOTIFICATION_DELIVERED,
|
NOTIFICATION_DELIVERED,
|
||||||
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
||||||
NOTIFICATION_STATUS_TYPES_NON_BILLABLE,
|
NOTIFICATION_STATUS_TYPES_NON_BILLABLE)
|
||||||
Notification)
|
|
||||||
from tests.app.conftest import sample_notification, sample_email_template, sample_letter_template, sample_service
|
from tests.app.conftest import sample_notification, sample_email_template, sample_letter_template, sample_service
|
||||||
from tests.app.db import create_notification
|
from tests.app.db import create_notification
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
|
|
||||||
|
from tests.conftest import set_config
|
||||||
|
|
||||||
|
|
||||||
def test_get_rates_for_year(notify_db, notify_db_session):
|
def test_get_rates_for_year(notify_db, notify_db_session):
|
||||||
set_up_rate(notify_db, datetime(2016, 5, 18), 0.016)
|
set_up_rate(notify_db, datetime(2016, 5, 18), 0.016)
|
||||||
@@ -266,218 +268,235 @@ def set_up_rate(notify_db, start_date, value):
|
|||||||
|
|
||||||
@freeze_time("2016-01-10 12:00:00.000000")
|
@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):
|
def test_returns_total_billable_units_for_sms_notifications(notify_db, notify_db_session, sample_service):
|
||||||
set_up_rate(notify_db, datetime(2016, 1, 1), 0.016)
|
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||||
|
|
||||||
sample_notification(
|
set_up_rate(notify_db, datetime(2016, 1, 1), 0.016)
|
||||||
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)
|
sample_notification(
|
||||||
end = datetime.utcnow() + timedelta(minutes=10)
|
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)
|
||||||
|
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[0] == 10
|
start = datetime.utcnow() - timedelta(minutes=10)
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 0.16
|
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")
|
@freeze_time("2016-01-10 12:00:00.000000")
|
||||||
def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notifications(
|
def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notifications(
|
||||||
notify_db, notify_db_session, sample_service
|
notify_db, notify_db_session, sample_service
|
||||||
):
|
):
|
||||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||||
|
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||||
|
|
||||||
sample_notification(
|
sample_notification(
|
||||||
notify_db, notify_db_session, service=sample_service, rate_multiplier=1.0, status=NOTIFICATION_DELIVERED)
|
notify_db, notify_db_session, service=sample_service, rate_multiplier=1.0, status=NOTIFICATION_DELIVERED)
|
||||||
sample_notification(
|
sample_notification(
|
||||||
notify_db, notify_db_session, service=sample_service, rate_multiplier=2.0, status=NOTIFICATION_DELIVERED)
|
notify_db, notify_db_session, service=sample_service, rate_multiplier=2.0, status=NOTIFICATION_DELIVERED)
|
||||||
sample_notification(
|
sample_notification(
|
||||||
notify_db, notify_db_session, service=sample_service, rate_multiplier=5.0, status=NOTIFICATION_DELIVERED)
|
notify_db, notify_db_session, service=sample_service, rate_multiplier=5.0, status=NOTIFICATION_DELIVERED)
|
||||||
sample_notification(
|
sample_notification(
|
||||||
notify_db, notify_db_session, service=sample_service, rate_multiplier=10.0, status=NOTIFICATION_DELIVERED)
|
notify_db, notify_db_session, service=sample_service, rate_multiplier=10.0, status=NOTIFICATION_DELIVERED)
|
||||||
|
|
||||||
start = datetime.utcnow() - timedelta(minutes=10)
|
start = datetime.utcnow() - timedelta(minutes=10)
|
||||||
end = 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)[0] == 18
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 45
|
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(
|
def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notifications_for_several_rates(
|
||||||
notify_db, notify_db_session, sample_service
|
notify_db, notify_db_session, sample_service
|
||||||
):
|
):
|
||||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2)
|
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||||
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)
|
set_up_rate(notify_db, datetime(2016, 1, 1), 2)
|
||||||
eligble_rate_2 = datetime(2016, 11, 1)
|
set_up_rate(notify_db, datetime(2016, 10, 1), 4)
|
||||||
eligble_rate_3 = datetime(2017, 2, 1)
|
set_up_rate(notify_db, datetime(2017, 1, 1), 6)
|
||||||
|
|
||||||
sample_notification(
|
eligble_rate_1 = datetime(2016, 2, 1)
|
||||||
notify_db,
|
eligble_rate_2 = datetime(2016, 11, 1)
|
||||||
notify_db_session,
|
eligble_rate_3 = datetime(2017, 2, 1)
|
||||||
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
|
|
||||||
):
|
|
||||||
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(
|
sample_notification(
|
||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
service=sample_service,
|
service=sample_service,
|
||||||
rate_multiplier=1.0,
|
rate_multiplier=1.0,
|
||||||
status=NOTIFICATION_DELIVERED,
|
status=NOTIFICATION_DELIVERED,
|
||||||
created_at=created_at)
|
created_at=eligble_rate_1)
|
||||||
|
|
||||||
make_notification(eligble_rate_1_start)
|
sample_notification(
|
||||||
make_notification(eligble_rate_1_end)
|
notify_db,
|
||||||
make_notification(eligble_rate_2_start)
|
notify_db_session,
|
||||||
make_notification(eligble_rate_2_end)
|
service=sample_service,
|
||||||
make_notification(eligble_rate_3_start)
|
rate_multiplier=2.0,
|
||||||
make_notification(eligble_rate_3_whenever)
|
status=NOTIFICATION_DELIVERED,
|
||||||
|
created_at=eligble_rate_2)
|
||||||
|
|
||||||
start = datetime(2016, 1, 1)
|
sample_notification(
|
||||||
end = datetime(2018, 1, 1)
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service=sample_service,
|
||||||
|
rate_multiplier=5.0,
|
||||||
|
status=NOTIFICATION_DELIVERED,
|
||||||
|
created_at=eligble_rate_3)
|
||||||
|
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[0] == 6
|
start = datetime(2016, 1, 1)
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 24.0
|
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")
|
@freeze_time("2016-01-10 12:00:00.000000")
|
||||||
def test_returns_total_billable_units_for_sms_notifications_ignoring_letters_and_emails(
|
def test_returns_total_billable_units_for_sms_notifications_ignoring_letters_and_emails(
|
||||||
notify_db, notify_db_session, sample_service
|
notify_db, notify_db_session, sample_service
|
||||||
):
|
):
|
||||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||||
|
|
||||||
email_template = sample_email_template(notify_db, notify_db_session, service=sample_service)
|
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||||
letter_template = sample_letter_template(sample_service)
|
|
||||||
|
|
||||||
sample_notification(
|
email_template = sample_email_template(notify_db, notify_db_session, service=sample_service)
|
||||||
notify_db,
|
letter_template = sample_letter_template(sample_service)
|
||||||
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)
|
sample_notification(
|
||||||
end = datetime.utcnow() + timedelta(minutes=10)
|
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
|
||||||
|
)
|
||||||
|
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[0] == 2
|
start = datetime.utcnow() - timedelta(minutes=10)
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 5
|
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")
|
@freeze_time("2016-01-10 12:00:00.000000")
|
||||||
def test_returns_total_billable_units_for_sms_notifications_for_only_requested_service(
|
def test_returns_total_billable_units_for_sms_notifications_for_only_requested_service(
|
||||||
notify_db, notify_db_session
|
notify_db, notify_db_session
|
||||||
):
|
):
|
||||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||||
|
|
||||||
service_1 = sample_service(notify_db, notify_db_session, service_name=str(uuid.uuid4()))
|
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||||
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(
|
service_1 = sample_service(notify_db, notify_db_session, service_name=str(uuid.uuid4()))
|
||||||
notify_db,
|
service_2 = sample_service(notify_db, notify_db_session, service_name=str(uuid.uuid4()))
|
||||||
notify_db_session,
|
service_3 = sample_service(notify_db, notify_db_session, service_name=str(uuid.uuid4()))
|
||||||
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)
|
sample_notification(
|
||||||
end = datetime.utcnow() + timedelta(minutes=10)
|
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
|
||||||
|
)
|
||||||
|
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, service_1.id)[0] == 2
|
start = datetime.utcnow() - timedelta(minutes=10)
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, service_1.id)[1] == 5
|
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")
|
@freeze_time("2016-01-10 12:00:00.000000")
|
||||||
def test_returns_total_billable_units_for_sms_notifications_handling_null_values(
|
def test_returns_total_billable_units_for_sms_notifications_handling_null_values(
|
||||||
notify_db, notify_db_session, sample_service
|
notify_db, notify_db_session, sample_service
|
||||||
):
|
):
|
||||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||||
|
|
||||||
sample_notification(
|
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
service=sample_service,
|
|
||||||
billable_units=2,
|
|
||||||
rate_multiplier=None,
|
|
||||||
status=NOTIFICATION_DELIVERED)
|
|
||||||
|
|
||||||
start = datetime.utcnow() - timedelta(minutes=10)
|
sample_notification(
|
||||||
end = datetime.utcnow() + timedelta(minutes=10)
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service=sample_service,
|
||||||
|
billable_units=2,
|
||||||
|
rate_multiplier=None,
|
||||||
|
status=NOTIFICATION_DELIVERED)
|
||||||
|
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[0] == 2
|
start = datetime.utcnow() - timedelta(minutes=10)
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 5
|
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', ([
|
@pytest.mark.parametrize('billable_units, states', ([
|
||||||
@@ -488,57 +507,61 @@ def test_returns_total_billable_units_for_sms_notifications_handling_null_values
|
|||||||
def test_ignores_non_billable_states_when_returning_billable_units_for_sms_notifications(
|
def test_ignores_non_billable_states_when_returning_billable_units_for_sms_notifications(
|
||||||
notify_db, notify_db_session, sample_service, billable_units, states
|
notify_db, notify_db_session, sample_service, billable_units, states
|
||||||
):
|
):
|
||||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
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:
|
for state in states:
|
||||||
sample_notification(
|
sample_notification(
|
||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
service=sample_service,
|
service=sample_service,
|
||||||
billable_units=1,
|
billable_units=1,
|
||||||
rate_multiplier=None,
|
rate_multiplier=None,
|
||||||
status=state)
|
status=state)
|
||||||
|
|
||||||
start = datetime.utcnow() - timedelta(minutes=10)
|
start = datetime.utcnow() - timedelta(minutes=10)
|
||||||
end = datetime.utcnow() + timedelta(minutes=10)
|
end = datetime.utcnow() + timedelta(minutes=10)
|
||||||
|
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||||
start, end, sample_service.id
|
start, end, sample_service.id
|
||||||
)[0] == billable_units
|
)[0] == billable_units
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||||
start, end, sample_service.id
|
start, end, sample_service.id
|
||||||
)[1] == billable_units * 2.5
|
)[1] == billable_units * 2.5
|
||||||
|
|
||||||
|
|
||||||
@freeze_time("2016-01-10 12:00:00.000000")
|
@freeze_time("2016-01-10 12:00:00.000000")
|
||||||
def test_restricts_to_time_period_when_returning_billable_units_for_sms_notifications(
|
def test_restricts_to_time_period_when_returning_billable_units_for_sms_notifications(
|
||||||
notify_db, notify_db_session, sample_service
|
notify_db, notify_db_session, sample_service
|
||||||
):
|
):
|
||||||
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
|
||||||
|
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
|
||||||
|
|
||||||
sample_notification(
|
sample_notification(
|
||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
service=sample_service,
|
service=sample_service,
|
||||||
billable_units=1,
|
billable_units=1,
|
||||||
rate_multiplier=1.0,
|
rate_multiplier=1.0,
|
||||||
created_at=datetime.utcnow() - timedelta(minutes=100),
|
created_at=datetime.utcnow() - timedelta(minutes=100),
|
||||||
status=NOTIFICATION_DELIVERED)
|
status=NOTIFICATION_DELIVERED)
|
||||||
|
|
||||||
sample_notification(
|
sample_notification(
|
||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
service=sample_service,
|
service=sample_service,
|
||||||
billable_units=1,
|
billable_units=1,
|
||||||
rate_multiplier=1.0,
|
rate_multiplier=1.0,
|
||||||
created_at=datetime.utcnow() - timedelta(minutes=5),
|
created_at=datetime.utcnow() - timedelta(minutes=5),
|
||||||
status=NOTIFICATION_DELIVERED)
|
status=NOTIFICATION_DELIVERED)
|
||||||
|
|
||||||
start = datetime.utcnow() - timedelta(minutes=10)
|
start = datetime.utcnow() - timedelta(minutes=10)
|
||||||
end = 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(
|
||||||
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 2.5
|
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(
|
def test_returns_zero_if_no_matching_rows_when_returning_billable_units_for_sms_notifications(
|
||||||
@@ -604,3 +627,86 @@ def test_should_calculate_rate_boundaries_for_billing_query_for_three_relevant_r
|
|||||||
assert rate_boundaries[2]['start_date'] == rate_3_valid_from
|
assert rate_boundaries[2]['start_date'] == rate_3_valid_from
|
||||||
assert rate_boundaries[2]['end_date'] == end_date
|
assert rate_boundaries[2]['end_date'] == end_date
|
||||||
assert rate_boundaries[2]['rate'] == 0.06
|
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
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ from app.models import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from tests.app.db import create_user
|
from tests.app.db import create_user
|
||||||
|
from tests.conftest import set_config_values
|
||||||
|
|
||||||
|
|
||||||
def test_get_service_list(client, service_factory):
|
def test_get_service_list(client, service_factory):
|
||||||
@@ -147,7 +148,32 @@ def test_get_service_by_id(client, sample_service):
|
|||||||
assert json_resp['data']['sms_sender'] == current_app.config['FROM_NUMBER']
|
assert json_resp['data']['sms_sender'] == current_app.config['FROM_NUMBER']
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_service_by_id_returns_free_sms_limit(client, sample_service):
|
||||||
|
|
||||||
|
auth_header = create_authorization_header()
|
||||||
|
resp = client.get(
|
||||||
|
'/service/{}'.format(sample_service.id),
|
||||||
|
headers=[auth_header]
|
||||||
|
)
|
||||||
|
assert resp.status_code == 200
|
||||||
|
json_resp = json.loads(resp.get_data(as_text=True))
|
||||||
|
assert json_resp['data']['free_sms_fragment_limit'] == 250000
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_detailed_service_by_id_returns_free_sms_limit(client, sample_service):
|
||||||
|
|
||||||
|
auth_header = create_authorization_header()
|
||||||
|
resp = client.get(
|
||||||
|
'/service/{}?detailed=True'.format(sample_service.id),
|
||||||
|
headers=[auth_header]
|
||||||
|
)
|
||||||
|
assert resp.status_code == 200
|
||||||
|
json_resp = json.loads(resp.get_data(as_text=True))
|
||||||
|
assert json_resp['data']['free_sms_fragment_limit'] == 250000
|
||||||
|
|
||||||
|
|
||||||
def test_get_service_list_has_default_permissions(client, service_factory):
|
def test_get_service_list_has_default_permissions(client, service_factory):
|
||||||
|
service_factory.get('one')
|
||||||
service_factory.get('one')
|
service_factory.get('one')
|
||||||
service_factory.get('two')
|
service_factory.get('two')
|
||||||
service_factory.get('three')
|
service_factory.get('three')
|
||||||
@@ -2032,7 +2058,7 @@ def test_get_yearly_billing_usage_count_returns_from_cache_if_present(client, sa
|
|||||||
'/service/{}/yearly-sms-billable-units?year=2016'.format(sample_service.id),
|
'/service/{}/yearly-sms-billable-units?year=2016'.format(sample_service.id),
|
||||||
headers=[create_authorization_header()]
|
headers=[create_authorization_header()]
|
||||||
)
|
)
|
||||||
print(response.get_data(as_text=True))
|
response.get_data(as_text=True)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert json.loads(response.get_data(as_text=True)) == {
|
assert json.loads(response.get_data(as_text=True)) == {
|
||||||
'billable_sms_units': 50,
|
'billable_sms_units': 50,
|
||||||
|
|||||||
Reference in New Issue
Block a user