summaryrefslogtreecommitdiff
path: root/oslo_db/tests/sqlalchemy/test_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'oslo_db/tests/sqlalchemy/test_utils.py')
-rw-r--r--oslo_db/tests/sqlalchemy/test_utils.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/oslo_db/tests/sqlalchemy/test_utils.py b/oslo_db/tests/sqlalchemy/test_utils.py
index 19f78ce..6ab2c70 100644
--- a/oslo_db/tests/sqlalchemy/test_utils.py
+++ b/oslo_db/tests/sqlalchemy/test_utils.py
@@ -93,6 +93,28 @@ class FakeTable(Base):
pass
+class FakeTableWithMultipleKeys(Base):
+ __tablename__ = 'fake_table_multiple_keys'
+
+ key1 = Column(String(50), primary_key=True)
+ key2 = Column(String(50), primary_key=True)
+ key3 = Column(String(50))
+
+
+class FakeTableWithIndexes(Base):
+ __tablename__ = 'fake_table_unique_index'
+
+ id = Column(String(50), primary_key=True)
+ key1 = Column(String(50))
+ key2 = Column(String(50))
+ key3 = Column(String(50))
+
+ __table_args__ = (
+ Index('idx_unique', 'key1', 'key2', unique=True),
+ Index('idx_unique', 'key1', 'key3', unique=False),
+ )
+
+
class FakeModel(object):
def __init__(self, values):
self.values = values
@@ -276,6 +298,74 @@ class TestPaginateQuery(test_base.BaseTestCase):
sort_dirs=['asc', 'desc'])
+class Test_UnstableSortingOrder(test_base.BaseTestCase):
+ def test_multiple_primary_keys_stable(self):
+ self.assertTrue(
+ utils._stable_sorting_order(
+ FakeTableWithMultipleKeys, ['key1', 'key2']))
+
+ def test_multiple_primary_keys_unstable(self):
+ self.assertFalse(
+ utils._stable_sorting_order(
+ FakeTableWithMultipleKeys, ['key1', 'key3']))
+
+ def test_unique_index_stable(self):
+ self.assertTrue(
+ utils._stable_sorting_order(
+ FakeTableWithIndexes, ['key1', 'key2']))
+
+ def test_unique_index_unstable(self):
+ self.assertFalse(
+ utils._stable_sorting_order(
+ FakeTableWithIndexes, ['key1', 'key3']))
+
+
+class TestGetUniqueKeys(test_base.BaseTestCase):
+ def test_multiple_primary_keys(self):
+ self.assertEqual(
+ [{'key1', 'key2'}],
+ utils._get_unique_keys(FakeTableWithMultipleKeys))
+
+ def test_unique_index(self):
+ self.assertEqual(
+ [{'id'}, {'key1', 'key2'}],
+ utils._get_unique_keys(FakeTableWithIndexes))
+
+ def test_cache(self):
+
+ class CacheTable(object):
+ info = {}
+ constraints_called = 0
+ indexes_called = 0
+
+ @property
+ def constraints(self):
+ self.constraints_called += 1
+ return []
+
+ @property
+ def indexes(self):
+ self.indexes_called += 1
+ return []
+
+ class CacheModel(object):
+ __table__ = CacheTable()
+
+ model = CacheModel()
+ self.assertNotIn('oslodb_unique_keys', CacheTable.info)
+ utils._get_unique_keys(model)
+
+ self.assertIn('oslodb_unique_keys', CacheTable.info)
+ self.assertEqual(1, model.__table__.constraints_called)
+ self.assertEqual(1, model.__table__.indexes_called)
+
+ for i in range(10):
+ utils._get_unique_keys(model)
+
+ self.assertEqual(1, model.__table__.constraints_called)
+ self.assertEqual(1, model.__table__.indexes_called)
+
+
class TestPaginateQueryActualSQL(test_base.BaseTestCase):
def test_paginate_on_hybrid_assert_stmt(self):