From f9414c99e853aa740eace00c14b47874cc691ec8 Mon Sep 17 00:00:00 2001 From: hewei Date: Thu, 15 Nov 2018 20:44:29 +0800 Subject: [PATCH] Fix error in resource_help and add UT The func should be used in the condition of 'policies to policy' Add test UT in test_resource_helper Change-Id: I41fa8b0b029398ea9c3977fabd649a094a9538ac --- tacker/api/v1/resource_helper.py | 7 ++- .../tests/unit/api/v1/test_resource_helper.py | 61 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tacker/tests/unit/api/v1/test_resource_helper.py diff --git a/tacker/api/v1/resource_helper.py b/tacker/api/v1/resource_helper.py index 0f1c482e1..3390d4dad 100644 --- a/tacker/api/v1/resource_helper.py +++ b/tacker/api/v1/resource_helper.py @@ -30,7 +30,12 @@ def build_plural_mappings(special_mappings, resource_map): """ plural_mappings = {} for plural in resource_map: - singular = special_mappings.get(plural, plural[:-1]) + singular = special_mappings.get(plural) + if not singular: + if plural.endswith('ies'): + singular = "%sy" % plural[:-3] + else: + singular = plural[:-1] plural_mappings[plural] = singular return plural_mappings diff --git a/tacker/tests/unit/api/v1/test_resource_helper.py b/tacker/tests/unit/api/v1/test_resource_helper.py new file mode 100644 index 000000000..026823f5f --- /dev/null +++ b/tacker/tests/unit/api/v1/test_resource_helper.py @@ -0,0 +1,61 @@ +# Copyright (c) 2014-2018 China Mobile (SuZhou) Software Technology Co.,Ltd. +# All Rights Reserved. +# +# 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. + +from tacker.api.v1.resource_helper import build_plural_mappings +from tacker.tests import base + + +class ResourceHelperTestCase(base.BaseTestCase): + + def test_build_plural_mappings(self): + special_mappings = {} + resource_map = { + 'vims': { + 'id': { + 'allow_post': False, + 'allow_put': False, + } + }, + 'vnffgs': { + 'id': { + 'allow_post': False, + 'allow_put': False, + } + }, + } + + expected_res = {'vnffgs': 'vnffg', 'vims': 'vim'} + result = build_plural_mappings(special_mappings, resource_map) + self.assertEqual(expected_res, result) + + def test_build_plural_mappings_with_suffix_y(self): + special_mappings = {} + resource_map = { + 'policies': { + 'id': { + 'allow_post': False, + } + }, + 'vnffgs': { + 'id': { + 'allow_post': False, + 'allow_put': False, + } + }, + } + + expected_res = {'vnffgs': 'vnffg', 'policies': 'policy'} + result = build_plural_mappings(special_mappings, resource_map) + self.assertEqual(expected_res, result)