nova/nova/tests/unit/test_json_ref.py
Takashi NATSUME 1eec451b1b Tests: autospecs all the mock.patch usages
By default, mock.patch's autospec argument is None, meaning that
there's no signature checking for the patched methods and functions.

oslotest.mock_fixture.patch_mock_module fixes a few issues within
mock.patch functions, as well as setting autospec=True by default,
unless otherwise specified or new_callable, create, spec arguments
are passed in.

Co-Authored-By: Claudiu Belu <cbelu@cloudbasesolutions.com>
Change-Id: I4484e63c97bd1cdde3d88855eabe7545784f365e
Closes-Bug: #1735588
2019-08-19 09:43:10 -05:00

171 lines
6.6 KiB
Python

# 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.
import copy
import mock
from nova import test
from nova.tests import json_ref
class TestJsonRef(test.NoDBTestCase):
def test_update_dict_recursively(self):
input = {'foo': 1,
'bar': 13,
'a list': [],
'nesting': {
'baz': 42,
'foobar': 121
}}
d = copy.deepcopy(input)
json_ref._update_dict_recursively(d, {})
self.assertDictEqual(input, d)
d = copy.deepcopy(input)
json_ref._update_dict_recursively(d, {'foo': 111,
'new_key': 1,
'nesting': {
'baz': 142,
'new_nested': 1
}})
expected = copy.deepcopy(input)
expected['foo'] = 111
expected['new_key'] = 1
expected['nesting']['baz'] = 142
expected['nesting']['new_nested'] = 1
self.assertDictEqual(expected, d)
d = copy.deepcopy(input)
json_ref._update_dict_recursively(d, {'nesting': 1})
expected = copy.deepcopy(input)
expected['nesting'] = 1
self.assertDictEqual(expected, d)
d = copy.deepcopy(input)
@mock.patch('oslo_serialization.jsonutils.load')
@mock.patch('six.moves.builtins.open', new_callable=mock.mock_open())
def test_resolve_ref(self, mock_open, mock_json_load):
mock_json_load.return_value = {'baz': 13}
actual = json_ref.resolve_refs(
{'foo': 1,
'bar': {'$ref': 'another.json#'}},
'some/base/path/')
self.assertDictEqual({'foo': 1,
'bar': {'baz': 13}},
actual)
mock_open.assert_called_once_with('some/base/path/another.json', 'r+b')
@mock.patch('oslo_serialization.jsonutils.load')
@mock.patch('six.moves.builtins.open', new_callable=mock.mock_open())
def test_resolve_ref_recursively(self, mock_open, mock_json_load):
mock_json_load.side_effect = [
# this is the content of direct_ref.json
{'baz': 13,
'nesting': {'$ref': 'subdir/nested_ref.json#'}},
# this is the content of subdir/nested_ref.json
{'a deep key': 'happiness'}]
actual = json_ref.resolve_refs(
{'foo': 1,
'bar': {'$ref': 'direct_ref.json#'}},
'some/base/path/')
self.assertDictEqual({'foo': 1,
'bar': {'baz': 13,
'nesting':
{'a deep key': 'happiness'}}},
actual)
mock_open.assert_any_call('some/base/path/direct_ref.json', 'r+b')
mock_open.assert_any_call('some/base/path/subdir/nested_ref.json',
'r+b')
@mock.patch('oslo_serialization.jsonutils.load')
@mock.patch('six.moves.builtins.open', new_callable=mock.mock_open())
def test_resolve_ref_with_override(self, mock_open, mock_json_load):
mock_json_load.return_value = {'baz': 13,
'boo': 42}
actual = json_ref.resolve_refs(
{'foo': 1,
'bar': {'$ref': 'another.json#',
'boo': 0}},
'some/base/path/')
self.assertDictEqual({'foo': 1,
'bar': {'baz': 13,
'boo': 0}},
actual)
mock_open.assert_called_once_with('some/base/path/another.json', 'r+b')
@mock.patch('oslo_serialization.jsonutils.load')
@mock.patch('six.moves.builtins.open', new_callable=mock.mock_open())
def test_resolve_ref_with_nested_override(self, mock_open, mock_json_load):
mock_json_load.return_value = {'baz': 13,
'boo': {'a': 1,
'b': 2}}
actual = json_ref.resolve_refs(
{'foo': 1,
'bar': {'$ref': 'another.json#',
'boo': {'b': 3,
'c': 13}}},
'some/base/path/')
self.assertDictEqual({'foo': 1,
'bar': {'baz': 13,
'boo': {'a': 1,
'b': 3,
'c': 13}}},
actual)
mock_open.assert_called_once_with('some/base/path/another.json', 'r+b')
@mock.patch('oslo_serialization.jsonutils.load')
@mock.patch('six.moves.builtins.open', new_callable=mock.mock_open())
def test_resolve_ref_with_override_having_refs(self, mock_open,
mock_json_load):
mock_json_load.side_effect = [
{'baz': 13,
'boo': 42},
{'something': 0}]
actual = json_ref.resolve_refs(
{'foo': 1,
'bar': {'$ref': 'another.json#',
'boo': {'$ref': 'override_ref.json#'}}},
'some/base/path/')
self.assertDictEqual({'foo': 1,
'bar': {'baz': 13,
'boo': {'something': 0}}},
actual)
self.assertEqual(2, mock_open.call_count)
# any_order=True is needed as context manager calls also done on open
mock_open.assert_has_calls(
[mock.call('some/base/path/another.json', 'r+b'),
mock.call('some/base/path/override_ref.json', 'r+b')],
any_order=True)
def test_ref_with_json_path_not_supported(self):
self.assertRaises(
NotImplementedError, json_ref.resolve_refs,
{'foo': 1,
'bar': {'$ref': 'another.json#/key-in-another',
'boo': {'b': 3,
'c': 13}}},
'some/base/path/')