16
16
17
17
USER_AGENT_TOKEN = "PySQL e2e Tests"
18
18
19
+
19
20
def sqlalchemy_1_3 ():
20
21
import sqlalchemy
22
+
21
23
return sqlalchemy .__version__ .startswith ("1.3" )
22
24
25
+
23
26
def version_agnostic_select (object_to_select , * args , ** kwargs ):
24
27
"""
25
28
SQLAlchemy==1.3.x requires arguments to select() to be a Python list
26
-
29
+
27
30
https://docs.sqlalchemy.org/en/20/changelog/migration_14.html#orm-query-is-internally-unified-with-select-update-delete-2-0-style-execution-available
28
31
"""
29
32
30
33
if sqlalchemy_1_3 ():
31
34
return select ([object_to_select ], * args , ** kwargs )
32
35
else :
33
36
return select (object_to_select , * args , ** kwargs )
34
-
37
+
38
+
35
39
def version_agnostic_connect_arguments (catalog = None , schema = None ) -> Tuple [str , dict ]:
36
-
40
+
37
41
HOST = os .environ .get ("host" )
38
42
HTTP_PATH = os .environ .get ("http_path" )
39
43
ACCESS_TOKEN = os .environ .get ("access_token" )
@@ -44,29 +48,34 @@ def version_agnostic_connect_arguments(catalog=None, schema=None) -> Tuple[str,
44
48
45
49
if sqlalchemy_1_3 ():
46
50
conn_string = f"databricks://token:{ ACCESS_TOKEN } @{ HOST } "
47
- connect_args = {** ua_connect_args ,
51
+ connect_args = {
52
+ ** ua_connect_args ,
48
53
"http_path" : HTTP_PATH ,
49
54
"server_hostname" : HOST ,
50
55
"catalog" : CATALOG ,
51
- "schema" : SCHEMA
56
+ "schema" : SCHEMA ,
52
57
}
53
58
54
59
return conn_string , connect_args
55
60
else :
56
- return f"databricks://token:{ ACCESS_TOKEN } @{ HOST } ?http_path={ HTTP_PATH } &catalog={ CATALOG } &schema={ SCHEMA } " , ua_connect_args
57
-
58
-
61
+ return (
62
+ f"databricks://token:{ ACCESS_TOKEN } @{ HOST } ?http_path={ HTTP_PATH } &catalog={ CATALOG } &schema={ SCHEMA } " ,
63
+ ua_connect_args ,
64
+ )
59
65
60
66
61
67
@pytest .fixture
62
68
def db_engine () -> Engine :
63
69
conn_string , connect_args = version_agnostic_connect_arguments ()
64
70
return create_engine (conn_string , connect_args = connect_args )
65
71
72
+
66
73
@pytest .fixture
67
74
def samples_engine () -> Engine :
68
75
69
- conn_string , connect_args = version_agnostic_connect_arguments (catalog = "samples" , schema = "nyctaxi" )
76
+ conn_string , connect_args = version_agnostic_connect_arguments (
77
+ catalog = "samples" , schema = "nyctaxi"
78
+ )
70
79
return create_engine (conn_string , connect_args = connect_args )
71
80
72
81
@@ -130,7 +139,7 @@ def test_pandas_upload(db_engine, metadata_obj):
130
139
db_engine .execute ("DROP TABLE mock_data" )
131
140
132
141
133
- def test_create_table_not_null (db_engine , metadata_obj :MetaData ):
142
+ def test_create_table_not_null (db_engine , metadata_obj : MetaData ):
134
143
135
144
table_name = "PySQLTest_{}" .format (datetime .datetime .utcnow ().strftime ("%s" ))
136
145
@@ -301,29 +310,33 @@ def test_dialect_type_mappings(base, db_engine, metadata_obj: MetaData):
301
310
302
311
metadata_obj .drop_all ()
303
312
313
+
304
314
def test_inspector_smoke_test (samples_engine : Engine ):
305
- """It does not appear that 3L namespace is supported here
306
- """
315
+ """It does not appear that 3L namespace is supported here"""
307
316
308
317
from sqlalchemy .engine .reflection import Inspector
309
- schema , table = "nyctaxi" , "trips"
310
-
318
+
319
+ schema , table = "nyctaxi" , "trips"
320
+
311
321
try :
312
322
inspector = Inspector .from_engine (samples_engine )
313
323
except Exception as e :
314
324
assert False , f"Could not build inspector: { e } "
315
325
316
326
# Expect six columns
317
327
columns = inspector .get_columns (table , schema = schema )
318
-
328
+
319
329
# Expect zero views, but the method should return
320
330
views = inspector .get_view_names (schema = schema )
321
331
322
- assert len (columns ) == 6 , "Dialect did not find the expected number of columns in samples.nyctaxi.trips"
332
+ assert (
333
+ len (columns ) == 6
334
+ ), "Dialect did not find the expected number of columns in samples.nyctaxi.trips"
323
335
assert len (views ) == 0 , "Views could not be fetched"
324
336
337
+
325
338
def test_get_table_names_smoke_test (samples_engine : Engine ):
326
-
339
+
327
340
with samples_engine .connect () as conn :
328
341
_names = samples_engine .table_names (schema = "nyctaxi" , connection = conn )
329
342
_names is not None , "get_table_names did not succeed"
0 commit comments