Files
sqlalchemy/test/sql/indexes.py
T
Mike Bayer e812785c2d - logging is now implemented via standard python "logging" module.
"echo" keyword parameters are still functional but set/unset
log levels for their respective classes/instances.  all logging
can be controlled directly through the Python API by setting
INFO and DEBUG levels for loggers in the "sqlalchemy" namespace.
class-level logging is under "sqlalchemy.<module>.<classname>",
instance-level logging under "sqlalchemy.<module>.<classname>.<hexid>".
Test suite includes "--log-info" and "--log-debug" arguments
which work independently of --verbose/--quiet.  Logging added
to orm to allow tracking of mapper configurations, row iteration
fixes [ticket:229] [ticket:79]
2006-09-24 23:59:22 +00:00

122 lines
4.7 KiB
Python

import testbase
from sqlalchemy import *
import sys
class IndexTest(testbase.AssertMixin):
def setUp(self):
global metadata
metadata = BoundMetaData(testbase.db)
def tearDown(self):
metadata.drop_all()
def test_constraint(self):
employees = Table('employees', metadata,
Column('id', Integer),
Column('soc', String(40)),
Column('name', String(30)),
PrimaryKeyConstraint('id', 'soc')
)
elements = Table('elements', metadata,
Column('id', Integer),
Column('stuff', String(30)),
Column('emp_id', Integer),
Column('emp_soc', String(40)),
PrimaryKeyConstraint('id'),
ForeignKeyConstraint(['emp_id', 'emp_soc'], ['employees.id', 'employees.soc'])
)
metadata.create_all()
def test_index_create(self):
employees = Table('employees', metadata,
Column('id', Integer, primary_key=True),
Column('first_name', String(30)),
Column('last_name', String(30)),
Column('email_address', String(30)))
employees.create()
i = Index('employee_name_index',
employees.c.last_name, employees.c.first_name)
i.create()
assert employees.indexes['employee_name_index'] is i
i2 = Index('employee_email_index',
employees.c.email_address, unique=True)
i2.create()
assert employees.indexes['employee_email_index'] is i2
def test_index_create_camelcase(self):
"""test that mixed-case index identifiers are legal"""
employees = Table('companyEmployees', metadata,
Column('id', Integer, primary_key=True),
Column('firstName', String(30)),
Column('lastName', String(30)),
Column('emailAddress', String(30)))
employees.create()
i = Index('employeeNameIndex',
employees.c.lastName, employees.c.firstName)
i.create()
i = Index('employeeEmailIndex',
employees.c.emailAddress, unique=True)
i.create()
# Check that the table is useable. This is mostly for pg,
# which can be somewhat sticky with mixed-case identifiers
employees.insert().execute(firstName='Joe', lastName='Smith', id=0)
ss = employees.select().execute().fetchall()
assert ss[0].firstName == 'Joe'
assert ss[0].lastName == 'Smith'
def test_index_create_inline(self):
"""Test indexes defined with tables"""
events = Table('events', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(30), unique=True),
Column('location', String(30), index=True),
Column('sport', String(30),
unique='sport_announcer'),
Column('announcer', String(30),
unique='sport_announcer'),
Column('winner', String(30), index='idx_winners'))
index_names = [ ix.name for ix in events.indexes ]
assert 'ux_events_name' in index_names
assert 'ix_events_location' in index_names
assert 'sport_announcer' in index_names
assert 'idx_winners' in index_names
assert len(index_names) == 4
capt = []
connection = testbase.db.connect()
def proxy(statement, parameters):
capt.append(statement)
capt.append(repr(parameters))
connection.proxy(statement, parameters)
schemagen = testbase.db.dialect.schemagenerator(testbase.db, proxy)
events.accept_schema_visitor(schemagen)
assert capt[0].strip().startswith('CREATE TABLE events')
assert capt[2].strip() == \
'CREATE UNIQUE INDEX ux_events_name ON events (name)'
assert capt[4].strip() == \
'CREATE INDEX ix_events_location ON events (location)'
assert capt[6].strip() == \
'CREATE UNIQUE INDEX sport_announcer ON events (sport, announcer)'
assert capt[8].strip() == \
'CREATE INDEX idx_winners ON events (winner)'
# verify that the table is functional
events.insert().execute(id=1, name='hockey finals', location='rink',
sport='hockey', announcer='some canadian',
winner='sweden')
ss = events.select().execute().fetchall()
if __name__ == "__main__":
testbase.main()