This repository was archived by the owner on May 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtest_suite.py
More file actions
542 lines (435 loc) · 18.4 KB
/
test_suite.py
File metadata and controls
542 lines (435 loc) · 18.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
# -*- coding: utf-8 -*-
#
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from sqlalchemy.testing import config, db, engines
from sqlalchemy.testing import eq_
from sqlalchemy.testing import provide_metadata
from sqlalchemy.testing.schema import Column
from sqlalchemy.testing.schema import Table
from sqlalchemy import literal_column
from sqlalchemy import bindparam, case, literal, select, util
from sqlalchemy import exists
from sqlalchemy import Boolean
from sqlalchemy import String
from sqlalchemy.types import Integer
from sqlalchemy.testing import requires
from google.api_core.datetime_helpers import DatetimeWithNanoseconds
from sqlalchemy.testing.suite.test_ddl import * # noqa: F401, F403
from sqlalchemy.testing.suite.test_cte import * # noqa: F401, F403
from sqlalchemy.testing.suite.test_dialect import * # noqa: F401, F403
from sqlalchemy.testing.suite.test_update_delete import * # noqa: F401, F403
from sqlalchemy.testing.suite.test_cte import CTETest as _CTETest
from sqlalchemy.testing.suite.test_ddl import TableDDLTest as _TableDDLTest
from sqlalchemy.testing.suite.test_ddl import (
LongNameBlowoutTest as _LongNameBlowoutTest,
)
from sqlalchemy.testing.suite.test_dialect import EscapingTest as _EscapingTest
from sqlalchemy.testing.suite.test_select import ExistsTest as _ExistsTest
from sqlalchemy.testing.suite.test_types import BooleanTest as _BooleanTest
from sqlalchemy.testing.suite.test_types import IntegerTest as _IntegerTest
from sqlalchemy.testing.suite.test_types import ( # noqa: F401, F403
DateTest as _DateTest,
DateTimeHistoricTest,
DateTimeCoercedToDateTimeTest as _DateTimeCoercedToDateTimeTest,
DateTimeMicrosecondsTest as _DateTimeMicrosecondsTest,
DateTimeTest as _DateTimeTest,
TimeTest as _TimeTest,
TimeMicrosecondsTest as _TimeMicrosecondsTest,
TimestampMicrosecondsTest,
)
config.test_schema = ""
def drop_all_tables(metadata, bind):
"""
SPANNER OVERRIDE:
Spanner DBAPI does not execute DDL statements unless followed by a
non DDL statement, which is preventing correct table clean up.
Here need to call the method `connection.run_prior_DDL_statements()`
manually after `DROP TABLE` executed, especially when `autocommit=False`.
"""
engines.testing_reaper.close_all()
if hasattr(bind, "close"):
bind.close()
if not config.db.dialect.supports_alter:
from . import assertions
with assertions.expect_warnings("Can't sort tables", assert_=False):
metadata.drop_all(bind)
else:
metadata.drop_all(bind)
list_connection = list(engines.testing_reaper.conns)
if list_connection:
list_connection[0][0].run_prior_DDL_statements()
engines.drop_all_tables = drop_all_tables
class EscapingTest(_EscapingTest):
@provide_metadata
def test_percent_sign_round_trip(self):
"""Test that the DBAPI accommodates for escaped / nonescaped
percent signs in a way that matches the compiler
SPANNER OVERRIDE
Cloud Spanner supports tables with empty primary key, but
only single one row can be inserted into such a table -
following insertions will fail with `Row [] already exists".
Overriding the test to avoid the same failure.
"""
m = self.metadata
t = Table("t", m, Column("data", String(50)))
t.create(config.db)
with config.db.begin() as conn:
conn.execute(t.insert(), dict(data="some % value"))
eq_(
conn.scalar(
select([t.c.data]).where(
t.c.data == literal_column("'some % value'")
)
),
"some % value",
)
conn.execute(t.delete())
conn.execute(t.insert(), dict(data="some %% other value"))
eq_(
conn.scalar(
select([t.c.data]).where(
t.c.data == literal_column("'some %% other value'")
)
),
"some %% other value",
)
class CTETest(_CTETest):
@pytest.mark.skip("INSERT from WITH subquery is not supported")
def test_insert_from_select_round_trip(self):
"""
The test checks if an INSERT can be done from a cte, like:
WITH some_cte AS (...)
INSERT INTO some_other_table (... SELECT * FROM some_cte)
Such queries are not supported by Spanner.
"""
pass
@pytest.mark.skip("DELETE from WITH subquery is not supported")
def test_delete_scalar_subq_round_trip(self):
"""
The test checks if a DELETE can be done from a cte, like:
WITH some_cte AS (...)
DELETE FROM some_other_table (... SELECT * FROM some_cte)
Such queries are not supported by Spanner.
"""
pass
@pytest.mark.skip("DELETE from WITH subquery is not supported")
def test_delete_from_round_trip(self):
"""
The test checks if a DELETE can be done from a cte, like:
WITH some_cte AS (...)
DELETE FROM some_other_table (... SELECT * FROM some_cte)
Such queries are not supported by Spanner.
"""
pass
@pytest.mark.skip("UPDATE from WITH subquery is not supported")
def test_update_from_round_trip(self):
"""
The test checks if an UPDATE can be done from a cte, like:
WITH some_cte AS (...)
UPDATE some_other_table
SET (... SELECT * FROM some_cte)
Such queries are not supported by Spanner.
"""
pass
@pytest.mark.skip("WITH RECURSIVE subqueries are not supported")
def test_select_recursive_round_trip(self):
pass
class BooleanTest(_BooleanTest):
def test_render_literal_bool(self):
"""
SPANNER OVERRIDE:
Cloud Spanner supports tables with an empty primary key, but
only a single row can be inserted into such a table -
following insertions will fail with `Row [] already exists".
Overriding the test to avoid the same failure.
"""
self._literal_round_trip(Boolean(), [True], [True])
self._literal_round_trip(Boolean(), [False], [False])
class ExistsTest(_ExistsTest):
def test_select_exists(self, connection):
"""
SPANNER OVERRIDE:
The original test is trying to execute a query like:
SELECT ...
WHERE EXISTS (SELECT ...)
SELECT WHERE without FROM clause is not supported by Spanner.
Rewriting the test to force it to generate a query like:
SELECT EXISTS (SELECT ...)
"""
stuff = self.tables.stuff
eq_(
connection.execute(
select((exists().where(stuff.c.data == "some data"),))
).fetchall(),
[(True,)],
)
def test_select_exists_false(self, connection):
"""
SPANNER OVERRIDE:
The original test is trying to execute a query like:
SELECT ...
WHERE EXISTS (SELECT ...)
SELECT WHERE without FROM clause is not supported by Spanner.
Rewriting the test to force it to generate a query like:
SELECT EXISTS (SELECT ...)
"""
stuff = self.tables.stuff
eq_(
connection.execute(
select((exists().where(stuff.c.data == "no data"),))
).fetchall(),
[(False,)],
)
class TableDDLTest(_TableDDLTest):
@pytest.mark.skip(
"Spanner table name must start with an uppercase or lowercase letter"
)
def test_underscore_names(self):
pass
@pytest.mark.skip("Max identifier length in Spanner is 128")
class LongNameBlowoutTest(_LongNameBlowoutTest):
pass
class DateTest(_DateTest):
def test_round_trip(self):
"""
SPANNER OVERRIDE:
Cloud Spanner supports tables with an empty primary key, but only one
row can be inserted into such a table - following insertions will fail
with `400 id must not be NULL in table date_table`.
Overriding the tests to add a manual primary key value to avoid the same
failures.
"""
date_table = self.tables.date_table
config.db.execute(date_table.insert(), {"id": 1, "date_data": self.data})
row = config.db.execute(select([date_table.c.date_data])).first()
compare = self.compare or self.data
eq_(row, (compare,))
assert isinstance(row[0], type(compare))
def test_null(self):
"""
SPANNER OVERRIDE:
Cloud Spanner supports tables with an empty primary key, but only one
row can be inserted into such a table - following insertions will fail
with `400 id must not be NULL in table date_table`.
Overriding the tests to add a manual primary key value to avoid the same
failures.
"""
date_table = self.tables.date_table
config.db.execute(date_table.insert(), {"id": 1, "date_data": None})
row = config.db.execute(select([date_table.c.date_data])).first()
eq_(row, (None,))
@requires.standalone_null_binds_whereclause
def test_null_bound_comparison(self):
"""
SPANNER OVERRIDE:
Cloud Spanner supports tables with an empty primary key, but only one
row can be inserted into such a table - following insertions will fail
with `400 id must not be NULL in table date_table`.
Overriding the tests to add a manual primary key value to avoid the same
failures.
"""
# this test is based on an Oracle issue observed in #4886.
# passing NULL for an expression that needs to be interpreted as
# a certain type, does the DBAPI have the info it needs to do this.
date_table = self.tables.date_table
with config.db.connect() as conn:
result = conn.execute(
date_table.insert(), {"id": 1, "date_data": self.data}
)
id_ = result.inserted_primary_key[0]
stmt = select([date_table.c.id]).where(
case(
[
(
bindparam("foo", type_=self.datatype)
!= None, # noqa: E711,
bindparam("foo", type_=self.datatype),
)
],
else_=date_table.c.date_data,
)
== date_table.c.date_data
)
row = conn.execute(stmt, {"foo": None}).first()
eq_(row[0], id_)
class DateTimeMicrosecondsTest(_DateTimeMicrosecondsTest):
@classmethod
def define_tables(cls, metadata):
"""
SPANNER OVERRIDE:
Spanner is not able cleanup data and drop the table correctly,
table already exists after related tests finished, so it doesn't
create a new table and insertions for tests for other data types
will fail with `400 Invalid value for column date_data in
table date_table: Expected DATE`.
Overriding the tests to create a new table for tests to avoid the same
failures.
"""
Table(
"datetime_table",
metadata,
Column("id", Integer, primary_key=True, test_needs_autoincrement=True),
Column("date_data", cls.datatype),
)
def test_null(self):
"""
SPANNER OVERRIDE:
Cloud Spanner supports tables with an empty primary key, but only one
row can be inserted into such a table - following insertions will fail
with `400 id must not be NULL in table datetime_table`.
Overriding the tests to add a manual primary key value to avoid the same
failures.
"""
date_table = self.tables.datetime_table
config.db.execute(date_table.insert(), {"id": 1, "date_data": None})
row = config.db.execute(select([date_table.c.date_data])).first()
eq_(row, (None,))
def test_round_trip(self):
"""
SPANNER OVERRIDE:
Cloud Spanner supports tables with an empty primary key, but only one
row can be inserted into such a table - following insertions will fail
with `400 id must not be NULL in table datetime_table`.
Overriding the tests to add a manual primary key value to avoid the same
failures.
Spanner converts timestamp into `%Y-%m-%dT%H:%M:%S.%fZ` format, so to avoid
assert failures convert datetime input to the desire timestamp format.
"""
date_table = self.tables.datetime_table
config.db.execute(date_table.insert(), {"id": 1, "date_data": self.data})
row = config.db.execute(select([date_table.c.date_data])).first()
compare = self.compare or self.data
compare = compare.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
eq_(row[0].rfc3339(), compare)
assert isinstance(row[0], DatetimeWithNanoseconds)
@requires.standalone_null_binds_whereclause
def test_null_bound_comparison(self):
"""
SPANNER OVERRIDE:
Cloud Spanner supports tables with an empty primary key, but only one
row can be inserted into such a table - following insertions will fail
with `400 id must not be NULL in table datetime_table`.
Overriding the tests to add a manual primary key value to avoid the same
failures.
"""
# this test is based on an Oracle issue observed in #4886.
# passing NULL for an expression that needs to be interpreted as
# a certain type, does the DBAPI have the info it needs to do this.
date_table = self.tables.datetime_table
with config.db.connect() as conn:
result = conn.execute(
date_table.insert(), {"id": 1, "date_data": self.data}
)
id_ = result.inserted_primary_key[0]
stmt = select([date_table.c.id]).where(
case(
[
(
bindparam("foo", type_=self.datatype)
!= None, # noqa: E711,
bindparam("foo", type_=self.datatype),
)
],
else_=date_table.c.date_data,
)
== date_table.c.date_data
)
row = conn.execute(stmt, {"foo": None}).first()
eq_(row[0], id_)
class DateTimeTest(_DateTimeTest, DateTimeMicrosecondsTest):
"""
SPANNER OVERRIDE:
DateTimeTest tests have the same failures same as DateTimeMicrosecondsTest tests,
so to avoid those failures and maintain DRY concept just inherit the class to run
tests successfully.
"""
pass
@pytest.mark.skip("Spanner doesn't support Time data type.")
class TimeTests(_TimeMicrosecondsTest, _TimeTest):
pass
@pytest.mark.skip("Spanner doesn't coerce dates from datetime.")
class DateTimeCoercedToDateTimeTest(_DateTimeCoercedToDateTimeTest):
pass
class IntegerTest(_IntegerTest):
@provide_metadata
def _round_trip(self, datatype, data):
"""
SPANNER OVERRIDE:
This is the helper method for integer class tests which creates a table and
performs an insert operation.
Cloud Spanner supports tables with an empty primary key, but only one
row can be inserted into such a table - following insertions will fail with
`400 id must not be NULL in table date_table`.
Overriding the tests and adding a manual primary key value to avoid the same
failures and deleting the table at the end.
"""
metadata = self.metadata
int_table = Table(
"integer_table",
metadata,
Column("id", Integer, primary_key=True, test_needs_autoincrement=True),
Column("integer_data", datatype),
)
metadata.create_all(config.db)
config.db.execute(int_table.insert(), {"id": 1, "integer_data": data})
row = config.db.execute(select([int_table.c.integer_data])).first()
eq_(row, (data,))
if util.py3k:
assert isinstance(row[0], int)
else:
assert isinstance(row[0], (long, int)) # noqa
config.db.execute(int_table.delete())
@provide_metadata
def _literal_round_trip(self, type_, input_, output, filter_=None):
"""
SPANNER OVERRIDE:
Spanner DBAPI does not execute DDL statements unless followed by a
non DDL statement, which is preventing correct table clean up.
The table already exists after related tests finish, so it doesn't
create a new table and when running tests for other data types
insertions will fail with `400 Duplicate name in schema: t`.
Overriding the tests to create and drop a new table to prevent
database existence errors.
"""
# for literal, we test the literal render in an INSERT
# into a typed column. we can then SELECT it back as its
# official type; ideally we'd be able to use CAST here
# but MySQL in particular can't CAST fully
t = Table("int_t", self.metadata, Column("x", type_))
t.create()
with db.connect() as conn:
for value in input_:
ins = (
t.insert()
.values(x=literal(value))
.compile(
dialect=db.dialect, compile_kwargs=dict(literal_binds=True),
)
)
conn.execute(ins)
conn.execute("SELECT 1")
if self.supports_whereclause:
stmt = t.select().where(t.c.x == literal(value))
else:
stmt = t.select()
stmt = stmt.compile(
dialect=db.dialect, compile_kwargs=dict(literal_binds=True),
)
for row in conn.execute(stmt):
value = row[0]
if filter_ is not None:
value = filter_(value)
assert value in output