From cc3d5ba8d11e226c831998f66c8aaa6081bc90eb Mon Sep 17 00:00:00 2001 From: venusbb Date: Mon, 30 Oct 2017 17:10:12 +0000 Subject: [PATCH 1/4] Added logic to return past and future free_sms_limit_data that dont exist --- app/billing/rest.py | 33 +++++++++---- tests/app/billing/test_billing.py | 80 +++++++++++++++++++++++++------ tests/app/service/test_rest.py | 8 ++-- 3 files changed, 95 insertions(+), 26 deletions(-) diff --git a/app/billing/rest.py b/app/billing/rest.py index a29e31be7..3de7b5fbb 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -106,17 +106,34 @@ def get_free_sms_fragment_limit(service_id): financial_year_start = request.args.get('financial_year_start') if financial_year_start is None: - results = dao_get_all_free_sms_fragment_limit(service_id) - - if len(results) == 0: - raise InvalidRequest('no annual billing information for this service', status_code=404) - return jsonify(data=[row.serialize_free_sms_items() for row in results]), 200 + # return a list of all entries related to the service + sms_list = dao_get_all_free_sms_fragment_limit(service_id) + if len(sms_list) == 0: + raise InvalidRequest('no free-sms-fragment-limit list for this service in DB', status_code=404) + return jsonify([row.serialize_free_sms_items() for row in sms_list]), 200 else: result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start) - if result is None: - raise InvalidRequest('no free-sms-fragment-limit-info for this service and year', status_code=404) - return jsonify(data=result.serialize_free_sms_items()), 200 + if result 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, + # and return that number. If all fails, we return InvalidRequest. + sms_list = dao_get_all_free_sms_fragment_limit(service_id) + if len(sms_list) == 0: + raise InvalidRequest('no free-sms-fragment-limit entry for this service in DB', status_code=404) + + if int(financial_year_start) < get_current_financial_year_start_year(): + result = sms_list[0] + else: + result = sms_list[-1] # The newest entry + + new_sms = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start, + free_sms_fragment_limit=result.free_sms_fragment_limit) + + dao_create_or_update_annual_billing_for_year(new_sms) + + return jsonify(result.serialize_free_sms_items()), 200 @billing_blueprint.route('/free-sms-fragment-limit', methods=["POST"]) diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index 68175d704..945e3714f 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -282,8 +282,8 @@ def test_create_free_sms_fragment_limit(client, sample_service): 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['data']['financial_year_start'] == 2017 - assert json_resp['data']['free_sms_fragment_limit'] == 250 + assert json_resp['financial_year_start'] == 2017 + assert json_resp['free_sms_fragment_limit'] == 250 def test_update_free_sms_fragment_limit(client, sample_service): @@ -306,8 +306,8 @@ def test_update_free_sms_fragment_limit(client, sample_service): assert response.status_code == 201 assert response_get.status_code == 200 - assert json_resp['data']['financial_year_start'] == 2016 - assert json_resp['data']['free_sms_fragment_limit'] == 9999 + assert json_resp['financial_year_start'] == 2016 + assert json_resp['free_sms_fragment_limit'] == 9999 def test_get_free_sms_fragment_limit_year_return_correct_data(client, sample_service): @@ -326,7 +326,7 @@ def test_get_free_sms_fragment_limit_year_return_correct_data(client, sample_ser 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] + assert json_resp['free_sms_fragment_limit'] == limits[i] def test_get_free_sms_fragment_limit_for_all_years(client, sample_service): @@ -345,19 +345,18 @@ def test_get_free_sms_fragment_limit_for_all_years(client, sample_service): 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 + assert len(json_resp) == 3 print(json_resp) 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] + assert json_resp[i]['free_sms_fragment_limit'] == limits[i] + assert json_resp[i]['financial_year_start'] == years[i] -def test_get_free_sms_fragment_limit_no_year_data_return_404(client, sample_service): +def test_get_free_sms_fragment_limit_no_service_return_404(client, sample_service): response_get = client.get( - 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(sample_service.id, 1999), + 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(uuid.uuid4(), 1999), headers=[('Content-Type', 'application/json'), create_authorization_header()]) - json_resp = json.loads(response_get.get_data(as_text=True)) assert response_get.status_code == 404 @@ -378,7 +377,7 @@ def test_get_free_sms_fragment_limit_current_year(client, sample_service): json_resp = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 - assert json_resp['data']['free_sms_fragment_limit'] == 250000 + assert json_resp['free_sms_fragment_limit'] == 250000 def test_post_free_sms_fragment_limit_current_year(client, sample_service): @@ -395,5 +394,58 @@ def test_post_free_sms_fragment_limit_current_year(client, sample_service): assert response.status_code == 201 assert response_get.status_code == 200 - assert json_resp['data']['financial_year_start'] == get_current_financial_year_start_year() - assert json_resp['data']['free_sms_fragment_limit'] == 7777 + assert json_resp['financial_year_start'] == get_current_financial_year_start_year() + assert json_resp['free_sms_fragment_limit'] == 7777 + + +def test_get_free_sms_fragment_limit_past_year_get_db_oldest_record(client, sample_service): + response = client.get( + 'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + sms_list = json.loads(response.get_data(as_text=True)) + past_year = sms_list[0]['financial_year_start'] - 1 + + # create an older entry than the one created in sample service has an entry already + annual_billing = {'financial_year_start': past_year, 'free_sms_fragment_limit': 1000} + response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), + data=json.dumps(annual_billing), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + + response = client.get( + 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(sample_service.id, past_year - 2), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + json_resp = json.loads(response.get_data(as_text=True)) + + assert response.status_code == 200 + assert json_resp['free_sms_fragment_limit'] == 1000 + + +def test_get_free_sms_fragment_limit_future_year_get_and_create_db_newest_record(client, sample_service): + + response = client.get( + 'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + sms_list = json.loads(response.get_data(as_text=True)) + next_year = sms_list[-1]['financial_year_start'] + 1 + + # create an older entry than the one created in sample service has an entry already + annual_billing = {'financial_year_start': next_year, 'free_sms_fragment_limit': 1000} + response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), + data=json.dumps(annual_billing), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + # request one year ahead that record doesn't exist yet + response = client.get( + 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(sample_service.id, next_year + 2), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + json_resp = json.loads(response.get_data(as_text=True)) + + # get a list of request and see if the newe + response = client.get( + 'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + sms_new_list = json.loads(response.get_data(as_text=True)) + assert response.status_code == 200 + assert len(sms_new_list) == 3 + assert sms_new_list[0]['free_sms_fragment_limit'] == 250000 + assert sms_new_list[1]['free_sms_fragment_limit'] == 1000 + assert sms_new_list[2]['free_sms_fragment_limit'] == 1000 diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index e317f7a24..af462bb73 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -374,9 +374,9 @@ def test_create_service_free_sms_fragment_limit_is_optional(client, sample_user) .format(service_id, get_current_financial_year_start_year()), headers=[('Content-Type', 'application/json'), create_authorization_header()]) json_resp = json.loads(annual_billing.get_data(as_text=True)) - assert json_resp['data']['free_sms_fragment_limit'] == 9999 + assert json_resp['free_sms_fragment_limit'] == 9999 # TODO: Remove this after the new data is used - assert json_resp['data']['free_sms_fragment_limit'] == 9999 + assert json_resp['free_sms_fragment_limit'] == 9999 data2 = { 'name': 'service 2', @@ -402,9 +402,9 @@ def test_create_service_free_sms_fragment_limit_is_optional(client, sample_user) .format(service_id, get_current_financial_year_start_year()), headers=[('Content-Type', 'application/json'), create_authorization_header()]) json_resp = json.loads(annual_billing.get_data(as_text=True)) - assert json_resp['data']['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] + assert json_resp['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] # TODO: Remove this after the new data is used - assert json_resp['data']['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] + assert json_resp['free_sms_fragment_limit'] == current_app.config['FREE_SMS_TIER_FRAGMENT_COUNT'] def test_should_error_if_created_by_missing(notify_api, sample_user): From 6f7793d761d2a38b0e37952e97bf7c2d0202becc Mon Sep 17 00:00:00 2001 From: venusbb Date: Thu, 2 Nov 2017 12:19:17 +0000 Subject: [PATCH 2/4] - Add update dao_update_annual_billing_for_current_and_future_years - moved get_current_financial_year_start_year from service.utils to dao.date_utils - Moved logic for data persistence from rest to dao when updating records in db --- app/billing/billing_schemas.py | 2 +- app/billing/rest.py | 73 ++++----- app/dao/annual_billing_dao.py | 42 ++++- app/dao/date_util.py | 9 ++ app/service/utils.py | 9 -- tests/app/billing/test_billing.py | 192 +++++++---------------- tests/app/dao/test_annual_billing_dao.py | 68 ++++---- tests/app/db.py | 17 +- tests/app/service/test_rest.py | 2 +- tests/app/service/test_utils.py | 2 +- 10 files changed, 186 insertions(+), 230 deletions(-) diff --git a/app/billing/billing_schemas.py b/app/billing/billing_schemas.py index f9f592e7d..631c782f7 100644 --- a/app/billing/billing_schemas.py +++ b/app/billing/billing_schemas.py @@ -10,5 +10,5 @@ create_or_update_free_sms_fragment_limit_schema = { "free_sms_fragment_limit": {"type": "integer", "minimum": 1}, "financial_year_start": {"type": "integer", "minimum": 2016} }, - "required": ["free_sms_fragment_limit", "financial_year_start"] + "required": ["free_sms_fragment_limit"] } diff --git a/app/billing/rest.py b/app/billing/rest.py index 3de7b5fbb..2a80c97d9 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -13,12 +13,12 @@ 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_or_update_annual_billing_for_year) + dao_create_or_update_annual_billing_for_year, + dao_update_annual_billing_for_current_and_future_years) 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 -from app.models import AnnualBilling -from app.service.utils import get_current_financial_year_start_year +from app.dao.date_util import get_current_financial_year_start_year billing_blueprint = Blueprint( 'billing', @@ -97,66 +97,51 @@ def _transform_billing_for_month(billing_for_month): @billing_blueprint.route('/free-sms-fragment-limit', methods=["GET"]) -@billing_blueprint.route('/free-sms-fragment-limit/current-year', methods=["GET"]) def get_free_sms_fragment_limit(service_id): - if request.path.split('/')[-1] == 'current-year': - financial_year_start = get_current_financial_year_start_year() - else: - financial_year_start = request.args.get('financial_year_start') + financial_year_start = request.args.get('financial_year_start') - if financial_year_start is None: - # return a list of all entries related to the service + result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start) + + if result 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, + # and return that number. If all fails, we return InvalidRequest. sms_list = dao_get_all_free_sms_fragment_limit(service_id) - if len(sms_list) == 0: - raise InvalidRequest('no free-sms-fragment-limit list for this service in DB', status_code=404) - return jsonify([row.serialize_free_sms_items() for row in sms_list]), 200 - else: - result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start) - if result 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, - # and return that number. If all fails, we return InvalidRequest. - sms_list = dao_get_all_free_sms_fragment_limit(service_id) - if len(sms_list) == 0: - raise InvalidRequest('no free-sms-fragment-limit entry for this service in DB', status_code=404) + if not sms_list: + raise InvalidRequest('no free-sms-fragment-limit entry for service {} in DB'.format(service_id), 404) + else: + if financial_year_start is None: + financial_year_start = get_current_financial_year_start_year() if int(financial_year_start) < get_current_financial_year_start_year(): - result = sms_list[0] + result = sms_list[0] # The oldest entry else: - result = sms_list[-1] # The newest entry + result = sms_list[-1] # The newest entry - new_sms = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start, - free_sms_fragment_limit=result.free_sms_fragment_limit) + result = dao_create_or_update_annual_billing_for_year(service_id, result.free_sms_fragment_limit, + financial_year_start) - dao_create_or_update_annual_billing_for_year(new_sms) - - return jsonify(result.serialize_free_sms_items()), 200 + return jsonify(result.serialize_free_sms_items()), 200 @billing_blueprint.route('/free-sms-fragment-limit', methods=["POST"]) def create_or_update_free_sms_fragment_limit(service_id): - dict_arg = request.get_json() + req_args = request.get_json() - if 'financial_year_start' not in dict_arg: - dict_arg['financial_year_start'] = get_current_financial_year_start_year() - - form = validate(dict_arg, create_or_update_free_sms_fragment_limit_schema) + form = validate(req_args, create_or_update_free_sms_fragment_limit_schema) financial_year_start = form.get('financial_year_start') free_sms_fragment_limit = form.get('free_sms_fragment_limit') - result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start) - - if result: - result.free_sms_fragment_limit = free_sms_fragment_limit + current_year = get_current_financial_year_start_year() + if financial_year_start is None or financial_year_start >= current_year: + dao_update_annual_billing_for_current_and_future_years(service_id, free_sms_fragment_limit) else: - result = 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(service_id, + free_sms_fragment_limit, financial_year_start) - dao_create_or_update_annual_billing_for_year(result) - - return jsonify(data=form), 201 + return jsonify(form), 201 diff --git a/app/dao/annual_billing_dao.py b/app/dao/annual_billing_dao.py index 2df3e1497..66d4ae740 100644 --- a/app/dao/annual_billing_dao.py +++ b/app/dao/annual_billing_dao.py @@ -4,8 +4,24 @@ from app.dao.dao_utils import ( version_class ) from app.models import AnnualBilling -from datetime import datetime -from app.service.utils import get_current_financial_year_start_year +from app.dao.date_util import get_current_financial_year_start_year + + +@transactional +def dao_create_or_update_annual_billing_for_year(service_id, free_sms_fragment_limit, financial_year_start=None): + + if not financial_year_start: + financial_year_start = get_current_financial_year_start_year() + + result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start) + + if result: + result.free_sms_fragment_limit = free_sms_fragment_limit + else: + result = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start, + free_sms_fragment_limit=free_sms_fragment_limit) + db.session.add(result) + return result def dao_get_annual_billing(service_id): @@ -14,16 +30,28 @@ def dao_get_annual_billing(service_id): ).order_by(AnnualBilling.financial_year_start).all() -def dao_create_or_update_annual_billing_for_year(annual_billing): - db.session.add(annual_billing) - db.session.commit() +@transactional +def dao_update_annual_billing_for_current_and_future_years(service_id, free_sms_fragment_limit, + financial_year_start=None): + if not financial_year_start: + financial_year_start = get_current_financial_year_start_year() + + updated = AnnualBilling.query.filter( + AnnualBilling.service_id == service_id, + AnnualBilling.financial_year_start >= financial_year_start + ).update( + {'free_sms_fragment_limit': free_sms_fragment_limit} + ) -def dao_get_free_sms_fragment_limit_for_year(service_id, year): +def dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start=None): + + if not financial_year_start: + financial_year_start = get_current_financial_year_start_year() return AnnualBilling.query.filter_by( service_id=service_id, - financial_year_start=year + financial_year_start=financial_year_start ).first() diff --git a/app/dao/date_util.py b/app/dao/date_util.py index a0044517d..b2c4d32bc 100644 --- a/app/dao/date_util.py +++ b/app/dao/date_util.py @@ -45,3 +45,12 @@ def get_month_start_and_end_date_in_utc(month_year): first_day = datetime(month_year.year, month_year.month, 1, 0, 0, 0) last_day = datetime(month_year.year, month_year.month, num_days, 23, 59, 59, 99999) return convert_bst_to_utc(first_day), convert_bst_to_utc(last_day) + + +def get_current_financial_year_start_year(): + now = datetime.now() + financial_year_start = now.year + start_date, end_date = get_financial_year(now.year) + if now < start_date: + financial_year_start = financial_year_start - 1 + return financial_year_start diff --git a/app/service/utils.py b/app/service/utils.py index 475d420e2..111ffa80a 100644 --- a/app/service/utils.py +++ b/app/service/utils.py @@ -53,12 +53,3 @@ def service_allowed_to_send_to(recipient, service, key_type): whitelist_members ) ) - - -def get_current_financial_year_start_year(): - now = datetime.now() - financial_year_start = now.year - start_date, end_date = get_financial_year(now.year) - if now < start_date: - financial_year_start = financial_year_start - 1 - return financial_year_start diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index 945e3714f..83e15755d 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -15,9 +15,11 @@ from tests.app.db import ( from tests import create_authorization_header -from app.service.utils import get_current_financial_year_start_year +from app.dao.date_util import get_current_financial_year_start_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) @@ -268,9 +270,9 @@ def test_create_update_free_sms_fragment_limit_invalid_schema(client, sample_ser assert 'JSON' in json_resp['message'] -def test_create_free_sms_fragment_limit(client, sample_service): +def test_create_free_sms_fragment_limit_current_year(client, sample_service): - data = {'financial_year_start': 2017, 'free_sms_fragment_limit': 250} + 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()]) @@ -282,20 +284,15 @@ def test_create_free_sms_fragment_limit(client, sample_service): 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'] == 2017 - assert json_resp['free_sms_fragment_limit'] == 250 + assert json_resp['financial_year_start'] == get_current_financial_year_start_year() + assert json_resp['free_sms_fragment_limit'] == 9999 -def test_update_free_sms_fragment_limit(client, sample_service): +def test_create_free_sms_fragment_limit_past_year(client, sample_service): - data_old = {'financial_year_start': 2016, 'free_sms_fragment_limit': 1000} + data = {'financial_year_start': 2016, 'free_sms_fragment_limit': 9999} response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), - data=json.dumps(data_old), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - - data_new = {'financial_year_start': 2016, 'free_sms_fragment_limit': 9999} - response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), - data=json.dumps(data_new), + data=json.dumps(data), headers=[('Content-Type', 'application/json'), create_authorization_header()]) response_get = client.get( @@ -303,149 +300,74 @@ 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)) - assert response.status_code == 201 assert response_get.status_code == 200 assert json_resp['financial_year_start'] == 2016 assert json_resp['free_sms_fragment_limit'] == 9999 -def test_get_free_sms_fragment_limit_year_return_correct_data(client, sample_service): - years = [2016, 2017, 2018] - limits = [1000, 2000, 3000] - - for i in range(0, len(years)): - annual_billing = {'financial_year_start': years[i], 'free_sms_fragment_limit': limits[i]} - response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), - data=json.dumps(annual_billing), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - - 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['free_sms_fragment_limit'] == limits[i] - - -def test_get_free_sms_fragment_limit_for_all_years(client, sample_service): - years = [2016, 2017, 2018] - limits = [1000, 2000, 3000] - - for i in range(0, len(years)): - annual_billing = {'financial_year_start': years[i], 'free_sms_fragment_limit': limits[i]} - response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), - data=json.dumps(annual_billing), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - - 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) == 3 - print(json_resp) - for i in [0, 1, 2]: - assert json_resp[i]['free_sms_fragment_limit'] == limits[i] - assert json_resp[i]['financial_year_start'] == years[i] - - -def test_get_free_sms_fragment_limit_no_service_return_404(client, sample_service): - - response_get = client.get( - 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(uuid.uuid4(), 1999), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - - assert response_get.status_code == 404 - - -def test_get_free_sms_fragment_limit_unknown_service_id_return_404(client): - - response_get = client.get( - 'service/{}/billing/free-sms-fragment-limit'.format(uuid.uuid4()), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - json_resp = json.loads(response_get.get_data(as_text=True)) - assert response_get.status_code == 404 - - -def test_get_free_sms_fragment_limit_current_year(client, sample_service): - response = client.get( - 'service/{}/billing/free-sms-fragment-limit/current-year'.format(sample_service.id, True), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - json_resp = json.loads(response.get_data(as_text=True)) - - assert response.status_code == 200 - assert json_resp['free_sms_fragment_limit'] == 250000 - - -def test_post_free_sms_fragment_limit_current_year(client, sample_service): - - data_new = {'free_sms_fragment_limit': 7777} +def test_update_free_sms_fragment_limit(client, sample_service): + current_year = get_current_financial_year_start_year() + 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), headers=[('Content-Type', 'application/json'), create_authorization_header()]) response_get = client.get( - 'service/{}/billing/free-sms-fragment-limit/current-year'.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'] == current_year + assert json_resp['free_sms_fragment_limit'] == 9999 + + +def test_get_free_sms_fragment_limit_current_year(client, sample_service): + + current_year = get_current_financial_year_start_year() + create_annual_billing(sample_service.id, 9999, current_year - 1) + + response_get = client.get( + '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 json_resp['financial_year_start'] == get_current_financial_year_start_year() - assert json_resp['free_sms_fragment_limit'] == 7777 + assert json_resp['free_sms_fragment_limit'] == 250000 -def test_get_free_sms_fragment_limit_past_year_get_db_oldest_record(client, sample_service): - response = client.get( - 'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), +def test_get_free_sms_fragment_limit_past_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) + + res_get = client.get( + 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}' + .format(sample_service.id, current_year - 2), headers=[('Content-Type', 'application/json'), create_authorization_header()]) - sms_list = json.loads(response.get_data(as_text=True)) - past_year = sms_list[0]['financial_year_start'] - 1 + json_resp = json.loads(res_get.get_data(as_text=True)) - # create an older entry than the one created in sample service has an entry already - annual_billing = {'financial_year_start': past_year, 'free_sms_fragment_limit': 1000} - response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), - data=json.dumps(annual_billing), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) + assert res_get.status_code == 200 + assert json_resp['financial_year_start'] == current_year - 1 + assert json_resp['free_sms_fragment_limit'] == 9999 - response = client.get( - 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(sample_service.id, past_year - 2), + +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) + + res_get = client.get( + 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}' + .format(sample_service.id, current_year + 2), headers=[('Content-Type', 'application/json'), create_authorization_header()]) - json_resp = json.loads(response.get_data(as_text=True)) + json_resp = json.loads(res_get.get_data(as_text=True)) - assert response.status_code == 200 - assert json_resp['free_sms_fragment_limit'] == 1000 - - -def test_get_free_sms_fragment_limit_future_year_get_and_create_db_newest_record(client, sample_service): - - response = client.get( - 'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - sms_list = json.loads(response.get_data(as_text=True)) - next_year = sms_list[-1]['financial_year_start'] + 1 - - # create an older entry than the one created in sample service has an entry already - annual_billing = {'financial_year_start': next_year, 'free_sms_fragment_limit': 1000} - response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), - data=json.dumps(annual_billing), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - # request one year ahead that record doesn't exist yet - response = client.get( - 'service/{}/billing/free-sms-fragment-limit?financial_year_start={}'.format(sample_service.id, next_year + 2), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - json_resp = json.loads(response.get_data(as_text=True)) - - # get a list of request and see if the newe - response = client.get( - 'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) - sms_new_list = json.loads(response.get_data(as_text=True)) - assert response.status_code == 200 - assert len(sms_new_list) == 3 - assert sms_new_list[0]['free_sms_fragment_limit'] == 250000 - assert sms_new_list[1]['free_sms_fragment_limit'] == 1000 - assert sms_new_list[2]['free_sms_fragment_limit'] == 1000 + assert res_get.status_code == 200 + assert json_resp['financial_year_start'] == current_year + 2 + assert json_resp['free_sms_fragment_limit'] == 10000 diff --git a/tests/app/dao/test_annual_billing_dao.py b/tests/app/dao/test_annual_billing_dao.py index 10c6cd201..7cbd2b096 100644 --- a/tests/app/dao/test_annual_billing_dao.py +++ b/tests/app/dao/test_annual_billing_dao.py @@ -1,10 +1,12 @@ -from app.service.utils import get_current_financial_year_start_year +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_get_annual_billing, + dao_update_annual_billing_for_current_and_future_years, ) +from tests.app.db import create_annual_billing def test_get_sample_service_has_default_free_sms_fragment_limit(notify_db_session, sample_service): @@ -18,40 +20,44 @@ def test_get_sample_service_has_default_free_sms_fragment_limit(notify_db_sessio def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service): - year = 1999 - old_limit = 1000 new_limit = 9999 - - data = AnnualBilling( - free_sms_fragment_limit=old_limit, - financial_year_start=year, - service_id=sample_service.id, - ) - - dao_create_or_update_annual_billing_for_year(data) - data.free_sms_fragment_limit = new_limit - dao_create_or_update_annual_billing_for_year(data) + year = get_current_financial_year_start_year() + dao_create_or_update_annual_billing_for_year(sample_service.id, new_limit, year) 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 -def test_create_then_get_annual_billing(notify_db_session, sample_service): - years = [1999, 2001] - limits = [1000, 2000] +def test_create_annual_billing_not_specify_year(notify_db_session, sample_service): - for i in [0, 1]: - 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) + dao_create_or_update_annual_billing_for_year(sample_service.id, 9999) - free_limit = dao_get_annual_billing(sample_service.id) - assert len(free_limit) == 3 # sample service already has one entry - assert free_limit[0].free_sms_fragment_limit == 1000 - assert free_limit[0].financial_year_start == 1999 - assert free_limit[0].service_id == sample_service.id - assert free_limit[1].free_sms_fragment_limit == 2000 - assert free_limit[1].financial_year_start == 2001 + free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id) + + assert free_limit.free_sms_fragment_limit == 9999 + + +def test_create_annual_billing_specify_year(notify_db_session, sample_service): + + dao_create_or_update_annual_billing_for_year(sample_service.id, 9999, 2016) + + free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, 2016) + + assert free_limit.free_sms_fragment_limit == 9999 + + +def test_dao_update_annual_billing_for_current_and_future_years(notify_db_session, sample_service): + current_year = get_current_financial_year_start_year() + limits = [240000, 250000, 260000, 270000] + create_annual_billing(sample_service.id, limits[0], current_year - 1) + create_annual_billing(sample_service.id, limits[2], current_year + 1) + create_annual_billing(sample_service.id, limits[3], current_year + 2) + + dao_update_annual_billing_for_current_and_future_years(sample_service.id, 9999, current_year) + + free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year - 1) + assert free_limit.free_sms_fragment_limit == 240000 + + for year in range(current_year, current_year + 3): + free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, year) + assert free_limit.free_sms_fragment_limit == 9999 diff --git a/tests/app/db.py b/tests/app/db.py index 0cdf7cfee..a4fa459ba 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -26,7 +26,8 @@ from app.models import ( EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE, - KEY_TYPE_NORMAL + KEY_TYPE_NORMAL, + AnnualBilling, ) from app.dao.users_dao import save_model_user from app.dao.notifications_dao import dao_create_notification, dao_created_scheduled_notification @@ -407,3 +408,17 @@ def create_reply_to_email_for_notification( db.session.commit() return reply_to + + +def create_annual_billing( + service_id, free_sms_fragment_limit, financial_year_start +): + annual_billing = AnnualBilling( + service_id=service_id, + free_sms_fragment_limit=free_sms_fragment_limit, + financial_year_start=financial_year_start + ) + db.session.add(annual_billing) + db.session.commit() + + return annual_billing diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index af462bb73..b281e133b 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -37,7 +37,7 @@ from tests.app.db import ( create_service_sms_sender ) from tests.app.db import create_user -from app.service.utils import get_current_financial_year_start_year +from app.dao.date_util import get_current_financial_year_start_year def test_get_service_list(client, service_factory): diff --git a/tests/app/service/test_utils.py b/tests/app/service/test_utils.py index 4d59af22b..71b2770bb 100644 --- a/tests/app/service/test_utils.py +++ b/tests/app/service/test_utils.py @@ -1,4 +1,4 @@ -from app.service.utils import get_current_financial_year_start_year +from app.dao.date_util import get_current_financial_year_start_year from freezegun import freeze_time From ad386b7d282026a30295702df43b3fbf2a5fa62d Mon Sep 17 00:00:00 2001 From: venusbb Date: Thu, 2 Nov 2017 17:02:00 +0000 Subject: [PATCH 3/4] removed unused import and minor syntax --- app/billing/rest.py | 15 ++++++++------- app/dao/annual_billing_dao.py | 3 +-- tests/app/billing/test_billing.py | 23 ++++++++++++++++------- tests/app/dao/test_annual_billing_dao.py | 3 +-- 4 files changed, 26 insertions(+), 18 deletions(-) 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 From 0c3e8627b9241fc93fd04c4203d24fa32814c6a3 Mon Sep 17 00:00:00 2001 From: venusbb Date: Fri, 3 Nov 2017 11:50:17 +0000 Subject: [PATCH 4/4] minor style change --- tests/app/billing/test_billing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index 80d273f24..7895bd8a9 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -332,7 +332,7 @@ def test_update_free_sms_fragment_limit(client, sample_service): def test_get_free_sms_fragment_limit_current_year(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, free_sms_fragment_limit=9999, financial_year_start=current_year - 1) response_get = client.get( 'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),