1818# However, if you have executed another commercial license agreement
1919# with Crate these terms will supersede the license and you may use the
2020# software solely pursuant to the terms of the relevant commercial agreement.
21+ import warnings
2122from textwrap import dedent
2223from unittest import mock , skipIf , TestCase
2324from unittest .mock import MagicMock , patch
2728
2829import sqlalchemy as sa
2930from sqlalchemy .sql import text , Update
31+
32+ from crate .testing .util import ExtraAssertions
33+
3034try :
3135 from sqlalchemy .orm import declarative_base
3236except ImportError :
@@ -288,7 +292,7 @@ def execute_wrapper(self, query, *args, **kwargs):
288292
289293
290294@patch ('crate.client.connection.Cursor' , FakeCursor )
291- class SqlAlchemyDDLCompilerTest (CompilerTestCase ):
295+ class SqlAlchemyDDLCompilerTest (CompilerTestCase , ExtraAssertions ):
292296 """
293297 Verify a few scenarios regarding the DDL compiler.
294298 """
@@ -330,26 +334,39 @@ class ItemStore(Base):
330334 )
331335 root = sa .orm .relationship (RootStore , back_populates = "items" )
332336
333- self .metadata .create_all (self .engine , tables = [RootStore .__table__ ], checkfirst = False )
334- self .assertEqual (self .executed_statement , dedent ("""
335- CREATE TABLE testdrive.root (
336- \t id INT NOT NULL,
337- \t name STRING,
338- \t PRIMARY KEY (id)
339- )
340-
341- """ )) # noqa: W291
342-
343- self .metadata .create_all (self .engine , tables = [ItemStore .__table__ ], checkfirst = False )
344- self .assertEqual (self .executed_statement , dedent ("""
345- CREATE TABLE testdrive.item (
346- \t id INT NOT NULL,
347- \t name STRING,
348- \t root_id INT,
349- \t PRIMARY KEY (id)
350- )
351-
352- """ )) # noqa: W291, W293
337+ with warnings .catch_warnings (record = True ) as w :
338+
339+ # Cause all warnings to always be triggered.
340+ warnings .simplefilter ("always" )
341+
342+ # Verify SQL DDL statement.
343+ self .metadata .create_all (self .engine , tables = [RootStore .__table__ ], checkfirst = False )
344+ self .assertEqual (self .executed_statement , dedent ("""
345+ CREATE TABLE testdrive.root (
346+ \t id INT NOT NULL,
347+ \t name STRING,
348+ \t PRIMARY KEY (id)
349+ )
350+
351+ """ )) # noqa: W291, W293
352+
353+ # Verify SQL DDL statement.
354+ self .metadata .create_all (self .engine , tables = [ItemStore .__table__ ], checkfirst = False )
355+ self .assertEqual (self .executed_statement , dedent ("""
356+ CREATE TABLE testdrive.item (
357+ \t id INT NOT NULL,
358+ \t name STRING,
359+ \t root_id INT,
360+ \t PRIMARY KEY (id)
361+ )
362+
363+ """ )) # noqa: W291, W293
364+
365+ # Verify if corresponding warning is emitted.
366+ self .assertEqual (len (w ), 1 )
367+ self .assertIsSubclass (w [- 1 ].category , UserWarning )
368+ self .assertIn ("CrateDB does not support foreign key constraints, "
369+ "they will be omitted when generating DDL statements." , str (w [- 1 ].message ))
353370
354371 def test_ddl_with_unique_key (self ):
355372 """
@@ -366,12 +383,24 @@ class FooBar(Base):
366383 id = sa .Column (sa .Integer , primary_key = True )
367384 name = sa .Column (sa .String , unique = True )
368385
369- self .metadata .create_all (self .engine , tables = [FooBar .__table__ ], checkfirst = False )
370- self .assertEqual (self .executed_statement , dedent ("""
371- CREATE TABLE testdrive.foobar (
372- \t id INT NOT NULL,
373- \t name STRING,
374- \t PRIMARY KEY (id)
375- )
376-
377- """ )) # noqa: W291
386+ with warnings .catch_warnings (record = True ) as w :
387+
388+ # Cause all warnings to always be triggered.
389+ warnings .simplefilter ("always" )
390+
391+ # Verify SQL DDL statement.
392+ self .metadata .create_all (self .engine , tables = [FooBar .__table__ ], checkfirst = False )
393+ self .assertEqual (self .executed_statement , dedent ("""
394+ CREATE TABLE testdrive.foobar (
395+ \t id INT NOT NULL,
396+ \t name STRING,
397+ \t PRIMARY KEY (id)
398+ )
399+
400+ """ )) # noqa: W291, W293
401+
402+ # Verify if corresponding warning is emitted.
403+ self .assertEqual (len (w ), 1 )
404+ self .assertIsSubclass (w [- 1 ].category , UserWarning )
405+ self .assertIn ("CrateDB does not support unique constraints, "
406+ "they will be omitted when generating DDL statements." , str (w [- 1 ].message ))
0 commit comments