1
from test.lib.testing import assert_raises
2
from test.lib.testing import assert_raises_message
3
from test.lib.testing import emits_warning
1
from sqlalchemy.testing import assert_raises
2
from sqlalchemy.testing import assert_raises_message
3
from sqlalchemy.testing import emits_warning
6
6
from sqlalchemy import Integer, String, UniqueConstraint, \
7
7
CheckConstraint, ForeignKey, MetaData, Sequence, \
8
8
ForeignKeyConstraint, ColumnDefault, Index, event,\
10
from test.lib.schema import Table, Column
9
events, Unicode, types as sqltypes
10
from sqlalchemy.testing.schema import Table, Column
11
11
from sqlalchemy import schema, exc
12
12
import sqlalchemy as tsa
13
from test.lib import fixtures
14
from test.lib import testing
15
from test.lib.testing import ComparesTables, AssertsCompiledSQL
16
from test.lib.testing import eq_, is_
13
from sqlalchemy.testing import fixtures
14
from sqlalchemy import testing
15
from sqlalchemy.testing import ComparesTables, AssertsCompiledSQL
16
from sqlalchemy.testing import eq_, is_
18
18
class MetaDataTest(fixtures.TestBase, ComparesTables):
19
19
def test_metadata_connect(self):
148
148
def test_dupe_tables(self):
149
149
metadata = self.metadata
150
150
Table('table1', metadata,
151
Column('col1', Integer, primary_key=True),
152
Column('col2', String(20)))
151
Column('col1', Integer, primary_key=True),
152
Column('col2', String(20)))
154
154
metadata.create_all()
155
155
Table('table1', metadata, autoload=True)
157
157
Table('table1', metadata,
158
Column('col1', Integer, primary_key=True),
159
Column('col2', String(20)))
158
Column('col1', Integer, primary_key=True),
159
Column('col2', String(20)))
160
160
assert_raises_message(
161
161
tsa.exc.InvalidRequestError,
162
"Table 'table1' is already defined for this "\
163
"MetaData instance. Specify 'extend_existing=True' "\
164
"to redefine options and columns on an existing "\
162
"Table 'table1' is already defined for this "
163
"MetaData instance. Specify 'extend_existing=True' "
164
"to redefine options and columns on an existing "
532
545
for i, (name, metadata, schema, quote_schema,
533
546
exp_schema, exp_quote_schema) in enumerate([
534
('t1', m1, None, None, 'sch1', None),
535
('t2', m1, 'sch2', None, 'sch2', None),
536
('t3', m1, 'sch2', True, 'sch2', True),
537
('t4', m1, 'sch1', None, 'sch1', None),
538
('t1', m2, None, None, 'sch1', True),
539
('t2', m2, 'sch2', None, 'sch2', None),
540
('t3', m2, 'sch2', True, 'sch2', True),
541
('t4', m2, 'sch1', None, 'sch1', None),
542
('t1', m3, None, None, 'sch1', False),
543
('t2', m3, 'sch2', None, 'sch2', None),
544
('t3', m3, 'sch2', True, 'sch2', True),
545
('t4', m3, 'sch1', None, 'sch1', None),
546
('t1', m4, None, None, None, None),
547
('t2', m4, 'sch2', None, 'sch2', None),
548
('t3', m4, 'sch2', True, 'sch2', True),
549
('t4', m4, 'sch1', None, 'sch1', None),
547
('t1', m1, None, None, 'sch1', None),
548
('t2', m1, 'sch2', None, 'sch2', None),
549
('t3', m1, 'sch2', True, 'sch2', True),
550
('t4', m1, 'sch1', None, 'sch1', None),
551
('t1', m2, None, None, 'sch1', True),
552
('t2', m2, 'sch2', None, 'sch2', None),
553
('t3', m2, 'sch2', True, 'sch2', True),
554
('t4', m2, 'sch1', None, 'sch1', None),
555
('t1', m3, None, None, 'sch1', False),
556
('t2', m3, 'sch2', None, 'sch2', None),
557
('t3', m3, 'sch2', True, 'sch2', True),
558
('t4', m3, 'sch1', None, 'sch1', None),
559
('t1', m4, None, None, None, None),
560
('t2', m4, 'sch2', None, 'sch2', None),
561
('t3', m4, 'sch2', True, 'sch2', True),
562
('t4', m4, 'sch1', None, 'sch1', None),
552
565
if schema is not None:
553
566
kw['schema'] = schema
555
568
kw['quote_schema'] = quote_schema
556
569
t = Table(name, metadata, **kw)
557
570
eq_(t.schema, exp_schema, "test %d, table schema" % i)
558
eq_(t.quote_schema, exp_quote_schema, "test %d, table quote_schema" % i)
571
eq_(t.quote_schema, exp_quote_schema,
572
"test %d, table quote_schema" % i)
559
573
seq = Sequence(name, metadata=metadata, **kw)
560
574
eq_(seq.schema, exp_schema, "test %d, seq schema" % i)
561
eq_(seq.quote_schema, exp_quote_schema, "test %d, seq quote_schema" % i)
575
eq_(seq.quote_schema, exp_quote_schema,
576
"test %d, seq quote_schema" % i)
563
578
def test_manual_dependencies(self):
564
579
meta = MetaData()
736
752
is_(t._autoincrement_column, t.c.id)
754
class SchemaTypeTest(fixtures.TestBase):
755
class MyType(sqltypes.SchemaType, sqltypes.TypeEngine):
760
def _set_table(self, column, table):
761
super(SchemaTypeTest.MyType, self)._set_table(column, table)
765
def _on_table_create(self, target, bind, **kw):
766
self.evt_targets += (target,)
768
def test_independent_schema(self):
770
type_ = self.MyType(schema="q")
771
t1 = Table('x', m, Column("y", type_), schema="z")
772
eq_(t1.c.y.type.schema, "q")
774
def test_inherit_schema(self):
776
type_ = self.MyType(schema="q", inherit_schema=True)
777
t1 = Table('x', m, Column("y", type_), schema="z")
778
eq_(t1.c.y.type.schema, "z")
780
def test_independent_schema_enum(self):
782
type_ = sqltypes.Enum("a", schema="q")
783
t1 = Table('x', m, Column("y", type_), schema="z")
784
eq_(t1.c.y.type.schema, "q")
786
def test_inherit_schema_enum(self):
788
type_ = sqltypes.Enum("a", "b", "c", schema="q", inherit_schema=True)
789
t1 = Table('x', m, Column("y", type_), schema="z")
790
eq_(t1.c.y.type.schema, "z")
792
def test_tometadata_copy_type(self):
795
type_ = self.MyType()
796
t1 = Table('x', m1, Column("y", type_))
799
t2 = t1.tometadata(m2)
802
is_(t2.c.y.type.metadata, None)
804
# our test type sets table, though
805
is_(t2.c.y.type.table, t2)
807
def test_tometadata_independent_schema(self):
810
type_ = self.MyType()
811
t1 = Table('x', m1, Column("y", type_))
814
t2 = t1.tometadata(m2, schema="bar")
816
eq_(t2.c.y.type.schema, None)
818
def test_tometadata_inherit_schema(self):
821
type_ = self.MyType(inherit_schema=True)
822
t1 = Table('x', m1, Column("y", type_))
825
t2 = t1.tometadata(m2, schema="bar")
827
eq_(t1.c.y.type.schema, None)
828
eq_(t2.c.y.type.schema, "bar")
830
def test_tometadata_independent_events(self):
833
type_ = self.MyType()
834
t1 = Table('x', m1, Column("y", type_))
837
t2 = t1.tometadata(m2)
839
t1.dispatch.before_create(t1, testing.db)
840
eq_(t1.c.y.type.evt_targets, (t1,))
841
eq_(t2.c.y.type.evt_targets, ())
843
t2.dispatch.before_create(t2, testing.db)
844
t2.dispatch.before_create(t2, testing.db)
845
eq_(t1.c.y.type.evt_targets, (t1,))
846
eq_(t2.c.y.type.evt_targets, (t2, t2))
738
850
class SchemaTest(fixtures.TestBase, AssertsCompiledSQL):
740
852
def test_default_schema_metadata_fk(self):
1071
1183
assert s1.c.a.references(t1.c.a)
1072
1184
assert not s1.c.a.references(t1.c.b)
1186
def test_invalid_composite_fk_check(self):
1188
t1 = Table('t1', m, Column('x', Integer), Column('y', Integer),
1189
ForeignKeyConstraint(['x', 'y'], ['t2.x', 't3.y'])
1191
t2 = Table('t2', m, Column('x', Integer))
1192
t3 = Table('t3', m, Column('y', Integer))
1194
assert_raises_message(
1196
r"ForeignKeyConstraint on t1\(x, y\) refers to "
1197
"multiple remote tables: t2 and t3",
1200
assert_raises_message(
1202
r"ForeignKeyConstraint on t1\(x, y\) refers to "
1203
"multiple remote tables: t2 and t3",
1207
assert_raises_message(
1209
r"ForeignKeyConstraint on t1\(x, y\) refers to "
1210
"multiple remote tables: t2 and t3",
1211
schema.CreateTable(t1).compile
1214
def test_constraint_copied_to_proxy_ok(self):
1216
t1 = Table('t1', m, Column('id', Integer, primary_key=True))
1217
t2 = Table('t2', m, Column('id', Integer, ForeignKey('t1.id'),
1220
s = tsa.select([t2])
1221
t2fk = list(t2.c.id.foreign_keys)[0]
1222
sfk = list(s.c.id.foreign_keys)[0]
1224
# the two FKs share the ForeignKeyConstraint
1230
# but the ForeignKeyConstraint isn't
1231
# aware of the select's FK
1233
t2fk.constraint.elements,
1074
1238
class ColumnDefinitionTest(AssertsCompiledSQL, fixtures.TestBase):
1075
1239
"""Test Column() construction."""
1217
1381
getattr, select([t1.select().alias()]), 'c'
1384
def test_custom_create(self):
1385
from sqlalchemy.ext.compiler import compiles, deregister
1387
@compiles(schema.CreateColumn)
1388
def compile(element, compiler, **kw):
1389
column = element.element
1391
if "special" not in column.info:
1392
return compiler.visit_create_column(element, **kw)
1394
text = "%s SPECIAL DIRECTIVE %s" % (
1396
compiler.type_compiler.process(column.type)
1398
default = compiler.get_column_default_string(column)
1399
if default is not None:
1400
text += " DEFAULT " + default
1402
if not column.nullable:
1405
if column.constraints:
1407
compiler.process(const)
1408
for const in column.constraints)
1411
t = Table('mytable', MetaData(),
1412
Column('x', Integer, info={"special": True}, primary_key=True),
1413
Column('y', String(50)),
1414
Column('z', String(20), info={"special": True})
1417
self.assert_compile(
1418
schema.CreateTable(t),
1419
"CREATE TABLE mytable (x SPECIAL DIRECTIVE INTEGER "
1420
"NOT NULL, y VARCHAR(50), "
1421
"z SPECIAL DIRECTIVE VARCHAR(20), PRIMARY KEY (x))"
1424
deregister(schema.CreateColumn)
1220
1426
class ColumnDefaultsTest(fixtures.TestBase):
1221
1427
"""test assignment of default fixures to columns"""
1229
1435
assert c.server_default is target
1230
1436
assert target.column is c
1438
def test_onupdate_default_not_server_default_one(self):
1439
target1 = schema.DefaultClause('y')
1440
target2 = schema.DefaultClause('z')
1442
c = self._fixture(server_default=target1, server_onupdate=target2)
1443
eq_(c.server_default.arg, 'y')
1444
eq_(c.server_onupdate.arg, 'z')
1446
def test_onupdate_default_not_server_default_two(self):
1447
target1 = schema.DefaultClause('y', for_update=True)
1448
target2 = schema.DefaultClause('z', for_update=True)
1450
c = self._fixture(server_default=target1, server_onupdate=target2)
1451
eq_(c.server_default.arg, 'y')
1452
eq_(c.server_onupdate.arg, 'z')
1454
def test_onupdate_default_not_server_default_three(self):
1455
target1 = schema.DefaultClause('y', for_update=False)
1456
target2 = schema.DefaultClause('z', for_update=True)
1458
c = self._fixture(target1, target2)
1459
eq_(c.server_default.arg, 'y')
1460
eq_(c.server_onupdate.arg, 'z')
1462
def test_onupdate_default_not_server_default_four(self):
1463
target1 = schema.DefaultClause('y', for_update=False)
1465
c = self._fixture(server_onupdate=target1)
1466
is_(c.server_default, None)
1467
eq_(c.server_onupdate.arg, 'y')
1232
1469
def test_server_default_keyword_as_schemaitem(self):
1233
1470
target = schema.DefaultClause('y')
1234
1471
c = self._fixture(server_default=target)
1407
'PrimaryKeyConstraint->Table', 'PrimaryKeyConstraint->t1',
1408
'ForeignKeyConstraint->Table', 'ForeignKeyConstraint->t1',
1409
'UniqueConstraint->Table', 'UniqueConstraint->t1',
1410
'PrimaryKeyConstraint->Table', 'PrimaryKeyConstraint->t2',
1411
'CheckConstraint->Table', 'CheckConstraint->t2',
1412
'UniqueConstraint->Table', 'UniqueConstraint->t2'
1647
'PrimaryKeyConstraint->Table', 'PrimaryKeyConstraint->t1',
1648
'ForeignKeyConstraint->Table', 'ForeignKeyConstraint->t1',
1649
'UniqueConstraint->Table', 'UniqueConstraint->t1',
1650
'PrimaryKeyConstraint->Table', 'PrimaryKeyConstraint->t2',
1651
'CheckConstraint->Table', 'CheckConstraint->t2',
1652
'UniqueConstraint->Table', 'UniqueConstraint->t2'