From 4f3637cce8ae324b8765e3d9e047a104164056fe Mon Sep 17 00:00:00 2001 From: Lingxian Kong Date: Thu, 1 Jun 2017 11:53:23 +1200 Subject: [PATCH] Add unit tests for service modules Change-Id: Ic02c481759e1027078793865205849d1ab597107 --- .../unit/service/api/v2/test_products.py | 31 ++++++++ .../unit/service/api/v2/test_quotations.py | 73 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 distil/tests/unit/service/api/v2/test_products.py create mode 100644 distil/tests/unit/service/api/v2/test_quotations.py diff --git a/distil/tests/unit/service/api/v2/test_products.py b/distil/tests/unit/service/api/v2/test_products.py new file mode 100644 index 0000000..b400fc5 --- /dev/null +++ b/distil/tests/unit/service/api/v2/test_products.py @@ -0,0 +1,31 @@ +# Copyright (C) 2017 Catalyst IT Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock + +from distil.service.api.v2 import products +from distil.tests.unit import base + + +class ProductsTest(base.DistilTestCase): + @mock.patch('stevedore.driver.DriverManager') + def test_get_products(self, mock_driver): + fake_region = 'nz-1' + + driver_manager = mock_driver.return_value + driver = driver_manager.driver + + products.get_products([fake_region]) + + driver.get_products.assert_called_once_with([fake_region]) diff --git a/distil/tests/unit/service/api/v2/test_quotations.py b/distil/tests/unit/service/api/v2/test_quotations.py new file mode 100644 index 0000000..f537491 --- /dev/null +++ b/distil/tests/unit/service/api/v2/test_quotations.py @@ -0,0 +1,73 @@ +# Copyright (C) 2017 Catalyst IT Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +import json + +import mock + +from distil.service.api.v2 import quotations +from distil.tests.unit import base + + +class QuotationsTest(base.DistilWithDbTestCase): + @mock.patch('stevedore.driver.DriverManager') + @mock.patch('distil.db.api.project_get') + @mock.patch('distil.db.api.usage_get') + @mock.patch('distil.db.api.resource_get_by_ids') + def test_get_quotations(self, mock_get_resources, mock_get_usage, + mock_get_project, mock_driver): + self.override_config('keystone_authtoken', region_name='region-1') + + class Project(object): + def __init__(self, id, name): + self.id = id + self.name = name + + mock_get_project.return_value = Project('123', 'fake_project') + + usage = [ + { + 'resource_id': '111', + 'service': 'srv1', + 'volume': 10, + 'unit': 'byte', + }, + { + 'resource_id': '222', + 'service': 'srv2', + 'volume': 20, + 'unit': 'byte', + } + ] + mock_get_usage.return_value = usage + + class Resource(object): + def __init__(self, id, info): + self.id = id + self.info = info + + res1 = Resource('111', json.dumps({'name': 'resource1'})) + res2 = Resource('222', json.dumps({'name': 'resource2'})) + mock_get_resources.return_value = [res1, res2] + + driver_manager = mock_driver.return_value + driver = driver_manager.driver + + quotations.get_quotations('123', detailed=False) + + driver.get_quotations.assert_called_once_with( + 'region-1', '123', + measurements=usage, + resources=[res1, res2], + detailed=False + )