Rest back objects for test_objects

When test_objects run, it will load some dummy objects such as Foo
etc, the problem is that Foo did not have '_type' field, so the
unit test will be failed.

This patch was resetting objects for all test cases in test_objects.

Closes-Bug: #1413144
Implements part of bp increase-test-coverage

Change-Id: I84067db21cb087c2694145a5a733d867422b73a5
This commit is contained in:
Jay Lau (Guangya Liu) 2015-01-22 01:04:50 -05:00
parent 0d5f6b9c3a
commit 18b098043a
2 changed files with 30 additions and 2 deletions

View File

@ -15,6 +15,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
import os
from oslo.config import cfg
@ -24,6 +25,7 @@ from pecan import testing
import testscenarios
from magnum.common import context as magnum_context
from magnum.objects import base as objects_base
from magnum.tests import conf_fixture
@ -51,6 +53,13 @@ class TestCase(base.BaseTestCase):
self.context = magnum_context.RequestContext()
self.useFixture(conf_fixture.ConfFixture(cfg.CONF))
self._base_test_obj_backup = copy.copy(
objects_base.MagnumObject._obj_classes)
self.addCleanup(self._restore_obj_registry)
def _restore_obj_registry(self):
objects_base.MagnumObject._obj_classes = self._base_test_obj_backup
def tearDown(self):
super(TestCase, self).tearDown()
pecan.set_config({}, overwrite=True)

View File

@ -299,7 +299,7 @@ class _TestObject(object):
self.assertEqual('loaded!', obj.bar)
expected = {'magnum_object.name': 'MyObj',
'magnum_object.namespace': 'magnum',
'magnum_object.version': '1.5',
'magnum_object.version': '1.0',
'magnum_object.changes': ['bar'],
'magnum_object.data': {'foo': 1,
'bar': 'loaded!'}}
@ -394,7 +394,7 @@ class _TestObject(object):
obj.updated_at = dt
expected = {'magnum_object.name': 'MyObj',
'magnum_object.namespace': 'magnum',
'magnum_object.version': '1.5',
'magnum_object.version': '1.0',
'magnum_object.changes':
['created_at', 'updated_at'],
'magnum_object.data':
@ -492,6 +492,7 @@ class TestObjectListBase(test_base.TestCase):
objlist.objects = [1, 2, 3]
self.assertEqual(list(objlist), objlist.objects)
self.assertEqual(3, len(objlist))
self.assertIn(2, objlist)
self.assertEqual([1], list(objlist[:1]))
self.assertEqual('foo', objlist[:1]._context)
self.assertEqual(3, objlist[2])
@ -538,6 +539,24 @@ class TestObjectListBase(test_base.TestCase):
if issubclass(obj_class, base.ObjectListBase):
self._test_object_list_version_mappings(obj_class)
def test_list_changes(self):
class Foo(base.ObjectListBase, base.MagnumObject):
pass
class Bar(base.MagnumObject):
fields = {'foo': str}
obj = Foo(self.context, objects=[])
self.assertEqual(set(['objects']), obj.obj_what_changed())
obj.objects.append(Bar(self.context, foo='test'))
self.assertEqual(set(['objects']), obj.obj_what_changed())
obj.obj_reset_changes()
# This should still look dirty because the child is dirty
self.assertEqual(set(['objects']), obj.obj_what_changed())
obj.objects[0].obj_reset_changes()
# This should now look clean because the child is clean
self.assertEqual(set(), obj.obj_what_changed())
class TestObjectSerializer(test_base.TestCase):