diff --git a/app/billing/rest.py b/app/billing/rest.py index f25d282dd..7d254c751 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -13,8 +13,7 @@ from app.models import SMS_TYPE, EMAIL_TYPE from app.utils import convert_utc_to_bst from app.dao.annual_billing_dao import (dao_get_free_sms_fragment_limit_for_year, dao_get_all_free_sms_fragment_limit, - dao_create_new_annual_billing_for_year, - dao_update_new_free_sms_fragment_limit_for_year) + dao_create_or_update_annual_billing_for_year) from app.billing.billing_schemas import create_or_update_free_sms_fragment_limit_schema from app.errors import InvalidRequest from app.schema_validation import validate @@ -96,17 +95,6 @@ def _transform_billing_for_month(billing_for_month): } -# @billing_blueprint.route('/annual-billing', methods=["GET"]) -# def get_annual_billing(service_id): -# -# results = dao_get_annual_billing(service_id) -# -# if len(results)==0: -# raise InvalidRequest('no annual billing information for this service', status_code=404) -# -# return jsonify(data=[row.serialize() for row in results]), 200 - - @billing_blueprint.route('/free-sms-fragment-limit', methods=["GET"]) def get_free_sms_fragment_limit(service_id): @@ -136,13 +124,13 @@ def create_or_update_free_sms_fragment_limit(service_id): result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start) - annual_billing = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start, - free_sms_fragment_limit=free_sms_fragment_limit) - - if result is None: - dao_create_new_annual_billing_for_year(annual_billing) + if result: + result.free_sms_fragment_limit = free_sms_fragment_limit + dao_create_or_update_annual_billing_for_year(result) else: - dao_update_new_free_sms_fragment_limit_for_year(annual_billing) + annual_billing = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start, + free_sms_fragment_limit=free_sms_fragment_limit) + dao_create_or_update_annual_billing_for_year(annual_billing) return jsonify(data=form), 201 diff --git a/app/dao/annual_billing_dao.py b/app/dao/annual_billing_dao.py index 9d28cca44..2343104b3 100644 --- a/app/dao/annual_billing_dao.py +++ b/app/dao/annual_billing_dao.py @@ -13,8 +13,9 @@ def dao_get_annual_billing(service_id): ).all() -def dao_create_new_annual_billing_for_year(annual_billing): - annual_billing.id = create_uuid() +def dao_create_or_update_annual_billing_for_year(annual_billing): + if annual_billing.id is None: + annual_billing.id = create_uuid() db.session.add(annual_billing) db.session.commit() @@ -32,8 +33,3 @@ def dao_get_all_free_sms_fragment_limit(service_id): return AnnualBilling.query.filter_by( service_id=service_id, ).all() - - -def dao_update_new_free_sms_fragment_limit_for_year(annual_billing): - db.session.add(annual_billing) - db.session.commit() diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index edf04016b..e49cea604 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -14,7 +14,8 @@ from tests.app.db import ( ) from tests import create_authorization_header -from app.dao.annual_billing_dao import dao_get_free_sms_fragment_limit_for_year, dao_create_new_annual_billing_for_year +from app.dao.annual_billing_dao import (dao_get_free_sms_fragment_limit_for_year, + dao_create_or_update_annual_billing_for_year) from app.models import AnnualBilling APR_2016_MONTH_START = datetime(2016, 3, 31, 23, 00, 00) @@ -255,36 +256,6 @@ def test_transform_billing_calculates_with_different_rate_multipliers(sample_ser }) -# def test_get_annual_billing(client, sample_service): -# -# years = [2016, 2017, 2018] -# for y in years: -# data = AnnualBilling( -# free_sms_fragment_limit=250000, -# financial_year_start=y, -# service_id=sample_service.id, -# ) -# dao_create_new_free_sms_fragment_limit_for_year(data) -# -# response = client.get('service/{}/annual-billing'.format(sample_service.id), -# headers=[('Content-Type', 'application/json'), create_authorization_header()]) -# -# json_resp = json.loads(response.get_data(as_text=True)) -# -# assert len(json_resp['data']) == 3 -# assert response.status_code == 200 -# -# -# def test_get_annual_billing_no_data_throws_error(client, sample_service): -# -# response = client.get('service/{}/annual-billing'.format(sample_service.id), -# headers=[('Content-Type', 'application/json'), create_authorization_header()]) -# -# json_resp = json.loads(response.get_data(as_text=True)) -# -# assert response.status_code == 404 - - def test_get_free_sms_fragment_limit(client, sample_service): years = [2016, 2017, 2018] sms_allowance = [1000, 2000, 3000] @@ -296,7 +267,7 @@ def test_get_free_sms_fragment_limit(client, sample_service): financial_year_start=y, service_id=sample_service.id, ) - dao_create_new_annual_billing_for_year(data) + dao_create_or_update_annual_billing_for_year(data) response = client.get('service/{}/billing/free-sms-fragment-limit?financial_year_start=2017' .format(sample_service.id), @@ -367,9 +338,51 @@ def test_update_free_sms_fragment_limit(client, sample_service): headers=[('Content-Type', 'application/json'), create_authorization_header()]) json_resp = json.loads(response_get.get_data(as_text=True)) - new_free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, 2015) assert response.status_code == 201 assert response_get.status_code == 200 assert json_resp['data']['financial_year_start'] == 2015 assert json_resp['data']['free_sms_fragment_limit'] == 9999 + + +def test_get_free_sms_fragment_limit_year_return_correct_data(client, sample_service): + years = [2015, 2016, 2017] + limits = [1000, 2000, 3000] + for i in range(0, len(years)): + data = AnnualBilling( + free_sms_fragment_limit=limits[i], + financial_year_start=years[i], + service_id=sample_service.id, + ) + dao_create_or_update_annual_billing_for_year(data) + + for i in range(0, len(years)): + response_get = client.get( + 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(sample_service.id, years[i]), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + json_resp = json.loads(response_get.get_data(as_text=True)) + assert response_get.status_code == 200 + assert json_resp['data']['free_sms_fragment_limit'] == limits[i] + + +def test_get_free_sms_fragment_limit_for_all_year(client, sample_service): + years = [2015, 2016, 2017] + limits = [1000, 2000, 3000] + for i in range(0, len(years)): + data = AnnualBilling( + free_sms_fragment_limit=limits[i], + financial_year_start=years[i], + service_id=sample_service.id, + ) + dao_create_or_update_annual_billing_for_year(data) + + response_get = client.get( + # Not specify a particular year to return all data for that service + 'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + json_resp = json.loads(response_get.get_data(as_text=True)) + assert response_get.status_code == 200 + assert len(json_resp['data']) == 3 + for i in [0, 1, 2]: + assert json_resp['data'][i]['free_sms_fragment_limit'] == limits[i] + assert json_resp['data'][i]['financial_year_start'] == years[i] diff --git a/tests/app/dao/test_annual_billing_dao.py b/tests/app/dao/test_annual_billing_dao.py index 9046583d6..73b230431 100644 --- a/tests/app/dao/test_annual_billing_dao.py +++ b/tests/app/dao/test_annual_billing_dao.py @@ -5,26 +5,28 @@ from app.models import AnnualBilling from app.dao.annual_billing_dao import ( - dao_create_new_annual_billing_for_year, - dao_get_free_sms_fragment_limit_for_year, - dao_update_new_free_sms_fragment_limit_for_year + dao_create_or_update_annual_billing_for_year, + dao_get_free_sms_fragment_limit_for_year ) def test_dao_create_get_free_sms_fragment_limit(notify_db_session, sample_service): - year = 2016 - data = AnnualBilling( - free_sms_fragment_limit=250000, - financial_year_start=year, - service_id=sample_service.id, - ) - dao_create_new_annual_billing_for_year(data) + years = [2015, 2016, 2017] + free_limit_data = [1000, 2000, 3000] + for i in range(0, len(years)): + data = AnnualBilling( + free_sms_fragment_limit=free_limit_data[i], + financial_year_start=years[i], + service_id=sample_service.id, + ) + dao_create_or_update_annual_billing_for_year(data) - free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, year) + for i in range(0, len(years)): + free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, years[i]) - assert free_limit.free_sms_fragment_limit == 250000 - assert free_limit.financial_year_start == year - assert free_limit.service_id == sample_service.id + assert free_limit.free_sms_fragment_limit == free_limit_data[i] + assert free_limit.financial_year_start == years[i] + assert free_limit.service_id == sample_service.id def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service): @@ -38,9 +40,9 @@ def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service): service_id=sample_service.id, ) - dao_create_new_annual_billing_for_year(data) + dao_create_or_update_annual_billing_for_year(data) data.free_sms_fragment_limit = new_limit - dao_update_new_free_sms_fragment_limit_for_year(data) + dao_create_or_update_annual_billing_for_year(data) new_free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, year) assert new_free_limit.free_sms_fragment_limit == new_limit