From 772d40b0172ae0dc0616813185a4b139fc1f2e18 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 8 Mar 2012 21:36:13 -0600 Subject: [PATCH] Creating a model unit test to test the data() functionality. * Created factories for instanciating models behind the scenes * Stubbed out the remote calls to novaclient --- reddwarf/tests/factories/__init__.py | 16 +++++ reddwarf/tests/factories/models.py | 34 +++++++++++ reddwarf/tests/unit/test_database_models.py | 68 +++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 reddwarf/tests/factories/__init__.py create mode 100644 reddwarf/tests/factories/models.py create mode 100644 reddwarf/tests/unit/test_database_models.py diff --git a/reddwarf/tests/factories/__init__.py b/reddwarf/tests/factories/__init__.py new file mode 100644 index 0000000000..d65c689a83 --- /dev/null +++ b/reddwarf/tests/factories/__init__.py @@ -0,0 +1,16 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# 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. diff --git a/reddwarf/tests/factories/models.py b/reddwarf/tests/factories/models.py new file mode 100644 index 0000000000..7638bfdf4d --- /dev/null +++ b/reddwarf/tests/factories/models.py @@ -0,0 +1,34 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# 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 factory + +from reddwarf.common import context +from reddwarf.common import utils +from reddwarf.database import models + + +class Instance(factory.Factory): + FACTORY_FOR = models.Instance + context = context.ReddwarfContext() + uuid = utils.generate_uuid() + + +def factory_create(model_to_create, **kwargs): + return model_to_create(**kwargs) + +factory.Factory.set_creation_function(factory_create) diff --git a/reddwarf/tests/unit/test_database_models.py b/reddwarf/tests/unit/test_database_models.py new file mode 100644 index 0000000000..67c88f0c48 --- /dev/null +++ b/reddwarf/tests/unit/test_database_models.py @@ -0,0 +1,68 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# Copyright 2011 OpenStack LLC. +# 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 mox +import novaclient + +from reddwarf import tests +from reddwarf.common import exception +from reddwarf.common import utils +from reddwarf.database import models +from reddwarf.db import db_query +from reddwarf.tests import unit +from reddwarf.tests.factories import models as factory_models + + +class TestInstance(tests.BaseTest): + + FAKE_SERVER = None + + def setUp(self): + super(TestInstance, self).setUp() + + def mock_out_client(self): + """Stubs out a fake server returned from novaclient. + This is akin to calling Client.servers.get(uuid) + and getting the server object back.""" + self.FAKE_SERVER = self.mock.CreateMock(object) + self.FAKE_SERVER.name = 'my_name' + self.FAKE_SERVER.status = 'ACTIVE' + self.FAKE_SERVER.updated = utils.utcnow() + self.FAKE_SERVER.id = utils.generate_uuid() + self.FAKE_SERVER.flavor = ('http://localhost/1234/flavors/', + '52415800-8b69-11e0-9b19-734f1195ff37') + + client = self.mock.CreateMock(novaclient.v1_1.Client) + servers = self.mock.CreateMock(novaclient.v1_1.servers.ServerManager) + servers.get(mox.IgnoreArg()).AndReturn(self.FAKE_SERVER) + client.servers = servers + self.mock.StubOutWithMock(models.RemoteModelBase, 'get_client') + models.RemoteModelBase.get_client(mox.IgnoreArg()).AndReturn(client) + self.mock.ReplayAll() + + def test_create_instance_data(self): + """This ensures the data() call in a new + Instance object returns the proper mapped data + to a dict from attr's""" + self.mock_out_client() + # Creates the instance via __init__ + instance = factory_models.Instance().data() + + self.assertEqual(instance['name'], self.FAKE_SERVER.name) + self.assertEqual(instance['status'], self.FAKE_SERVER.status) + self.assertEqual(instance['updated'], self.FAKE_SERVER.updated) + self.assertEqual(instance['id'], self.FAKE_SERVER.id) + self.assertEqual(instance['flavor'], self.FAKE_SERVER.flavor)