Fixes the SQL model tests

The tests were not completely accurate. The expected columns list
didn't need to have all of the columns. Also the defaults for Boolean
columns were not being checked.

Change-Id: I558870792a836ed775da3f683ed1279cc58216b0
This commit is contained in:
David Stanek 2015-02-26 03:43:15 +00:00
parent c3f77c9a57
commit 04402d497c
2 changed files with 58 additions and 7 deletions

View File

@ -21,7 +21,8 @@ class SqlPolicyAssociationTable(test_backend_sql.SqlModels):
"""Set of tests for checking SQL Policy Association Mapping.""" """Set of tests for checking SQL Policy Association Mapping."""
def test_policy_association_mapping(self): def test_policy_association_mapping(self):
cols = (('policy_id', sql.String, 64), cols = (('id', sql.String, 64),
('policy_id', sql.String, 64),
('endpoint_id', sql.String, 64), ('endpoint_id', sql.String, 64),
('service_id', sql.String, 64), ('service_id', sql.String, 64),
('region_id', sql.String, 64)) ('region_id', sql.String, 64))

View File

@ -67,18 +67,67 @@ class SqlModels(SqlTests):
s = sqlalchemy.select([table]) s = sqlalchemy.select([table])
return s return s
def assertExpectedSchema(self, table, cols): def assertExpectedSchema(self, table, expected_schema):
"""Assert that a table's schema is what we expect.
:param string table: the name of the table to inspect
:param tuple expected_schema: a tuple of tuples containing the
expected schema
:raises: AssertionError
The expected_shema format is simply::
(
('column name', sql type, qualifying detail),
...
)
The qualifying detail varies based on the type of the column::
- sql.Boolean columns must indicate the column's default value or
None if there is no default
- Columns with a length, like sql.String, must indicate the
column's length
- All other column types should use None
Example::
cols = (('id', sql.String, 64),
('enabled', sql.Boolean, True),
('extra', sql.JsonBlob, None))
self.assertExpectedSchema('table_name', cols)
"""
table = self.select_table(table) table = self.select_table(table)
for col, type_, length in cols:
self.assertIsInstance(table.c[col].type, type_) actual_schema = []
if length: for column in table.c:
self.assertEqual(length, table.c[col].type.length) if isinstance(column.type, sql.Boolean):
default = None
if column._proxies[0].default:
default = column._proxies[0].default.arg
actual_schema.append((column.name, type(column.type), default))
elif (hasattr(column.type, 'length') and
not isinstance(column.type, sql.Enum)):
# NOTE(dstanek): Even though sql.Enum columns have a length
# set we don't want to catch them here. Maybe in the future
# we'll check to see that they contain a list of the correct
# possible values.
actual_schema.append((column.name,
type(column.type),
column.type.length))
else:
actual_schema.append((column.name, type(column.type), None))
self.assertEqual(list(sorted(expected_schema)),
list(sorted(actual_schema)))
def test_user_model(self): def test_user_model(self):
cols = (('id', sql.String, 64), cols = (('id', sql.String, 64),
('name', sql.String, 255), ('name', sql.String, 255),
('password', sql.String, 128), ('password', sql.String, 128),
('domain_id', sql.String, 64), ('domain_id', sql.String, 64),
('default_project_id', sql.String, 64),
('enabled', sql.Boolean, None), ('enabled', sql.Boolean, None),
('extra', sql.JsonBlob, None)) ('extra', sql.JsonBlob, None))
self.assertExpectedSchema('user', cols) self.assertExpectedSchema('user', cols)
@ -94,7 +143,8 @@ class SqlModels(SqlTests):
def test_domain_model(self): def test_domain_model(self):
cols = (('id', sql.String, 64), cols = (('id', sql.String, 64),
('name', sql.String, 64), ('name', sql.String, 64),
('enabled', sql.Boolean, None)) ('enabled', sql.Boolean, True),
('extra', sql.JsonBlob, None))
self.assertExpectedSchema('domain', cols) self.assertExpectedSchema('domain', cols)
def test_project_model(self): def test_project_model(self):