diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-04-19 12:31:19 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-04-19 12:31:19 -0400 |
| commit | c33d0378802abbc729de55ba205a4309e5d68f6b (patch) | |
| tree | 1e8994db447e9908827c6494db8d0241a7a6de52 /test/sql | |
| parent | 1fb4ad75a38ce84d0e7b170927025500b73b5519 (diff) | |
| download | sqlalchemy-c33d0378802abbc729de55ba205a4309e5d68f6b.tar.gz | |
- Liberalized the contract for :class:`.Index` a bit in that you can
specify a :func:`.text` expression as the target; the index no longer
needs to have a table-bound column present if the index is to be
manually added to the table, either via inline declaration or via
:meth:`.Table.append_constraint`. fixes #3028
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_constraints.py | 43 | ||||
| -rw-r--r-- | test/sql/test_metadata.py | 71 |
2 files changed, 102 insertions, 12 deletions
diff --git a/test/sql/test_constraints.py b/test/sql/test_constraints.py index b174ef3c1..8dd15eb02 100644 --- a/test/sql/test_constraints.py +++ b/test/sql/test_constraints.py @@ -1,7 +1,7 @@ from sqlalchemy.testing import assert_raises, assert_raises_message from sqlalchemy import Table, Integer, String, Column, PrimaryKeyConstraint,\ ForeignKeyConstraint, ForeignKey, UniqueConstraint, Index, MetaData, \ - CheckConstraint, func + CheckConstraint, func, text from sqlalchemy import exc, schema from sqlalchemy.testing import fixtures, AssertsExecutionResults, \ AssertsCompiledSQL @@ -323,7 +323,7 @@ class ConstraintGenTest(fixtures.TestBase, AssertsExecutionResults): class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = 'default' - def test_create_plain(self): + def test_create_index_plain(self): t = Table('t', MetaData(), Column('x', Integer)) i = Index("xyz", t.c.x) self.assert_compile( @@ -331,19 +331,19 @@ class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL): "CREATE INDEX xyz ON t (x)" ) - def test_drop_plain_unattached(self): + def test_drop_index_plain_unattached(self): self.assert_compile( schema.DropIndex(Index(name="xyz")), "DROP INDEX xyz" ) - def test_drop_plain(self): + def test_drop_index_plain(self): self.assert_compile( schema.DropIndex(Index(name="xyz")), "DROP INDEX xyz" ) - def test_create_schema(self): + def test_create_index_schema(self): t = Table('t', MetaData(), Column('x', Integer), schema="foo") i = Index("xyz", t.c.x) self.assert_compile( @@ -351,7 +351,7 @@ class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL): "CREATE INDEX xyz ON foo.t (x)" ) - def test_drop_schema(self): + def test_drop_index_schema(self): t = Table('t', MetaData(), Column('x', Integer), schema="foo") i = Index("xyz", t.c.x) self.assert_compile( @@ -360,7 +360,7 @@ class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL): ) - def test_too_long_idx_name(self): + def test_too_long_index_name(self): dialect = testing.db.dialect.__class__() for max_ident, max_index in [(22, None), (256, 22)]: @@ -414,6 +414,32 @@ class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL): dialect=testing.db.dialect ) + def test_index_against_text_separate(self): + metadata = MetaData() + idx = Index('y', text("some_function(q)")) + t = Table('x', metadata, + Column('q', String(50)) + ) + t.append_constraint(idx) + self.assert_compile( + schema.CreateIndex(idx), + "CREATE INDEX y ON x (some_function(q))" + ) + + def test_index_against_text_inline(self): + metadata = MetaData() + idx = Index('y', text("some_function(q)")) + x = Table('x', metadata, + Column('q', String(50)), + idx + ) + + self.assert_compile( + schema.CreateIndex(idx), + "CREATE INDEX y ON x (some_function(q))" + ) + + def test_index_declaration_inline(self): metadata = MetaData() @@ -933,9 +959,6 @@ class ConstraintAPITest(fixtures.TestBase): def test_no_warning_w_no_columns(self): - # I think the test here is, there is no warning. - # people want to create empty indexes for the purpose of - # a drop. idx = Index(name="foo") assert_raises_message( diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 118fdf157..e4047872d 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -7,7 +7,7 @@ from sqlalchemy import Integer, String, UniqueConstraint, \ CheckConstraint, ForeignKey, MetaData, Sequence, \ ForeignKeyConstraint, PrimaryKeyConstraint, ColumnDefault, Index, event,\ events, Unicode, types as sqltypes, bindparam, \ - Table, Column, Boolean, Enum + Table, Column, Boolean, Enum, func, text from sqlalchemy import schema, exc import sqlalchemy as tsa from sqlalchemy.testing import fixtures @@ -451,7 +451,7 @@ class MetaDataTest(fixtures.TestBase, ComparesTables): "Column('x', String(), table=<bar>), schema=None)"), (schema.DefaultGenerator(for_update=True), "DefaultGenerator(for_update=True)"), - (schema.Index("bar", "c"), "Index('bar')"), + (schema.Index("bar", "c"), "Index('bar', 'c')"), (i1, "Index('bar', Column('x', Integer(), table=<foo>))"), (schema.FetchedValue(), "FetchedValue()"), (ck, @@ -1440,6 +1440,73 @@ class UseExistingTest(fixtures.TablesTest): extend_existing=True) assert "foo" in users.c +class IndexTest(fixtures.TestBase): + def _assert(self, t, i, columns=True): + eq_(t.indexes, set([i])) + if columns: + eq_(list(i.columns), [t.c.x]) + else: + eq_(list(i.columns), []) + assert i.table is t + + def test_separate_decl_columns(self): + m = MetaData() + t = Table('t', m, Column('x', Integer)) + i = Index('i', t.c.x) + self._assert(t, i) + + def test_separate_decl_columns_functional(self): + m = MetaData() + t = Table('t', m, Column('x', Integer)) + i = Index('i', func.foo(t.c.x)) + self._assert(t, i) + + def test_inline_decl_columns(self): + m = MetaData() + c = Column('x', Integer) + i = Index('i', c) + t = Table('t', m, c, i) + self._assert(t, i) + + def test_inline_decl_columns_functional(self): + m = MetaData() + c = Column('x', Integer) + i = Index('i', func.foo(c)) + t = Table('t', m, c, i) + self._assert(t, i) + + def test_inline_decl_string(self): + m = MetaData() + i = Index('i', "x") + t = Table('t', m, Column('x', Integer), i) + self._assert(t, i) + + def test_inline_decl_textonly(self): + m = MetaData() + i = Index('i', text("foobar(x)")) + t = Table('t', m, Column('x', Integer), i) + self._assert(t, i, columns=False) + + def test_separate_decl_textonly(self): + m = MetaData() + i = Index('i', text("foobar(x)")) + t = Table('t', m, Column('x', Integer)) + t.append_constraint(i) + self._assert(t, i, columns=False) + + def test_unnamed_column_exception(self): + # this can occur in some declarative situations + c = Column(Integer) + idx = Index('q', c) + m = MetaData() + t = Table('t', m, Column('q')) + assert_raises_message( + exc.ArgumentError, + "Can't add unnamed column to column collection", + t.append_constraint, idx + ) + + class ConstraintTest(fixtures.TestBase): def _single_fixture(self): m = MetaData() |
