modified serialized method and schema

This commit is contained in:
venusbb
2017-10-26 13:25:11 +01:00
parent e88b311c8e
commit c10cde6b22
5 changed files with 31 additions and 14 deletions

View File

@@ -8,7 +8,7 @@ create_or_update_free_sms_fragment_limit_schema = {
"title": "Create", "title": "Create",
"properties": { "properties": {
"free_sms_fragment_limit": {"type": "integer", "minimum": 1}, "free_sms_fragment_limit": {"type": "integer", "minimum": 1},
"financial_year_start": {"type": "integer", "minimum": 1} "financial_year_start": {"type": "integer", "minimum": 2016}
}, },
"required": ["free_sms_fragment_limit", "financial_year_start"] "required": ["free_sms_fragment_limit", "financial_year_start"]
} }

View File

@@ -105,13 +105,13 @@ def get_free_sms_fragment_limit(service_id):
if len(results) == 0: if len(results) == 0:
raise InvalidRequest('no annual billing information for this service', status_code=404) raise InvalidRequest('no annual billing information for this service', status_code=404)
return jsonify(data=[row.serialize() for row in results]), 200 return jsonify(data=[row.serialize_free_sms_items() for row in results]), 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: if result is None:
raise InvalidRequest('no free-sms-fragment-limit-info for this service and year', status_code=404) raise InvalidRequest('no free-sms-fragment-limit-info for this service and year', status_code=404)
return jsonify(data=result.serialize()), 200 return jsonify(data=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

@@ -278,12 +278,29 @@ class AnnualBilling(db.Model):
UniqueConstraint('financial_year_start', 'service_id', name='ix_annual_billing_service_id') UniqueConstraint('financial_year_start', 'service_id', name='ix_annual_billing_service_id')
service = db.relationship(Service, backref=db.backref("annual_billing", uselist=True)) service = db.relationship(Service, backref=db.backref("annual_billing", uselist=True))
def serialize(self): def serialize_free_sms_items(self):
return { return {
'free_sms_fragment_limit': self.free_sms_fragment_limit, 'free_sms_fragment_limit': self.free_sms_fragment_limit,
'financial_year_start': self.financial_year_start, 'financial_year_start': self.financial_year_start,
} }
def serialize(self):
def serialize_service():
return {
"id": str(self.service_id),
"name": self.service.name
}
return{
"id": str(self.id),
'free_sms_fragment_limit': self.free_sms_fragment_limit,
'service_id': self.service_id,
'financial_year_start': self.financial_year_start,
"created_at": self.created_at.strftime(DATETIME_FORMAT),
"updated_at": self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None,
"service": serialize_service() if self.service else None,
}
class InboundNumber(db.Model): class InboundNumber(db.Model):
__tablename__ = "inbound_numbers" __tablename__ = "inbound_numbers"

View File

@@ -288,30 +288,30 @@ def test_create_free_sms_fragment_limit(client, sample_service):
def test_update_free_sms_fragment_limit(client, sample_service): def test_update_free_sms_fragment_limit(client, sample_service):
data_old = {'financial_year_start': 2015, 'free_sms_fragment_limit': 1000} data_old = {'financial_year_start': 2016, 'free_sms_fragment_limit': 1000}
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_old),
headers=[('Content-Type', 'application/json'), create_authorization_header()]) headers=[('Content-Type', 'application/json'), create_authorization_header()])
data_new = {'financial_year_start': 2015, 'free_sms_fragment_limit': 9999} data_new = {'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_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?financial_year_start=2015'.format(sample_service.id), 'service/{}/billing/free-sms-fragment-limit?financial_year_start=2016'.format(sample_service.id),
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['data']['financial_year_start'] == 2015 assert json_resp['data']['financial_year_start'] == 2016
assert json_resp['data']['free_sms_fragment_limit'] == 9999 assert json_resp['data']['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):
years = [2015, 2016, 2017] years = [2016, 2017, 2018]
limits = [1000, 2000, 3000] limits = [1000, 2000, 3000]
for i in range(0, len(years)): for i in range(0, len(years)):
@@ -330,7 +330,7 @@ def test_get_free_sms_fragment_limit_year_return_correct_data(client, sample_ser
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):
years = [2015, 2016, 2017] years = [2016, 2017, 2018]
limits = [1000, 2000, 3000] limits = [1000, 2000, 3000]
for i in range(0, len(years)): for i in range(0, len(years)):

View File

@@ -3,13 +3,13 @@ from freezegun import freeze_time
# see get_financial_year for conversion of financial years. # see get_financial_year for conversion of financial years.
@freeze_time("2001-03-31 22:59:59.999999") @freeze_time("2017-03-31 22:59:59.999999")
def test_get_current_financial_year_start_year_before_march(): def test_get_current_financial_year_start_year_before_march():
current_fy = get_current_financial_year_start_year() current_fy = get_current_financial_year_start_year()
assert current_fy == 2000 assert current_fy == 2016
@freeze_time("2001-03-31 23:00:00.000000") @freeze_time("2017-03-31 23:00:00.000000")
def test_get_current_financial_year_start_year_after_april(): def test_get_current_financial_year_start_year_after_april():
current_fy = get_current_financial_year_start_year() current_fy = get_current_financial_year_start_year()
assert current_fy == 2001 assert current_fy == 2017