Added logic to return past and future free_sms_limit_data that dont exist

This commit is contained in:
venusbb
2017-10-30 17:10:12 +00:00
parent dd4554fad2
commit cc3d5ba8d1
3 changed files with 95 additions and 26 deletions

View File

@@ -106,17 +106,34 @@ def get_free_sms_fragment_limit(service_id):
financial_year_start = request.args.get('financial_year_start') financial_year_start = request.args.get('financial_year_start')
if financial_year_start is None: if financial_year_start is None:
results = dao_get_all_free_sms_fragment_limit(service_id) # return a list of all entries related to the service
sms_list = dao_get_all_free_sms_fragment_limit(service_id)
if len(results) == 0: if len(sms_list) == 0:
raise InvalidRequest('no annual billing information for this service', status_code=404) raise InvalidRequest('no free-sms-fragment-limit list for this service in DB', status_code=404)
return jsonify(data=[row.serialize_free_sms_items() for row in results]), 200 return jsonify([row.serialize_free_sms_items() for row in sms_list]), 200
else: else:
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start) 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"]) @billing_blueprint.route('/free-sms-fragment-limit', methods=["POST"])

View File

@@ -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)) json_resp = json.loads(response_get.get_data(as_text=True))
assert response.status_code == 201 assert response.status_code == 201
assert response_get.status_code == 200 assert response_get.status_code == 200
assert json_resp['data']['financial_year_start'] == 2017 assert json_resp['financial_year_start'] == 2017
assert json_resp['data']['free_sms_fragment_limit'] == 250 assert json_resp['free_sms_fragment_limit'] == 250
def test_update_free_sms_fragment_limit(client, sample_service): 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.status_code == 201
assert response_get.status_code == 200 assert response_get.status_code == 200
assert json_resp['data']['financial_year_start'] == 2016 assert json_resp['financial_year_start'] == 2016
assert json_resp['data']['free_sms_fragment_limit'] == 9999 assert json_resp['free_sms_fragment_limit'] == 9999
def test_get_free_sms_fragment_limit_year_return_correct_data(client, sample_service): 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()]) headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response_get.get_data(as_text=True)) json_resp = json.loads(response_get.get_data(as_text=True))
assert response_get.status_code == 200 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): 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()]) headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(response_get.get_data(as_text=True)) json_resp = json.loads(response_get.get_data(as_text=True))
assert response_get.status_code == 200 assert response_get.status_code == 200
assert len(json_resp['data']) == 3 assert len(json_resp) == 3
print(json_resp) print(json_resp)
for i in [0, 1, 2]: for i in [0, 1, 2]:
assert json_resp['data'][i]['free_sms_fragment_limit'] == limits[i] assert json_resp[i]['free_sms_fragment_limit'] == limits[i]
assert json_resp['data'][i]['financial_year_start'] == years[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( 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()]) 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 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)) json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 200 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): 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.status_code == 201
assert response_get.status_code == 200 assert response_get.status_code == 200
assert json_resp['data']['financial_year_start'] == get_current_financial_year_start_year() assert json_resp['financial_year_start'] == get_current_financial_year_start_year()
assert json_resp['data']['free_sms_fragment_limit'] == 7777 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

View File

@@ -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()), .format(service_id, get_current_financial_year_start_year()),
headers=[('Content-Type', 'application/json'), create_authorization_header()]) headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(annual_billing.get_data(as_text=True)) 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 # 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 = { data2 = {
'name': 'service 2', '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()), .format(service_id, get_current_financial_year_start_year()),
headers=[('Content-Type', 'application/json'), create_authorization_header()]) headers=[('Content-Type', 'application/json'), create_authorization_header()])
json_resp = json.loads(annual_billing.get_data(as_text=True)) 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 # 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): def test_should_error_if_created_by_missing(notify_api, sample_user):