From a937295708bce2c10bdfef54568e8cd8fcf2479c Mon Sep 17 00:00:00 2001 From: Julian Sy Date: Thu, 22 Sep 2016 21:21:17 +0000 Subject: [PATCH] Increase unit test coverage for dsl attribute_store and context_manager. Implements bp: murano-unit-test-coverage Co-Authored-By: David Purcell Co-Authored-By: Samantha Blanco Co-Authored-By: Felipe Monteiro Change-Id: Ie4b9a20173bcbbc1c162f5a9e9fcdf1abcd971ce --- murano/tests/unit/dsl/test_attribute_store.py | 133 ++++++++++++++++++ murano/tests/unit/dsl/test_context_manager.py | 44 ++++++ 2 files changed, 177 insertions(+) create mode 100644 murano/tests/unit/dsl/test_attribute_store.py create mode 100644 murano/tests/unit/dsl/test_context_manager.py diff --git a/murano/tests/unit/dsl/test_attribute_store.py b/murano/tests/unit/dsl/test_attribute_store.py new file mode 100644 index 00000000..34565b56 --- /dev/null +++ b/murano/tests/unit/dsl/test_attribute_store.py @@ -0,0 +1,133 @@ +# 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 mock + +from murano.dsl import attribute_store +from murano.dsl import dsl +from murano.dsl import dsl_types +from murano.tests.unit.dsl.foundation import test_case + + +class TestAttributeStore(test_case.DslTestCase): + def setUp(self): + super(TestAttributeStore, self).setUp() + self.attribute_store = attribute_store.AttributeStore() + self.fake_object = mock.MagicMock(object_id=mock.sentinel.oid) + self.tagged_obj = dsl.MuranoObjectInterface(self.fake_object) + self.owner_type = dsl_types.MuranoTypeReference(self.fake_object) + self.owner_type.type.name = mock.sentinel.typename + self.name = 'foobar' + + def test_get_attribute_key(self): + oid, typename = mock.sentinel.oid, mock.sentinel.typename + + key = self.attribute_store._get_attribute_key( + self.tagged_obj, self.owner_type, self.name) + expected_key = (oid, (typename, 'foobar')) + + self.assertEqual(expected_key, key) + + @mock.patch.object(attribute_store.AttributeStore, '_get_attribute_key', + return_value=(mock.sentinel.key1, mock.sentinel.key2)) + def test_get(self, mock_get_attr_key): + key1, key2 = mock.sentinel.key1, mock.sentinel.key2 + get_val = mock.sentinel.get_val + + self.attribute_store._attributes = mock.MagicMock() + self.attribute_store._attributes[key1].get.return_value = get_val + val = self.attribute_store.get( + self.tagged_obj, self.owner_type, self.name) + + mock_get_attr_key.assert_called_with( + self.tagged_obj, self.owner_type, self.name) + self.attribute_store._attributes[key1].get.assert_called_with(key2) + self.assertEqual(get_val, val) + + @mock.patch.object(attribute_store.AttributeStore, '_get_attribute_key', + return_value=(mock.sentinel.key1, mock.sentinel.key2)) + def test_set_object_if(self, mock_get_attr_key): + val = dsl.MuranoObjectInterface(self.fake_object) + self.attribute_store._attributes = mock.MagicMock() + self.attribute_store.set( + self.tagged_obj, self.owner_type, self.name, val) + + @mock.patch.object(attribute_store.AttributeStore, '_get_attribute_key', + return_value=(mock.sentinel.key1, mock.sentinel.key2)) + def test_set_object(self, mock_get_attr_key): + key1, key2 = mock.sentinel.key1, mock.sentinel.key2 + + val = dsl_types.MuranoObject() + val.object_id = mock.sentinel.oid + self.attribute_store.set( + self.tagged_obj, self.owner_type, self.name, val) + self.assertEqual(self.attribute_store._attributes[key1][key2], + mock.sentinel.oid) + + @mock.patch.object(attribute_store.AttributeStore, '_get_attribute_key', + return_value=(mock.sentinel.key1, mock.sentinel.key2)) + def test_set_none(self, mock_get_attr_key): + key1, key2 = mock.sentinel.key1, mock.sentinel.key2 + + val = None + self.attribute_store._attributes = mock.MagicMock() + self.attribute_store.set( + self.tagged_obj, self.owner_type, self.name, val) + + self.attribute_store._attributes[key1].pop.assert_called_with( + key2, None) + + def test_serialize(self): + known_objects = ['obj1', 'obj3'] + self.attribute_store._attributes = { + 'obj1': { + ('foo', 'obj11'): 11, + ('bar', 'obj12'): 12 + }, + 'obj2': { + ('baz', 'obj21'): 21 + }, + 'obj3': { + ('foobar', 'obj31'): 31 + } + } + val = self.attribute_store.serialize(known_objects) + expected = [ + ['obj1', 'foo', 'obj11', 11], + ['obj1', 'bar', 'obj12', 12], + ['obj3', 'foobar', 'obj31', 31]] + + self.assertEqual(sorted(expected), sorted(val)) + + def test_load(self): + data = [ + ['a', 'b', 'c', 'd'], + ['a', 'f', 'g', None], + ['b', 'i', 'j', 'k'] + ] + + self.attribute_store.load(data) + expected = {'a': {('b', 'c'): 'd'}, + 'b': {('i', 'j'): 'k'}} + self.assertEqual(expected, self.attribute_store._attributes) + + def test_forget_object_if(self): + obj = dsl.MuranoObjectInterface(mock.MagicMock(object_id='bar')) + self.attribute_store._attributes = {'foo': 42, 'bar': 43} + self.attribute_store.forget_object(obj) + self.assertEqual({'foo': 42}, self.attribute_store._attributes) + + def test_forget_object(self): + obj = dsl_types.MuranoObject() + obj.object_id = 'foo' + self.attribute_store._attributes = {'foo': 42, 'bar': 43} + self.attribute_store.forget_object(obj) + self.assertEqual({'bar': 43}, self.attribute_store._attributes) diff --git a/murano/tests/unit/dsl/test_context_manager.py b/murano/tests/unit/dsl/test_context_manager.py new file mode 100644 index 00000000..7c9d8c97 --- /dev/null +++ b/murano/tests/unit/dsl/test_context_manager.py @@ -0,0 +1,44 @@ +# 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 mock + +from murano.dsl import context_manager +from murano.dsl import yaql_integration +from murano.tests.unit.dsl.foundation import test_case + + +class TestContextManager(test_case.DslTestCase): + def setUp(self): + super(TestContextManager, self).setUp() + self.context_manager = context_manager.ContextManager() + + @mock.patch.object(yaql_integration, 'create_context', return_value='foo') + def test_create_root_context(self, mock_create_context): + val = self.context_manager.create_root_context('myrunver') + + self.assertEqual('foo', val) + mock_create_context.assert_called_with('myrunver') + + def test_create_package_context(self): + package = mock.MagicMock(context='mycontext') + self.assertEqual('mycontext', + self.context_manager.create_package_context(package)) + + def test_create_type_context(self): + murano_type = mock.MagicMock(context='mycontext') + self.assertEqual('mycontext', + self.context_manager.create_type_context(murano_type)) + + def test_create_object_context(self): + obj = 'obj' + self.assertIs(None, self.context_manager.create_object_context(obj))