forked from googleapis/python-bigquery-sqlalchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sqlalchemy_bigquery.py
More file actions
604 lines (483 loc) · 22.4 KB
/
test_sqlalchemy_bigquery.py
File metadata and controls
604 lines (483 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from google.api_core.exceptions import BadRequest
from pybigquery.api import ApiClient
from pybigquery.sqlalchemy_bigquery import BigQueryDialect
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import Table, MetaData, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import types, func, case, inspect
from sqlalchemy.sql import expression, select, literal_column
from sqlalchemy.exc import NoSuchTableError
from sqlalchemy.orm import sessionmaker
from pytz import timezone
import pytest
import sqlalchemy
import datetime
import decimal
ONE_ROW_CONTENTS_EXPANDED = [
588,
datetime.datetime(2013, 10, 10, 11, 27, 16, tzinfo=timezone('UTC')),
'W 52 St & 11 Ave',
40.76727216,
decimal.Decimal('40.76727216'),
False,
datetime.date(2013, 10, 10),
datetime.datetime(2013, 10, 10, 11, 27, 16),
datetime.time(11, 27, 16),
b'\xef',
{u'age': 100, u'name': u'John Doe'},
'John Doe',
100,
{u'record': {u'age': 200, u'name': u'John Doe 2'}}, {u'age': 200, u'name': u'John Doe 2'},
'John Doe 2',
200,
[1, 2, 3],
]
ONE_ROW_CONTENTS = [
588,
datetime.datetime(2013, 10, 10, 11, 27, 16, tzinfo=timezone('UTC')),
'W 52 St & 11 Ave',
40.76727216,
decimal.Decimal('40.76727216'),
False,
datetime.date(2013, 10, 10),
datetime.datetime(2013, 10, 10, 11, 27, 16),
datetime.time(11, 27, 16),
b'\xef',
{
'name': 'John Doe',
'age': 100,
},
{
'record': {
'name': 'John Doe 2',
'age': 200,
}
},
[1, 2, 3],
]
ONE_ROW_CONTENTS_DML = [
588,
datetime.datetime(2013, 10, 10, 11, 27, 16, tzinfo=timezone('UTC')),
'test',
40.76727216,
decimal.Decimal('40.76727216'),
False,
datetime.date(2013, 10, 10),
datetime.datetime(2013, 10, 10, 11, 27, 16),
datetime.time(11, 27, 16),
'test_bytes'
]
SAMPLE_COLUMNS = [
{'name': 'integer', 'type': types.Integer(), 'nullable': True, 'default': None},
{'name': 'timestamp', 'type': types.TIMESTAMP(), 'nullable': True, 'default': None},
{'name': 'string', 'type': types.String(), 'nullable': True, 'default': None},
{'name': 'float', 'type': types.Float(), 'nullable': True, 'default': None},
{'name': 'numeric', 'type': types.DECIMAL(), 'nullable': True, 'default': None},
{'name': 'boolean', 'type': types.Boolean(), 'nullable': True, 'default': None},
{'name': 'date', 'type': types.DATE(), 'nullable': True, 'default': None},
{'name': 'datetime', 'type': types.DATETIME(), 'nullable': True, 'default': None},
{'name': 'time', 'type': types.TIME(), 'nullable': True, 'default': None},
{'name': 'bytes', 'type': types.BINARY(), 'nullable': True, 'default': None},
{
'name': 'record',
'type': types.JSON(),
'nullable': True,
'default': None,
'comment': 'In Standard SQL this data type is a STRUCT<name STRING, age INT64>.',
},
{'name': 'record.name', 'type': types.String(), 'nullable': True, 'default': None},
{'name': 'record.age', 'type': types.Integer(), 'nullable': True, 'default': None},
{'name': 'nested_record', 'type': types.JSON(), 'nullable': True, 'default': None},
{'name': 'nested_record.record', 'type': types.JSON(), 'nullable': True, 'default': None},
{'name': 'nested_record.record.name', 'type': types.String(), 'nullable': True, 'default': None},
{'name': 'nested_record.record.age', 'type': types.Integer(), 'nullable': True, 'default': None},
{'name': 'array', 'type': types.ARRAY(types.Integer()), 'nullable': True, 'default': None},
]
@pytest.fixture(scope='session')
def engine():
engine = create_engine('bigquery://', echo=True)
return engine
@pytest.fixture(scope='session')
def dialect():
return BigQueryDialect()
@pytest.fixture(scope='session')
def engine_using_test_dataset():
engine = create_engine('bigquery:///test_pybigquery', echo=True)
return engine
@pytest.fixture(scope='session')
def engine_with_location():
engine = create_engine('bigquery://', echo=True, location="asia-northeast1")
return engine
@pytest.fixture(scope='session')
def table(engine):
return Table('test_pybigquery.sample', MetaData(bind=engine), autoload=True)
@pytest.fixture(scope='session')
def table_using_test_dataset(engine_using_test_dataset):
return Table('sample', MetaData(bind=engine_using_test_dataset), autoload=True)
@pytest.fixture(scope='session')
def table_one_row(engine):
return Table('test_pybigquery.sample_one_row', MetaData(bind=engine), autoload=True)
@pytest.fixture(scope='session')
def table_dml(engine):
return Table('test_pybigquery.sample_dml', MetaData(bind=engine), autoload=True)
@pytest.fixture(scope='session')
def session(engine):
Session = sessionmaker(bind=engine)
session = Session()
return session
@pytest.fixture(scope='session')
def session_using_test_dataset(engine_using_test_dataset):
Session = sessionmaker(bind=engine_using_test_dataset)
session = Session()
return session
@pytest.fixture(scope='session')
def inspector(engine):
return inspect(engine)
@pytest.fixture(scope='session')
def inspector_using_test_dataset(engine_using_test_dataset):
return inspect(engine_using_test_dataset)
@pytest.fixture(scope='session')
def query():
def query(table):
col1 = literal_column("TIMESTAMP_TRUNC(timestamp, DAY)").label("timestamp_label")
col2 = func.sum(table.c.integer)
# Test rendering of nested labels. Full expression should render in SELECT, but
# ORDER/GROUP BY should use label only.
col3 = func.sum(func.sum(table.c.integer.label("inner")).label("outer")).over().label('outer')
query = (
select([
col1,
col2,
col3,
])
.where(col1 < '2017-01-01 00:00:00')
.group_by(col1)
.order_by(col2)
)
return query
return query
@pytest.fixture(scope='session')
def api_client():
return ApiClient()
def test_dry_run(engine, api_client):
sql = 'SELECT * FROM test_pybigquery.sample_one_row'
assert api_client.dry_run_query(sql).total_bytes_processed == 148
sql = 'SELECT * FROM sample_one_row'
with pytest.raises(BadRequest) as excinfo:
api_client.dry_run_query(sql)
expected_message = 'Table name "sample_one_row" missing dataset while no default dataset is set in the request.'
assert expected_message in str(excinfo.value.message)
def test_engine_with_dataset(engine_using_test_dataset):
rows = engine_using_test_dataset.execute('SELECT * FROM sample_one_row').fetchall()
assert list(rows[0]) == ONE_ROW_CONTENTS
table_one_row = Table('sample_one_row', MetaData(bind=engine_using_test_dataset), autoload=True)
rows = table_one_row.select().execute().fetchall()
assert list(rows[0]) == ONE_ROW_CONTENTS_EXPANDED
table_one_row = Table('test_pybigquery.sample_one_row', MetaData(bind=engine_using_test_dataset), autoload=True)
rows = table_one_row.select().execute().fetchall()
# verify that we are pulling from the specifically-named dataset,
# instead of pulling from the default dataset of the engine (which
# does not have this table at all)
assert list(rows[0]) == ONE_ROW_CONTENTS_EXPANDED
def test_dataset_location(engine_with_location):
rows = engine_with_location.execute('SELECT * FROM test_pybigquery_location.sample_one_row').fetchall()
assert list(rows[0]) == ONE_ROW_CONTENTS
def test_reflect_select(table, table_using_test_dataset):
for table in [table, table_using_test_dataset]:
assert table.comment == "A sample table containing most data types."
assert len(table.c) == 18
assert isinstance(table.c.integer, Column)
assert isinstance(table.c.integer.type, types.Integer)
assert isinstance(table.c.timestamp.type, types.TIMESTAMP)
assert isinstance(table.c.string.type, types.String)
assert isinstance(table.c.float.type, types.Float)
assert isinstance(table.c.boolean.type, types.Boolean)
assert isinstance(table.c.date.type, types.DATE)
assert isinstance(table.c.datetime.type, types.DATETIME)
assert isinstance(table.c.time.type, types.TIME)
assert isinstance(table.c.bytes.type, types.BINARY)
assert isinstance(table.c['record.age'].type, types.Integer)
assert isinstance(table.c['record.name'].type, types.String)
assert isinstance(table.c['nested_record.record.age'].type, types.Integer)
assert isinstance(table.c['nested_record.record.name'].type, types.String)
assert isinstance(table.c.array.type, types.ARRAY)
rows = table.select().execute().fetchall()
assert len(rows) == 1000
def test_content_from_raw_queries(engine):
rows = engine.execute('SELECT * FROM test_pybigquery.sample_one_row').fetchall()
assert list(rows[0]) == ONE_ROW_CONTENTS
def test_record_content_from_raw_queries(engine):
rows = engine.execute('SELECT record.name FROM test_pybigquery.sample_one_row').fetchall()
assert rows[0][0] == 'John Doe'
def test_content_from_reflect(engine, table_one_row):
rows = table_one_row.select().execute().fetchall()
assert list(rows[0]) == ONE_ROW_CONTENTS_EXPANDED
def test_unicode(engine, table_one_row):
unicode_str = "白人看不懂"
returned_str = sqlalchemy.select(
[expression.bindparam("好", unicode_str)],
from_obj=table_one_row,
).scalar()
assert returned_str == unicode_str
def test_reflect_select_shared_table(engine):
one_row = Table('bigquery-public-data.samples.natality', MetaData(bind=engine), autoload=True)
row = one_row.select().limit(1).execute().first()
assert len(row) >= 1
def test_reflect_table_does_not_exist(engine):
with pytest.raises(NoSuchTableError):
Table('test_pybigquery.table_does_not_exist', MetaData(bind=engine), autoload=True)
assert Table('test_pybigquery.table_does_not_exist', MetaData(bind=engine)).exists() is False
def test_reflect_dataset_does_not_exist(engine):
with pytest.raises(NoSuchTableError):
Table('dataset_does_not_exist.table_does_not_exist', MetaData(bind=engine), autoload=True)
def test_tables_list(engine, engine_using_test_dataset):
tables = engine.table_names()
assert 'test_pybigquery.sample' in tables
assert 'test_pybigquery.sample_one_row' in tables
assert 'test_pybigquery.sample_dml' in tables
assert 'test_pybigquery.sample_view' not in tables
tables = engine_using_test_dataset.table_names()
assert 'sample' in tables
assert 'sample_one_row' in tables
assert 'sample_dml' in tables
assert 'sample_view' not in tables
def test_group_by(session, table, session_using_test_dataset, table_using_test_dataset):
"""labels in SELECT clause should be correclty formatted (dots are replaced with underscores)"""
for session, table in [(session, table), (session_using_test_dataset, table_using_test_dataset)]:
result = session.query(table.c.string, func.count(table.c.integer)).group_by(table.c.string).all()
assert len(result) > 0
def test_nested_labels(engine, table):
col = table.c.integer
exprs = [
sqlalchemy.func.sum(
sqlalchemy.func.sum(col.label("inner")).label("outer")
).over(),
sqlalchemy.func.sum(
sqlalchemy.case([[
sqlalchemy.literal(True),
col.label("inner"),
]]).label("outer")
),
sqlalchemy.func.sum(
sqlalchemy.func.sum(
sqlalchemy.case([[
sqlalchemy.literal(True), col.label("inner")
]]).label("middle")
).label("outer")
).over(),
]
for expr in exprs:
sql = str(expr.compile(engine))
assert "inner" not in sql
assert "middle" not in sql
assert "outer" not in sql
def test_session_query(session, table, session_using_test_dataset, table_using_test_dataset):
for session, table in [(session, table), (session_using_test_dataset, table_using_test_dataset)]:
col_concat = func.concat(table.c.string).label('concat')
result = (
session
.query(
table.c.string,
col_concat,
func.avg(table.c.integer),
func.sum(case(
[(table.c.boolean == sqlalchemy.literal(True), 1)],
else_=0
))
)
.group_by(table.c.string, col_concat)
.having(func.avg(table.c.integer) > 10)
).all()
assert len(result) > 0
def test_labels(session, table, session_using_test_dataset, table_using_test_dataset):
for session, table in [(session, table), (session_using_test_dataset, table_using_test_dataset)]:
result = (
session
.query(
# Valid
table.c.string.label('abc'),
# Invalid, needs to start with underscore
table.c.string.label('123'),
# Valid
table.c.string.label('_123abc'),
# Invalid, contains illegal characters
table.c.string.label('!@#$%^&*()~`'),
)
)
result = result.all()
assert len(result) > 0
def test_custom_expression(engine, engine_using_test_dataset, table, table_using_test_dataset, query):
"""GROUP BY clause should use labels instead of expressions"""
q = query(table)
result = engine.execute(q).fetchall()
assert len(result) > 0
q = query(table_using_test_dataset)
result = engine_using_test_dataset.execute(q).fetchall()
assert len(result) > 0
def test_compiled_query_literal_binds(engine, engine_using_test_dataset, table, table_using_test_dataset, query):
q = query(table)
compiled = q.compile(engine, compile_kwargs={"literal_binds": True})
result = engine.execute(compiled).fetchall()
assert len(result) > 0
q = query(table_using_test_dataset)
compiled = q.compile(engine_using_test_dataset, compile_kwargs={"literal_binds": True})
result = engine_using_test_dataset.execute(compiled).fetchall()
assert len(result) > 0
@pytest.mark.parametrize(["column", "processed"], [
(types.String(), "STRING"),
(types.NUMERIC(), "NUMERIC"),
(types.ARRAY(types.String), "ARRAY<STRING>"),
])
def test_compile_types(engine, column, processed):
result = engine.dialect.type_compiler.process(column)
assert result == processed
def test_joins(session, table, table_one_row):
result = (session.query(table.c.string, func.count(table_one_row.c.integer))
.join(table_one_row, table_one_row.c.string == table.c.string)
.group_by(table.c.string).all())
assert len(result) > 0
def test_querying_wildcard_tables(engine):
table = Table('bigquery-public-data.noaa_gsod.gsod*', MetaData(bind=engine), autoload=True)
rows = table.select().limit(1).execute().first()
assert len(rows) > 0
def test_dml(engine, session, table_dml):
# test insert
engine.execute(table_dml.insert(ONE_ROW_CONTENTS_DML))
result = table_dml.select().execute().fetchall()
assert len(result) == 1
# test update
session.query(table_dml)\
.filter(table_dml.c.string == 'test')\
.update({'string': 'updated_row'}, synchronize_session=False)
updated_result = table_dml.select().execute().fetchone()
assert updated_result['test_pybigquery.sample_dml_string'] == 'updated_row'
# test delete
session.query(table_dml).filter(table_dml.c.string == 'updated_row').delete(synchronize_session=False)
result = table_dml.select().execute().fetchall()
assert len(result) == 0
def test_create_table(engine):
meta = MetaData()
Table(
'test_pybigquery.test_table_create', meta,
Column('integer_c', sqlalchemy.Integer, doc="column description"),
Column('float_c', sqlalchemy.Float),
Column('decimal_c', sqlalchemy.DECIMAL),
Column('string_c', sqlalchemy.String),
Column('text_c', sqlalchemy.Text),
Column('boolean_c', sqlalchemy.Boolean),
Column('timestamp_c', sqlalchemy.TIMESTAMP),
Column('datetime_c', sqlalchemy.DATETIME),
Column('date_c', sqlalchemy.DATE),
Column('time_c', sqlalchemy.TIME),
Column('binary_c', sqlalchemy.BINARY),
bigquery_description="test table description",
bigquery_friendly_name="test table name"
)
meta.create_all(engine)
meta.drop_all(engine)
# Test creating tables with declarative_base
Base = declarative_base()
class TableTest(Base):
__tablename__ = 'test_pybigquery.test_table_create2'
integer_c = Column(sqlalchemy.Integer, primary_key=True)
float_c = Column(sqlalchemy.Float)
Base.metadata.create_all(engine)
Base.metadata.drop_all(engine)
def test_schemas_names(inspector, inspector_using_test_dataset):
datasets = inspector.get_schema_names()
assert 'test_pybigquery' in datasets
datasets = inspector_using_test_dataset.get_schema_names()
assert 'test_pybigquery' in datasets
def test_table_names_in_schema(inspector, inspector_using_test_dataset):
tables = inspector.get_table_names('test_pybigquery')
assert 'test_pybigquery.sample' in tables
assert 'test_pybigquery.sample_one_row' in tables
assert 'test_pybigquery.sample_dml' in tables
assert 'test_pybigquery.sample_view' not in tables
assert len(tables) == 3
tables = inspector_using_test_dataset.get_table_names()
assert 'sample' in tables
assert 'sample_one_row' in tables
assert 'sample_dml' in tables
assert 'sample_view' not in tables
assert len(tables) == 3
def test_view_names(inspector, inspector_using_test_dataset):
view_names = inspector.get_view_names()
assert "test_pybigquery.sample_view" in view_names
assert "test_pybigquery.sample" not in view_names
view_names = inspector_using_test_dataset.get_view_names()
assert "sample_view" in view_names
assert "sample" not in view_names
def test_get_indexes(inspector, inspector_using_test_dataset):
for _ in ['test_pybigquery.sample', 'test_pybigquery.sample_one_row']:
indexes = inspector.get_indexes('test_pybigquery.sample')
assert len(indexes) == 2
assert indexes[0] == {'name': 'partition', 'column_names': ['timestamp'], 'unique': False}
assert indexes[1] == {'name': 'clustering', 'column_names': ['integer', 'string'], 'unique': False}
def test_get_columns(inspector, inspector_using_test_dataset):
columns_without_schema = inspector.get_columns('test_pybigquery.sample')
columns_schema = inspector.get_columns('sample', 'test_pybigquery')
columns_queries = [columns_without_schema, columns_schema]
for columns in columns_queries:
for i, col in enumerate(columns):
sample_col = SAMPLE_COLUMNS[i]
assert col['comment'] == sample_col.get('comment')
assert col['default'] == sample_col['default']
assert col['name'] == sample_col['name']
assert col['nullable'] == sample_col['nullable']
assert col['type'].__class__.__name__ == sample_col['type'].__class__.__name__
columns_without_schema = inspector_using_test_dataset.get_columns('sample')
columns_schema = inspector_using_test_dataset.get_columns('sample', 'test_pybigquery')
columns_queries = [columns_without_schema, columns_schema]
for columns in columns_queries:
for i, col in enumerate(columns):
sample_col = SAMPLE_COLUMNS[i]
assert col['comment'] == sample_col.get('comment')
assert col['default'] == sample_col['default']
assert col['name'] == sample_col['name']
assert col['nullable'] == sample_col['nullable']
assert col['type'].__class__.__name__ == sample_col['type'].__class__.__name__
@pytest.mark.parametrize('provided_schema_name,provided_table_name,client_project',
[
('dataset', 'table', 'project'),
(None, 'dataset.table', 'project'),
(None, 'project.dataset.table', 'other_project'),
('project', 'dataset.table', 'other_project'),
('project.dataset', 'table', 'other_project'),
])
def test_table_reference(dialect, provided_schema_name,
provided_table_name, client_project):
ref = dialect._table_reference(provided_schema_name,
provided_table_name,
client_project)
assert ref.table_id == 'table'
assert ref.dataset_id == 'dataset'
assert ref.project == 'project'
@pytest.mark.parametrize('provided_schema_name,provided_table_name,client_project',
[
('project.dataset', 'other_dataset.table', 'project'),
('project.dataset', 'other_project.dataset.table', 'project'),
('project.dataset.something_else', 'table', 'project'),
(None, 'project.dataset.table.something_else', 'project'),
])
def test_invalid_table_reference(dialect, provided_schema_name,
provided_table_name, client_project):
with pytest.raises(ValueError):
dialect._table_reference(provided_schema_name,
provided_table_name,
client_project)
def test_has_table(engine, engine_using_test_dataset):
assert engine.has_table('sample', 'test_pybigquery') is True
assert engine.has_table('test_pybigquery.sample') is True
assert engine.has_table('test_pybigquery.nonexistent_table') is False
assert engine.has_table('nonexistent_table', 'nonexistent_dataset') is False
assert engine.has_table('sample_alt', 'test_pybigquery_alt') is True
assert engine.has_table('test_pybigquery_alt.sample_alt') is True
assert engine_using_test_dataset.has_table('sample') is True
assert engine_using_test_dataset.has_table('sample', 'test_pybigquery') is True
assert engine_using_test_dataset.has_table('test_pybigquery.sample') is True
assert engine_using_test_dataset.has_table('sample_alt') is False
assert engine_using_test_dataset.has_table('sample_alt', 'test_pybigquery_alt') is True
assert engine_using_test_dataset.has_table('test_pybigquery_alt.sample_alt') is True