diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-03-24 10:55:29 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-03-24 10:55:29 -0400 |
| commit | bdcaa0f6ca580b54b9c25178441fcbe8f2a4e387 (patch) | |
| tree | 61a80475ad22f83067ab0b2fe1df65b709f3a2c1 /test/sql | |
| parent | 04545727d4117db51786189e591b1777bde1a40b (diff) | |
| download | sqlalchemy-bdcaa0f6ca580b54b9c25178441fcbe8f2a4e387.tar.gz | |
- The "auto-attach" feature of constraints such as :class:`.UniqueConstraint`
and :class:`.CheckConstraint` has been further enhanced such that
when the constraint is associated with non-table-bound :class:`.Column`
objects, the constraint will set up event listeners with the
columns themselves such that the constraint auto attaches at the
same time the columns are associated with the table. This in particular
helps in some edge cases in declarative but is also of general use.
fixes #3341
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_constraints.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/test/sql/test_constraints.py b/test/sql/test_constraints.py index eb558fc95..d024e1a27 100644 --- a/test/sql/test_constraints.py +++ b/test/sql/test_constraints.py @@ -1052,6 +1052,103 @@ class ConstraintAPITest(fixtures.TestBase): assert c not in t.constraints assert c not in t2.constraints + def test_auto_append_ck_on_col_attach_one(self): + m = MetaData() + + a = Column('a', Integer) + b = Column('b', Integer) + ck = CheckConstraint(a > b) + + t = Table('tbl', m, a, b) + assert ck in t.constraints + + def test_auto_append_ck_on_col_attach_two(self): + m = MetaData() + + a = Column('a', Integer) + b = Column('b', Integer) + c = Column('c', Integer) + ck = CheckConstraint(a > b + c) + + t = Table('tbl', m, a) + assert ck not in t.constraints + + t.append_column(b) + assert ck not in t.constraints + + t.append_column(c) + assert ck in t.constraints + + def test_auto_append_ck_on_col_attach_three(self): + m = MetaData() + + a = Column('a', Integer) + b = Column('b', Integer) + c = Column('c', Integer) + ck = CheckConstraint(a > b + c) + + t = Table('tbl', m, a) + assert ck not in t.constraints + + t.append_column(b) + assert ck not in t.constraints + + t2 = Table('t2', m) + t2.append_column(c) + + # two different tables, so CheckConstraint does nothing. + assert ck not in t.constraints + + def test_auto_append_uq_on_col_attach_one(self): + m = MetaData() + + a = Column('a', Integer) + b = Column('b', Integer) + uq = UniqueConstraint(a, b) + + t = Table('tbl', m, a, b) + assert uq in t.constraints + + def test_auto_append_uq_on_col_attach_two(self): + m = MetaData() + + a = Column('a', Integer) + b = Column('b', Integer) + c = Column('c', Integer) + uq = UniqueConstraint(a, b, c) + + t = Table('tbl', m, a) + assert uq not in t.constraints + + t.append_column(b) + assert uq not in t.constraints + + t.append_column(c) + assert uq in t.constraints + + def test_auto_append_uq_on_col_attach_three(self): + m = MetaData() + + a = Column('a', Integer) + b = Column('b', Integer) + c = Column('c', Integer) + uq = UniqueConstraint(a, b, c) + + t = Table('tbl', m, a) + assert uq not in t.constraints + + t.append_column(b) + assert uq not in t.constraints + + t2 = Table('t2', m) + + # two different tables, so UniqueConstraint raises + assert_raises_message( + exc.ArgumentError, + r"Column\(s\) 't2\.c' are not part of table 'tbl'\.", + t2.append_column, c + ) + def test_index_asserts_cols_standalone(self): metadata = MetaData() |
