From 96dce7686c6a32ab03ce2ccaabb7a9b4ff0fe56b Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 28 Jan 2012 15:54:28 -0500 Subject: - [feature] New reflection feature "autoload_replace"; when set to False on Table, the Table can be autoloaded without existing columns being replaced. Allows more flexible chains of Table construction/reflection to be constructed, including that it helps with combining Declarative with table reflection. See the new example on the wiki. [ticket:2356] - [bug] Improved the API for add_column() such that if the same column is added to its own table, an error is not raised and the constraints don't get doubled up. Also helps with some reflection/declarative patterns. [ticket:2356] --- test/engine/test_reflection.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'test/engine/test_reflection.py') diff --git a/test/engine/test_reflection.py b/test/engine/test_reflection.py index e32c868d6..13c8ee0ef 100644 --- a/test/engine/test_reflection.py +++ b/test/engine/test_reflection.py @@ -160,6 +160,27 @@ class ReflectionTest(fixtures.TestBase, ComparesTables): set(['z']) ) + m4 = MetaData() + old_z = Column('z', String, primary_key=True) + old_y = Column('y', String) + old_q = Column('q', Integer) + t4 = Table('t', m4, old_z, old_q) + eq_(t4.primary_key.columns, (t4.c.z, )) + t4 = Table('t', m4, old_y, + extend_existing=True, + autoload=True, + autoload_replace=False, + autoload_with=testing.db) + eq_( + set(t4.columns.keys()), + set(['x', 'y', 'z', 'q', 'id']) + ) + eq_(t4.primary_key.columns, (t4.c.id, )) + assert t4.c.z is old_z + assert t4.c.y is old_y + assert t4.c.z.type._type_affinity is String + assert t4.c.q is old_q + @testing.emits_warning(r".*omitted columns") @testing.provide_metadata def test_include_columns_indexes(self): @@ -181,6 +202,27 @@ class ReflectionTest(fixtures.TestBase, ComparesTables): t2 = Table('t1', m2, autoload=True, include_columns=['a', 'b']) assert len(t2.indexes) == 2 + @testing.provide_metadata + def test_autoload_replace(self): + a = Table('a', self.metadata, Column('id', Integer, primary_key=True)) + b = Table('b', self.metadata, Column('id', Integer, primary_key=True), + Column('a_id', Integer)) + self.metadata.create_all() + + m2 = MetaData() + b2 = Table('b', m2, Column('a_id', Integer, sa.ForeignKey('a.id'))) + a2 = Table('a', m2, autoload=True, autoload_with=testing.db) + b2 = Table('b', m2, extend_existing=True, autoload=True, + autoload_with=testing.db, + autoload_replace=False) + + assert b2.c.id is not None + assert b2.c.a_id.references(a2.c.id) + eq_(len(b2.constraints), 2) + + def test_autoload_replace_arg(self): + Table('t', MetaData(), autoload_replace=False) + @testing.provide_metadata def test_autoincrement_col(self): """test that 'autoincrement' is reflected according to sqla's policy. -- cgit v1.2.1