- 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:
venusbb
2017-11-02 12:19:17 +00:00
parent cc3d5ba8d1
commit 6f7793d761
10 changed files with 186 additions and 230 deletions

View File

@@ -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