1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
from .. import config
from .. import fixtures
from ..assertions import eq_
from ..assertions import is_true
from ..config import requirements
from ..schema import Column
from ..schema import Table
from ... import inspect
from ... import Integer
from ... import MetaData
from ... import Sequence
from ... import String
from ... import testing
class SequenceTest(fixtures.TablesTest):
__requires__ = ("sequences",)
__backend__ = True
run_create_tables = "each"
@classmethod
def define_tables(cls, metadata):
Table(
"seq_pk",
metadata,
Column("id", Integer, Sequence("tab_id_seq"), primary_key=True,),
Column("data", String(50)),
)
Table(
"seq_opt_pk",
metadata,
Column(
"id",
Integer,
Sequence("tab_id_seq", data_type=Integer, optional=True),
primary_key=True,
),
Column("data", String(50)),
)
def test_insert_roundtrip(self, connection):
connection.execute(self.tables.seq_pk.insert(), data="some data")
self._assert_round_trip(self.tables.seq_pk, connection)
def test_insert_lastrowid(self, connection):
r = connection.execute(self.tables.seq_pk.insert(), data="some data")
eq_(
r.inserted_primary_key, (testing.db.dialect.default_sequence_base,)
)
def test_nextval_direct(self, connection):
r = connection.execute(self.tables.seq_pk.c.id.default)
eq_(r, testing.db.dialect.default_sequence_base)
@requirements.sequences_optional
def test_optional_seq(self, connection):
r = connection.execute(
self.tables.seq_opt_pk.insert(), data="some data"
)
eq_(r.inserted_primary_key, (1,))
def _assert_round_trip(self, table, conn):
row = conn.execute(table.select()).first()
eq_(row, (testing.db.dialect.default_sequence_base, "some data"))
class SequenceCompilerTest(testing.AssertsCompiledSQL, fixtures.TestBase):
__requires__ = ("sequences",)
__backend__ = True
def test_literal_binds_inline_compile(self, connection):
table = Table(
"x",
MetaData(),
Column("y", Integer, Sequence("y_seq")),
Column("q", Integer),
)
stmt = table.insert().values(q=5)
seq_nextval = connection.dialect.statement_compiler(
statement=None, dialect=connection.dialect
).visit_sequence(Sequence("y_seq"))
self.assert_compile(
stmt,
"INSERT INTO x (y, q) VALUES (%s, 5)" % (seq_nextval,),
literal_binds=True,
dialect=connection.dialect,
)
class HasSequenceTest(fixtures.TablesTest):
run_deletes = None
__requires__ = ("sequences",)
__backend__ = True
@classmethod
def define_tables(cls, metadata):
Sequence("user_id_seq", metadata=metadata)
Sequence("other_seq", metadata=metadata)
if testing.requires.schemas.enabled:
Sequence(
"user_id_seq", schema=config.test_schema, metadata=metadata
)
Sequence(
"schema_seq", schema=config.test_schema, metadata=metadata
)
Table(
"user_id_table", metadata, Column("id", Integer, primary_key=True),
)
def test_has_sequence(self, connection):
eq_(
inspect(connection).has_sequence("user_id_seq"), True,
)
def test_has_sequence_other_object(self, connection):
eq_(
inspect(connection).has_sequence("user_id_table"), False,
)
@testing.requires.schemas
def test_has_sequence_schema(self, connection):
eq_(
inspect(connection).has_sequence(
"user_id_seq", schema=config.test_schema
),
True,
)
def test_has_sequence_neg(self, connection):
eq_(
inspect(connection).has_sequence("some_sequence"), False,
)
@testing.requires.schemas
def test_has_sequence_schemas_neg(self, connection):
eq_(
inspect(connection).has_sequence(
"some_sequence", schema=config.test_schema
),
False,
)
@testing.requires.schemas
def test_has_sequence_default_not_in_remote(self, connection):
eq_(
inspect(connection).has_sequence(
"other_sequence", schema=config.test_schema
),
False,
)
@testing.requires.schemas
def test_has_sequence_remote_not_in_default(self, connection):
eq_(
inspect(connection).has_sequence("schema_seq"), False,
)
def test_get_sequence_names(self, connection):
exp = {"other_seq", "user_id_seq"}
res = set(inspect(connection).get_sequence_names())
is_true(res.intersection(exp) == exp)
is_true("schema_seq" not in res)
@testing.requires.schemas
def test_get_sequence_names_no_sequence_schema(self, connection):
eq_(
inspect(connection).get_sequence_names(
schema=config.test_schema_2
),
[],
)
@testing.requires.schemas
def test_get_sequence_names_sequences_schema(self, connection):
eq_(
sorted(
inspect(connection).get_sequence_names(
schema=config.test_schema
)
),
["schema_seq", "user_id_seq"],
)
class HasSequenceTestEmpty(fixtures.TestBase):
__requires__ = ("sequences",)
__backend__ = True
def test_get_sequence_names_no_sequence(self, connection):
eq_(
inspect(connection).get_sequence_names(), [],
)
|