From c10cde6b224b698e936ad76bc13f97ea84d470af Mon Sep 17 00:00:00 2001 From: venusbb Date: Thu, 26 Oct 2017 13:25:11 +0100 Subject: [PATCH] modified serialized method and schema --- app/billing/billing_schemas.py | 2 +- app/billing/rest.py | 4 ++-- app/models.py | 19 ++++++++++++++++++- tests/app/billing/test_billing.py | 12 ++++++------ tests/app/service/test_utils.py | 8 ++++---- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/app/billing/billing_schemas.py b/app/billing/billing_schemas.py index 0cd35208f..f9f592e7d 100644 --- a/app/billing/billing_schemas.py +++ b/app/billing/billing_schemas.py @@ -8,7 +8,7 @@ create_or_update_free_sms_fragment_limit_schema = { "title": "Create", "properties": { "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"] } diff --git a/app/billing/rest.py b/app/billing/rest.py index 7d73bc098..f4c337903 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -105,13 +105,13 @@ def get_free_sms_fragment_limit(service_id): if len(results) == 0: 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: 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()), 200 + return jsonify(data=result.serialize_free_sms_items()), 200 @billing_blueprint.route('/free-sms-fragment-limit', methods=["POST"]) diff --git a/app/models.py b/app/models.py index d59164896..485edf944 100644 --- a/app/models.py +++ b/app/models.py @@ -278,12 +278,29 @@ class AnnualBilling(db.Model): UniqueConstraint('financial_year_start', 'service_id', name='ix_annual_billing_service_id') service = db.relationship(Service, backref=db.backref("annual_billing", uselist=True)) - def serialize(self): + def serialize_free_sms_items(self): return { 'free_sms_fragment_limit': self.free_sms_fragment_limit, '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): __tablename__ = "inbound_numbers" diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index 737ccab11..e6655f293 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -288,30 +288,30 @@ def test_create_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), data=json.dumps(data_old), 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), data=json.dumps(data_new), headers=[('Content-Type', 'application/json'), create_authorization_header()]) 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()]) 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['data']['financial_year_start'] == 2015 + assert json_resp['data']['financial_year_start'] == 2016 assert json_resp['data']['free_sms_fragment_limit'] == 9999 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] 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): - years = [2015, 2016, 2017] + years = [2016, 2017, 2018] limits = [1000, 2000, 3000] for i in range(0, len(years)): diff --git a/tests/app/service/test_utils.py b/tests/app/service/test_utils.py index 09ce33f7d..4d59af22b 100644 --- a/tests/app/service/test_utils.py +++ b/tests/app/service/test_utils.py @@ -3,13 +3,13 @@ from freezegun import freeze_time # 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(): 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(): current_fy = get_current_financial_year_start_year() - assert current_fy == 2001 + assert current_fy == 2017