diff --git a/app/billing/rest.py b/app/billing/rest.py index 2a80c97d9..614842d3f 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -101,9 +101,9 @@ def get_free_sms_fragment_limit(service_id): financial_year_start = request.args.get('financial_year_start') - result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start) + annual_billing = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start) - if result is None: + if annual_billing is None: # An entry does not exist in annual_billing table for that service and year. If it is a past year, # we return the oldest entry. # If it is the current or future years, we create an entry in the db table using the newest record, @@ -117,14 +117,15 @@ def get_free_sms_fragment_limit(service_id): financial_year_start = get_current_financial_year_start_year() if int(financial_year_start) < get_current_financial_year_start_year(): - result = sms_list[0] # The oldest entry + annual_billing = sms_list[0] # The oldest entry else: - result = sms_list[-1] # The newest entry + annual_billing = sms_list[-1] # The newest entry - result = dao_create_or_update_annual_billing_for_year(service_id, result.free_sms_fragment_limit, - financial_year_start) + annual_billing = dao_create_or_update_annual_billing_for_year(service_id, + annual_billing.free_sms_fragment_limit, + financial_year_start) - return jsonify(result.serialize_free_sms_items()), 200 + return jsonify(annual_billing.serialize_free_sms_items()), 200 @billing_blueprint.route('/free-sms-fragment-limit', methods=["POST"]) diff --git a/app/dao/annual_billing_dao.py b/app/dao/annual_billing_dao.py index 66d4ae740..37361d7b1 100644 --- a/app/dao/annual_billing_dao.py +++ b/app/dao/annual_billing_dao.py @@ -1,7 +1,6 @@ -from app import db, create_uuid +from app import db from app.dao.dao_utils import ( transactional, - version_class ) from app.models import AnnualBilling from app.dao.date_util import get_current_financial_year_start_year diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index 83e15755d..80d273f24 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -16,10 +16,9 @@ from tests.app.db import ( from tests import create_authorization_header from app.dao.date_util import get_current_financial_year_start_year +from app.dao.annual_billing_dao import dao_get_free_sms_fragment_limit_for_year from tests.app.db import create_annual_billing -import uuid -import app APR_2016_MONTH_START = datetime(2016, 3, 31, 23, 00, 00) APR_2016_MONTH_END = datetime(2016, 4, 30, 22, 59, 59, 99999) @@ -271,20 +270,20 @@ def test_create_update_free_sms_fragment_limit_invalid_schema(client, sample_ser def test_create_free_sms_fragment_limit_current_year(client, sample_service): - + current_year = get_current_financial_year_start_year() data = {'free_sms_fragment_limit': 9999} response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), data=json.dumps(data), headers=[('Content-Type', 'application/json'), create_authorization_header()]) response_get = client.get( - 'service/{}/billing/free-sms-fragment-limit?financial_year_start=2017'.format(sample_service.id), + 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(sample_service.id, current_year), headers=[('Content-Type', 'application/json'), create_authorization_header()]) json_resp = json.loads(response_get.get_data(as_text=True)) assert response.status_code == 201 assert response_get.status_code == 200 - assert json_resp['financial_year_start'] == get_current_financial_year_start_year() + assert json_resp['financial_year_start'] == current_year assert json_resp['free_sms_fragment_limit'] == 9999 @@ -308,6 +307,10 @@ def test_create_free_sms_fragment_limit_past_year(client, sample_service): def test_update_free_sms_fragment_limit(client, sample_service): current_year = get_current_financial_year_start_year() + + annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year) + assert annual_billing.free_sms_fragment_limit == 250000 + data_new = {'financial_year_start': current_year, 'free_sms_fragment_limit': 9999} response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), data=json.dumps(data_new), @@ -346,6 +349,9 @@ def test_get_free_sms_fragment_limit_past_year_not_exist(client, sample_service) create_annual_billing(sample_service.id, 9999, current_year - 1) create_annual_billing(sample_service.id, 10000, current_year + 1) + annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year - 2) + assert annual_billing is None + res_get = client.get( 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}' .format(sample_service.id, current_year - 2), @@ -359,8 +365,11 @@ def test_get_free_sms_fragment_limit_past_year_not_exist(client, sample_service) def test_get_free_sms_fragment_limit_future_year_not_exist(client, sample_service): current_year = get_current_financial_year_start_year() - create_annual_billing(sample_service.id, 9999, current_year - 1) - create_annual_billing(sample_service.id, 10000, current_year + 1) + create_annual_billing(sample_service.id, free_sms_fragment_limit=9999, financial_year_start=current_year - 1) + create_annual_billing(sample_service.id, free_sms_fragment_limit=10000, financial_year_start=current_year + 1) + + annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year + 2) + assert annual_billing is None res_get = client.get( 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}' diff --git a/tests/app/dao/test_annual_billing_dao.py b/tests/app/dao/test_annual_billing_dao.py index 7cbd2b096..3e475ef5c 100644 --- a/tests/app/dao/test_annual_billing_dao.py +++ b/tests/app/dao/test_annual_billing_dao.py @@ -1,9 +1,8 @@ from app.dao.date_util import get_current_financial_year_start_year -from app.models import AnnualBilling + from app.dao.annual_billing_dao import ( dao_create_or_update_annual_billing_for_year, dao_get_free_sms_fragment_limit_for_year, - dao_get_annual_billing, dao_update_annual_billing_for_current_and_future_years, ) from tests.app.db import create_annual_billing