def test_dao_update_annual_billing_for_future_years(notify_db_session, sample_service): current_year = get_current_financial_year_start_year() limits = [1, 2, 3, 4] 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_future_years(sample_service.id, 9999, current_year) assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year - 1).free_sms_fragment_limit == 1 # current year is not created assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year) is None assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year + 1).free_sms_fragment_limit == 9999 assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year + 2).free_sms_fragment_limit == 9999
def get_free_sms_fragment_limit(service_id): financial_year_start = request.args.get('financial_year_start') annual_billing = dao_get_free_sms_fragment_limit_for_year( service_id, financial_year_start) 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, # and return that number. If all fails, we return InvalidRequest. sms_list = dao_get_all_free_sms_fragment_limit(service_id) 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(): # return the earliest historical entry annual_billing = sms_list[0] # The oldest entry else: annual_billing = sms_list[-1] # The newest entry annual_billing = dao_create_or_update_annual_billing_for_year( service_id, annual_billing.free_sms_fragment_limit, financial_year_start) return jsonify(annual_billing.serialize_free_sms_items()), 200
def test_create_annual_billing(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_free_sms_fragment_limit(notify_db_session, sample_service): new_limit = 9999 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_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, 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={}".format(sample_service.id, current_year + 2), headers=[("Content-Type", "application/json"), create_authorization_header()], ) json_resp = json.loads(res_get.get_data(as_text=True)) assert res_get.status_code == 200 assert json_resp["financial_year_start"] == current_year + 2 assert json_resp["free_sms_fragment_limit"] == 10000
def test_update_free_sms_fragment_limit_data(client, sample_service): current_year = get_current_financial_year_start_year() create_annual_billing(sample_service.id, free_sms_fragment_limit=250000, financial_year_start=current_year - 1) update_free_sms_fragment_limit_data(sample_service.id, 9999, current_year) annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year) assert annual_billing.free_sms_fragment_limit == 9999
def test_create_or_update_free_sms_fragment_limit_past_year_doenst_update_other_years( admin_request, sample_service, update_existing ): current_year = get_current_financial_year_start_year() create_annual_billing(sample_service.id, 1, current_year) if update_existing: create_annual_billing(sample_service.id, 1, current_year - 1) data = {"financial_year_start": current_year - 1, "free_sms_fragment_limit": 9999} admin_request.post( "billing.create_or_update_free_sms_fragment_limit", service_id=sample_service.id, _data=data, _expected_status=201, ) assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year - 1).free_sms_fragment_limit == 9999 assert dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year).free_sms_fragment_limit == 1
def test_create_free_sms_fragment_limit_current_year_updates_future_years(admin_request, sample_service): current_year = get_current_financial_year_start_year() future_billing = create_annual_billing(sample_service.id, 1, current_year + 1) admin_request.post( 'billing.create_or_update_free_sms_fragment_limit', service_id=sample_service.id, _data={'free_sms_fragment_limit': 9999}, _expected_status=201 ) current_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year) assert future_billing.free_sms_fragment_limit == 9999 assert current_billing.financial_year_start == current_year assert current_billing.free_sms_fragment_limit == 9999
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) 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), headers=[('Content-Type', 'application/json'), create_authorization_header()]) json_resp = json.loads(res_get.get_data(as_text=True)) assert res_get.status_code == 200 assert json_resp['financial_year_start'] == current_year - 1 assert json_resp['free_sms_fragment_limit'] == 9999
def get_free_sms_fragment_limit(service_id): financial_year_start = request.args.get('financial_year_start') annual_billing = dao_get_free_sms_fragment_limit_for_year( service_id, financial_year_start) if annual_billing is None: service = Service.query.get(service_id) # An entry does not exist in annual_billing table for that service and year. # Set the annual billing to the default free allowance based on the organisation type of the service. annual_billing = set_default_free_allowance_for_service( service=service, year_start=int(financial_year_start) if financial_year_start else None) return jsonify(annual_billing.serialize_free_sms_items()), 200