test_sequence.py 8.5 KB

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