diff options
author | Roman Podolyaka <roman.podolyaka@gmail.com> | 2013-06-09 19:07:00 +0300 |
---|---|---|
committer | Roman Podolyaka <roman.podolyaka@gmail.com> | 2013-06-09 23:49:55 +0300 |
commit | c69fe4acf8929856735e5d90adb7f6b6d5ebcd46 (patch) | |
tree | e193633bdf96f536d826122a1186d067dbc9890f /test/engine/test_reflection.py | |
parent | f65ddee93a7143924b417e1c988802f10d0c7b11 (diff) | |
download | sqlalchemy-pr/4.tar.gz |
Add basic support of unique constraints reflectionpr/4
Inspection API already supports reflection of table
indexes information and those also include unique
constraints (at least for PostgreSQL and MySQL).
But it could be actually useful to distinguish between
indexes and plain unique constraints (though both are
implemented in the same way internally in RDBMS).
This change adds a new method to Inspection API - get_unique_constraints()
and implements it for SQLite, PostgreSQL and MySQL dialects.
Diffstat (limited to 'test/engine/test_reflection.py')
-rw-r--r-- | test/engine/test_reflection.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/test/engine/test_reflection.py b/test/engine/test_reflection.py index ac0fa5153..fd9411874 100644 --- a/test/engine/test_reflection.py +++ b/test/engine/test_reflection.py @@ -1,3 +1,5 @@ +import operator + import unicodedata import sqlalchemy as sa from sqlalchemy import schema, events, event, inspect @@ -878,6 +880,41 @@ class ReflectionTest(fixtures.TestBase, ComparesTables): assert set([t2.c.name, t2.c.id]) == set(r2.columns) assert set([t2.c.name]) == set(r3.columns) + @testing.provide_metadata + def test_unique_constraints_reflection(self): + uniques = sorted( + [ + {'name': 'unique_a_b_c', 'column_names': ['a', 'b', 'c']}, + {'name': 'unique_a_c', 'column_names': ['a', 'c']}, + {'name': 'unique_b_c', 'column_names': ['b', 'c']}, + ], + key=operator.itemgetter('name') + ) + + try: + orig_meta = sa.MetaData(bind=testing.db) + table = Table( + 'testtbl', orig_meta, + Column('a', sa.String(20)), + Column('b', sa.String(30)), + Column('c', sa.Integer), + ) + for uc in uniques: + table.append_constraint( + sa.UniqueConstraint(*uc['column_names'], name=uc['name']) + ) + orig_meta.create_all() + + inspector = inspect(testing.db) + reflected = sorted( + inspector.get_unique_constraints('testtbl'), + key=operator.itemgetter('name') + ) + + assert uniques == reflected + finally: + testing.db.execute('drop table if exists testtbl;') + @testing.requires.views @testing.provide_metadata def test_views(self): |