1
from .. import fixtures, config
2
from ..config import requirements
3
from ..assertions import eq_
4
from ... import testing
6
from ... import Integer, String, Sequence, schema
8
from ..schema import Table, Column
10
class SequenceTest(fixtures.TablesTest):
11
__requires__ = ('sequences',)
13
run_create_tables = 'each'
16
def define_tables(cls, metadata):
17
Table('seq_pk', metadata,
18
Column('id', Integer, Sequence('tab_id_seq'), primary_key=True),
19
Column('data', String(50))
22
Table('seq_opt_pk', metadata,
23
Column('id', Integer, Sequence('tab_id_seq', optional=True),
25
Column('data', String(50))
28
def test_insert_roundtrip(self):
30
self.tables.seq_pk.insert(),
33
self._assert_round_trip(self.tables.seq_pk, config.db)
35
def test_insert_lastrowid(self):
36
r = config.db.execute(
37
self.tables.seq_pk.insert(),
41
r.inserted_primary_key,
45
def test_nextval_direct(self):
46
r = config.db.execute(
47
self.tables.seq_pk.c.id.default
53
@requirements.sequences_optional
54
def test_optional_seq(self):
55
r = config.db.execute(
56
self.tables.seq_opt_pk.insert(),
60
r.inserted_primary_key,
65
def _assert_round_trip(self, table, conn):
66
row = conn.execute(table.select()).first()
73
class HasSequenceTest(fixtures.TestBase):
74
__requires__ = 'sequences',
76
def test_has_sequence(self):
77
s1 = Sequence('user_id_seq')
78
testing.db.execute(schema.CreateSequence(s1))
80
eq_(testing.db.dialect.has_sequence(testing.db,
83
testing.db.execute(schema.DropSequence(s1))
85
@testing.requires.schemas
86
def test_has_sequence_schema(self):
87
s1 = Sequence('user_id_seq', schema="test_schema")
88
testing.db.execute(schema.CreateSequence(s1))
90
eq_(testing.db.dialect.has_sequence(testing.db,
91
'user_id_seq', schema="test_schema"), True)
93
testing.db.execute(schema.DropSequence(s1))
95
def test_has_sequence_neg(self):
96
eq_(testing.db.dialect.has_sequence(testing.db, 'user_id_seq'),
99
@testing.requires.schemas
100
def test_has_sequence_schemas_neg(self):
101
eq_(testing.db.dialect.has_sequence(testing.db, 'user_id_seq',
102
schema="test_schema"),
105
@testing.requires.schemas
106
def test_has_sequence_default_not_in_remote(self):
107
s1 = Sequence('user_id_seq')
108
testing.db.execute(schema.CreateSequence(s1))
110
eq_(testing.db.dialect.has_sequence(testing.db, 'user_id_seq',
111
schema="test_schema"),
114
testing.db.execute(schema.DropSequence(s1))
116
@testing.requires.schemas
117
def test_has_sequence_remote_not_in_default(self):
118
s1 = Sequence('user_id_seq', schema="test_schema")
119
testing.db.execute(schema.CreateSequence(s1))
121
eq_(testing.db.dialect.has_sequence(testing.db, 'user_id_seq'),
124
testing.db.execute(schema.DropSequence(s1))