summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEric Windisch <eric@cloudscaling.com>2012-12-04 10:40:10 -0500
committerMark McLoughlin <markmc@redhat.com>2013-02-05 10:50:12 +0000
commit13a9e6dca7742493253d5b9b4bedb01d07fb242d (patch)
tree08455b67bf0e13593ad34260eb7fd7dd71ff36f9 /tests
parentbd5d9f08ecc3c1fade6dce809ee9edef1c226e54 (diff)
downloadoslo-13a9e6dca7742493253d5b9b4bedb01d07fb242d.tar.gz
oslo-13a9e6dca7742493253d5b9b4bedb01d07fb242d.tar.xz
oslo-13a9e6dca7742493253d5b9b4bedb01d07fb242d.zip
Import sqlalchemy session/models/utils
Bring in session, base model, utilities, and tests for sqlalchemy from Nova. Add sqlalchemy to pip-requires and and python-mysql to test-requires. Partially implements blueprint common-db Change-Id: I3e0065cdac87e10c4e0742d66c293c72bb3acbb2
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/db/__init__.py16
-rw-r--r--tests/unit/db/sqlalchemy/__init__.py16
-rw-r--r--tests/unit/db/sqlalchemy/test_models.py72
-rw-r--r--tests/unit/db/sqlalchemy/test_sqlalchemy.py72
4 files changed, 176 insertions, 0 deletions
diff --git a/tests/unit/db/__init__.py b/tests/unit/db/__init__.py
new file mode 100644
index 0000000..1b9b60d
--- /dev/null
+++ b/tests/unit/db/__init__.py
@@ -0,0 +1,16 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 Cloudscaling Group, Inc
+# 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/tests/unit/db/sqlalchemy/__init__.py b/tests/unit/db/sqlalchemy/__init__.py
new file mode 100644
index 0000000..1b9b60d
--- /dev/null
+++ b/tests/unit/db/sqlalchemy/__init__.py
@@ -0,0 +1,16 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 Cloudscaling Group, Inc
+# 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/tests/unit/db/sqlalchemy/test_models.py b/tests/unit/db/sqlalchemy/test_models.py
new file mode 100644
index 0000000..17f1c1b
--- /dev/null
+++ b/tests/unit/db/sqlalchemy/test_models.py
@@ -0,0 +1,72 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 Cloudscaling Group, Inc.
+# 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 openstack.common.db.sqlalchemy.models as models
+from tests import utils as test_utils
+
+
+class ModelBaseTest(test_utils.BaseTestCase):
+
+ def test_modelbase_has_dict_methods(self):
+ dict_methods = ('__getitem__',
+ '__setitem__',
+ '__iter__',
+ 'get',
+ 'next',
+ 'update',
+ 'iteritems')
+ for method in dict_methods:
+ self.assertTrue(hasattr(models.ModelBase, method))
+
+ def test_modelbase_set(self):
+ mb = models.ModelBase()
+ mb['world'] = 'hello'
+ self.assertEqual(mb['world'], 'hello')
+
+ def test_modelbase_update(self):
+ mb = models.ModelBase()
+ h = {'a': '1', 'b': '2'}
+ mb.update(h)
+ for key in h.keys():
+ self.assertEqual(mb[key], h[key])
+
+ def test_modelbase_iteritems(self):
+ self.skipTest("Requires DB")
+ mb = models.ModelBase()
+ h = {'a': '1', 'b': '2'}
+ mb.update(h)
+ for key, value in mb.iteritems():
+ self.assertEqual(h[key], value)
+
+ def test_modelbase_iter(self):
+ self.skipTest("Requires DB")
+ mb = models.ModelBase()
+ h = {'a': '1', 'b': '2'}
+ mb.update(h)
+ i = iter(mb)
+
+ min_items = len(h)
+ found_items = 0
+ while True:
+ r = next(i, None)
+ if r is None:
+ break
+
+ self.assertTrue(r in h)
+ found_items += 1
+
+ self.assertEqual(min_items, found_items)
diff --git a/tests/unit/db/sqlalchemy/test_sqlalchemy.py b/tests/unit/db/sqlalchemy/test_sqlalchemy.py
new file mode 100644
index 0000000..d4894ba
--- /dev/null
+++ b/tests/unit/db/sqlalchemy/test_sqlalchemy.py
@@ -0,0 +1,72 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+# Copyright (c) 2012 Rackspace Hosting
+# 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.
+
+"""Unit tests for SQLAlchemy specific code."""
+
+from eventlet import db_pool
+try:
+ import MySQLdb
+ HAS_MYSQLDB = True
+except ImportError:
+ HAS_MYSQLDB = False
+
+from openstack.common import context
+from openstack.common import exception
+from openstack.common.db.sqlalchemy import session
+from tests import utils as test_utils
+
+
+class TestException(exception.OpenstackException):
+ pass
+
+
+class DbPoolTestCase(test_utils.BaseTestCase):
+ def setUp(self):
+ super(DbPoolTestCase, self).setUp()
+ if not HAS_MYSQLDB:
+ self.skipTest("Required module MySQLdb missing.")
+ self.config(sql_dbpool_enable=True)
+ self.user_id = 'fake'
+ self.project_id = 'fake'
+ self.context = context.RequestContext(self.user_id, self.project_id)
+
+ def test_db_pool_option(self):
+ self.config(sql_idle_timeout=11, sql_min_pool_size=21,
+ sql_max_pool_size=42)
+
+ info = {}
+
+ class FakeConnectionPool(db_pool.ConnectionPool):
+ def __init__(self, mod_name, **kwargs):
+ info['module'] = mod_name
+ info['kwargs'] = kwargs
+ super(FakeConnectionPool, self).__init__(mod_name,
+ **kwargs)
+
+ def connect(self, *args, **kwargs):
+ raise TestException()
+
+ self.stubs.Set(db_pool, 'ConnectionPool',
+ FakeConnectionPool)
+
+ sql_connection = 'mysql://user:pass@127.0.0.1/nova'
+ self.assertRaises(TestException, session.create_engine,
+ sql_connection)
+
+ self.assertEqual(info['module'], MySQLdb)
+ self.assertEqual(info['kwargs']['max_idle'], 11)
+ self.assertEqual(info['kwargs']['min_size'], 21)
+ self.assertEqual(info['kwargs']['max_size'], 42)