18
18
# However, if you have executed another commercial license agreement
19
19
# with Crate these terms will supersede the license and you may use the
20
20
# software solely pursuant to the terms of the relevant commercial agreement.
21
+ import warnings
21
22
from textwrap import dedent
22
23
from unittest import mock , skipIf , TestCase
23
24
from unittest .mock import MagicMock , patch
27
28
28
29
import sqlalchemy as sa
29
30
from sqlalchemy .sql import text , Update
31
+
32
+ from crate .testing .util import ExtraAssertions
33
+
30
34
try :
31
35
from sqlalchemy .orm import declarative_base
32
36
except ImportError :
@@ -288,7 +292,7 @@ def execute_wrapper(self, query, *args, **kwargs):
288
292
289
293
290
294
@patch ('crate.client.connection.Cursor' , FakeCursor )
291
- class SqlAlchemyDDLCompilerTest (CompilerTestCase ):
295
+ class SqlAlchemyDDLCompilerTest (CompilerTestCase , ExtraAssertions ):
292
296
"""
293
297
Verify a few scenarios regarding the DDL compiler.
294
298
"""
@@ -330,26 +334,39 @@ class ItemStore(Base):
330
334
)
331
335
root = sa .orm .relationship (RootStore , back_populates = "items" )
332
336
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 ))
353
370
354
371
def test_ddl_with_unique_key (self ):
355
372
"""
@@ -366,12 +383,24 @@ class FooBar(Base):
366
383
id = sa .Column (sa .Integer , primary_key = True )
367
384
name = sa .Column (sa .String , unique = True )
368
385
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