allow with kwargs (option override) to create nested objects
[osm/NBI.git] / osm_nbi / tests / test_base_topic.py
1 #! /usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 __author__ = "Alfonso Tierno, alfonso.tiernosepulveda@telefonica.com"
18 __date__ = "2020-06-17"
19
20 import unittest
21 from unittest import TestCase
22 # from unittest.mock import Mock
23 # from osm_common import dbbase, fsbase, msgbase
24 from osm_nbi.base_topic import BaseTopic, EngineException
25
26
27 class Test_BaseTopic(TestCase):
28
29 @classmethod
30 def setUpClass(cls):
31 cls.test_name = "test-base-topic"
32
33 @classmethod
34 def tearDownClass(cls):
35 pass
36
37 def setUp(self):
38 pass
39 # self.db = Mock(dbbase.DbBase())
40 # self.fs = Mock(fsbase.FsBase())
41 # self.msg = Mock(msgbase.MsgBase())
42 # self.auth = Mock(authconn.Authconn(None, None, None))
43
44 def test_update_input_with_kwargs(self):
45
46 test_set = (
47 # (descriptor content, kwargs, expected descriptor (None=fails), message)
48 ({"a": {"none": None}}, {"a.b.num": "v"}, {"a": {"none": None, "b": {"num": "v"}}}, "create dict"),
49 ({"a": {"none": None}}, {"a.none.num": "v"}, {"a": {"none": {"num": "v"}}}, "create dict over none"),
50 ({"a": {"b": {"num": 4}}}, {"a.b.num": "v"}, {"a": {"b": {"num": "v"}}}, "replace_number"),
51 ({"a": {"b": {"num": 4}}}, {"a.b.num.c.d": "v"}, {"a": {"b": {"num": {"c": {"d": "v"}}}}},
52 "create dict over number"),
53 ({"a": {"b": {"num": 4}}}, {"a.b": "v"}, {"a": {"b": "v"}}, "replace dict with a string"),
54 ({"a": {"b": {"num": 4}}}, {"a.b": None}, {"a": {}}, "replace dict with None"),
55 ({"a": [{"b": {"num": 4}}]}, {"a.b.num": "v"}, None, "create dict over list should fail"),
56 ({"a": [{"b": {"num": 4}}]}, {"a.0.b.num": "v"}, {"a": [{"b": {"num": "v"}}]}, "set list"),
57 ({"a": [{"b": {"num": 4}}]}, {"a.3.b.num": "v"},
58 {"a": [{"b": {"num": 4}}, None, None, {"b": {"num": "v"}}]}, "expand list"),
59 ({"a": [[4]]}, {"a.0.0": "v"}, {"a": [["v"]]}, "set nested list"),
60 ({"a": [[4]]}, {"a.0.2": "v"}, {"a": [[4, None, "v"]]}, "expand nested list"),
61 ({"a": [[4]]}, {"a.2.2": "v"}, {"a": [[4], None, {"2": "v"}]}, "expand list and add number key"),
62 ({"a": None}, {"b.c": "v"}, {"a": None, "b": {"c": "v"}}, "expand at root"),
63 )
64 for desc, kwargs, expected, message in test_set:
65 if expected is None:
66 self.assertRaises(EngineException, BaseTopic._update_input_with_kwargs, desc, kwargs)
67 else:
68 BaseTopic._update_input_with_kwargs(desc, kwargs)
69 self.assertEqual(desc, expected, message)
70
71
72 if __name__ == '__main__':
73 unittest.main()