summaryrefslogtreecommitdiffstats
path: root/tests/unit
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2013-02-20 21:17:02 +0000
committerGerrit Code Review <review@openstack.org>2013-02-20 21:17:02 +0000
commit7e6513dedc58577c187d69d576659bc7f2ed7a87 (patch)
treecdf9621bd7f99076ae0e42a4c80470168bb5e815 /tests/unit
parenta6260da3e3696e77e3e7fcd13acae416dc34809d (diff)
parent02c12aade7a0c28c66cb45b54786c90c0ae8fb09 (diff)
Merge "Move DB thread pooling to DB API loader"
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/db/sqlalchemy/test_sqlalchemy.py40
-rw-r--r--tests/unit/db/test_api.py87
2 files changed, 87 insertions, 40 deletions
diff --git a/tests/unit/db/sqlalchemy/test_sqlalchemy.py b/tests/unit/db/sqlalchemy/test_sqlalchemy.py
index c063f0d..6b84f10 100644
--- a/tests/unit/db/sqlalchemy/test_sqlalchemy.py
+++ b/tests/unit/db/sqlalchemy/test_sqlalchemy.py
@@ -16,8 +16,6 @@
"""Unit tests for SQLAlchemy specific code."""
-from eventlet import db_pool
-
from sqlalchemy import Column, MetaData, Table, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import DateTime, Integer
@@ -34,44 +32,6 @@ class TestException(Exception):
pass
-class DbPoolTestCase(test_utils.BaseTestCase):
- def setUp(self):
- super(DbPoolTestCase, self).setUp()
- if MySQLdb is None:
- self.skipTest("Required module MySQLdb missing.")
- self.config(sql_dbpool_enable=True)
- self.user_id = 'fake'
- self.project_id = 'fake'
-
- 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)
-
-
BASE = declarative_base()
_TABLE_NAME = '__tmp__test__tmp__'
diff --git a/tests/unit/db/test_api.py b/tests/unit/db/test_api.py
new file mode 100644
index 0000000..a31ffd0
--- /dev/null
+++ b/tests/unit/db/test_api.py
@@ -0,0 +1,87 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+# Copyright (c) 2013 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 DB API."""
+
+from eventlet import tpool
+
+from openstack.common.db import api
+from tests import utils as test_utils
+
+
+def get_backend():
+ return DBAPI()
+
+
+class DBAPI(object):
+ def api_class_call1(_self, *args, **kwargs):
+ return args, kwargs
+
+
+class DBAPITestCase(test_utils.BaseTestCase):
+ def test_dbapi_api_class_method_and_tpool_false(self):
+ backend_mapping = {'test_known': 'tests.unit.db.test_api'}
+ self.config(db_backend='test_known', dbapi_use_tpool=False)
+
+ info = dict(tpool=False)
+ orig_execute = tpool.execute
+
+ def our_execute(*args, **kwargs):
+ info['tpool'] = True
+ return orig_execute(*args, **kwargs)
+
+ self.stubs.Set(tpool, 'execute', our_execute)
+
+ dbapi = api.DBAPI(backend_mapping=backend_mapping)
+ result = dbapi.api_class_call1(1, 2, kwarg1='meow')
+ expected = ((1, 2), {'kwarg1': 'meow'})
+ self.assertEqual(expected, result)
+ self.assertFalse(info['tpool'])
+
+ def test_dbapi_api_class_method_and_tpool_true(self):
+ backend_mapping = {'test_known': 'tests.unit.db.test_api'}
+ self.config(db_backend='test_known', dbapi_use_tpool=True)
+
+ info = dict(tpool=False)
+ orig_execute = tpool.execute
+
+ def our_execute(*args, **kwargs):
+ info['tpool'] = True
+ return orig_execute(*args, **kwargs)
+
+ self.stubs.Set(tpool, 'execute', our_execute)
+
+ dbapi = api.DBAPI(backend_mapping=backend_mapping)
+ result = dbapi.api_class_call1(1, 2, kwarg1='meow')
+ expected = ((1, 2), {'kwarg1': 'meow'})
+ self.assertEqual(expected, result)
+ self.assertTrue(info['tpool'])
+
+ def test_dbapi_full_path_module_method(self):
+ self.config(db_backend='tests.unit.db.test_api')
+ dbapi = api.DBAPI()
+ result = dbapi.api_class_call1(1, 2, kwarg1='meow')
+ expected = ((1, 2), {'kwarg1': 'meow'})
+ self.assertEqual(expected, result)
+
+ def test_dbapi_unknown_invalid_backend(self):
+ self.config(db_backend='tests.unit.db.not_existant')
+ dbapi = api.DBAPI()
+
+ def call_it():
+ dbapi.api_class_call1(1, 2, kwarg1='meow')
+
+ self.assertRaises(ImportError, call_it)