- Fix up free tier on the service object, use it only on dump not create/update in marshmallow

- Ensure tests leave config as was after a test run that alters free tier quantity
This commit is contained in:
Martyn Inglis
2017-06-07 09:58:57 +01:00
parent cad195949a
commit d97c7c8e56
4 changed files with 274 additions and 259 deletions

View File

@@ -175,7 +175,7 @@ class ProviderDetailsHistorySchema(BaseSchema):
class ServiceSchema(BaseSchema): class ServiceSchema(BaseSchema):
free_sms_fragment_limit = fields.Method('get_free_sms_fragment_limit') 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')
@@ -191,6 +191,7 @@ class ServiceSchema(BaseSchema):
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',

View File

@@ -21,6 +21,8 @@ from tests.app.conftest import sample_notification, sample_email_template, sampl
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,7 +268,7 @@ 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):
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
set_up_rate(notify_db, datetime(2016, 1, 1), 0.016) set_up_rate(notify_db, datetime(2016, 1, 1), 0.016)
@@ -282,16 +284,17 @@ def test_returns_total_billable_units_for_sms_notifications(notify_db, notify_db
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] == 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)[1] == 0.16 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
): ):
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
sample_notification( sample_notification(
@@ -313,7 +316,7 @@ def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notificati
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
): ):
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 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, 1, 1), 2)
set_up_rate(notify_db, datetime(2016, 10, 1), 4) set_up_rate(notify_db, datetime(2016, 10, 1), 4)
@@ -356,7 +359,7 @@ def test_returns_total_billable_units_multiplied_by_multipler_for_sms_notificati
def test_returns_total_billable_units_for_sms_notifications_for_several_rates_where_dates_match_rate_boundary( def test_returns_total_billable_units_for_sms_notifications_for_several_rates_where_dates_match_rate_boundary(
notify_db, notify_db_session, sample_service notify_db, notify_db_session, sample_service
): ):
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 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, 1, 1), 2)
set_up_rate(notify_db, datetime(2016, 10, 1), 4) set_up_rate(notify_db, datetime(2016, 10, 1), 4)
@@ -388,15 +391,17 @@ def test_returns_total_billable_units_for_sms_notifications_for_several_rates_wh
start = datetime(2016, 1, 1) start = datetime(2016, 1, 1)
end = datetime(2018, 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(
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, sample_service.id)[1] == 24.0 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
): ):
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
@@ -436,7 +441,7 @@ def test_returns_total_billable_units_for_sms_notifications_ignoring_letters_and
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
): ):
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
@@ -475,7 +480,7 @@ def test_returns_total_billable_units_for_sms_notifications_for_only_requested_s
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
): ):
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
@@ -502,9 +507,7 @@ 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
): ):
with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
for state in states: for state in states:
@@ -531,8 +534,7 @@ def test_ignores_non_billable_states_when_returning_billable_units_for_sms_notif
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
): ):
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 0 with set_config(current_app, 'FREE_SMS_TIER_FRAGMENT_COUNT', 0):
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
sample_notification( sample_notification(
@@ -556,8 +558,10 @@ def test_restricts_to_time_period_when_returning_billable_units_for_sms_notifica
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(
@@ -629,6 +633,8 @@ def test_should_calculate_rate_boundaries_for_billing_query_for_three_relevant_r
def test_deducts_free_tier_from_bill( def test_deducts_free_tier_from_bill(
notify_db, notify_db_session 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 current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = 1
set_up_rate(notify_db, datetime(2016, 1, 1), 2.5) set_up_rate(notify_db, datetime(2016, 1, 1), 2.5)
@@ -653,6 +659,8 @@ def test_deducts_free_tier_from_bill(
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)[0] == 2
assert get_total_billable_units_for_sent_sms_notifications_in_date_range(start, end, service_1.id)[1] == 2.5 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") @freeze_time("2016-01-10 12:00:00.000000")
@@ -663,8 +671,9 @@ def test_deducts_free_tier_from_bill(
def test_deducts_free_tier_from_bill_across_rate_boundaries( def test_deducts_free_tier_from_bill_across_rate_boundaries(
notify_db, notify_db_session, sample_service, free_tier, expected_cost 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 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, 1, 1), 2)
set_up_rate(notify_db, datetime(2016, 10, 1), 4) set_up_rate(notify_db, datetime(2016, 10, 1), 4)
set_up_rate(notify_db, datetime(2017, 1, 1), 6) set_up_rate(notify_db, datetime(2017, 1, 1), 6)
@@ -699,3 +708,5 @@ def test_deducts_free_tier_from_bill_across_rate_boundaries(
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] == expected_cost )[1] == expected_cost
finally:
current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] = start_value

View File

@@ -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):
@@ -148,6 +149,7 @@ def test_get_service_by_id(client, sample_service):
def test_get_service_by_id_returns_free_sms_limit(client, sample_service): def test_get_service_by_id_returns_free_sms_limit(client, sample_service):
auth_header = create_authorization_header() auth_header = create_authorization_header()
resp = client.get( resp = client.get(
'/service/{}'.format(sample_service.id), '/service/{}'.format(sample_service.id),
@@ -2001,7 +2003,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,

View File

@@ -115,6 +115,7 @@ def set_config(app, name, value):
old_val = app.config.get(name) old_val = app.config.get(name)
app.config[name] = value app.config[name] = value
yield yield
print(app.config)
app.config[name] = old_val app.config[name] = old_val