test_reflection.py 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557
  1. # testing/suite/test_reflection.py
  2. # Copyright (C) 2005-2026 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. import contextlib
  9. import operator
  10. import re
  11. import sqlalchemy as sa
  12. from .. import config
  13. from .. import engines
  14. from .. import eq_
  15. from .. import eq_regex
  16. from .. import expect_raises
  17. from .. import expect_raises_message
  18. from .. import expect_warnings
  19. from .. import fixtures
  20. from .. import is_
  21. from ..provision import get_temp_table_name
  22. from ..provision import temp_table_keyword_args
  23. from ..schema import Column
  24. from ..schema import Table
  25. from ... import Boolean
  26. from ... import DateTime
  27. from ... import event
  28. from ... import ForeignKey
  29. from ... import func
  30. from ... import Identity
  31. from ... import inspect
  32. from ... import Integer
  33. from ... import MetaData
  34. from ... import String
  35. from ... import testing
  36. from ... import types as sql_types
  37. from ...engine import Inspector
  38. from ...engine import ObjectKind
  39. from ...engine import ObjectScope
  40. from ...exc import NoSuchTableError
  41. from ...exc import UnreflectableTableError
  42. from ...schema import DDL
  43. from ...schema import Index
  44. from ...sql.elements import quoted_name
  45. from ...sql.schema import BLANK_SCHEMA
  46. from ...testing import ComparesIndexes
  47. from ...testing import ComparesTables
  48. from ...testing import is_false
  49. from ...testing import is_none
  50. from ...testing import is_true
  51. from ...testing import mock
  52. metadata, users = None, None
  53. class OneConnectionTablesTest(fixtures.TablesTest):
  54. @classmethod
  55. def setup_bind(cls):
  56. # TODO: when temp tables are subject to server reset,
  57. # this will also have to disable that server reset from
  58. # happening
  59. if config.requirements.independent_connections.enabled:
  60. from sqlalchemy import pool
  61. return engines.testing_engine(
  62. options=dict(poolclass=pool.StaticPool, scope="class"),
  63. )
  64. else:
  65. return config.db
  66. class HasTableTest(OneConnectionTablesTest):
  67. __sparse_driver_backend__ = True
  68. run_deletes = None
  69. @classmethod
  70. def define_tables(cls, metadata):
  71. Table(
  72. "test_table",
  73. metadata,
  74. Column("id", Integer, primary_key=True),
  75. Column("data", String(50)),
  76. )
  77. if testing.requires.schemas.enabled:
  78. Table(
  79. "test_table_s",
  80. metadata,
  81. Column("id", Integer, primary_key=True),
  82. Column("data", String(50)),
  83. schema=config.test_schema,
  84. )
  85. if testing.requires.view_reflection:
  86. cls.define_views(metadata)
  87. if testing.requires.has_temp_table.enabled:
  88. cls.define_temp_tables(metadata)
  89. @classmethod
  90. def define_views(cls, metadata):
  91. query = "CREATE VIEW vv AS SELECT id, data FROM test_table"
  92. event.listen(metadata, "after_create", DDL(query))
  93. event.listen(metadata, "before_drop", DDL("DROP VIEW vv"))
  94. if testing.requires.schemas.enabled:
  95. query = (
  96. "CREATE VIEW %s.vv AS SELECT id, data FROM %s.test_table_s"
  97. % (
  98. config.test_schema,
  99. config.test_schema,
  100. )
  101. )
  102. event.listen(metadata, "after_create", DDL(query))
  103. event.listen(
  104. metadata,
  105. "before_drop",
  106. DDL("DROP VIEW %s.vv" % (config.test_schema)),
  107. )
  108. @classmethod
  109. def temp_table_name(cls):
  110. return get_temp_table_name(
  111. config, config.db, f"user_tmp_{config.ident}"
  112. )
  113. @classmethod
  114. def define_temp_tables(cls, metadata):
  115. kw = temp_table_keyword_args(config, config.db)
  116. table_name = cls.temp_table_name()
  117. user_tmp = Table(
  118. table_name,
  119. metadata,
  120. Column("id", sa.INT, primary_key=True),
  121. Column("name", sa.VARCHAR(50)),
  122. **kw,
  123. )
  124. if (
  125. testing.requires.view_reflection.enabled
  126. and testing.requires.temporary_views.enabled
  127. ):
  128. event.listen(
  129. user_tmp,
  130. "after_create",
  131. DDL(
  132. "create temporary view user_tmp_v as "
  133. "select * from user_tmp_%s" % config.ident
  134. ),
  135. )
  136. event.listen(user_tmp, "before_drop", DDL("drop view user_tmp_v"))
  137. def test_has_table(self):
  138. with config.db.begin() as conn:
  139. is_true(config.db.dialect.has_table(conn, "test_table"))
  140. is_false(config.db.dialect.has_table(conn, "test_table_s"))
  141. is_false(config.db.dialect.has_table(conn, "nonexistent_table"))
  142. def test_has_table_cache(self, metadata):
  143. insp = inspect(config.db)
  144. is_true(insp.has_table("test_table"))
  145. nt = Table("new_table", metadata, Column("col", Integer))
  146. is_false(insp.has_table("new_table"))
  147. nt.create(config.db)
  148. try:
  149. is_false(insp.has_table("new_table"))
  150. insp.clear_cache()
  151. is_true(insp.has_table("new_table"))
  152. finally:
  153. nt.drop(config.db)
  154. @testing.requires.schemas
  155. def test_has_table_schema(self):
  156. with config.db.begin() as conn:
  157. is_false(
  158. config.db.dialect.has_table(
  159. conn, "test_table", schema=config.test_schema
  160. )
  161. )
  162. is_true(
  163. config.db.dialect.has_table(
  164. conn, "test_table_s", schema=config.test_schema
  165. )
  166. )
  167. is_false(
  168. config.db.dialect.has_table(
  169. conn, "nonexistent_table", schema=config.test_schema
  170. )
  171. )
  172. @testing.requires.schemas
  173. def test_has_table_nonexistent_schema(self):
  174. with config.db.begin() as conn:
  175. is_false(
  176. config.db.dialect.has_table(
  177. conn, "test_table", schema="nonexistent_schema"
  178. )
  179. )
  180. @testing.requires.views
  181. def test_has_table_view(self, connection):
  182. insp = inspect(connection)
  183. is_true(insp.has_table("vv"))
  184. @testing.requires.has_temp_table
  185. def test_has_table_temp_table(self, connection):
  186. insp = inspect(connection)
  187. temp_table_name = self.temp_table_name()
  188. is_true(insp.has_table(temp_table_name))
  189. @testing.requires.has_temp_table
  190. @testing.requires.view_reflection
  191. @testing.requires.temporary_views
  192. def test_has_table_temp_view(self, connection):
  193. insp = inspect(connection)
  194. is_true(insp.has_table("user_tmp_v"))
  195. @testing.requires.views
  196. @testing.requires.schemas
  197. def test_has_table_view_schema(self, connection):
  198. insp = inspect(connection)
  199. is_true(insp.has_table("vv", config.test_schema))
  200. class HasIndexTest(fixtures.TablesTest):
  201. __sparse_driver_backend__ = True
  202. __requires__ = ("index_reflection",)
  203. @classmethod
  204. def define_tables(cls, metadata):
  205. tt = Table(
  206. "test_table",
  207. metadata,
  208. Column("id", Integer, primary_key=True),
  209. Column("data", String(50)),
  210. Column("data2", String(50)),
  211. )
  212. Index("my_idx", tt.c.data)
  213. if testing.requires.schemas.enabled:
  214. tt = Table(
  215. "test_table",
  216. metadata,
  217. Column("id", Integer, primary_key=True),
  218. Column("data", String(50)),
  219. schema=config.test_schema,
  220. )
  221. Index("my_idx_s", tt.c.data)
  222. kind = testing.combinations("dialect", "inspector", argnames="kind")
  223. def _has_index(self, kind, conn):
  224. if kind == "dialect":
  225. return lambda *a, **k: config.db.dialect.has_index(conn, *a, **k)
  226. else:
  227. return inspect(conn).has_index
  228. @kind
  229. def test_has_index(self, kind, connection, metadata):
  230. meth = self._has_index(kind, connection)
  231. assert meth("test_table", "my_idx")
  232. assert not meth("test_table", "my_idx_s")
  233. assert not meth("nonexistent_table", "my_idx")
  234. assert not meth("test_table", "nonexistent_idx")
  235. assert not meth("test_table", "my_idx_2")
  236. assert not meth("test_table_2", "my_idx_3")
  237. idx = Index("my_idx_2", self.tables.test_table.c.data2)
  238. tbl = Table(
  239. "test_table_2",
  240. metadata,
  241. Column("foo", Integer),
  242. Index("my_idx_3", "foo"),
  243. )
  244. idx.create(connection)
  245. tbl.create(connection)
  246. try:
  247. if kind == "inspector":
  248. assert not meth("test_table", "my_idx_2")
  249. assert not meth("test_table_2", "my_idx_3")
  250. meth.__self__.clear_cache()
  251. assert meth("test_table", "my_idx_2") is True
  252. assert meth("test_table_2", "my_idx_3") is True
  253. finally:
  254. tbl.drop(connection)
  255. idx.drop(connection)
  256. @testing.requires.schemas
  257. @kind
  258. def test_has_index_schema(self, kind, connection):
  259. meth = self._has_index(kind, connection)
  260. assert meth("test_table", "my_idx_s", schema=config.test_schema)
  261. assert not meth("test_table", "my_idx", schema=config.test_schema)
  262. assert not meth(
  263. "nonexistent_table", "my_idx_s", schema=config.test_schema
  264. )
  265. assert not meth(
  266. "test_table", "nonexistent_idx_s", schema=config.test_schema
  267. )
  268. class BizarroCharacterTest(fixtures.TestBase):
  269. __sparse_driver_backend__ = True
  270. def column_names():
  271. return testing.combinations(
  272. ("plainname",),
  273. ("(3)",),
  274. ("col%p",),
  275. ("[brack]",),
  276. argnames="columnname",
  277. )
  278. def table_names():
  279. return testing.combinations(
  280. ("plain",),
  281. ("(2)",),
  282. ("per % cent",),
  283. ("[brackets]",),
  284. argnames="tablename",
  285. )
  286. @testing.variation("use_composite", [True, False])
  287. @column_names()
  288. @table_names()
  289. @testing.requires.foreign_key_constraint_reflection
  290. def test_fk_ref(
  291. self, connection, metadata, use_composite, tablename, columnname
  292. ):
  293. """tests for #10275"""
  294. tt = Table(
  295. tablename,
  296. metadata,
  297. Column(columnname, Integer, key="id", primary_key=True),
  298. test_needs_fk=True,
  299. )
  300. if use_composite:
  301. tt.append_column(Column("id2", Integer, primary_key=True))
  302. if use_composite:
  303. Table(
  304. "other",
  305. metadata,
  306. Column("id", Integer, primary_key=True),
  307. Column("ref", Integer),
  308. Column("ref2", Integer),
  309. sa.ForeignKeyConstraint(["ref", "ref2"], [tt.c.id, tt.c.id2]),
  310. test_needs_fk=True,
  311. )
  312. else:
  313. Table(
  314. "other",
  315. metadata,
  316. Column("id", Integer, primary_key=True),
  317. Column("ref", ForeignKey(tt.c.id)),
  318. test_needs_fk=True,
  319. )
  320. metadata.create_all(connection)
  321. m2 = MetaData()
  322. o2 = Table("other", m2, autoload_with=connection)
  323. t1 = m2.tables[tablename]
  324. assert o2.c.ref.references(t1.c[0])
  325. if use_composite:
  326. assert o2.c.ref2.references(t1.c[1])
  327. @column_names()
  328. @table_names()
  329. @testing.requires.identity_columns
  330. def test_reflect_identity(
  331. self, tablename, columnname, connection, metadata
  332. ):
  333. Table(
  334. tablename,
  335. metadata,
  336. Column(columnname, Integer, Identity(), primary_key=True),
  337. )
  338. metadata.create_all(connection)
  339. insp = inspect(connection)
  340. eq_(insp.get_columns(tablename)[0]["identity"]["start"], 1)
  341. @column_names()
  342. @table_names()
  343. @testing.requires.comment_reflection
  344. def test_reflect_comments(
  345. self, tablename, columnname, connection, metadata
  346. ):
  347. Table(
  348. tablename,
  349. metadata,
  350. Column("id", Integer, primary_key=True),
  351. Column(columnname, Integer, comment="some comment"),
  352. )
  353. metadata.create_all(connection)
  354. insp = inspect(connection)
  355. eq_(insp.get_columns(tablename)[1]["comment"], "some comment")
  356. class TempTableElementsTest(fixtures.TestBase):
  357. __sparse_driver_backend__ = True
  358. __requires__ = ("temp_table_reflection",)
  359. @testing.fixture
  360. def tablename(self):
  361. return get_temp_table_name(
  362. config, config.db, f"ident_tmp_{config.ident}"
  363. )
  364. @testing.requires.identity_columns
  365. def test_reflect_identity(self, tablename, connection, metadata):
  366. Table(
  367. tablename,
  368. metadata,
  369. Column("id", Integer, Identity(), primary_key=True),
  370. )
  371. metadata.create_all(connection)
  372. insp = inspect(connection)
  373. eq_(insp.get_columns(tablename)[0]["identity"]["start"], 1)
  374. @testing.requires.temp_table_comment_reflection
  375. def test_reflect_comments(self, tablename, connection, metadata):
  376. Table(
  377. tablename,
  378. metadata,
  379. Column("id", Integer, primary_key=True),
  380. Column("foobar", Integer, comment="some comment"),
  381. )
  382. metadata.create_all(connection)
  383. insp = inspect(connection)
  384. eq_(insp.get_columns(tablename)[1]["comment"], "some comment")
  385. class QuotedNameArgumentTest(fixtures.TablesTest):
  386. run_create_tables = "once"
  387. __sparse_driver_backend__ = True
  388. @classmethod
  389. def define_tables(cls, metadata):
  390. Table(
  391. "quote ' one",
  392. metadata,
  393. Column("id", Integer),
  394. Column("name", String(50)),
  395. Column("data", String(50)),
  396. Column("related_id", Integer),
  397. sa.PrimaryKeyConstraint("id", name="pk quote ' one"),
  398. sa.Index("ix quote ' one", "name"),
  399. sa.UniqueConstraint(
  400. "data",
  401. name="uq quote' one",
  402. ),
  403. sa.ForeignKeyConstraint(
  404. ["id"], ["related.id"], name="fk quote ' one"
  405. ),
  406. sa.CheckConstraint("name != 'foo'", name="ck quote ' one"),
  407. comment=r"""quote ' one comment""",
  408. test_needs_fk=True,
  409. )
  410. if testing.requires.symbol_names_w_double_quote.enabled:
  411. Table(
  412. 'quote " two',
  413. metadata,
  414. Column("id", Integer),
  415. Column("name", String(50)),
  416. Column("data", String(50)),
  417. Column("related_id", Integer),
  418. sa.PrimaryKeyConstraint("id", name='pk quote " two'),
  419. sa.Index('ix quote " two', "name"),
  420. sa.UniqueConstraint(
  421. "data",
  422. name='uq quote" two',
  423. ),
  424. sa.ForeignKeyConstraint(
  425. ["id"], ["related.id"], name='fk quote " two'
  426. ),
  427. sa.CheckConstraint("name != 'foo'", name='ck quote " two '),
  428. comment=r"""quote " two comment""",
  429. test_needs_fk=True,
  430. )
  431. Table(
  432. "related",
  433. metadata,
  434. Column("id", Integer, primary_key=True),
  435. Column("related", Integer),
  436. test_needs_fk=True,
  437. )
  438. if testing.requires.view_column_reflection.enabled:
  439. if testing.requires.symbol_names_w_double_quote.enabled:
  440. names = [
  441. "quote ' one",
  442. 'quote " two',
  443. ]
  444. else:
  445. names = [
  446. "quote ' one",
  447. ]
  448. for name in names:
  449. query = "CREATE VIEW %s AS SELECT * FROM %s" % (
  450. config.db.dialect.identifier_preparer.quote(
  451. "view %s" % name
  452. ),
  453. config.db.dialect.identifier_preparer.quote(name),
  454. )
  455. event.listen(metadata, "after_create", DDL(query))
  456. event.listen(
  457. metadata,
  458. "before_drop",
  459. DDL(
  460. "DROP VIEW %s"
  461. % config.db.dialect.identifier_preparer.quote(
  462. "view %s" % name
  463. )
  464. ),
  465. )
  466. def quote_fixtures(fn):
  467. return testing.combinations(
  468. ("quote ' one",),
  469. ('quote " two', testing.requires.symbol_names_w_double_quote),
  470. )(fn)
  471. @quote_fixtures
  472. def test_get_table_options(self, name):
  473. insp = inspect(config.db)
  474. if testing.requires.reflect_table_options.enabled:
  475. res = insp.get_table_options(name)
  476. is_true(isinstance(res, dict))
  477. else:
  478. with expect_raises(NotImplementedError):
  479. insp.get_table_options(name)
  480. @quote_fixtures
  481. @testing.requires.view_column_reflection
  482. def test_get_view_definition(self, name):
  483. insp = inspect(config.db)
  484. assert insp.get_view_definition("view %s" % name)
  485. @quote_fixtures
  486. def test_get_columns(self, name):
  487. insp = inspect(config.db)
  488. assert insp.get_columns(name)
  489. @quote_fixtures
  490. def test_get_pk_constraint(self, name):
  491. insp = inspect(config.db)
  492. assert insp.get_pk_constraint(name)
  493. @quote_fixtures
  494. @testing.requires.foreign_key_constraint_reflection
  495. def test_get_foreign_keys(self, name):
  496. insp = inspect(config.db)
  497. assert insp.get_foreign_keys(name)
  498. @quote_fixtures
  499. @testing.requires.index_reflection
  500. def test_get_indexes(self, name):
  501. insp = inspect(config.db)
  502. assert insp.get_indexes(name)
  503. @quote_fixtures
  504. @testing.requires.unique_constraint_reflection
  505. def test_get_unique_constraints(self, name):
  506. insp = inspect(config.db)
  507. assert insp.get_unique_constraints(name)
  508. @quote_fixtures
  509. @testing.requires.comment_reflection
  510. def test_get_table_comment(self, name):
  511. insp = inspect(config.db)
  512. assert insp.get_table_comment(name)
  513. @quote_fixtures
  514. @testing.requires.check_constraint_reflection
  515. def test_get_check_constraints(self, name):
  516. insp = inspect(config.db)
  517. assert insp.get_check_constraints(name)
  518. def _multi_combination(fn):
  519. schema = testing.combinations(
  520. None,
  521. (
  522. lambda: config.test_schema,
  523. testing.requires.schemas,
  524. ),
  525. argnames="schema",
  526. )
  527. scope = testing.combinations(
  528. ObjectScope.DEFAULT,
  529. ObjectScope.TEMPORARY,
  530. ObjectScope.ANY,
  531. argnames="scope",
  532. )
  533. kind = testing.combinations(
  534. ObjectKind.TABLE,
  535. ObjectKind.VIEW,
  536. ObjectKind.MATERIALIZED_VIEW,
  537. ObjectKind.ANY,
  538. ObjectKind.ANY_VIEW,
  539. ObjectKind.TABLE | ObjectKind.VIEW,
  540. ObjectKind.TABLE | ObjectKind.MATERIALIZED_VIEW,
  541. argnames="kind",
  542. )
  543. filter_names = testing.combinations(True, False, argnames="use_filter")
  544. return schema(scope(kind(filter_names(fn))))
  545. class ComponentReflectionTest(ComparesTables, OneConnectionTablesTest):
  546. run_inserts = run_deletes = None
  547. __sparse_driver_backend__ = True
  548. @classmethod
  549. def define_tables(cls, metadata):
  550. cls.define_reflected_tables(metadata, None)
  551. if testing.requires.schemas.enabled:
  552. cls.define_reflected_tables(metadata, testing.config.test_schema)
  553. @classmethod
  554. def define_reflected_tables(cls, metadata, schema):
  555. if schema:
  556. schema_prefix = schema + "."
  557. else:
  558. schema_prefix = ""
  559. if testing.requires.self_referential_foreign_keys.enabled:
  560. parent_id_args = (
  561. ForeignKey(
  562. "%susers.user_id" % schema_prefix, name="user_id_fk"
  563. ),
  564. )
  565. else:
  566. parent_id_args = ()
  567. users = Table(
  568. "users",
  569. metadata,
  570. Column("user_id", sa.INT, primary_key=True),
  571. Column("test1", sa.CHAR(5), nullable=False),
  572. Column("test2", sa.Float(), nullable=False),
  573. Column("parent_user_id", sa.Integer, *parent_id_args),
  574. sa.CheckConstraint(
  575. "test2 > 0",
  576. name="zz_test2_gt_zero",
  577. comment="users check constraint",
  578. ),
  579. sa.CheckConstraint("test2 <= 1000"),
  580. schema=schema,
  581. test_needs_fk=True,
  582. )
  583. Table(
  584. "dingalings",
  585. metadata,
  586. Column("dingaling_id", sa.Integer, primary_key=True),
  587. Column(
  588. "address_id",
  589. sa.Integer,
  590. ForeignKey(
  591. "%semail_addresses.address_id" % schema_prefix,
  592. name="zz_email_add_id_fg",
  593. comment="di fk comment",
  594. ),
  595. ),
  596. Column(
  597. "id_user",
  598. sa.Integer,
  599. ForeignKey("%susers.user_id" % schema_prefix),
  600. ),
  601. Column("data", sa.String(30), unique=True),
  602. sa.CheckConstraint(
  603. "address_id > 0 AND address_id < 1000",
  604. name="address_id_gt_zero",
  605. ),
  606. sa.UniqueConstraint(
  607. "address_id",
  608. "dingaling_id",
  609. name="zz_dingalings_multiple",
  610. comment="di unique comment",
  611. ),
  612. schema=schema,
  613. test_needs_fk=True,
  614. )
  615. Table(
  616. "email_addresses",
  617. metadata,
  618. Column("address_id", sa.Integer),
  619. Column("remote_user_id", sa.Integer, ForeignKey(users.c.user_id)),
  620. Column("email_address", sa.String(20), index=True),
  621. sa.PrimaryKeyConstraint(
  622. "address_id", name="email_ad_pk", comment="ea pk comment"
  623. ),
  624. schema=schema,
  625. test_needs_fk=True,
  626. )
  627. Table(
  628. "comment_test",
  629. metadata,
  630. Column("id", sa.Integer, primary_key=True, comment="id comment"),
  631. Column("data", sa.String(20), comment="data % comment"),
  632. Column(
  633. "d2",
  634. sa.String(20),
  635. comment=r"""Comment types type speedily ' " \ '' Fun!""",
  636. ),
  637. Column("d3", sa.String(42), comment="Comment\nwith\rescapes"),
  638. schema=schema,
  639. comment=r"""the test % ' " \ table comment""",
  640. )
  641. Table(
  642. "no_constraints",
  643. metadata,
  644. Column("data", sa.String(20)),
  645. schema=schema,
  646. comment="no\nconstraints\rhas\fescaped\vcomment",
  647. )
  648. if testing.requires.cross_schema_fk_reflection.enabled:
  649. if schema is None:
  650. Table(
  651. "local_table",
  652. metadata,
  653. Column("id", sa.Integer, primary_key=True),
  654. Column("data", sa.String(20)),
  655. Column(
  656. "remote_id",
  657. ForeignKey(
  658. "%s.remote_table_2.id" % testing.config.test_schema
  659. ),
  660. ),
  661. test_needs_fk=True,
  662. schema=config.db.dialect.default_schema_name,
  663. )
  664. else:
  665. Table(
  666. "remote_table",
  667. metadata,
  668. Column("id", sa.Integer, primary_key=True),
  669. Column(
  670. "local_id",
  671. ForeignKey(
  672. "%s.local_table.id"
  673. % config.db.dialect.default_schema_name
  674. ),
  675. ),
  676. Column("data", sa.String(20)),
  677. schema=schema,
  678. test_needs_fk=True,
  679. )
  680. Table(
  681. "remote_table_2",
  682. metadata,
  683. Column("id", sa.Integer, primary_key=True),
  684. Column("data", sa.String(20)),
  685. schema=schema,
  686. test_needs_fk=True,
  687. )
  688. if testing.requires.index_reflection.enabled:
  689. Index("users_t_idx", users.c.test1, users.c.test2, unique=True)
  690. Index(
  691. "users_all_idx", users.c.user_id, users.c.test2, users.c.test1
  692. )
  693. if not schema:
  694. # test_needs_fk is at the moment to force MySQL InnoDB
  695. noncol_idx_test_nopk = Table(
  696. "noncol_idx_test_nopk",
  697. metadata,
  698. Column("q", sa.String(5)),
  699. test_needs_fk=True,
  700. )
  701. noncol_idx_test_pk = Table(
  702. "noncol_idx_test_pk",
  703. metadata,
  704. Column("id", sa.Integer, primary_key=True),
  705. Column("q", sa.String(5)),
  706. test_needs_fk=True,
  707. )
  708. if (
  709. testing.requires.indexes_with_ascdesc.enabled
  710. and testing.requires.reflect_indexes_with_ascdesc.enabled
  711. ):
  712. Index("noncol_idx_nopk", noncol_idx_test_nopk.c.q.desc())
  713. Index("noncol_idx_pk", noncol_idx_test_pk.c.q.desc())
  714. if testing.requires.view_column_reflection.enabled:
  715. cls.define_views(metadata, schema)
  716. if not schema and testing.requires.temp_table_reflection.enabled:
  717. cls.define_temp_tables(metadata)
  718. @classmethod
  719. def temp_table_name(cls):
  720. return get_temp_table_name(
  721. config, config.db, f"user_tmp_{config.ident}"
  722. )
  723. @classmethod
  724. def define_temp_tables(cls, metadata):
  725. kw = temp_table_keyword_args(config, config.db)
  726. table_name = cls.temp_table_name()
  727. user_tmp = Table(
  728. table_name,
  729. metadata,
  730. Column("id", sa.INT, primary_key=True),
  731. Column("name", sa.VARCHAR(50)),
  732. Column("foo", sa.INT),
  733. # disambiguate temp table unique constraint names. this is
  734. # pretty arbitrary for a generic dialect however we are doing
  735. # it to suit SQL Server which will produce name conflicts for
  736. # unique constraints created against temp tables in different
  737. # databases.
  738. # https://www.arbinada.com/en/node/1645
  739. sa.UniqueConstraint("name", name=f"user_tmp_uq_{config.ident}"),
  740. sa.Index("user_tmp_ix", "foo"),
  741. **kw,
  742. )
  743. if (
  744. testing.requires.view_reflection.enabled
  745. and testing.requires.temporary_views.enabled
  746. ):
  747. event.listen(
  748. user_tmp,
  749. "after_create",
  750. DDL(
  751. "create temporary view user_tmp_v as "
  752. "select * from user_tmp_%s" % config.ident
  753. ),
  754. )
  755. event.listen(user_tmp, "before_drop", DDL("drop view user_tmp_v"))
  756. @classmethod
  757. def define_views(cls, metadata, schema):
  758. if testing.requires.materialized_views.enabled:
  759. materialized = {"dingalings"}
  760. else:
  761. materialized = set()
  762. for table_name in ("users", "email_addresses", "dingalings"):
  763. fullname = table_name
  764. if schema:
  765. fullname = f"{schema}.{table_name}"
  766. view_name = fullname + "_v"
  767. prefix = "MATERIALIZED " if table_name in materialized else ""
  768. query = (
  769. f"CREATE {prefix}VIEW {view_name} AS SELECT * FROM {fullname}"
  770. )
  771. event.listen(metadata, "after_create", DDL(query))
  772. if table_name in materialized:
  773. index_name = "mat_index"
  774. if schema and testing.against("oracle"):
  775. index_name = f"{schema}.{index_name}"
  776. idx = f"CREATE INDEX {index_name} ON {view_name}(data)"
  777. event.listen(metadata, "after_create", DDL(idx))
  778. event.listen(
  779. metadata, "before_drop", DDL(f"DROP {prefix}VIEW {view_name}")
  780. )
  781. def _resolve_kind(self, kind, tables, views, materialized):
  782. res = {}
  783. if ObjectKind.TABLE in kind:
  784. res.update(tables)
  785. if ObjectKind.VIEW in kind:
  786. res.update(views)
  787. if ObjectKind.MATERIALIZED_VIEW in kind:
  788. res.update(materialized)
  789. return res
  790. def _resolve_views(self, views, materialized):
  791. if not testing.requires.view_column_reflection.enabled:
  792. materialized.clear()
  793. views.clear()
  794. elif not testing.requires.materialized_views.enabled:
  795. views.update(materialized)
  796. materialized.clear()
  797. def _resolve_names(self, schema, scope, filter_names, values):
  798. scope_filter = lambda _: True # noqa: E731
  799. if scope is ObjectScope.DEFAULT:
  800. scope_filter = lambda k: "tmp" not in k[1] # noqa: E731
  801. if scope is ObjectScope.TEMPORARY:
  802. scope_filter = lambda k: "tmp" in k[1] # noqa: E731
  803. removed = {
  804. None: {"remote_table", "remote_table_2"},
  805. testing.config.test_schema: {
  806. "local_table",
  807. "noncol_idx_test_nopk",
  808. "noncol_idx_test_pk",
  809. "user_tmp_v",
  810. self.temp_table_name(),
  811. },
  812. }
  813. if not testing.requires.cross_schema_fk_reflection.enabled:
  814. removed[None].add("local_table")
  815. removed[testing.config.test_schema].update(
  816. ["remote_table", "remote_table_2"]
  817. )
  818. if not testing.requires.index_reflection.enabled:
  819. removed[None].update(
  820. ["noncol_idx_test_nopk", "noncol_idx_test_pk"]
  821. )
  822. if (
  823. not testing.requires.temp_table_reflection.enabled
  824. or not testing.requires.temp_table_names.enabled
  825. ):
  826. removed[None].update(["user_tmp_v", self.temp_table_name()])
  827. if not testing.requires.temporary_views.enabled:
  828. removed[None].update(["user_tmp_v"])
  829. res = {
  830. k: v
  831. for k, v in values.items()
  832. if scope_filter(k)
  833. and k[1] not in removed[schema]
  834. and (not filter_names or k[1] in filter_names)
  835. }
  836. return res
  837. def exp_options(
  838. self,
  839. schema=None,
  840. scope=ObjectScope.ANY,
  841. kind=ObjectKind.ANY,
  842. filter_names=None,
  843. ):
  844. materialized = {(schema, "dingalings_v"): mock.ANY}
  845. views = {
  846. (schema, "email_addresses_v"): mock.ANY,
  847. (schema, "users_v"): mock.ANY,
  848. (schema, "user_tmp_v"): mock.ANY,
  849. }
  850. self._resolve_views(views, materialized)
  851. tables = {
  852. (schema, "users"): mock.ANY,
  853. (schema, "dingalings"): mock.ANY,
  854. (schema, "email_addresses"): mock.ANY,
  855. (schema, "comment_test"): mock.ANY,
  856. (schema, "no_constraints"): mock.ANY,
  857. (schema, "local_table"): mock.ANY,
  858. (schema, "remote_table"): mock.ANY,
  859. (schema, "remote_table_2"): mock.ANY,
  860. (schema, "noncol_idx_test_nopk"): mock.ANY,
  861. (schema, "noncol_idx_test_pk"): mock.ANY,
  862. (schema, self.temp_table_name()): mock.ANY,
  863. }
  864. res = self._resolve_kind(kind, tables, views, materialized)
  865. res = self._resolve_names(schema, scope, filter_names, res)
  866. return res
  867. def exp_comments(
  868. self,
  869. schema=None,
  870. scope=ObjectScope.ANY,
  871. kind=ObjectKind.ANY,
  872. filter_names=None,
  873. ):
  874. empty = {"text": None}
  875. materialized = {(schema, "dingalings_v"): empty}
  876. views = {
  877. (schema, "email_addresses_v"): empty,
  878. (schema, "users_v"): empty,
  879. (schema, "user_tmp_v"): empty,
  880. }
  881. self._resolve_views(views, materialized)
  882. tables = {
  883. (schema, "users"): empty,
  884. (schema, "dingalings"): empty,
  885. (schema, "email_addresses"): empty,
  886. (schema, "comment_test"): {
  887. "text": r"""the test % ' " \ table comment"""
  888. },
  889. (schema, "no_constraints"): {
  890. "text": "no\nconstraints\rhas\fescaped\vcomment"
  891. },
  892. (schema, "local_table"): empty,
  893. (schema, "remote_table"): empty,
  894. (schema, "remote_table_2"): empty,
  895. (schema, "noncol_idx_test_nopk"): empty,
  896. (schema, "noncol_idx_test_pk"): empty,
  897. (schema, self.temp_table_name()): empty,
  898. }
  899. res = self._resolve_kind(kind, tables, views, materialized)
  900. res = self._resolve_names(schema, scope, filter_names, res)
  901. return res
  902. def exp_columns(
  903. self,
  904. schema=None,
  905. scope=ObjectScope.ANY,
  906. kind=ObjectKind.ANY,
  907. filter_names=None,
  908. ):
  909. def col(
  910. name, auto=False, default=mock.ANY, comment=None, nullable=True
  911. ):
  912. res = {
  913. "name": name,
  914. "autoincrement": auto,
  915. "type": mock.ANY,
  916. "default": default,
  917. "comment": comment,
  918. "nullable": nullable,
  919. }
  920. if auto == "omit":
  921. res.pop("autoincrement")
  922. return res
  923. def pk(name, **kw):
  924. kw = {"auto": True, "default": mock.ANY, "nullable": False, **kw}
  925. return col(name, **kw)
  926. materialized = {
  927. (schema, "dingalings_v"): [
  928. col("dingaling_id", auto="omit", nullable=mock.ANY),
  929. col("address_id"),
  930. col("id_user"),
  931. col("data"),
  932. ]
  933. }
  934. views = {
  935. (schema, "email_addresses_v"): [
  936. col("address_id", auto="omit", nullable=mock.ANY),
  937. col("remote_user_id"),
  938. col("email_address"),
  939. ],
  940. (schema, "users_v"): [
  941. col("user_id", auto="omit", nullable=mock.ANY),
  942. col("test1", nullable=mock.ANY),
  943. col("test2", nullable=mock.ANY),
  944. col("parent_user_id"),
  945. ],
  946. (schema, "user_tmp_v"): [
  947. col("id", auto="omit", nullable=mock.ANY),
  948. col("name"),
  949. col("foo"),
  950. ],
  951. }
  952. self._resolve_views(views, materialized)
  953. tables = {
  954. (schema, "users"): [
  955. pk("user_id"),
  956. col("test1", nullable=False),
  957. col("test2", nullable=False),
  958. col("parent_user_id"),
  959. ],
  960. (schema, "dingalings"): [
  961. pk("dingaling_id"),
  962. col("address_id"),
  963. col("id_user"),
  964. col("data"),
  965. ],
  966. (schema, "email_addresses"): [
  967. pk("address_id"),
  968. col("remote_user_id"),
  969. col("email_address"),
  970. ],
  971. (schema, "comment_test"): [
  972. pk("id", comment="id comment"),
  973. col("data", comment="data % comment"),
  974. col(
  975. "d2",
  976. comment=r"""Comment types type speedily ' " \ '' Fun!""",
  977. ),
  978. col("d3", comment="Comment\nwith\rescapes"),
  979. ],
  980. (schema, "no_constraints"): [col("data")],
  981. (schema, "local_table"): [pk("id"), col("data"), col("remote_id")],
  982. (schema, "remote_table"): [pk("id"), col("local_id"), col("data")],
  983. (schema, "remote_table_2"): [pk("id"), col("data")],
  984. (schema, "noncol_idx_test_nopk"): [col("q")],
  985. (schema, "noncol_idx_test_pk"): [pk("id"), col("q")],
  986. (schema, self.temp_table_name()): [
  987. pk("id"),
  988. col("name"),
  989. col("foo"),
  990. ],
  991. }
  992. res = self._resolve_kind(kind, tables, views, materialized)
  993. res = self._resolve_names(schema, scope, filter_names, res)
  994. return res
  995. @property
  996. def _required_column_keys(self):
  997. return {"name", "type", "nullable", "default"}
  998. def exp_pks(
  999. self,
  1000. schema=None,
  1001. scope=ObjectScope.ANY,
  1002. kind=ObjectKind.ANY,
  1003. filter_names=None,
  1004. ):
  1005. def pk(*cols, name=mock.ANY, comment=None):
  1006. return {
  1007. "constrained_columns": list(cols),
  1008. "name": name,
  1009. "comment": comment,
  1010. }
  1011. empty = pk(name=None)
  1012. if testing.requires.materialized_views_reflect_pk.enabled:
  1013. materialized = {(schema, "dingalings_v"): pk("dingaling_id")}
  1014. else:
  1015. materialized = {(schema, "dingalings_v"): empty}
  1016. views = {
  1017. (schema, "email_addresses_v"): empty,
  1018. (schema, "users_v"): empty,
  1019. (schema, "user_tmp_v"): empty,
  1020. }
  1021. self._resolve_views(views, materialized)
  1022. tables = {
  1023. (schema, "users"): pk("user_id"),
  1024. (schema, "dingalings"): pk("dingaling_id"),
  1025. (schema, "email_addresses"): pk(
  1026. "address_id", name="email_ad_pk", comment="ea pk comment"
  1027. ),
  1028. (schema, "comment_test"): pk("id"),
  1029. (schema, "no_constraints"): empty,
  1030. (schema, "local_table"): pk("id"),
  1031. (schema, "remote_table"): pk("id"),
  1032. (schema, "remote_table_2"): pk("id"),
  1033. (schema, "noncol_idx_test_nopk"): empty,
  1034. (schema, "noncol_idx_test_pk"): pk("id"),
  1035. (schema, self.temp_table_name()): pk("id"),
  1036. }
  1037. if not testing.requires.reflects_pk_names.enabled:
  1038. for val in tables.values():
  1039. if val["name"] is not None:
  1040. val["name"] = mock.ANY
  1041. res = self._resolve_kind(kind, tables, views, materialized)
  1042. res = self._resolve_names(schema, scope, filter_names, res)
  1043. return res
  1044. @property
  1045. def _required_pk_keys(self):
  1046. return {"name", "constrained_columns"}
  1047. def exp_fks(
  1048. self,
  1049. schema=None,
  1050. scope=ObjectScope.ANY,
  1051. kind=ObjectKind.ANY,
  1052. filter_names=None,
  1053. ):
  1054. class tt:
  1055. def __eq__(self, other):
  1056. return (
  1057. other is None
  1058. or config.db.dialect.default_schema_name == other
  1059. )
  1060. def fk(
  1061. cols,
  1062. ref_col,
  1063. ref_table,
  1064. ref_schema=schema,
  1065. name=mock.ANY,
  1066. comment=None,
  1067. ):
  1068. return {
  1069. "constrained_columns": cols,
  1070. "referred_columns": ref_col,
  1071. "name": name,
  1072. "options": mock.ANY,
  1073. "referred_schema": (
  1074. ref_schema if ref_schema is not None else tt()
  1075. ),
  1076. "referred_table": ref_table,
  1077. "comment": comment,
  1078. }
  1079. materialized = {(schema, "dingalings_v"): []}
  1080. views = {
  1081. (schema, "email_addresses_v"): [],
  1082. (schema, "users_v"): [],
  1083. (schema, "user_tmp_v"): [],
  1084. }
  1085. self._resolve_views(views, materialized)
  1086. tables = {
  1087. (schema, "users"): [
  1088. fk(["parent_user_id"], ["user_id"], "users", name="user_id_fk")
  1089. ],
  1090. (schema, "dingalings"): [
  1091. fk(["id_user"], ["user_id"], "users"),
  1092. fk(
  1093. ["address_id"],
  1094. ["address_id"],
  1095. "email_addresses",
  1096. name="zz_email_add_id_fg",
  1097. comment="di fk comment",
  1098. ),
  1099. ],
  1100. (schema, "email_addresses"): [
  1101. fk(["remote_user_id"], ["user_id"], "users")
  1102. ],
  1103. (schema, "comment_test"): [],
  1104. (schema, "no_constraints"): [],
  1105. (schema, "local_table"): [
  1106. fk(
  1107. ["remote_id"],
  1108. ["id"],
  1109. "remote_table_2",
  1110. ref_schema=config.test_schema,
  1111. )
  1112. ],
  1113. (schema, "remote_table"): [
  1114. fk(["local_id"], ["id"], "local_table", ref_schema=None)
  1115. ],
  1116. (schema, "remote_table_2"): [],
  1117. (schema, "noncol_idx_test_nopk"): [],
  1118. (schema, "noncol_idx_test_pk"): [],
  1119. (schema, self.temp_table_name()): [],
  1120. }
  1121. if not testing.requires.self_referential_foreign_keys.enabled:
  1122. tables[(schema, "users")].clear()
  1123. if not testing.requires.named_constraints.enabled:
  1124. for vals in tables.values():
  1125. for val in vals:
  1126. if val["name"] is not mock.ANY:
  1127. val["name"] = mock.ANY
  1128. res = self._resolve_kind(kind, tables, views, materialized)
  1129. res = self._resolve_names(schema, scope, filter_names, res)
  1130. return res
  1131. @property
  1132. def _required_fk_keys(self):
  1133. return {
  1134. "name",
  1135. "constrained_columns",
  1136. "referred_schema",
  1137. "referred_table",
  1138. "referred_columns",
  1139. }
  1140. def exp_indexes(
  1141. self,
  1142. schema=None,
  1143. scope=ObjectScope.ANY,
  1144. kind=ObjectKind.ANY,
  1145. filter_names=None,
  1146. ):
  1147. def idx(
  1148. *cols,
  1149. name,
  1150. unique=False,
  1151. column_sorting=None,
  1152. duplicates=False,
  1153. fk=False,
  1154. ):
  1155. fk_req = testing.requires.foreign_keys_reflect_as_index
  1156. dup_req = testing.requires.unique_constraints_reflect_as_index
  1157. sorting_expression = (
  1158. testing.requires.reflect_indexes_with_ascdesc_as_expression
  1159. )
  1160. if (fk and not fk_req.enabled) or (
  1161. duplicates and not dup_req.enabled
  1162. ):
  1163. return ()
  1164. res = {
  1165. "unique": unique,
  1166. "column_names": list(cols),
  1167. "name": name,
  1168. "dialect_options": mock.ANY,
  1169. "include_columns": [],
  1170. }
  1171. if column_sorting:
  1172. res["column_sorting"] = column_sorting
  1173. if sorting_expression.enabled:
  1174. res["expressions"] = orig = res["column_names"]
  1175. res["column_names"] = [
  1176. None if c in column_sorting else c for c in orig
  1177. ]
  1178. if duplicates:
  1179. res["duplicates_constraint"] = name
  1180. return [res]
  1181. materialized = {(schema, "dingalings_v"): []}
  1182. views = {
  1183. (schema, "email_addresses_v"): [],
  1184. (schema, "users_v"): [],
  1185. (schema, "user_tmp_v"): [],
  1186. }
  1187. self._resolve_views(views, materialized)
  1188. if materialized:
  1189. materialized[(schema, "dingalings_v")].extend(
  1190. idx("data", name="mat_index")
  1191. )
  1192. tables = {
  1193. (schema, "users"): [
  1194. *idx("parent_user_id", name="user_id_fk", fk=True),
  1195. *idx("user_id", "test2", "test1", name="users_all_idx"),
  1196. *idx("test1", "test2", name="users_t_idx", unique=True),
  1197. ],
  1198. (schema, "dingalings"): [
  1199. *idx("data", name=mock.ANY, unique=True, duplicates=True),
  1200. *idx("id_user", name=mock.ANY, fk=True),
  1201. *idx(
  1202. "address_id",
  1203. "dingaling_id",
  1204. name="zz_dingalings_multiple",
  1205. unique=True,
  1206. duplicates=True,
  1207. ),
  1208. ],
  1209. (schema, "email_addresses"): [
  1210. *idx("email_address", name=mock.ANY),
  1211. *idx("remote_user_id", name=mock.ANY, fk=True),
  1212. ],
  1213. (schema, "comment_test"): [],
  1214. (schema, "no_constraints"): [],
  1215. (schema, "local_table"): [
  1216. *idx("remote_id", name=mock.ANY, fk=True)
  1217. ],
  1218. (schema, "remote_table"): [
  1219. *idx("local_id", name=mock.ANY, fk=True)
  1220. ],
  1221. (schema, "remote_table_2"): [],
  1222. (schema, "noncol_idx_test_nopk"): [
  1223. *idx(
  1224. "q",
  1225. name="noncol_idx_nopk",
  1226. column_sorting={"q": ("desc",)},
  1227. )
  1228. ],
  1229. (schema, "noncol_idx_test_pk"): [
  1230. *idx(
  1231. "q", name="noncol_idx_pk", column_sorting={"q": ("desc",)}
  1232. )
  1233. ],
  1234. (schema, self.temp_table_name()): [
  1235. *idx("foo", name="user_tmp_ix"),
  1236. *idx(
  1237. "name",
  1238. name=f"user_tmp_uq_{config.ident}",
  1239. duplicates=True,
  1240. unique=True,
  1241. ),
  1242. ],
  1243. }
  1244. if (
  1245. not testing.requires.indexes_with_ascdesc.enabled
  1246. or not testing.requires.reflect_indexes_with_ascdesc.enabled
  1247. ):
  1248. tables[(schema, "noncol_idx_test_nopk")].clear()
  1249. tables[(schema, "noncol_idx_test_pk")].clear()
  1250. res = self._resolve_kind(kind, tables, views, materialized)
  1251. res = self._resolve_names(schema, scope, filter_names, res)
  1252. return res
  1253. @property
  1254. def _required_index_keys(self):
  1255. return {"name", "column_names", "unique"}
  1256. def exp_ucs(
  1257. self,
  1258. schema=None,
  1259. scope=ObjectScope.ANY,
  1260. kind=ObjectKind.ANY,
  1261. filter_names=None,
  1262. all_=False,
  1263. ):
  1264. def uc(
  1265. *cols, name, duplicates_index=None, is_index=False, comment=None
  1266. ):
  1267. req = testing.requires.unique_index_reflect_as_unique_constraints
  1268. if is_index and not req.enabled:
  1269. return ()
  1270. res = {
  1271. "column_names": list(cols),
  1272. "name": name,
  1273. "comment": comment,
  1274. }
  1275. if duplicates_index:
  1276. res["duplicates_index"] = duplicates_index
  1277. return [res]
  1278. materialized = {(schema, "dingalings_v"): []}
  1279. views = {
  1280. (schema, "email_addresses_v"): [],
  1281. (schema, "users_v"): [],
  1282. (schema, "user_tmp_v"): [],
  1283. }
  1284. self._resolve_views(views, materialized)
  1285. tables = {
  1286. (schema, "users"): [
  1287. *uc(
  1288. "test1",
  1289. "test2",
  1290. name="users_t_idx",
  1291. duplicates_index="users_t_idx",
  1292. is_index=True,
  1293. )
  1294. ],
  1295. (schema, "dingalings"): [
  1296. *uc("data", name=mock.ANY, duplicates_index=mock.ANY),
  1297. *uc(
  1298. "address_id",
  1299. "dingaling_id",
  1300. name="zz_dingalings_multiple",
  1301. duplicates_index="zz_dingalings_multiple",
  1302. comment="di unique comment",
  1303. ),
  1304. ],
  1305. (schema, "email_addresses"): [],
  1306. (schema, "comment_test"): [],
  1307. (schema, "no_constraints"): [],
  1308. (schema, "local_table"): [],
  1309. (schema, "remote_table"): [],
  1310. (schema, "remote_table_2"): [],
  1311. (schema, "noncol_idx_test_nopk"): [],
  1312. (schema, "noncol_idx_test_pk"): [],
  1313. (schema, self.temp_table_name()): [
  1314. *uc("name", name=f"user_tmp_uq_{config.ident}")
  1315. ],
  1316. }
  1317. if all_:
  1318. return {**materialized, **views, **tables}
  1319. else:
  1320. res = self._resolve_kind(kind, tables, views, materialized)
  1321. res = self._resolve_names(schema, scope, filter_names, res)
  1322. return res
  1323. @property
  1324. def _required_unique_cst_keys(self):
  1325. return {"name", "column_names"}
  1326. def exp_ccs(
  1327. self,
  1328. schema=None,
  1329. scope=ObjectScope.ANY,
  1330. kind=ObjectKind.ANY,
  1331. filter_names=None,
  1332. ):
  1333. class tt(str):
  1334. def __eq__(self, other):
  1335. res = (
  1336. other.lower()
  1337. .replace("(", "")
  1338. .replace(")", "")
  1339. .replace("`", "")
  1340. )
  1341. return self in res
  1342. def cc(text, name, comment=None):
  1343. return {"sqltext": tt(text), "name": name, "comment": comment}
  1344. # print({1: "test2 > (0)::double precision"} == {1: tt("test2 > 0")})
  1345. # assert 0
  1346. materialized = {(schema, "dingalings_v"): []}
  1347. views = {
  1348. (schema, "email_addresses_v"): [],
  1349. (schema, "users_v"): [],
  1350. (schema, "user_tmp_v"): [],
  1351. }
  1352. self._resolve_views(views, materialized)
  1353. tables = {
  1354. (schema, "users"): [
  1355. cc("test2 <= 1000", mock.ANY),
  1356. cc(
  1357. "test2 > 0",
  1358. "zz_test2_gt_zero",
  1359. comment="users check constraint",
  1360. ),
  1361. ],
  1362. (schema, "dingalings"): [
  1363. cc(
  1364. "address_id > 0 and address_id < 1000",
  1365. name="address_id_gt_zero",
  1366. ),
  1367. ],
  1368. (schema, "email_addresses"): [],
  1369. (schema, "comment_test"): [],
  1370. (schema, "no_constraints"): [],
  1371. (schema, "local_table"): [],
  1372. (schema, "remote_table"): [],
  1373. (schema, "remote_table_2"): [],
  1374. (schema, "noncol_idx_test_nopk"): [],
  1375. (schema, "noncol_idx_test_pk"): [],
  1376. (schema, self.temp_table_name()): [],
  1377. }
  1378. res = self._resolve_kind(kind, tables, views, materialized)
  1379. res = self._resolve_names(schema, scope, filter_names, res)
  1380. return res
  1381. @property
  1382. def _required_cc_keys(self):
  1383. return {"name", "sqltext"}
  1384. @testing.requires.schema_reflection
  1385. def test_get_schema_names(self, connection):
  1386. insp = inspect(connection)
  1387. is_true(testing.config.test_schema in insp.get_schema_names())
  1388. @testing.requires.schema_reflection
  1389. def test_has_schema(self, connection):
  1390. insp = inspect(connection)
  1391. is_true(insp.has_schema(testing.config.test_schema))
  1392. is_false(insp.has_schema("sa_fake_schema_foo"))
  1393. @testing.requires.schema_reflection
  1394. def test_get_schema_names_w_translate_map(self, connection):
  1395. """test #7300"""
  1396. connection = connection.execution_options(
  1397. schema_translate_map={
  1398. "foo": "bar",
  1399. BLANK_SCHEMA: testing.config.test_schema,
  1400. }
  1401. )
  1402. insp = inspect(connection)
  1403. is_true(testing.config.test_schema in insp.get_schema_names())
  1404. @testing.requires.schema_reflection
  1405. def test_has_schema_w_translate_map(self, connection):
  1406. connection = connection.execution_options(
  1407. schema_translate_map={
  1408. "foo": "bar",
  1409. BLANK_SCHEMA: testing.config.test_schema,
  1410. }
  1411. )
  1412. insp = inspect(connection)
  1413. is_true(insp.has_schema(testing.config.test_schema))
  1414. is_false(insp.has_schema("sa_fake_schema_foo"))
  1415. @testing.requires.schema_reflection
  1416. @testing.requires.schema_create_delete
  1417. def test_schema_cache(self, connection):
  1418. insp = inspect(connection)
  1419. is_false("foo_bar" in insp.get_schema_names())
  1420. is_false(insp.has_schema("foo_bar"))
  1421. connection.execute(DDL("CREATE SCHEMA foo_bar"))
  1422. try:
  1423. is_false("foo_bar" in insp.get_schema_names())
  1424. is_false(insp.has_schema("foo_bar"))
  1425. insp.clear_cache()
  1426. is_true("foo_bar" in insp.get_schema_names())
  1427. is_true(insp.has_schema("foo_bar"))
  1428. finally:
  1429. connection.execute(DDL("DROP SCHEMA foo_bar"))
  1430. @testing.requires.schema_reflection
  1431. def test_dialect_initialize(self):
  1432. engine = engines.testing_engine()
  1433. inspect(engine)
  1434. assert hasattr(engine.dialect, "default_schema_name")
  1435. @testing.requires.schema_reflection
  1436. def test_get_default_schema_name(self, connection):
  1437. insp = inspect(connection)
  1438. eq_(insp.default_schema_name, connection.dialect.default_schema_name)
  1439. @testing.combinations(
  1440. None,
  1441. ("foreign_key", testing.requires.foreign_key_constraint_reflection),
  1442. argnames="order_by",
  1443. )
  1444. @testing.combinations(
  1445. (True, testing.requires.schemas), False, argnames="use_schema"
  1446. )
  1447. def test_get_table_names(self, connection, order_by, use_schema):
  1448. if use_schema:
  1449. schema = config.test_schema
  1450. else:
  1451. schema = None
  1452. _ignore_tables = {
  1453. "comment_test",
  1454. "noncol_idx_test_pk",
  1455. "noncol_idx_test_nopk",
  1456. "local_table",
  1457. "remote_table",
  1458. "remote_table_2",
  1459. "no_constraints",
  1460. }
  1461. insp = inspect(connection)
  1462. if order_by:
  1463. tables = [
  1464. rec[0]
  1465. for rec in insp.get_sorted_table_and_fkc_names(schema)
  1466. if rec[0]
  1467. ]
  1468. else:
  1469. tables = insp.get_table_names(schema)
  1470. table_names = [t for t in tables if t not in _ignore_tables]
  1471. if order_by == "foreign_key":
  1472. answer = ["users", "email_addresses", "dingalings"]
  1473. eq_(table_names, answer)
  1474. else:
  1475. answer = ["dingalings", "email_addresses", "users"]
  1476. eq_(sorted(table_names), answer)
  1477. @testing.combinations(
  1478. (True, testing.requires.schemas), False, argnames="use_schema"
  1479. )
  1480. def test_get_view_names(self, connection, use_schema):
  1481. insp = inspect(connection)
  1482. if use_schema:
  1483. schema = config.test_schema
  1484. else:
  1485. schema = None
  1486. table_names = insp.get_view_names(schema)
  1487. if testing.requires.materialized_views.enabled:
  1488. eq_(sorted(table_names), ["email_addresses_v", "users_v"])
  1489. eq_(insp.get_materialized_view_names(schema), ["dingalings_v"])
  1490. else:
  1491. answer = ["dingalings_v", "email_addresses_v", "users_v"]
  1492. eq_(sorted(table_names), answer)
  1493. @testing.requires.temp_table_names
  1494. def test_get_temp_table_names(self, connection):
  1495. insp = inspect(connection)
  1496. temp_table_names = insp.get_temp_table_names()
  1497. eq_(sorted(temp_table_names), [f"user_tmp_{config.ident}"])
  1498. @testing.requires.view_reflection
  1499. @testing.requires.temporary_views
  1500. def test_get_temp_view_names(self, connection):
  1501. insp = inspect(connection)
  1502. temp_table_names = insp.get_temp_view_names()
  1503. eq_(sorted(temp_table_names), ["user_tmp_v"])
  1504. @testing.requires.comment_reflection
  1505. def test_get_comments(self, connection):
  1506. self._test_get_comments(connection)
  1507. @testing.requires.comment_reflection
  1508. @testing.requires.schemas
  1509. def test_get_comments_with_schema(self, connection):
  1510. self._test_get_comments(connection, testing.config.test_schema)
  1511. def _test_get_comments(self, connection, schema=None):
  1512. insp = inspect(connection)
  1513. exp = self.exp_comments(schema=schema)
  1514. eq_(
  1515. insp.get_table_comment("comment_test", schema=schema),
  1516. exp[(schema, "comment_test")],
  1517. )
  1518. eq_(
  1519. insp.get_table_comment("users", schema=schema),
  1520. exp[(schema, "users")],
  1521. )
  1522. eq_(
  1523. insp.get_table_comment("comment_test", schema=schema),
  1524. exp[(schema, "comment_test")],
  1525. )
  1526. no_cst = self.tables.no_constraints.name
  1527. eq_(
  1528. insp.get_table_comment(no_cst, schema=schema),
  1529. exp[(schema, no_cst)],
  1530. )
  1531. @testing.combinations(
  1532. (False, False),
  1533. (False, True, testing.requires.schemas),
  1534. (True, False, testing.requires.view_reflection),
  1535. (
  1536. True,
  1537. True,
  1538. testing.requires.schemas + testing.requires.view_reflection,
  1539. ),
  1540. argnames="use_views,use_schema",
  1541. )
  1542. def test_get_columns(self, connection, use_views, use_schema):
  1543. if use_schema:
  1544. schema = config.test_schema
  1545. else:
  1546. schema = None
  1547. users, addresses = (self.tables.users, self.tables.email_addresses)
  1548. if use_views:
  1549. table_names = ["users_v", "email_addresses_v", "dingalings_v"]
  1550. else:
  1551. table_names = ["users", "email_addresses"]
  1552. insp = inspect(connection)
  1553. for table_name, table in zip(table_names, (users, addresses)):
  1554. schema_name = schema
  1555. cols = insp.get_columns(table_name, schema=schema_name)
  1556. is_true(len(cols) > 0, len(cols))
  1557. # should be in order
  1558. for i, col in enumerate(table.columns):
  1559. eq_(col.name, cols[i]["name"])
  1560. ctype = cols[i]["type"].__class__
  1561. ctype_def = col.type
  1562. if isinstance(ctype_def, sa.types.TypeEngine):
  1563. ctype_def = ctype_def.__class__
  1564. # Oracle returns Date for DateTime.
  1565. if testing.against("oracle") and ctype_def in (
  1566. sql_types.Date,
  1567. sql_types.DateTime,
  1568. ):
  1569. ctype_def = sql_types.Date
  1570. # assert that the desired type and return type share
  1571. # a base within one of the generic types.
  1572. is_true(
  1573. len(
  1574. set(ctype.__mro__)
  1575. .intersection(ctype_def.__mro__)
  1576. .intersection(
  1577. [
  1578. sql_types.Integer,
  1579. sql_types.Numeric,
  1580. sql_types.DateTime,
  1581. sql_types.Date,
  1582. sql_types.Time,
  1583. sql_types.String,
  1584. sql_types._Binary,
  1585. ]
  1586. )
  1587. )
  1588. > 0,
  1589. "%s(%s), %s(%s)"
  1590. % (col.name, col.type, cols[i]["name"], ctype),
  1591. )
  1592. if not col.primary_key:
  1593. assert cols[i]["default"] is None
  1594. # The case of a table with no column
  1595. # is tested below in TableNoColumnsTest
  1596. @testing.requires.temp_table_reflection
  1597. def test_reflect_table_temp_table(self, connection):
  1598. table_name = self.temp_table_name()
  1599. user_tmp = self.tables[table_name]
  1600. reflected_user_tmp = Table(
  1601. table_name, MetaData(), autoload_with=connection
  1602. )
  1603. self.assert_tables_equal(
  1604. user_tmp, reflected_user_tmp, strict_constraints=False
  1605. )
  1606. @testing.requires.temp_table_reflection
  1607. def test_get_temp_table_columns(self, connection):
  1608. table_name = self.temp_table_name()
  1609. user_tmp = self.tables[table_name]
  1610. insp = inspect(connection)
  1611. cols = insp.get_columns(table_name)
  1612. is_true(len(cols) > 0, len(cols))
  1613. for i, col in enumerate(user_tmp.columns):
  1614. eq_(col.name, cols[i]["name"])
  1615. @testing.requires.temp_table_reflection
  1616. @testing.requires.view_column_reflection
  1617. @testing.requires.temporary_views
  1618. def test_get_temp_view_columns(self, connection):
  1619. insp = inspect(connection)
  1620. cols = insp.get_columns("user_tmp_v")
  1621. eq_([col["name"] for col in cols], ["id", "name", "foo"])
  1622. @testing.combinations(
  1623. (False,), (True, testing.requires.schemas), argnames="use_schema"
  1624. )
  1625. @testing.requires.primary_key_constraint_reflection
  1626. def test_get_pk_constraint(self, connection, use_schema):
  1627. if use_schema:
  1628. schema = testing.config.test_schema
  1629. else:
  1630. schema = None
  1631. users, addresses = self.tables.users, self.tables.email_addresses
  1632. insp = inspect(connection)
  1633. exp = self.exp_pks(schema=schema)
  1634. users_cons = insp.get_pk_constraint(users.name, schema=schema)
  1635. self._check_list(
  1636. [users_cons], [exp[(schema, users.name)]], self._required_pk_keys
  1637. )
  1638. addr_cons = insp.get_pk_constraint(addresses.name, schema=schema)
  1639. exp_cols = exp[(schema, addresses.name)]["constrained_columns"]
  1640. eq_(addr_cons["constrained_columns"], exp_cols)
  1641. with testing.requires.reflects_pk_names.fail_if():
  1642. eq_(addr_cons["name"], "email_ad_pk")
  1643. no_cst = self.tables.no_constraints.name
  1644. self._check_list(
  1645. [insp.get_pk_constraint(no_cst, schema=schema)],
  1646. [exp[(schema, no_cst)]],
  1647. self._required_pk_keys,
  1648. )
  1649. @testing.combinations(
  1650. "PK_test_table",
  1651. "pk_test_table",
  1652. "mixedCasePK",
  1653. "pk.with.dots",
  1654. argnames="pk_name",
  1655. )
  1656. @testing.requires.primary_key_constraint_reflection
  1657. @testing.requires.reflects_pk_names
  1658. def test_get_pk_constraint_quoted_name(
  1659. self, connection, metadata, pk_name
  1660. ):
  1661. """Test that primary key constraint names with various casing are
  1662. properly reflected."""
  1663. Table(
  1664. "test_table",
  1665. metadata,
  1666. Column("id", Integer),
  1667. Column("data", String(50)),
  1668. sa.PrimaryKeyConstraint("id", name=pk_name),
  1669. )
  1670. metadata.create_all(connection)
  1671. insp = inspect(connection)
  1672. pk_cons = insp.get_pk_constraint("test_table")
  1673. eq_(pk_cons["name"], pk_name)
  1674. eq_(pk_cons["constrained_columns"], ["id"])
  1675. @testing.combinations(
  1676. (False,), (True, testing.requires.schemas), argnames="use_schema"
  1677. )
  1678. @testing.requires.foreign_key_constraint_reflection
  1679. def test_get_foreign_keys(self, connection, use_schema):
  1680. if use_schema:
  1681. schema = config.test_schema
  1682. else:
  1683. schema = None
  1684. users, addresses = (self.tables.users, self.tables.email_addresses)
  1685. insp = inspect(connection)
  1686. expected_schema = schema
  1687. # users
  1688. if testing.requires.self_referential_foreign_keys.enabled:
  1689. users_fkeys = insp.get_foreign_keys(users.name, schema=schema)
  1690. fkey1 = users_fkeys[0]
  1691. with testing.requires.named_constraints.fail_if():
  1692. eq_(fkey1["name"], "user_id_fk")
  1693. eq_(fkey1["referred_schema"], expected_schema)
  1694. eq_(fkey1["referred_table"], users.name)
  1695. eq_(fkey1["referred_columns"], ["user_id"])
  1696. eq_(fkey1["constrained_columns"], ["parent_user_id"])
  1697. # addresses
  1698. addr_fkeys = insp.get_foreign_keys(addresses.name, schema=schema)
  1699. fkey1 = addr_fkeys[0]
  1700. with testing.requires.implicitly_named_constraints.fail_if():
  1701. is_true(fkey1["name"] is not None)
  1702. eq_(fkey1["referred_schema"], expected_schema)
  1703. eq_(fkey1["referred_table"], users.name)
  1704. eq_(fkey1["referred_columns"], ["user_id"])
  1705. eq_(fkey1["constrained_columns"], ["remote_user_id"])
  1706. no_cst = self.tables.no_constraints.name
  1707. eq_(insp.get_foreign_keys(no_cst, schema=schema), [])
  1708. @testing.combinations(
  1709. "FK_users_id",
  1710. "fk_users_id",
  1711. "mixedCaseName",
  1712. "fk.with.dots",
  1713. argnames="fk_name",
  1714. )
  1715. @testing.requires.foreign_key_constraint_reflection
  1716. def test_get_foreign_keys_quoted_name(self, connection, metadata, fk_name):
  1717. """Test that foreign key constraint names with various casing are
  1718. properly reflected."""
  1719. Table(
  1720. "users_ref",
  1721. metadata,
  1722. Column("user_id", Integer, primary_key=True),
  1723. test_needs_fk=True,
  1724. )
  1725. Table(
  1726. "user_orders",
  1727. metadata,
  1728. Column("order_id", Integer, primary_key=True),
  1729. Column("user_id", Integer),
  1730. sa.ForeignKeyConstraint(
  1731. ["user_id"],
  1732. ["users_ref.user_id"],
  1733. name=fk_name,
  1734. ),
  1735. test_needs_fk=True,
  1736. )
  1737. metadata.create_all(connection)
  1738. insp = inspect(connection)
  1739. fkeys = insp.get_foreign_keys("user_orders")
  1740. eq_(len(fkeys), 1)
  1741. fkey = fkeys[0]
  1742. with testing.requires.named_constraints.fail_if():
  1743. eq_(fkey["name"], fk_name)
  1744. eq_(fkey["referred_table"], "users_ref")
  1745. eq_(fkey["referred_columns"], ["user_id"])
  1746. eq_(fkey["constrained_columns"], ["user_id"])
  1747. @testing.requires.cross_schema_fk_reflection
  1748. @testing.requires.schemas
  1749. def test_get_inter_schema_foreign_keys(self, connection):
  1750. local_table, remote_table, remote_table_2 = self.tables(
  1751. "%s.local_table" % connection.dialect.default_schema_name,
  1752. "%s.remote_table" % testing.config.test_schema,
  1753. "%s.remote_table_2" % testing.config.test_schema,
  1754. )
  1755. insp = inspect(connection)
  1756. local_fkeys = insp.get_foreign_keys(local_table.name)
  1757. eq_(len(local_fkeys), 1)
  1758. fkey1 = local_fkeys[0]
  1759. eq_(fkey1["referred_schema"], testing.config.test_schema)
  1760. eq_(fkey1["referred_table"], remote_table_2.name)
  1761. eq_(fkey1["referred_columns"], ["id"])
  1762. eq_(fkey1["constrained_columns"], ["remote_id"])
  1763. remote_fkeys = insp.get_foreign_keys(
  1764. remote_table.name, schema=testing.config.test_schema
  1765. )
  1766. eq_(len(remote_fkeys), 1)
  1767. fkey2 = remote_fkeys[0]
  1768. is_true(
  1769. fkey2["referred_schema"]
  1770. in (
  1771. None,
  1772. connection.dialect.default_schema_name,
  1773. )
  1774. )
  1775. eq_(fkey2["referred_table"], local_table.name)
  1776. eq_(fkey2["referred_columns"], ["id"])
  1777. eq_(fkey2["constrained_columns"], ["local_id"])
  1778. @testing.combinations(
  1779. (False,), (True, testing.requires.schemas), argnames="use_schema"
  1780. )
  1781. @testing.requires.index_reflection
  1782. def test_get_indexes(self, connection, use_schema):
  1783. if use_schema:
  1784. schema = config.test_schema
  1785. else:
  1786. schema = None
  1787. # The database may decide to create indexes for foreign keys, etc.
  1788. # so there may be more indexes than expected.
  1789. insp = inspect(connection)
  1790. indexes = insp.get_indexes("users", schema=schema)
  1791. exp = self.exp_indexes(schema=schema)
  1792. self._check_list(
  1793. indexes, exp[(schema, "users")], self._required_index_keys
  1794. )
  1795. no_cst = self.tables.no_constraints.name
  1796. self._check_list(
  1797. insp.get_indexes(no_cst, schema=schema),
  1798. exp[(schema, no_cst)],
  1799. self._required_index_keys,
  1800. )
  1801. @testing.combinations(
  1802. ("noncol_idx_test_nopk", "noncol_idx_nopk"),
  1803. ("noncol_idx_test_pk", "noncol_idx_pk"),
  1804. argnames="tname,ixname",
  1805. )
  1806. @testing.requires.index_reflection
  1807. @testing.requires.indexes_with_ascdesc
  1808. @testing.requires.reflect_indexes_with_ascdesc
  1809. def test_get_noncol_index(self, connection, tname, ixname):
  1810. insp = inspect(connection)
  1811. indexes = insp.get_indexes(tname)
  1812. # reflecting an index that has "x DESC" in it as the column.
  1813. # the DB may or may not give us "x", but make sure we get the index
  1814. # back, it has a name, it's connected to the table.
  1815. expected_indexes = self.exp_indexes()[(None, tname)]
  1816. self._check_list(indexes, expected_indexes, self._required_index_keys)
  1817. t = Table(tname, MetaData(), autoload_with=connection)
  1818. eq_(len(t.indexes), 1)
  1819. is_(list(t.indexes)[0].table, t)
  1820. eq_(list(t.indexes)[0].name, ixname)
  1821. @testing.combinations(
  1822. "IX_test_data",
  1823. "ix_test_data",
  1824. "mixedCaseIndex",
  1825. "ix.with.dots",
  1826. argnames="idx_name",
  1827. )
  1828. @testing.requires.index_reflection
  1829. def test_get_indexes_quoted_name(self, connection, metadata, idx_name):
  1830. """Test that index names with various casing are properly reflected."""
  1831. t = Table(
  1832. "test_table",
  1833. metadata,
  1834. Column("id", Integer, primary_key=True),
  1835. Column("data", String(50)),
  1836. )
  1837. Index(idx_name, t.c.data)
  1838. metadata.create_all(connection)
  1839. insp = inspect(connection)
  1840. indexes = insp.get_indexes("test_table")
  1841. index_names = [idx["name"] for idx in indexes]
  1842. assert idx_name in index_names, f"Expected {idx_name} in {index_names}"
  1843. # Find the specific index
  1844. matching_idx = [idx for idx in indexes if idx["name"] == idx_name]
  1845. eq_(len(matching_idx), 1)
  1846. eq_(matching_idx[0]["column_names"], ["data"])
  1847. @testing.requires.temp_table_reflection
  1848. @testing.requires.unique_constraint_reflection
  1849. def test_get_temp_table_unique_constraints(self, connection):
  1850. insp = inspect(connection)
  1851. name = self.temp_table_name()
  1852. reflected = insp.get_unique_constraints(name)
  1853. exp = self.exp_ucs(all_=True)[(None, name)]
  1854. self._check_list(reflected, exp, self._required_index_keys)
  1855. @testing.requires.temp_table_reflect_indexes
  1856. def test_get_temp_table_indexes(self, connection):
  1857. insp = inspect(connection)
  1858. table_name = self.temp_table_name()
  1859. indexes = insp.get_indexes(table_name)
  1860. for ind in indexes:
  1861. ind.pop("dialect_options", None)
  1862. expected = [
  1863. {"unique": False, "column_names": ["foo"], "name": "user_tmp_ix"}
  1864. ]
  1865. if testing.requires.index_reflects_included_columns.enabled:
  1866. expected[0]["include_columns"] = []
  1867. eq_(
  1868. [idx for idx in indexes if idx["name"] == "user_tmp_ix"],
  1869. expected,
  1870. )
  1871. @testing.combinations(
  1872. (True, testing.requires.schemas), (False,), argnames="use_schema"
  1873. )
  1874. @testing.requires.unique_constraint_reflection
  1875. def test_get_unique_constraints(self, metadata, connection, use_schema):
  1876. # SQLite dialect needs to parse the names of the constraints
  1877. # separately from what it gets from PRAGMA index_list(), and
  1878. # then matches them up. so same set of column_names in two
  1879. # constraints will confuse it. Perhaps we should no longer
  1880. # bother with index_list() here since we have the whole
  1881. # CREATE TABLE?
  1882. if use_schema:
  1883. schema = config.test_schema
  1884. else:
  1885. schema = None
  1886. uniques = sorted(
  1887. [
  1888. {"name": "unique_a", "column_names": ["a"]},
  1889. {"name": "unique_a_b_c", "column_names": ["a", "b", "c"]},
  1890. {"name": "unique_c_a_b", "column_names": ["c", "a", "b"]},
  1891. {"name": "unique_asc_key", "column_names": ["asc", "key"]},
  1892. {"name": "i.have.dots", "column_names": ["b"]},
  1893. {"name": "i have spaces", "column_names": ["c"]},
  1894. ],
  1895. key=operator.itemgetter("name"),
  1896. )
  1897. table = Table(
  1898. "testtbl",
  1899. metadata,
  1900. Column("a", sa.String(20)),
  1901. Column("b", sa.String(30)),
  1902. Column("c", sa.Integer),
  1903. # reserved identifiers
  1904. Column("asc", sa.String(30)),
  1905. Column("key", sa.String(30)),
  1906. schema=schema,
  1907. )
  1908. for uc in uniques:
  1909. table.append_constraint(
  1910. sa.UniqueConstraint(*uc["column_names"], name=uc["name"])
  1911. )
  1912. table.create(connection)
  1913. insp = inspect(connection)
  1914. reflected = sorted(
  1915. insp.get_unique_constraints("testtbl", schema=schema),
  1916. key=operator.itemgetter("name"),
  1917. )
  1918. names_that_duplicate_index = set()
  1919. eq_(len(uniques), len(reflected))
  1920. for orig, refl in zip(uniques, reflected):
  1921. # Different dialects handle duplicate index and constraints
  1922. # differently, so ignore this flag
  1923. dupe = refl.pop("duplicates_index", None)
  1924. if dupe:
  1925. names_that_duplicate_index.add(dupe)
  1926. eq_(refl.pop("comment", None), None)
  1927. # ignore dialect_options
  1928. refl.pop("dialect_options", None)
  1929. eq_(orig, refl)
  1930. reflected_metadata = MetaData()
  1931. reflected = Table(
  1932. "testtbl",
  1933. reflected_metadata,
  1934. autoload_with=connection,
  1935. schema=schema,
  1936. )
  1937. # test "deduplicates for index" logic. MySQL and Oracle
  1938. # "unique constraints" are actually unique indexes (with possible
  1939. # exception of a unique that is a dupe of another one in the case
  1940. # of Oracle). make sure # they aren't duplicated.
  1941. idx_names = {idx.name for idx in reflected.indexes}
  1942. uq_names = {
  1943. uq.name
  1944. for uq in reflected.constraints
  1945. if isinstance(uq, sa.UniqueConstraint)
  1946. }.difference(["unique_c_a_b"])
  1947. assert not idx_names.intersection(uq_names)
  1948. if names_that_duplicate_index:
  1949. eq_(names_that_duplicate_index, idx_names)
  1950. eq_(uq_names, set())
  1951. no_cst = self.tables.no_constraints.name
  1952. eq_(insp.get_unique_constraints(no_cst, schema=schema), [])
  1953. @testing.combinations(
  1954. "UQ_email",
  1955. "uq_email",
  1956. "mixedCaseUQ",
  1957. "uq.with.dots",
  1958. argnames="uq_name",
  1959. )
  1960. @testing.requires.unique_constraint_reflection
  1961. def test_get_unique_constraints_quoted_name(
  1962. self, connection, metadata, uq_name
  1963. ):
  1964. """Test that unique constraint names with various casing are
  1965. properly reflected."""
  1966. Table(
  1967. "test_table",
  1968. metadata,
  1969. Column("id", Integer, primary_key=True),
  1970. Column("email", String(50)),
  1971. sa.UniqueConstraint("email", name=uq_name),
  1972. )
  1973. metadata.create_all(connection)
  1974. insp = inspect(connection)
  1975. uq_cons = insp.get_unique_constraints("test_table")
  1976. eq_(len(uq_cons), 1)
  1977. eq_(uq_cons[0]["name"], uq_name)
  1978. eq_(uq_cons[0]["column_names"], ["email"])
  1979. @testing.requires.view_reflection
  1980. @testing.combinations(
  1981. (False,), (True, testing.requires.schemas), argnames="use_schema"
  1982. )
  1983. def test_get_view_definition(self, connection, use_schema):
  1984. if use_schema:
  1985. schema = config.test_schema
  1986. else:
  1987. schema = None
  1988. insp = inspect(connection)
  1989. for view in ["users_v", "email_addresses_v", "dingalings_v"]:
  1990. v = insp.get_view_definition(view, schema=schema)
  1991. is_true(bool(v))
  1992. @testing.requires.view_reflection
  1993. def test_get_view_definition_does_not_exist(self, connection):
  1994. insp = inspect(connection)
  1995. with expect_raises(NoSuchTableError):
  1996. insp.get_view_definition("view_does_not_exist")
  1997. with expect_raises(NoSuchTableError):
  1998. insp.get_view_definition("users") # a table
  1999. @testing.requires.table_reflection
  2000. def test_autoincrement_col(self, connection):
  2001. """test that 'autoincrement' is reflected according to sqla's policy.
  2002. Don't mark this test as unsupported for any backend !
  2003. (technically it fails with MySQL InnoDB since "id" comes before "id2")
  2004. A backend is better off not returning "autoincrement" at all,
  2005. instead of potentially returning "False" for an auto-incrementing
  2006. primary key column.
  2007. """
  2008. insp = inspect(connection)
  2009. for tname, cname in [
  2010. ("users", "user_id"),
  2011. ("email_addresses", "address_id"),
  2012. ("dingalings", "dingaling_id"),
  2013. ]:
  2014. cols = insp.get_columns(tname)
  2015. id_ = {c["name"]: c for c in cols}[cname]
  2016. assert id_.get("autoincrement", True)
  2017. @testing.combinations(
  2018. (True, testing.requires.schemas), (False,), argnames="use_schema"
  2019. )
  2020. def test_get_table_options(self, use_schema):
  2021. insp = inspect(config.db)
  2022. schema = config.test_schema if use_schema else None
  2023. if testing.requires.reflect_table_options.enabled:
  2024. res = insp.get_table_options("users", schema=schema)
  2025. is_true(isinstance(res, dict))
  2026. # NOTE: can't really create a table with no option
  2027. res = insp.get_table_options("no_constraints", schema=schema)
  2028. is_true(isinstance(res, dict))
  2029. else:
  2030. with expect_raises(NotImplementedError):
  2031. insp.get_table_options("users", schema=schema)
  2032. @testing.combinations((True, testing.requires.schemas), False)
  2033. def test_multi_get_table_options(self, use_schema):
  2034. insp = inspect(config.db)
  2035. if testing.requires.reflect_table_options.enabled:
  2036. schema = config.test_schema if use_schema else None
  2037. res = insp.get_multi_table_options(schema=schema)
  2038. exp = {
  2039. (schema, table): insp.get_table_options(table, schema=schema)
  2040. for table in insp.get_table_names(schema=schema)
  2041. }
  2042. eq_(res, exp)
  2043. else:
  2044. with expect_raises(NotImplementedError):
  2045. insp.get_multi_table_options()
  2046. @testing.fixture
  2047. def get_multi_exp(self, connection):
  2048. def provide_fixture(
  2049. schema, scope, kind, use_filter, single_reflect_fn, exp_method
  2050. ):
  2051. insp = inspect(connection)
  2052. # call the reflection function at least once to avoid
  2053. # "Unexpected success" errors if the result is actually empty
  2054. # and NotImplementedError is not raised
  2055. single_reflect_fn(insp, "email_addresses")
  2056. kw = {"scope": scope, "kind": kind}
  2057. if schema:
  2058. schema = schema()
  2059. filter_names = []
  2060. if ObjectKind.TABLE in kind:
  2061. filter_names.extend(
  2062. ["comment_test", "users", "does-not-exist"]
  2063. )
  2064. if ObjectKind.VIEW in kind:
  2065. filter_names.extend(["email_addresses_v", "does-not-exist"])
  2066. if ObjectKind.MATERIALIZED_VIEW in kind:
  2067. filter_names.extend(["dingalings_v", "does-not-exist"])
  2068. if schema:
  2069. kw["schema"] = schema
  2070. if use_filter:
  2071. kw["filter_names"] = filter_names
  2072. exp = exp_method(
  2073. schema=schema,
  2074. scope=scope,
  2075. kind=kind,
  2076. filter_names=kw.get("filter_names"),
  2077. )
  2078. kws = [kw]
  2079. if scope == ObjectScope.DEFAULT:
  2080. nkw = kw.copy()
  2081. nkw.pop("scope")
  2082. kws.append(nkw)
  2083. if kind == ObjectKind.TABLE:
  2084. nkw = kw.copy()
  2085. nkw.pop("kind")
  2086. kws.append(nkw)
  2087. return inspect(connection), kws, exp
  2088. return provide_fixture
  2089. @testing.requires.reflect_table_options
  2090. @_multi_combination
  2091. def test_multi_get_table_options_tables(
  2092. self, get_multi_exp, schema, scope, kind, use_filter
  2093. ):
  2094. insp, kws, exp = get_multi_exp(
  2095. schema,
  2096. scope,
  2097. kind,
  2098. use_filter,
  2099. Inspector.get_table_options,
  2100. self.exp_options,
  2101. )
  2102. for kw in kws:
  2103. insp.clear_cache()
  2104. result = insp.get_multi_table_options(**kw)
  2105. eq_(result, exp)
  2106. @testing.requires.comment_reflection
  2107. @_multi_combination
  2108. def test_get_multi_table_comment(
  2109. self, get_multi_exp, schema, scope, kind, use_filter
  2110. ):
  2111. insp, kws, exp = get_multi_exp(
  2112. schema,
  2113. scope,
  2114. kind,
  2115. use_filter,
  2116. Inspector.get_table_comment,
  2117. self.exp_comments,
  2118. )
  2119. for kw in kws:
  2120. insp.clear_cache()
  2121. eq_(insp.get_multi_table_comment(**kw), exp)
  2122. def _check_expressions(self, result, exp, err_msg):
  2123. def _clean(text: str):
  2124. return re.sub(r"['\" ]", "", text).lower()
  2125. if isinstance(exp, dict):
  2126. eq_({_clean(e): v for e, v in result.items()}, exp, err_msg)
  2127. else:
  2128. eq_([_clean(e) for e in result], exp, err_msg)
  2129. def _check_list(self, result, exp, req_keys=None, msg=None):
  2130. if req_keys is None:
  2131. eq_(result, exp, msg)
  2132. else:
  2133. eq_(len(result), len(exp), msg)
  2134. for r, e in zip(result, exp):
  2135. for k in set(r) | set(e):
  2136. if k in req_keys or (k in r and k in e):
  2137. err_msg = f"{msg} - {k} - {r}"
  2138. if k in ("expressions", "column_sorting"):
  2139. self._check_expressions(r[k], e[k], err_msg)
  2140. else:
  2141. eq_(r[k], e[k], err_msg)
  2142. def _check_table_dict(self, result, exp, req_keys=None, make_lists=False):
  2143. eq_(set(result.keys()), set(exp.keys()))
  2144. for k in result:
  2145. r, e = result[k], exp[k]
  2146. if make_lists:
  2147. r, e = [r], [e]
  2148. self._check_list(r, e, req_keys, k)
  2149. @_multi_combination
  2150. def test_get_multi_columns(
  2151. self, get_multi_exp, schema, scope, kind, use_filter
  2152. ):
  2153. insp, kws, exp = get_multi_exp(
  2154. schema,
  2155. scope,
  2156. kind,
  2157. use_filter,
  2158. Inspector.get_columns,
  2159. self.exp_columns,
  2160. )
  2161. for kw in kws:
  2162. insp.clear_cache()
  2163. result = insp.get_multi_columns(**kw)
  2164. self._check_table_dict(result, exp, self._required_column_keys)
  2165. @testing.requires.primary_key_constraint_reflection
  2166. @_multi_combination
  2167. def test_get_multi_pk_constraint(
  2168. self, get_multi_exp, schema, scope, kind, use_filter
  2169. ):
  2170. insp, kws, exp = get_multi_exp(
  2171. schema,
  2172. scope,
  2173. kind,
  2174. use_filter,
  2175. Inspector.get_pk_constraint,
  2176. self.exp_pks,
  2177. )
  2178. for kw in kws:
  2179. insp.clear_cache()
  2180. result = insp.get_multi_pk_constraint(**kw)
  2181. self._check_table_dict(
  2182. result, exp, self._required_pk_keys, make_lists=True
  2183. )
  2184. def _adjust_sort(self, result, expected, key):
  2185. if not testing.requires.implicitly_named_constraints.enabled:
  2186. for obj in [result, expected]:
  2187. for val in obj.values():
  2188. if len(val) > 1 and any(
  2189. v.get("name") in (None, mock.ANY) for v in val
  2190. ):
  2191. val.sort(key=key)
  2192. @testing.requires.foreign_key_constraint_reflection
  2193. @_multi_combination
  2194. def test_get_multi_foreign_keys(
  2195. self, get_multi_exp, schema, scope, kind, use_filter
  2196. ):
  2197. insp, kws, exp = get_multi_exp(
  2198. schema,
  2199. scope,
  2200. kind,
  2201. use_filter,
  2202. Inspector.get_foreign_keys,
  2203. self.exp_fks,
  2204. )
  2205. for kw in kws:
  2206. insp.clear_cache()
  2207. result = insp.get_multi_foreign_keys(**kw)
  2208. self._adjust_sort(
  2209. result, exp, lambda d: tuple(d["constrained_columns"])
  2210. )
  2211. self._check_table_dict(result, exp, self._required_fk_keys)
  2212. @testing.requires.index_reflection
  2213. @_multi_combination
  2214. def test_get_multi_indexes(
  2215. self, get_multi_exp, schema, scope, kind, use_filter
  2216. ):
  2217. insp, kws, exp = get_multi_exp(
  2218. schema,
  2219. scope,
  2220. kind,
  2221. use_filter,
  2222. Inspector.get_indexes,
  2223. self.exp_indexes,
  2224. )
  2225. for kw in kws:
  2226. insp.clear_cache()
  2227. result = insp.get_multi_indexes(**kw)
  2228. self._check_table_dict(result, exp, self._required_index_keys)
  2229. @testing.requires.unique_constraint_reflection
  2230. @_multi_combination
  2231. def test_get_multi_unique_constraints(
  2232. self, get_multi_exp, schema, scope, kind, use_filter
  2233. ):
  2234. insp, kws, exp = get_multi_exp(
  2235. schema,
  2236. scope,
  2237. kind,
  2238. use_filter,
  2239. Inspector.get_unique_constraints,
  2240. self.exp_ucs,
  2241. )
  2242. for kw in kws:
  2243. insp.clear_cache()
  2244. result = insp.get_multi_unique_constraints(**kw)
  2245. self._adjust_sort(result, exp, lambda d: tuple(d["column_names"]))
  2246. self._check_table_dict(result, exp, self._required_unique_cst_keys)
  2247. @testing.requires.check_constraint_reflection
  2248. @_multi_combination
  2249. def test_get_multi_check_constraints(
  2250. self, get_multi_exp, schema, scope, kind, use_filter
  2251. ):
  2252. insp, kws, exp = get_multi_exp(
  2253. schema,
  2254. scope,
  2255. kind,
  2256. use_filter,
  2257. Inspector.get_check_constraints,
  2258. self.exp_ccs,
  2259. )
  2260. for kw in kws:
  2261. insp.clear_cache()
  2262. result = insp.get_multi_check_constraints(**kw)
  2263. self._adjust_sort(result, exp, lambda d: tuple(d["sqltext"]))
  2264. self._check_table_dict(result, exp, self._required_cc_keys)
  2265. @testing.combinations(
  2266. ("get_table_options", testing.requires.reflect_table_options),
  2267. "get_columns",
  2268. (
  2269. "get_pk_constraint",
  2270. testing.requires.primary_key_constraint_reflection,
  2271. ),
  2272. (
  2273. "get_foreign_keys",
  2274. testing.requires.foreign_key_constraint_reflection,
  2275. ),
  2276. ("get_indexes", testing.requires.index_reflection),
  2277. (
  2278. "get_unique_constraints",
  2279. testing.requires.unique_constraint_reflection,
  2280. ),
  2281. (
  2282. "get_check_constraints",
  2283. testing.requires.check_constraint_reflection,
  2284. ),
  2285. ("get_table_comment", testing.requires.comment_reflection),
  2286. argnames="method",
  2287. )
  2288. def test_not_existing_table(self, method, connection):
  2289. insp = inspect(connection)
  2290. meth = getattr(insp, method)
  2291. with expect_raises(NoSuchTableError):
  2292. meth("table_does_not_exists")
  2293. def test_unreflectable(self, connection):
  2294. mc = Inspector.get_multi_columns
  2295. def patched(*a, **k):
  2296. ur = k.setdefault("unreflectable", {})
  2297. ur[(None, "some_table")] = UnreflectableTableError("err")
  2298. return mc(*a, **k)
  2299. with mock.patch.object(Inspector, "get_multi_columns", patched):
  2300. with expect_raises_message(UnreflectableTableError, "err"):
  2301. inspect(connection).reflect_table(
  2302. Table("some_table", MetaData()), None
  2303. )
  2304. @testing.combinations(True, False, argnames="use_schema")
  2305. @testing.combinations(
  2306. (True, testing.requires.views), False, argnames="views"
  2307. )
  2308. def test_metadata(self, connection, use_schema, views):
  2309. m = MetaData()
  2310. schema = config.test_schema if use_schema else None
  2311. m.reflect(connection, schema=schema, views=views, resolve_fks=False)
  2312. insp = inspect(connection)
  2313. tables = insp.get_table_names(schema)
  2314. if views:
  2315. tables += insp.get_view_names(schema)
  2316. try:
  2317. tables += insp.get_materialized_view_names(schema)
  2318. except NotImplementedError:
  2319. pass
  2320. if schema:
  2321. tables = [f"{schema}.{t}" for t in tables]
  2322. eq_(sorted(m.tables), sorted(tables))
  2323. @testing.requires.comment_reflection
  2324. def test_comments_unicode(self, connection, metadata):
  2325. Table(
  2326. "unicode_comments",
  2327. metadata,
  2328. Column("unicode", Integer, comment="é試蛇ẟΩ"),
  2329. Column("emoji", Integer, comment="☁️✨"),
  2330. comment="試蛇ẟΩ✨",
  2331. )
  2332. metadata.create_all(connection)
  2333. insp = inspect(connection)
  2334. tc = insp.get_table_comment("unicode_comments")
  2335. eq_(tc, {"text": "試蛇ẟΩ✨"})
  2336. cols = insp.get_columns("unicode_comments")
  2337. value = {c["name"]: c["comment"] for c in cols}
  2338. exp = {"unicode": "é試蛇ẟΩ", "emoji": "☁️✨"}
  2339. eq_(value, exp)
  2340. @testing.requires.comment_reflection_full_unicode
  2341. def test_comments_unicode_full(self, connection, metadata):
  2342. Table(
  2343. "unicode_comments",
  2344. metadata,
  2345. Column("emoji", Integer, comment="🐍🧙🝝🧙‍♂️🧙‍♀️"),
  2346. comment="🎩🁰🝑🤷‍♀️🤷‍♂️",
  2347. )
  2348. metadata.create_all(connection)
  2349. insp = inspect(connection)
  2350. tc = insp.get_table_comment("unicode_comments")
  2351. eq_(tc, {"text": "🎩🁰🝑🤷‍♀️🤷‍♂️"})
  2352. c = insp.get_columns("unicode_comments")[0]
  2353. eq_({c["name"]: c["comment"]}, {"emoji": "🐍🧙🝝🧙‍♂️🧙‍♀️"})
  2354. @testing.requires.column_collation_reflection
  2355. @testing.requires.order_by_collation
  2356. def test_column_collation_reflection(self, connection, metadata):
  2357. collation = testing.requires.get_order_by_collation(config)
  2358. Table(
  2359. "t",
  2360. metadata,
  2361. Column("collated", sa.String(collation=collation)),
  2362. Column("not_collated", sa.String()),
  2363. )
  2364. metadata.create_all(connection)
  2365. m2 = MetaData()
  2366. t2 = Table("t", m2, autoload_with=connection)
  2367. eq_(t2.c.collated.type.collation, collation)
  2368. is_none(t2.c.not_collated.type.collation)
  2369. insp = inspect(connection)
  2370. collated, not_collated = insp.get_columns("t")
  2371. eq_(collated["type"].collation, collation)
  2372. is_none(not_collated["type"].collation)
  2373. class TableNoColumnsTest(fixtures.TestBase):
  2374. __requires__ = ("reflect_tables_no_columns",)
  2375. __sparse_driver_backend__ = True
  2376. @testing.fixture
  2377. def table_no_columns(self, connection, metadata):
  2378. Table("empty", metadata)
  2379. metadata.create_all(connection)
  2380. @testing.fixture
  2381. def view_no_columns(self, connection, metadata):
  2382. Table("empty", metadata)
  2383. event.listen(
  2384. metadata,
  2385. "after_create",
  2386. DDL("CREATE VIEW empty_v AS SELECT * FROM empty"),
  2387. )
  2388. # for transactional DDL the transaction is rolled back before this
  2389. # drop statement is invoked
  2390. event.listen(
  2391. metadata, "before_drop", DDL("DROP VIEW IF EXISTS empty_v")
  2392. )
  2393. metadata.create_all(connection)
  2394. def test_reflect_table_no_columns(self, connection, table_no_columns):
  2395. t2 = Table("empty", MetaData(), autoload_with=connection)
  2396. eq_(list(t2.c), [])
  2397. def test_get_columns_table_no_columns(self, connection, table_no_columns):
  2398. insp = inspect(connection)
  2399. eq_(insp.get_columns("empty"), [])
  2400. multi = insp.get_multi_columns()
  2401. eq_(multi, {(None, "empty"): []})
  2402. def test_reflect_incl_table_no_columns(self, connection, table_no_columns):
  2403. m = MetaData()
  2404. m.reflect(connection)
  2405. assert set(m.tables).intersection(["empty"])
  2406. @testing.requires.views
  2407. def test_reflect_view_no_columns(self, connection, view_no_columns):
  2408. t2 = Table("empty_v", MetaData(), autoload_with=connection)
  2409. eq_(list(t2.c), [])
  2410. @testing.requires.views
  2411. def test_get_columns_view_no_columns(self, connection, view_no_columns):
  2412. insp = inspect(connection)
  2413. eq_(insp.get_columns("empty_v"), [])
  2414. multi = insp.get_multi_columns(kind=ObjectKind.VIEW)
  2415. eq_(multi, {(None, "empty_v"): []})
  2416. class ComponentReflectionTestExtra(ComparesIndexes, fixtures.TestBase):
  2417. __sparse_driver_backend__ = True
  2418. @testing.fixture(params=[True, False])
  2419. def use_schema_fixture(self, request):
  2420. if request.param:
  2421. return config.test_schema
  2422. else:
  2423. return None
  2424. @testing.fixture()
  2425. def inspect_for_table(self, metadata, connection, use_schema_fixture):
  2426. @contextlib.contextmanager
  2427. def go(tablename):
  2428. yield use_schema_fixture, inspect(connection)
  2429. metadata.create_all(connection)
  2430. return go
  2431. def ck_eq(self, reflected, expected):
  2432. # trying to minimize effect of quoting, parenthesis, etc.
  2433. # may need to add more to this as new dialects get CHECK
  2434. # constraint reflection support
  2435. def normalize(sqltext):
  2436. return " ".join(
  2437. re.findall(r"and|\d|=|a|b|c|or|<|>", sqltext.lower(), re.I)
  2438. )
  2439. reflected = sorted(
  2440. [
  2441. {"name": item["name"], "sqltext": normalize(item["sqltext"])}
  2442. for item in reflected
  2443. ],
  2444. key=lambda item: (item["sqltext"]),
  2445. )
  2446. expected = sorted(
  2447. expected,
  2448. key=lambda item: (item["sqltext"]),
  2449. )
  2450. eq_(reflected, expected)
  2451. @testing.requires.check_constraint_reflection
  2452. def test_check_constraint_no_constraint(self, metadata, inspect_for_table):
  2453. with inspect_for_table("no_constraints") as (schema, inspector):
  2454. Table(
  2455. "no_constraints",
  2456. metadata,
  2457. Column("data", sa.String(20)),
  2458. schema=schema,
  2459. )
  2460. self.ck_eq(
  2461. inspector.get_check_constraints("no_constraints", schema=schema),
  2462. [],
  2463. )
  2464. @testing.requires.inline_check_constraint_reflection
  2465. @testing.combinations(
  2466. "my_inline", "MyInline", None, argnames="constraint_name"
  2467. )
  2468. def test_check_constraint_inline(
  2469. self, metadata, inspect_for_table, constraint_name
  2470. ):
  2471. with inspect_for_table("sa_cc") as (schema, inspector):
  2472. Table(
  2473. "sa_cc",
  2474. metadata,
  2475. Column("id", Integer(), primary_key=True),
  2476. Column(
  2477. "a",
  2478. Integer(),
  2479. sa.CheckConstraint(
  2480. "a > 1 AND a < 5", name=constraint_name
  2481. ),
  2482. ),
  2483. Column("data", String(50)),
  2484. schema=schema,
  2485. )
  2486. reflected = inspector.get_check_constraints("sa_cc", schema=schema)
  2487. self.ck_eq(
  2488. reflected,
  2489. [
  2490. {
  2491. "name": constraint_name or mock.ANY,
  2492. "sqltext": "a > 1 and a < 5",
  2493. },
  2494. ],
  2495. )
  2496. @testing.requires.check_constraint_reflection
  2497. @testing.combinations(
  2498. "my_ck_const", "MyCkConst", None, argnames="constraint_name"
  2499. )
  2500. def test_check_constraint_standalone(
  2501. self, metadata, inspect_for_table, constraint_name
  2502. ):
  2503. with inspect_for_table("sa_cc") as (schema, inspector):
  2504. Table(
  2505. "sa_cc",
  2506. metadata,
  2507. Column("a", Integer()),
  2508. sa.CheckConstraint(
  2509. "a = 1 OR (a > 2 AND a < 5)", name=constraint_name
  2510. ),
  2511. schema=schema,
  2512. )
  2513. reflected = inspector.get_check_constraints("sa_cc", schema=schema)
  2514. self.ck_eq(
  2515. reflected,
  2516. [
  2517. {
  2518. "name": constraint_name or mock.ANY,
  2519. "sqltext": "a = 1 or a > 2 and a < 5",
  2520. },
  2521. ],
  2522. )
  2523. @testing.requires.inline_check_constraint_reflection
  2524. def test_check_constraint_mixed(self, metadata, inspect_for_table):
  2525. with inspect_for_table("sa_cc") as (schema, inspector):
  2526. Table(
  2527. "sa_cc",
  2528. metadata,
  2529. Column("id", Integer(), primary_key=True),
  2530. Column("a", Integer(), sa.CheckConstraint("a > 1 AND a < 5")),
  2531. Column(
  2532. "b",
  2533. Integer(),
  2534. sa.CheckConstraint("b > 1 AND b < 5", name="my_inline"),
  2535. ),
  2536. Column("c", Integer()),
  2537. Column("data", String(50)),
  2538. sa.UniqueConstraint("data", name="some_uq"),
  2539. sa.CheckConstraint("c > 1 AND c < 5", name="cc1"),
  2540. sa.UniqueConstraint("c", name="some_c_uq"),
  2541. schema=schema,
  2542. )
  2543. reflected = inspector.get_check_constraints("sa_cc", schema=schema)
  2544. self.ck_eq(
  2545. reflected,
  2546. [
  2547. {"name": "cc1", "sqltext": "c > 1 and c < 5"},
  2548. {"name": "my_inline", "sqltext": "b > 1 and b < 5"},
  2549. {"name": mock.ANY, "sqltext": "a > 1 and a < 5"},
  2550. ],
  2551. )
  2552. @testing.requires.indexes_check_column_order
  2553. def test_index_column_order(self, metadata, inspect_for_table):
  2554. """test for #12894"""
  2555. with inspect_for_table("sa_multi_index") as (schema, inspector):
  2556. test_table = Table(
  2557. "sa_multi_index",
  2558. metadata,
  2559. Column("Column1", Integer, primary_key=True),
  2560. Column("Column2", Integer),
  2561. Column("Column3", Integer),
  2562. )
  2563. Index(
  2564. "Index_Example",
  2565. test_table.c.Column3,
  2566. test_table.c.Column1,
  2567. test_table.c.Column2,
  2568. )
  2569. indexes = inspector.get_indexes("sa_multi_index")
  2570. eq_(indexes[0]["column_names"], ["Column3", "Column1", "Column2"])
  2571. @testing.requires.indexes_with_expressions
  2572. def test_reflect_expression_based_indexes(self, metadata, connection):
  2573. t = Table(
  2574. "t",
  2575. metadata,
  2576. Column("x", String(30)),
  2577. Column("y", String(30)),
  2578. Column("z", String(30)),
  2579. )
  2580. Index("t_idx", func.lower(t.c.x), t.c.z, func.lower(t.c.y))
  2581. long_str = "long string " * 100
  2582. Index("t_idx_long", func.coalesce(t.c.x, long_str))
  2583. Index("t_idx_2", t.c.x)
  2584. metadata.create_all(connection)
  2585. insp = inspect(connection)
  2586. expected = [
  2587. {
  2588. "name": "t_idx_2",
  2589. "column_names": ["x"],
  2590. "unique": False,
  2591. "dialect_options": {},
  2592. }
  2593. ]
  2594. def completeIndex(entry):
  2595. if testing.requires.index_reflects_included_columns.enabled:
  2596. entry["include_columns"] = []
  2597. entry["dialect_options"] = {
  2598. f"{connection.engine.name}_include": []
  2599. }
  2600. else:
  2601. entry.setdefault("dialect_options", {})
  2602. completeIndex(expected[0])
  2603. class lower_index_str(str):
  2604. def __eq__(self, other):
  2605. ol = other.lower()
  2606. # test that lower and x or y are in the string
  2607. return "lower" in ol and ("x" in ol or "y" in ol)
  2608. class coalesce_index_str(str):
  2609. def __eq__(self, other):
  2610. # test that coalesce and the string is in other
  2611. return "coalesce" in other.lower() and long_str in other
  2612. if testing.requires.reflect_indexes_with_expressions.enabled:
  2613. expr_index = {
  2614. "name": "t_idx",
  2615. "column_names": [None, "z", None],
  2616. "expressions": [
  2617. lower_index_str("lower(x)"),
  2618. "z",
  2619. lower_index_str("lower(y)"),
  2620. ],
  2621. "unique": False,
  2622. }
  2623. completeIndex(expr_index)
  2624. expected.insert(0, expr_index)
  2625. expr_index_long = {
  2626. "name": "t_idx_long",
  2627. "column_names": [None],
  2628. "expressions": [
  2629. coalesce_index_str(f"coalesce(x, '{long_str}')")
  2630. ],
  2631. "unique": False,
  2632. }
  2633. completeIndex(expr_index_long)
  2634. expected.append(expr_index_long)
  2635. eq_(insp.get_indexes("t"), expected)
  2636. m2 = MetaData()
  2637. t2 = Table("t", m2, autoload_with=connection)
  2638. else:
  2639. with expect_warnings(
  2640. "Skipped unsupported reflection of expression-based "
  2641. "index t_idx"
  2642. ):
  2643. eq_(insp.get_indexes("t"), expected)
  2644. m2 = MetaData()
  2645. t2 = Table("t", m2, autoload_with=connection)
  2646. self.compare_table_index_with_expected(
  2647. t2, expected, connection.engine.name
  2648. )
  2649. @testing.requires.index_reflects_included_columns
  2650. def test_reflect_covering_index(self, metadata, connection):
  2651. t = Table(
  2652. "t",
  2653. metadata,
  2654. Column("x", String(30)),
  2655. Column("y", String(30)),
  2656. )
  2657. idx = Index("t_idx", t.c.x)
  2658. idx.dialect_options[connection.engine.name]["include"] = ["y"]
  2659. metadata.create_all(connection)
  2660. insp = inspect(connection)
  2661. get_indexes = insp.get_indexes("t")
  2662. eq_(
  2663. get_indexes,
  2664. [
  2665. {
  2666. "name": "t_idx",
  2667. "column_names": ["x"],
  2668. "include_columns": ["y"],
  2669. "unique": False,
  2670. "dialect_options": mock.ANY,
  2671. }
  2672. ],
  2673. )
  2674. eq_(
  2675. get_indexes[0]["dialect_options"][
  2676. "%s_include" % connection.engine.name
  2677. ],
  2678. ["y"],
  2679. )
  2680. t2 = Table("t", MetaData(), autoload_with=connection)
  2681. eq_(
  2682. list(t2.indexes)[0].dialect_options[connection.engine.name][
  2683. "include"
  2684. ],
  2685. ["y"],
  2686. )
  2687. def _type_round_trip(self, connection, metadata, *types):
  2688. t = Table(
  2689. "t",
  2690. metadata,
  2691. *[Column("t%d" % i, type_) for i, type_ in enumerate(types)],
  2692. )
  2693. t.create(connection)
  2694. return [c["type"] for c in inspect(connection).get_columns("t")]
  2695. @testing.requires.table_reflection
  2696. def test_numeric_reflection(self, connection, metadata):
  2697. for typ in self._type_round_trip(
  2698. connection, metadata, sql_types.Numeric(18, 5)
  2699. ):
  2700. assert isinstance(typ, sql_types.Numeric)
  2701. eq_(typ.precision, 18)
  2702. eq_(typ.scale, 5)
  2703. @testing.requires.table_reflection
  2704. @testing.combinations(
  2705. sql_types.String,
  2706. sql_types.VARCHAR,
  2707. sql_types.CHAR,
  2708. (sql_types.NVARCHAR, testing.requires.nvarchar_types),
  2709. (sql_types.NCHAR, testing.requires.nvarchar_types),
  2710. argnames="type_",
  2711. )
  2712. def test_string_length_reflection(self, connection, metadata, type_):
  2713. typ = self._type_round_trip(connection, metadata, type_(52))[0]
  2714. if issubclass(type_, sql_types.VARCHAR):
  2715. assert isinstance(typ, sql_types.VARCHAR)
  2716. elif issubclass(type_, sql_types.CHAR):
  2717. assert isinstance(typ, sql_types.CHAR)
  2718. else:
  2719. assert isinstance(typ, sql_types.String)
  2720. eq_(typ.length, 52)
  2721. assert isinstance(typ.length, int)
  2722. @testing.requires.table_reflection
  2723. def test_nullable_reflection(self, connection, metadata):
  2724. t = Table(
  2725. "t",
  2726. metadata,
  2727. Column("a", Integer, nullable=True),
  2728. Column("b", Integer, nullable=False),
  2729. )
  2730. t.create(connection)
  2731. eq_(
  2732. {
  2733. col["name"]: col["nullable"]
  2734. for col in inspect(connection).get_columns("t")
  2735. },
  2736. {"a": True, "b": False},
  2737. )
  2738. @testing.combinations(
  2739. (
  2740. None,
  2741. "CASCADE",
  2742. None,
  2743. testing.requires.foreign_key_constraint_option_reflection_ondelete,
  2744. ),
  2745. (
  2746. None,
  2747. None,
  2748. "SET NULL",
  2749. testing.requires.foreign_key_constraint_option_reflection_onupdate,
  2750. ),
  2751. (
  2752. {},
  2753. None,
  2754. "NO ACTION",
  2755. testing.requires.foreign_key_constraint_option_reflection_onupdate,
  2756. ),
  2757. (
  2758. {},
  2759. "NO ACTION",
  2760. None,
  2761. testing.requires.fk_constraint_option_reflection_ondelete_noaction,
  2762. ),
  2763. (
  2764. None,
  2765. None,
  2766. "RESTRICT",
  2767. testing.requires.fk_constraint_option_reflection_onupdate_restrict,
  2768. ),
  2769. (
  2770. None,
  2771. "RESTRICT",
  2772. None,
  2773. testing.requires.fk_constraint_option_reflection_ondelete_restrict,
  2774. ),
  2775. argnames="expected,ondelete,onupdate",
  2776. )
  2777. def test_get_foreign_key_options(
  2778. self, connection, metadata, expected, ondelete, onupdate
  2779. ):
  2780. options = {}
  2781. if ondelete:
  2782. options["ondelete"] = ondelete
  2783. if onupdate:
  2784. options["onupdate"] = onupdate
  2785. if expected is None:
  2786. expected = options
  2787. Table(
  2788. "x",
  2789. metadata,
  2790. Column("id", Integer, primary_key=True),
  2791. test_needs_fk=True,
  2792. )
  2793. Table(
  2794. "table",
  2795. metadata,
  2796. Column("id", Integer, primary_key=True),
  2797. Column("x_id", Integer, ForeignKey("x.id", name="xid")),
  2798. Column("test", String(10)),
  2799. test_needs_fk=True,
  2800. )
  2801. Table(
  2802. "user",
  2803. metadata,
  2804. Column("id", Integer, primary_key=True),
  2805. Column("name", String(50), nullable=False),
  2806. Column("tid", Integer),
  2807. sa.ForeignKeyConstraint(
  2808. ["tid"], ["table.id"], name="myfk", **options
  2809. ),
  2810. test_needs_fk=True,
  2811. )
  2812. metadata.create_all(connection)
  2813. insp = inspect(connection)
  2814. # test 'options' is always present for a backend
  2815. # that can reflect these, since alembic looks for this
  2816. opts = insp.get_foreign_keys("table")[0]["options"]
  2817. eq_({k: opts[k] for k in opts if opts[k]}, {})
  2818. opts = insp.get_foreign_keys("user")[0]["options"]
  2819. eq_(opts, expected)
  2820. # eq_(dict((k, opts[k]) for k in opts if opts[k]), expected)
  2821. @testing.combinations(
  2822. (Integer, sa.text("10"), r"'?10'?"),
  2823. (Integer, "10", r"'?10'?"),
  2824. (Boolean, sa.true(), r"1|true"),
  2825. (
  2826. Integer,
  2827. sa.text("3 + 5"),
  2828. r"3\+5",
  2829. testing.requires.expression_server_defaults,
  2830. ),
  2831. (
  2832. Integer,
  2833. sa.text("(3 * 5)"),
  2834. r"3\*5",
  2835. testing.requires.expression_server_defaults,
  2836. ),
  2837. (DateTime, func.now(), r"current_timestamp|now|getdate"),
  2838. (
  2839. Integer,
  2840. sa.literal_column("3") + sa.literal_column("5"),
  2841. r"3\+5",
  2842. testing.requires.expression_server_defaults,
  2843. ),
  2844. argnames="datatype, default, expected_reg",
  2845. )
  2846. @testing.requires.server_defaults
  2847. def test_server_defaults(
  2848. self, metadata, connection, datatype, default, expected_reg
  2849. ):
  2850. t = Table(
  2851. "t",
  2852. metadata,
  2853. Column("id", Integer, primary_key=True),
  2854. Column("thecol", datatype, server_default=default),
  2855. )
  2856. t.create(connection)
  2857. reflected = inspect(connection).get_columns("t")[1]["default"]
  2858. reflected_sanitized = re.sub(r"[\(\) \']", "", reflected)
  2859. eq_regex(reflected_sanitized, expected_reg, flags=re.IGNORECASE)
  2860. class NormalizedNameTest(fixtures.TablesTest):
  2861. __requires__ = ("denormalized_names",)
  2862. __sparse_driver_backend__ = True
  2863. @classmethod
  2864. def define_tables(cls, metadata):
  2865. Table(
  2866. quoted_name("t1", quote=True),
  2867. metadata,
  2868. Column("id", Integer, primary_key=True),
  2869. )
  2870. Table(
  2871. quoted_name("t2", quote=True),
  2872. metadata,
  2873. Column("id", Integer, primary_key=True),
  2874. Column("t1id", ForeignKey("t1.id")),
  2875. )
  2876. def test_reflect_lowercase_forced_tables(self):
  2877. m2 = MetaData()
  2878. t2_ref = Table(
  2879. quoted_name("t2", quote=True), m2, autoload_with=config.db
  2880. )
  2881. t1_ref = m2.tables["t1"]
  2882. assert t2_ref.c.t1id.references(t1_ref.c.id)
  2883. m3 = MetaData()
  2884. m3.reflect(
  2885. config.db, only=lambda name, m: name.lower() in ("t1", "t2")
  2886. )
  2887. assert m3.tables["t2"].c.t1id.references(m3.tables["t1"].c.id)
  2888. def test_get_table_names(self):
  2889. tablenames = [
  2890. t
  2891. for t in inspect(config.db).get_table_names()
  2892. if t.lower() in ("t1", "t2")
  2893. ]
  2894. eq_(tablenames[0].upper(), tablenames[0].lower())
  2895. eq_(tablenames[1].upper(), tablenames[1].lower())
  2896. class ComputedReflectionTest(fixtures.ComputedReflectionFixtureTest):
  2897. def test_computed_col_default_not_set(self):
  2898. insp = inspect(config.db)
  2899. cols = insp.get_columns("computed_default_table")
  2900. col_data = {c["name"]: c for c in cols}
  2901. is_true("42" in col_data["with_default"]["default"])
  2902. is_(col_data["normal"]["default"], None)
  2903. is_(col_data["computed_col"]["default"], None)
  2904. def test_get_column_returns_computed(self):
  2905. insp = inspect(config.db)
  2906. cols = insp.get_columns("computed_default_table")
  2907. data = {c["name"]: c for c in cols}
  2908. for key in ("id", "normal", "with_default"):
  2909. is_true("computed" not in data[key])
  2910. compData = data["computed_col"]
  2911. is_true("computed" in compData)
  2912. is_true("sqltext" in compData["computed"])
  2913. eq_(self.normalize(compData["computed"]["sqltext"]), "normal+42")
  2914. eq_(
  2915. "persisted" in compData["computed"],
  2916. testing.requires.computed_columns_reflect_persisted.enabled,
  2917. )
  2918. if testing.requires.computed_columns_reflect_persisted.enabled:
  2919. eq_(
  2920. compData["computed"]["persisted"],
  2921. testing.requires.computed_columns_default_persisted.enabled,
  2922. )
  2923. def check_column(self, data, column, sqltext, persisted):
  2924. is_true("computed" in data[column])
  2925. compData = data[column]["computed"]
  2926. eq_(self.normalize(compData["sqltext"]), sqltext)
  2927. if testing.requires.computed_columns_reflect_persisted.enabled:
  2928. is_true("persisted" in compData)
  2929. is_(compData["persisted"], persisted)
  2930. def test_get_column_returns_persisted(self):
  2931. insp = inspect(config.db)
  2932. cols = insp.get_columns("computed_column_table")
  2933. data = {c["name"]: c for c in cols}
  2934. self.check_column(
  2935. data,
  2936. "computed_no_flag",
  2937. "normal+42",
  2938. testing.requires.computed_columns_default_persisted.enabled,
  2939. )
  2940. if testing.requires.computed_columns_virtual.enabled:
  2941. self.check_column(
  2942. data,
  2943. "computed_virtual",
  2944. "normal+2",
  2945. False,
  2946. )
  2947. if testing.requires.computed_columns_stored.enabled:
  2948. self.check_column(
  2949. data,
  2950. "computed_stored",
  2951. "normal-42",
  2952. True,
  2953. )
  2954. @testing.requires.schemas
  2955. def test_get_column_returns_persisted_with_schema(self):
  2956. insp = inspect(config.db)
  2957. cols = insp.get_columns(
  2958. "computed_column_table", schema=config.test_schema
  2959. )
  2960. data = {c["name"]: c for c in cols}
  2961. self.check_column(
  2962. data,
  2963. "computed_no_flag",
  2964. "normal/42",
  2965. testing.requires.computed_columns_default_persisted.enabled,
  2966. )
  2967. if testing.requires.computed_columns_virtual.enabled:
  2968. self.check_column(
  2969. data,
  2970. "computed_virtual",
  2971. "normal/2",
  2972. False,
  2973. )
  2974. if testing.requires.computed_columns_stored.enabled:
  2975. self.check_column(
  2976. data,
  2977. "computed_stored",
  2978. "normal*42",
  2979. True,
  2980. )
  2981. class IdentityReflectionTest(fixtures.TablesTest):
  2982. run_inserts = run_deletes = None
  2983. __sparse_driver_backend__ = True
  2984. __requires__ = ("identity_columns", "table_reflection")
  2985. @classmethod
  2986. def define_tables(cls, metadata):
  2987. Table(
  2988. "t1",
  2989. metadata,
  2990. Column("normal", Integer),
  2991. Column("id1", Integer, Identity()),
  2992. )
  2993. Table(
  2994. "t2",
  2995. metadata,
  2996. Column(
  2997. "id2",
  2998. Integer,
  2999. Identity(
  3000. always=True,
  3001. start=2,
  3002. increment=3,
  3003. minvalue=-2,
  3004. maxvalue=42,
  3005. cycle=True,
  3006. cache=4,
  3007. ),
  3008. ),
  3009. )
  3010. if testing.requires.schemas.enabled:
  3011. Table(
  3012. "t1",
  3013. metadata,
  3014. Column("normal", Integer),
  3015. Column("id1", Integer, Identity(always=True, start=20)),
  3016. schema=config.test_schema,
  3017. )
  3018. def check(self, value, exp, approx):
  3019. if testing.requires.identity_columns_standard.enabled:
  3020. common_keys = (
  3021. "always",
  3022. "start",
  3023. "increment",
  3024. "minvalue",
  3025. "maxvalue",
  3026. "cycle",
  3027. "cache",
  3028. )
  3029. for k in list(value):
  3030. if k not in common_keys:
  3031. value.pop(k)
  3032. if approx:
  3033. eq_(len(value), len(exp))
  3034. for k in value:
  3035. if k == "minvalue":
  3036. is_true(value[k] <= exp[k])
  3037. elif k in {"maxvalue", "cache"}:
  3038. is_true(value[k] >= exp[k])
  3039. else:
  3040. eq_(value[k], exp[k], k)
  3041. else:
  3042. eq_(value, exp)
  3043. else:
  3044. eq_(value["start"], exp["start"])
  3045. eq_(value["increment"], exp["increment"])
  3046. def test_reflect_identity(self):
  3047. insp = inspect(config.db)
  3048. cols = insp.get_columns("t1") + insp.get_columns("t2")
  3049. for col in cols:
  3050. if col["name"] == "normal":
  3051. is_false("identity" in col)
  3052. elif col["name"] == "id1":
  3053. if "autoincrement" in col:
  3054. is_true(col["autoincrement"])
  3055. eq_(col["default"], None)
  3056. is_true("identity" in col)
  3057. self.check(
  3058. col["identity"],
  3059. dict(
  3060. always=False,
  3061. start=1,
  3062. increment=1,
  3063. minvalue=1,
  3064. maxvalue=2147483647,
  3065. cycle=False,
  3066. cache=1,
  3067. ),
  3068. approx=True,
  3069. )
  3070. elif col["name"] == "id2":
  3071. if "autoincrement" in col:
  3072. is_true(col["autoincrement"])
  3073. eq_(col["default"], None)
  3074. is_true("identity" in col)
  3075. self.check(
  3076. col["identity"],
  3077. dict(
  3078. always=True,
  3079. start=2,
  3080. increment=3,
  3081. minvalue=-2,
  3082. maxvalue=42,
  3083. cycle=True,
  3084. cache=4,
  3085. ),
  3086. approx=False,
  3087. )
  3088. @testing.requires.schemas
  3089. def test_reflect_identity_schema(self):
  3090. insp = inspect(config.db)
  3091. cols = insp.get_columns("t1", schema=config.test_schema)
  3092. for col in cols:
  3093. if col["name"] == "normal":
  3094. is_false("identity" in col)
  3095. elif col["name"] == "id1":
  3096. if "autoincrement" in col:
  3097. is_true(col["autoincrement"])
  3098. eq_(col["default"], None)
  3099. is_true("identity" in col)
  3100. self.check(
  3101. col["identity"],
  3102. dict(
  3103. always=True,
  3104. start=20,
  3105. increment=1,
  3106. minvalue=1,
  3107. maxvalue=2147483647,
  3108. cycle=False,
  3109. cache=1,
  3110. ),
  3111. approx=True,
  3112. )
  3113. class CompositeKeyReflectionTest(fixtures.TablesTest):
  3114. __sparse_driver_backend__ = True
  3115. @classmethod
  3116. def define_tables(cls, metadata):
  3117. tb1 = Table(
  3118. "tb1",
  3119. metadata,
  3120. Column("id", Integer),
  3121. Column("attr", Integer),
  3122. Column("name", sql_types.VARCHAR(20)),
  3123. sa.PrimaryKeyConstraint("name", "id", "attr", name="pk_tb1"),
  3124. schema=None,
  3125. test_needs_fk=True,
  3126. )
  3127. Table(
  3128. "tb2",
  3129. metadata,
  3130. Column("id", Integer, primary_key=True),
  3131. Column("pid", Integer),
  3132. Column("pattr", Integer),
  3133. Column("pname", sql_types.VARCHAR(20)),
  3134. sa.ForeignKeyConstraint(
  3135. ["pname", "pid", "pattr"],
  3136. [tb1.c.name, tb1.c.id, tb1.c.attr],
  3137. name="fk_tb1_name_id_attr",
  3138. ),
  3139. schema=None,
  3140. test_needs_fk=True,
  3141. )
  3142. @testing.requires.primary_key_constraint_reflection
  3143. def test_pk_column_order(self, connection):
  3144. # test for issue #5661
  3145. insp = inspect(connection)
  3146. primary_key = insp.get_pk_constraint(self.tables.tb1.name)
  3147. eq_(primary_key.get("constrained_columns"), ["name", "id", "attr"])
  3148. @testing.requires.foreign_key_constraint_reflection
  3149. def test_fk_column_order(self, connection):
  3150. # test for issue #5661
  3151. insp = inspect(connection)
  3152. foreign_keys = insp.get_foreign_keys(self.tables.tb2.name)
  3153. eq_(len(foreign_keys), 1)
  3154. fkey1 = foreign_keys[0]
  3155. eq_(fkey1.get("referred_columns"), ["name", "id", "attr"])
  3156. eq_(fkey1.get("constrained_columns"), ["pname", "pid", "pattr"])
  3157. __all__ = (
  3158. "ComponentReflectionTest",
  3159. "ComponentReflectionTestExtra",
  3160. "TableNoColumnsTest",
  3161. "QuotedNameArgumentTest",
  3162. "BizarroCharacterTest",
  3163. "HasTableTest",
  3164. "HasIndexTest",
  3165. "NormalizedNameTest",
  3166. "ComputedReflectionTest",
  3167. "IdentityReflectionTest",
  3168. "CompositeKeyReflectionTest",
  3169. "TempTableElementsTest",
  3170. )