blob: 5107680064a9422ecf7b1465a53cb3760708e23f [file] [log] [blame]
tiernoac55f062020-06-17 07:42:30 +00001#! /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
20import unittest
21from unittest import TestCase
garciadeblas4568a372021-03-24 09:19:48 +010022
tiernoac55f062020-06-17 07:42:30 +000023# from unittest.mock import Mock
24# from osm_common import dbbase, fsbase, msgbase
25from osm_nbi.base_topic import BaseTopic, EngineException
26
27
28class Test_BaseTopic(TestCase):
tiernoac55f062020-06-17 07:42:30 +000029 @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)
garciadeblas4568a372021-03-24 09:19:48 +010048 (
49 {"a": {"none": None}},
50 {"a.b.num": "v"},
51 {"a": {"none": None, "b": {"num": "v"}}},
52 "create dict",
53 ),
54 (
55 {"a": {"none": None}},
56 {"a.none.num": "v"},
57 {"a": {"none": {"num": "v"}}},
58 "create dict over none",
59 ),
60 (
61 {"a": {"b": {"num": 4}}},
62 {"a.b.num": "v"},
63 {"a": {"b": {"num": "v"}}},
64 "replace_number",
65 ),
66 (
67 {"a": {"b": {"num": 4}}},
68 {"a.b.num.c.d": "v"},
69 {"a": {"b": {"num": {"c": {"d": "v"}}}}},
70 "create dict over number",
71 ),
72 (
73 {"a": {"b": {"num": 4}}},
74 {"a.b": "v"},
75 {"a": {"b": "v"}},
76 "replace dict with a string",
77 ),
78 (
79 {"a": {"b": {"num": 4}}},
80 {"a.b": None},
81 {"a": {}},
82 "replace dict with None",
83 ),
84 (
85 {"a": [{"b": {"num": 4}}]},
86 {"a.b.num": "v"},
87 None,
88 "create dict over list should fail",
89 ),
90 (
91 {"a": [{"b": {"num": 4}}]},
92 {"a.0.b.num": "v"},
93 {"a": [{"b": {"num": "v"}}]},
94 "set list",
95 ),
96 (
97 {"a": [{"b": {"num": 4}}]},
98 {"a.3.b.num": "v"},
99 {"a": [{"b": {"num": 4}}, None, None, {"b": {"num": "v"}}]},
100 "expand list",
101 ),
tiernoac55f062020-06-17 07:42:30 +0000102 ({"a": [[4]]}, {"a.0.0": "v"}, {"a": [["v"]]}, "set nested list"),
garciadeblas4568a372021-03-24 09:19:48 +0100103 (
104 {"a": [[4]]},
105 {"a.0.2": "v"},
106 {"a": [[4, None, "v"]]},
107 "expand nested list",
108 ),
109 (
110 {"a": [[4]]},
111 {"a.2.2": "v"},
112 {"a": [[4], None, {"2": "v"}]},
113 "expand list and add number key",
114 ),
tiernoac55f062020-06-17 07:42:30 +0000115 ({"a": None}, {"b.c": "v"}, {"a": None, "b": {"c": "v"}}, "expand at root"),
116 )
117 for desc, kwargs, expected, message in test_set:
118 if expected is None:
garciadeblas4568a372021-03-24 09:19:48 +0100119 self.assertRaises(
120 EngineException, BaseTopic._update_input_with_kwargs, desc, kwargs
121 )
tiernoac55f062020-06-17 07:42:30 +0000122 else:
123 BaseTopic._update_input_with_kwargs(desc, kwargs)
124 self.assertEqual(desc, expected, message)
125
126
garciadeblas4568a372021-03-24 09:19:48 +0100127if __name__ == "__main__":
tiernoac55f062020-06-17 07:42:30 +0000128 unittest.main()