From 0b78c80624b94277961fe3d15bc501b0b7569ad5 Mon Sep 17 00:00:00 2001 From: Feilong Wang Date: Thu, 31 Aug 2017 16:11:07 +1200 Subject: [PATCH] Return numberic type for volume in /measurements Currently, the volume field in the response of /measurements API is returned as string type, which is not a best practice. This patch fixes it and changes the test accordingly. Change-Id: I394ae5a48f97f7efe1cad1ae7609b3fd6364bfae --- distil/service/api/v2/measurements.py | 3 ++- distil/tests/unit/api/test_api.py | 7 +++++-- distil/tests/unit/service/api/v2/test_measurements.py | 8 ++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/distil/service/api/v2/measurements.py b/distil/service/api/v2/measurements.py index a578918..97dc40b 100644 --- a/distil/service/api/v2/measurements.py +++ b/distil/service/api/v2/measurements.py @@ -17,6 +17,7 @@ import json from oslo_log import log as logging +from distil.common import constants from distil.common import general from distil.db import api as db_api @@ -35,7 +36,7 @@ def _build_project_dict(project, usage): for entry in usage: service = {'name': entry.get('service'), - 'volume': str(entry.get('volume')), + 'volume': float(entry.get('volume')), 'unit': entry.get('unit')} resource = project_dict['resources'][entry.get('resource_id')] diff --git a/distil/tests/unit/api/test_api.py b/distil/tests/unit/api/test_api.py index b24fcdb..a15aedc 100644 --- a/distil/tests/unit/api/test_api.py +++ b/distil/tests/unit/api/test_api.py @@ -102,8 +102,11 @@ class TestAPI(base.APITest): db_api.resource_add( default_project, res_id, {'type': 'Virtual Machine'} ) + # NOTE(flwang): Based on current data model of usage entry: + # volume = Column(Numeric(precision=20, scale=2), nullable=False) + # it's only necessary to test the case of scale=2. db_api.usage_add( - default_project, res_id, {'instance': 100}, 'hour', + default_project, res_id, {'instance': 100.12}, 'hour', datetime.strptime(start, constants.iso_time), datetime.strptime(end, constants.iso_time), ) @@ -131,7 +134,7 @@ class TestAPI(base.APITest): 'type': 'Virtual Machine', 'services': [{ 'name': 'instance', - 'volume': '100.00', + 'volume': 100.12, 'unit': 'hour' }] } diff --git a/distil/tests/unit/service/api/v2/test_measurements.py b/distil/tests/unit/service/api/v2/test_measurements.py index 9ee56de..38d1dec 100644 --- a/distil/tests/unit/service/api/v2/test_measurements.py +++ b/distil/tests/unit/service/api/v2/test_measurements.py @@ -42,13 +42,13 @@ class MeasurementsTest(base.DistilWithDbTestCase): { 'resource_id': '111', 'service': 'srv1', - 'volume': 10, + 'volume': 10.12, 'unit': 'byte', }, { 'resource_id': '222', 'service': 'srv2', - 'volume': 20, + 'volume': 20.1, 'unit': 'byte', } ] @@ -76,13 +76,13 @@ class MeasurementsTest(base.DistilWithDbTestCase): '111': { 'name': 'resource1', 'services': [ - {'name': 'srv1', 'volume': '10', 'unit': 'byte'} + {'name': 'srv1', 'volume': 10.12, 'unit': 'byte'} ] }, '222': { 'name': 'resource2', 'services': [ - {'name': 'srv2', 'volume': '20', 'unit': 'byte'} + {'name': 'srv2', 'volume': 20.1, 'unit': 'byte'} ] } }