mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
- 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
This commit is contained in:
@@ -10,5 +10,5 @@ create_or_update_free_sms_fragment_limit_schema = {
|
|||||||
"free_sms_fragment_limit": {"type": "integer", "minimum": 1},
|
"free_sms_fragment_limit": {"type": "integer", "minimum": 1},
|
||||||
"financial_year_start": {"type": "integer", "minimum": 2016}
|
"financial_year_start": {"type": "integer", "minimum": 2016}
|
||||||
},
|
},
|
||||||
"required": ["free_sms_fragment_limit", "financial_year_start"]
|
"required": ["free_sms_fragment_limit"]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ from app.models import SMS_TYPE, EMAIL_TYPE
|
|||||||
from app.utils import convert_utc_to_bst
|
from app.utils import convert_utc_to_bst
|
||||||
from app.dao.annual_billing_dao import (dao_get_free_sms_fragment_limit_for_year,
|
from app.dao.annual_billing_dao import (dao_get_free_sms_fragment_limit_for_year,
|
||||||
dao_get_all_free_sms_fragment_limit,
|
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.billing.billing_schemas import create_or_update_free_sms_fragment_limit_schema
|
||||||
from app.errors import InvalidRequest
|
from app.errors import InvalidRequest
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
from app.models import AnnualBilling
|
from app.dao.date_util import get_current_financial_year_start_year
|
||||||
from app.service.utils import get_current_financial_year_start_year
|
|
||||||
|
|
||||||
billing_blueprint = Blueprint(
|
billing_blueprint = Blueprint(
|
||||||
'billing',
|
'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', methods=["GET"])
|
||||||
@billing_blueprint.route('/free-sms-fragment-limit/current-year', methods=["GET"])
|
|
||||||
def get_free_sms_fragment_limit(service_id):
|
def get_free_sms_fragment_limit(service_id):
|
||||||
|
|
||||||
if request.path.split('/')[-1] == 'current-year':
|
financial_year_start = request.args.get('financial_year_start')
|
||||||
financial_year_start = get_current_financial_year_start_year()
|
|
||||||
else:
|
|
||||||
financial_year_start = request.args.get('financial_year_start')
|
|
||||||
|
|
||||||
if financial_year_start is None:
|
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
|
||||||
# return a list of all entries related to the service
|
|
||||||
|
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)
|
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:
|
if not sms_list:
|
||||||
# An entry does not exist in annual_billing table for that service and year. If it is a past year,
|
raise InvalidRequest('no free-sms-fragment-limit entry for service {} in DB'.format(service_id), 404)
|
||||||
# we return the oldest entry.
|
else:
|
||||||
# If it is the current or future years, we create an entry in the db table using the newest record,
|
if financial_year_start is None:
|
||||||
# and return that number. If all fails, we return InvalidRequest.
|
financial_year_start = get_current_financial_year_start_year()
|
||||||
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():
|
if int(financial_year_start) < get_current_financial_year_start_year():
|
||||||
result = sms_list[0]
|
result = sms_list[0] # The oldest entry
|
||||||
else:
|
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,
|
result = dao_create_or_update_annual_billing_for_year(service_id, result.free_sms_fragment_limit,
|
||||||
free_sms_fragment_limit=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"])
|
@billing_blueprint.route('/free-sms-fragment-limit', methods=["POST"])
|
||||||
def create_or_update_free_sms_fragment_limit(service_id):
|
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:
|
form = validate(req_args, create_or_update_free_sms_fragment_limit_schema)
|
||||||
dict_arg['financial_year_start'] = get_current_financial_year_start_year()
|
|
||||||
|
|
||||||
form = validate(dict_arg, create_or_update_free_sms_fragment_limit_schema)
|
|
||||||
|
|
||||||
financial_year_start = form.get('financial_year_start')
|
financial_year_start = form.get('financial_year_start')
|
||||||
free_sms_fragment_limit = form.get('free_sms_fragment_limit')
|
free_sms_fragment_limit = form.get('free_sms_fragment_limit')
|
||||||
|
|
||||||
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
|
current_year = get_current_financial_year_start_year()
|
||||||
|
if financial_year_start is None or financial_year_start >= current_year:
|
||||||
if result:
|
dao_update_annual_billing_for_current_and_future_years(service_id, free_sms_fragment_limit)
|
||||||
result.free_sms_fragment_limit = free_sms_fragment_limit
|
|
||||||
else:
|
else:
|
||||||
result = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start,
|
dao_create_or_update_annual_billing_for_year(service_id,
|
||||||
free_sms_fragment_limit=free_sms_fragment_limit)
|
free_sms_fragment_limit, financial_year_start)
|
||||||
|
|
||||||
dao_create_or_update_annual_billing_for_year(result)
|
return jsonify(form), 201
|
||||||
|
|
||||||
return jsonify(data=form), 201
|
|
||||||
|
|||||||
@@ -4,8 +4,24 @@ from app.dao.dao_utils import (
|
|||||||
version_class
|
version_class
|
||||||
)
|
)
|
||||||
from app.models import AnnualBilling
|
from app.models import AnnualBilling
|
||||||
from datetime import datetime
|
from app.dao.date_util import get_current_financial_year_start_year
|
||||||
from app.service.utils 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):
|
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()
|
).order_by(AnnualBilling.financial_year_start).all()
|
||||||
|
|
||||||
|
|
||||||
def dao_create_or_update_annual_billing_for_year(annual_billing):
|
@transactional
|
||||||
db.session.add(annual_billing)
|
def dao_update_annual_billing_for_current_and_future_years(service_id, free_sms_fragment_limit,
|
||||||
db.session.commit()
|
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(
|
return AnnualBilling.query.filter_by(
|
||||||
service_id=service_id,
|
service_id=service_id,
|
||||||
financial_year_start=year
|
financial_year_start=financial_year_start
|
||||||
).first()
|
).first()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
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)
|
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)
|
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
|
||||||
|
|||||||
@@ -53,12 +53,3 @@ def service_allowed_to_send_to(recipient, service, key_type):
|
|||||||
whitelist_members
|
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
|
|
||||||
|
|||||||
@@ -15,9 +15,11 @@ from tests.app.db import (
|
|||||||
|
|
||||||
from tests import create_authorization_header
|
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 uuid
|
||||||
|
import app
|
||||||
|
|
||||||
APR_2016_MONTH_START = datetime(2016, 3, 31, 23, 00, 00)
|
APR_2016_MONTH_START = datetime(2016, 3, 31, 23, 00, 00)
|
||||||
APR_2016_MONTH_END = datetime(2016, 4, 30, 22, 59, 59, 99999)
|
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']
|
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),
|
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
|
||||||
data=json.dumps(data),
|
data=json.dumps(data),
|
||||||
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
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))
|
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['financial_year_start'] == 2017
|
assert json_resp['financial_year_start'] == get_current_financial_year_start_year()
|
||||||
assert json_resp['free_sms_fragment_limit'] == 250
|
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),
|
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
|
||||||
data=json.dumps(data_old),
|
data=json.dumps(data),
|
||||||
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),
|
|
||||||
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
||||||
|
|
||||||
response_get = client.get(
|
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()])
|
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.status_code == 201
|
assert response.status_code == 201
|
||||||
assert response_get.status_code == 200
|
assert response_get.status_code == 200
|
||||||
assert json_resp['financial_year_start'] == 2016
|
assert json_resp['financial_year_start'] == 2016
|
||||||
assert json_resp['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_update_free_sms_fragment_limit(client, sample_service):
|
||||||
years = [2016, 2017, 2018]
|
current_year = get_current_financial_year_start_year()
|
||||||
limits = [1000, 2000, 3000]
|
data_new = {'financial_year_start': current_year, 'free_sms_fragment_limit': 9999}
|
||||||
|
|
||||||
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}
|
|
||||||
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
|
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
|
||||||
data=json.dumps(data_new),
|
data=json.dumps(data_new),
|
||||||
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
||||||
|
|
||||||
response_get = client.get(
|
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()])
|
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.status_code == 201
|
assert response.status_code == 201
|
||||||
assert response_get.status_code == 200
|
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['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):
|
def test_get_free_sms_fragment_limit_past_year_not_exist(client, sample_service):
|
||||||
response = client.get(
|
current_year = get_current_financial_year_start_year()
|
||||||
'service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
|
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()])
|
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
||||||
sms_list = json.loads(response.get_data(as_text=True))
|
json_resp = json.loads(res_get.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
|
assert res_get.status_code == 200
|
||||||
annual_billing = {'financial_year_start': past_year, 'free_sms_fragment_limit': 1000}
|
assert json_resp['financial_year_start'] == current_year - 1
|
||||||
response = client.post('service/{}/billing/free-sms-fragment-limit'.format(sample_service.id),
|
assert json_resp['free_sms_fragment_limit'] == 9999
|
||||||
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),
|
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()])
|
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 res_get.status_code == 200
|
||||||
assert json_resp['free_sms_fragment_limit'] == 1000
|
assert json_resp['financial_year_start'] == current_year + 2
|
||||||
|
assert json_resp['free_sms_fragment_limit'] == 10000
|
||||||
|
|
||||||
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
|
|
||||||
|
|||||||
@@ -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.models import AnnualBilling
|
||||||
from app.dao.annual_billing_dao import (
|
from app.dao.annual_billing_dao import (
|
||||||
dao_create_or_update_annual_billing_for_year,
|
dao_create_or_update_annual_billing_for_year,
|
||||||
dao_get_free_sms_fragment_limit_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):
|
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):
|
def test_dao_update_free_sms_fragment_limit(notify_db_session, sample_service):
|
||||||
year = 1999
|
|
||||||
old_limit = 1000
|
|
||||||
new_limit = 9999
|
new_limit = 9999
|
||||||
|
year = get_current_financial_year_start_year()
|
||||||
data = AnnualBilling(
|
dao_create_or_update_annual_billing_for_year(sample_service.id, new_limit, year)
|
||||||
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)
|
|
||||||
new_free_limit = dao_get_free_sms_fragment_limit_for_year(sample_service.id, 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
|
assert new_free_limit.free_sms_fragment_limit == new_limit
|
||||||
|
|
||||||
|
|
||||||
def test_create_then_get_annual_billing(notify_db_session, sample_service):
|
def test_create_annual_billing_not_specify_year(notify_db_session, sample_service):
|
||||||
years = [1999, 2001]
|
|
||||||
limits = [1000, 2000]
|
|
||||||
|
|
||||||
for i in [0, 1]:
|
dao_create_or_update_annual_billing_for_year(sample_service.id, 9999)
|
||||||
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)
|
|
||||||
|
|
||||||
free_limit = dao_get_annual_billing(sample_service.id)
|
free_limit = dao_get_free_sms_fragment_limit_for_year(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.free_sms_fragment_limit == 9999
|
||||||
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
|
def test_create_annual_billing_specify_year(notify_db_session, sample_service):
|
||||||
assert free_limit[1].financial_year_start == 2001
|
|
||||||
|
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
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ from app.models import (
|
|||||||
EMAIL_TYPE,
|
EMAIL_TYPE,
|
||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
INBOUND_SMS_TYPE,
|
INBOUND_SMS_TYPE,
|
||||||
KEY_TYPE_NORMAL
|
KEY_TYPE_NORMAL,
|
||||||
|
AnnualBilling,
|
||||||
)
|
)
|
||||||
from app.dao.users_dao import save_model_user
|
from app.dao.users_dao import save_model_user
|
||||||
from app.dao.notifications_dao import dao_create_notification, dao_created_scheduled_notification
|
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()
|
db.session.commit()
|
||||||
|
|
||||||
return reply_to
|
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
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ from tests.app.db import (
|
|||||||
create_service_sms_sender
|
create_service_sms_sender
|
||||||
)
|
)
|
||||||
from tests.app.db import create_user
|
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):
|
def test_get_service_list(client, service_factory):
|
||||||
|
|||||||
@@ -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 freezegun import freeze_time
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user