From: tierno Date: Wed, 17 Jun 2020 07:42:30 +0000 (+0000) Subject: allow with kwargs (option override) to create nested objects X-Git-Tag: release-v8.0-start~5 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F98%2F9098%2F4;p=osm%2FNBI.git allow with kwargs (option override) to create nested objects Change-Id: I4ca24e1117f86eaf6e4d18863c478a4d536e2374 Signed-off-by: tierno --- diff --git a/osm_nbi/base_topic.py b/osm_nbi/base_topic.py index 2986b81..130d4fc 100644 --- a/osm_nbi/base_topic.py +++ b/osm_nbi/base_topic.py @@ -325,12 +325,23 @@ class BaseTopic: update_content = update_content[kitem_old] if isinstance(update_content, dict): kitem_old = kitem + if not isinstance(update_content.get(kitem_old), (dict, list)): + update_content[kitem_old] = {} elif isinstance(update_content, list): + # key must be an index of the list, must be integer kitem_old = int(kitem) + # if index greater than list, extend the list + if kitem_old >= len(update_content): + update_content += [None] * (kitem_old - len(update_content) + 1) + if not isinstance(update_content[kitem_old], (dict, list)): + update_content[kitem_old] = {} else: raise EngineException( "Invalid query string '{}'. Descriptor is not a list nor dict at '{}'".format(k, kitem)) - update_content[kitem_old] = v if not yaml_format else safe_load(v) + if v is None: + del update_content[kitem_old] + else: + update_content[kitem_old] = v if not yaml_format else safe_load(v) except KeyError: raise EngineException( "Invalid query string '{}'. Descriptor does not contain '{}'".format(k, kitem_old)) diff --git a/osm_nbi/tests/test_base_topic.py b/osm_nbi/tests/test_base_topic.py new file mode 100755 index 0000000..a3fc32c --- /dev/null +++ b/osm_nbi/tests/test_base_topic.py @@ -0,0 +1,73 @@ +#! /usr/bin/python3 +# -*- coding: utf-8 -*- + +# 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. + +__author__ = "Alfonso Tierno, alfonso.tiernosepulveda@telefonica.com" +__date__ = "2020-06-17" + +import unittest +from unittest import TestCase +# from unittest.mock import Mock +# from osm_common import dbbase, fsbase, msgbase +from osm_nbi.base_topic import BaseTopic, EngineException + + +class Test_BaseTopic(TestCase): + + @classmethod + def setUpClass(cls): + cls.test_name = "test-base-topic" + + @classmethod + def tearDownClass(cls): + pass + + def setUp(self): + pass + # self.db = Mock(dbbase.DbBase()) + # self.fs = Mock(fsbase.FsBase()) + # self.msg = Mock(msgbase.MsgBase()) + # self.auth = Mock(authconn.Authconn(None, None, None)) + + def test_update_input_with_kwargs(self): + + test_set = ( + # (descriptor content, kwargs, expected descriptor (None=fails), message) + ({"a": {"none": None}}, {"a.b.num": "v"}, {"a": {"none": None, "b": {"num": "v"}}}, "create dict"), + ({"a": {"none": None}}, {"a.none.num": "v"}, {"a": {"none": {"num": "v"}}}, "create dict over none"), + ({"a": {"b": {"num": 4}}}, {"a.b.num": "v"}, {"a": {"b": {"num": "v"}}}, "replace_number"), + ({"a": {"b": {"num": 4}}}, {"a.b.num.c.d": "v"}, {"a": {"b": {"num": {"c": {"d": "v"}}}}}, + "create dict over number"), + ({"a": {"b": {"num": 4}}}, {"a.b": "v"}, {"a": {"b": "v"}}, "replace dict with a string"), + ({"a": {"b": {"num": 4}}}, {"a.b": None}, {"a": {}}, "replace dict with None"), + ({"a": [{"b": {"num": 4}}]}, {"a.b.num": "v"}, None, "create dict over list should fail"), + ({"a": [{"b": {"num": 4}}]}, {"a.0.b.num": "v"}, {"a": [{"b": {"num": "v"}}]}, "set list"), + ({"a": [{"b": {"num": 4}}]}, {"a.3.b.num": "v"}, + {"a": [{"b": {"num": 4}}, None, None, {"b": {"num": "v"}}]}, "expand list"), + ({"a": [[4]]}, {"a.0.0": "v"}, {"a": [["v"]]}, "set nested list"), + ({"a": [[4]]}, {"a.0.2": "v"}, {"a": [[4, None, "v"]]}, "expand nested list"), + ({"a": [[4]]}, {"a.2.2": "v"}, {"a": [[4], None, {"2": "v"}]}, "expand list and add number key"), + ({"a": None}, {"b.c": "v"}, {"a": None, "b": {"c": "v"}}, "expand at root"), + ) + for desc, kwargs, expected, message in test_set: + if expected is None: + self.assertRaises(EngineException, BaseTopic._update_input_with_kwargs, desc, kwargs) + else: + BaseTopic._update_input_with_kwargs(desc, kwargs) + self.assertEqual(desc, expected, message) + + +if __name__ == '__main__': + unittest.main()