test_sequence.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. # testing/suite/test_sequence.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: ignore-errors
  8. from .. import config
  9. from .. import fixtures
  10. from ..assertions import eq_
  11. from ..assertions import is_true
  12. from ..config import requirements
  13. from ..provision import normalize_sequence
  14. from ..schema import Column
  15. from ..schema import Table
  16. from ... import inspect
  17. from ... import Integer
  18. from ... import MetaData
  19. from ... import Sequence
  20. from ... import String
  21. from ... import testing
  22. class SequenceTest(fixtures.TablesTest):
  23. __requires__ = ("sequences",)
  24. __backend__ = True
  25. run_create_tables = "each"
  26. @classmethod
  27. def define_tables(cls, metadata):
  28. Table(
  29. "seq_pk",
  30. metadata,
  31. Column(
  32. "id",
  33. Integer,
  34. normalize_sequence(config, Sequence("tab_id_seq")),
  35. primary_key=True,
  36. ),
  37. Column("data", String(50)),
  38. )
  39. Table(
  40. "seq_opt_pk",
  41. metadata,
  42. Column(
  43. "id",
  44. Integer,
  45. normalize_sequence(
  46. config,
  47. Sequence("tab_id_seq", data_type=Integer, optional=True),
  48. ),
  49. primary_key=True,
  50. ),
  51. Column("data", String(50)),
  52. )
  53. Table(
  54. "seq_no_returning",
  55. metadata,
  56. Column(
  57. "id",
  58. Integer,
  59. normalize_sequence(config, Sequence("noret_id_seq")),
  60. primary_key=True,
  61. ),
  62. Column("data", String(50)),
  63. implicit_returning=False,
  64. )
  65. if testing.requires.schemas.enabled:
  66. Table(
  67. "seq_no_returning_sch",
  68. metadata,
  69. Column(
  70. "id",
  71. Integer,
  72. normalize_sequence(
  73. config,
  74. Sequence(
  75. "noret_sch_id_seq", schema=config.test_schema
  76. ),
  77. ),
  78. primary_key=True,
  79. ),
  80. Column("data", String(50)),
  81. implicit_returning=False,
  82. schema=config.test_schema,
  83. )
  84. def test_insert_roundtrip(self, connection):
  85. connection.execute(self.tables.seq_pk.insert(), dict(data="some data"))
  86. self._assert_round_trip(self.tables.seq_pk, connection)
  87. def test_insert_lastrowid(self, connection):
  88. r = connection.execute(
  89. self.tables.seq_pk.insert(), dict(data="some data")
  90. )
  91. eq_(
  92. r.inserted_primary_key, (testing.db.dialect.default_sequence_base,)
  93. )
  94. def test_nextval_direct(self, connection):
  95. r = connection.scalar(self.tables.seq_pk.c.id.default)
  96. eq_(r, testing.db.dialect.default_sequence_base)
  97. @requirements.sequences_optional
  98. def test_optional_seq(self, connection):
  99. r = connection.execute(
  100. self.tables.seq_opt_pk.insert(), dict(data="some data")
  101. )
  102. eq_(r.inserted_primary_key, (1,))
  103. def _assert_round_trip(self, table, conn):
  104. row = conn.execute(table.select()).first()
  105. eq_(row, (testing.db.dialect.default_sequence_base, "some data"))
  106. def test_insert_roundtrip_no_implicit_returning(self, connection):
  107. connection.execute(
  108. self.tables.seq_no_returning.insert(), dict(data="some data")
  109. )
  110. self._assert_round_trip(self.tables.seq_no_returning, connection)
  111. @testing.combinations((True,), (False,), argnames="implicit_returning")
  112. @testing.requires.schemas
  113. def test_insert_roundtrip_translate(self, connection, implicit_returning):
  114. seq_no_returning = Table(
  115. "seq_no_returning_sch",
  116. MetaData(),
  117. Column(
  118. "id",
  119. Integer,
  120. normalize_sequence(
  121. config, Sequence("noret_sch_id_seq", schema="alt_schema")
  122. ),
  123. primary_key=True,
  124. ),
  125. Column("data", String(50)),
  126. implicit_returning=implicit_returning,
  127. schema="alt_schema",
  128. )
  129. connection = connection.execution_options(
  130. schema_translate_map={"alt_schema": config.test_schema}
  131. )
  132. connection.execute(seq_no_returning.insert(), dict(data="some data"))
  133. self._assert_round_trip(seq_no_returning, connection)
  134. @testing.requires.schemas
  135. def test_nextval_direct_schema_translate(self, connection):
  136. seq = normalize_sequence(
  137. config, Sequence("noret_sch_id_seq", schema="alt_schema")
  138. )
  139. connection = connection.execution_options(
  140. schema_translate_map={"alt_schema": config.test_schema}
  141. )
  142. r = connection.scalar(seq)
  143. eq_(r, testing.db.dialect.default_sequence_base)
  144. class SequenceCompilerTest(testing.AssertsCompiledSQL, fixtures.TestBase):
  145. __requires__ = ("sequences",)
  146. __backend__ = True
  147. def test_literal_binds_inline_compile(self, connection):
  148. table = Table(
  149. "x",
  150. MetaData(),
  151. Column(
  152. "y", Integer, normalize_sequence(config, Sequence("y_seq"))
  153. ),
  154. Column("q", Integer),
  155. )
  156. stmt = table.insert().values(q=5)
  157. seq_nextval = connection.dialect.statement_compiler(
  158. statement=None, dialect=connection.dialect
  159. ).visit_sequence(normalize_sequence(config, Sequence("y_seq")))
  160. self.assert_compile(
  161. stmt,
  162. "INSERT INTO x (y, q) VALUES (%s, 5)" % (seq_nextval,),
  163. literal_binds=True,
  164. dialect=connection.dialect,
  165. )
  166. class HasSequenceTest(fixtures.TablesTest):
  167. run_deletes = None
  168. __requires__ = ("sequences",)
  169. __backend__ = True
  170. @classmethod
  171. def define_tables(cls, metadata):
  172. normalize_sequence(config, Sequence("user_id_seq", metadata=metadata))
  173. normalize_sequence(
  174. config,
  175. Sequence(
  176. "other_seq",
  177. metadata=metadata,
  178. nomaxvalue=True,
  179. nominvalue=True,
  180. ),
  181. )
  182. if testing.requires.schemas.enabled:
  183. normalize_sequence(
  184. config,
  185. Sequence(
  186. "user_id_seq", schema=config.test_schema, metadata=metadata
  187. ),
  188. )
  189. normalize_sequence(
  190. config,
  191. Sequence(
  192. "schema_seq", schema=config.test_schema, metadata=metadata
  193. ),
  194. )
  195. Table(
  196. "user_id_table",
  197. metadata,
  198. Column("id", Integer, primary_key=True),
  199. )
  200. def test_has_sequence(self, connection):
  201. eq_(inspect(connection).has_sequence("user_id_seq"), True)
  202. def test_has_sequence_cache(self, connection, metadata):
  203. insp = inspect(connection)
  204. eq_(insp.has_sequence("user_id_seq"), True)
  205. ss = normalize_sequence(config, Sequence("new_seq", metadata=metadata))
  206. eq_(insp.has_sequence("new_seq"), False)
  207. ss.create(connection)
  208. try:
  209. eq_(insp.has_sequence("new_seq"), False)
  210. insp.clear_cache()
  211. eq_(insp.has_sequence("new_seq"), True)
  212. finally:
  213. ss.drop(connection)
  214. def test_has_sequence_other_object(self, connection):
  215. eq_(inspect(connection).has_sequence("user_id_table"), False)
  216. @testing.requires.schemas
  217. def test_has_sequence_schema(self, connection):
  218. eq_(
  219. inspect(connection).has_sequence(
  220. "user_id_seq", schema=config.test_schema
  221. ),
  222. True,
  223. )
  224. def test_has_sequence_neg(self, connection):
  225. eq_(inspect(connection).has_sequence("some_sequence"), False)
  226. @testing.requires.schemas
  227. def test_has_sequence_schemas_neg(self, connection):
  228. eq_(
  229. inspect(connection).has_sequence(
  230. "some_sequence", schema=config.test_schema
  231. ),
  232. False,
  233. )
  234. @testing.requires.schemas
  235. def test_has_sequence_default_not_in_remote(self, connection):
  236. eq_(
  237. inspect(connection).has_sequence(
  238. "other_sequence", schema=config.test_schema
  239. ),
  240. False,
  241. )
  242. @testing.requires.schemas
  243. def test_has_sequence_remote_not_in_default(self, connection):
  244. eq_(inspect(connection).has_sequence("schema_seq"), False)
  245. def test_get_sequence_names(self, connection):
  246. exp = {"other_seq", "user_id_seq"}
  247. res = set(inspect(connection).get_sequence_names())
  248. is_true(res.intersection(exp) == exp)
  249. is_true("schema_seq" not in res)
  250. @testing.requires.schemas
  251. def test_get_sequence_names_no_sequence_schema(self, connection):
  252. eq_(
  253. inspect(connection).get_sequence_names(
  254. schema=config.test_schema_2
  255. ),
  256. [],
  257. )
  258. @testing.requires.schemas
  259. def test_get_sequence_names_sequences_schema(self, connection):
  260. eq_(
  261. sorted(
  262. inspect(connection).get_sequence_names(
  263. schema=config.test_schema
  264. )
  265. ),
  266. ["schema_seq", "user_id_seq"],
  267. )
  268. class HasSequenceTestEmpty(fixtures.TestBase):
  269. __requires__ = ("sequences",)
  270. __backend__ = True
  271. def test_get_sequence_names_no_sequence(self, connection):
  272. eq_(
  273. inspect(connection).get_sequence_names(),
  274. [],
  275. )