Skip to content

Commit 91c02e7

Browse files
committed
corrections to tests and addition of 2.0 conditionals
1 parent 1fa737b commit 91c02e7

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

packages/sqlalchemy-bigquery/tests/unit/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import sqlalchemy
2727

2828
from sqlalchemy_bigquery.base import BigQueryDDLCompiler, BigQueryDialect
29+
from sqlalchemy.sql.compiler import Compiled
2930

3031
from . import fauxdbi
3132

@@ -69,6 +70,18 @@ def ex(sql, *args, **kw):
6970

7071
ex("create table comments" " (key string primary key, comment string)")
7172

73+
# Modernize faux_conn for SQLAlchemy 2.0+ by allowing it to execute
74+
# Compiled objects (common in this test suite)
75+
if sqlalchemy_version >= packaging.version.parse("2.0"):
76+
original_execute = conn.execute
77+
78+
def execute(statement, *args, **kw):
79+
if isinstance(statement, Compiled):
80+
statement = sqlalchemy.text(str(statement))
81+
return original_execute(statement, *args, **kw)
82+
83+
conn.execute = execute
84+
7285
yield conn
7386
conn.close()
7487

packages/sqlalchemy-bigquery/tests/unit/test__struct.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
1717
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1818
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
1920
import datetime
2021

22+
import packaging.version
2123
import pytest
2224

2325
import sqlalchemy
2426

2527

28+
sqlalchemy_version = packaging.version.parse(sqlalchemy.__version__)
29+
SQL_2_0 = sqlalchemy_version >= packaging.version.parse("2.0")
30+
31+
2632
def _test_struct():
2733
from sqlalchemy_bigquery import STRUCT
2834

@@ -90,9 +96,24 @@ def test_struct_traversal_project(faux_conn, expr, sql):
9096
@pytest.mark.parametrize(
9197
"expr,sql",
9298
[
93-
(_col()["name"] == "x", "(`t`.`person`.name) = %(param_1:STRING)s"),
94-
(_col()["Name"] == "x", "(`t`.`person`.Name) = %(param_1:STRING)s"),
95-
(_col().NAME == "x", "(`t`.`person`.NAME) = %(param_1:STRING)s"),
99+
(
100+
_col()["name"] == "bob",
101+
"(`t`.`person`.name) = %(param_1:STRING)s"
102+
if not SQL_2_0
103+
else "`t`.`person`.name = %(param_1:STRING)s",
104+
),
105+
(
106+
_col()["Name"] == "bob",
107+
"(`t`.`person`.Name) = %(param_1:STRING)s"
108+
if not SQL_2_0
109+
else "`t`.`person`.Name = %(param_1:STRING)s",
110+
),
111+
(
112+
_col()["NAME"] == "bob",
113+
"(`t`.`person`.NAME) = %(param_1:STRING)s"
114+
if not SQL_2_0
115+
else "`t`.`person`.NAME = %(param_1:STRING)s",
116+
),
96117
(
97118
_col().children[0] == dict(name="foo", bdate=datetime.date(2020, 1, 1)),
98119
"(`t`.`person`.children)[OFFSET(%(param_1:INT64)s)]"
@@ -106,11 +127,17 @@ def test_struct_traversal_project(faux_conn, expr, sql):
106127
(
107128
_col().children[0]["bdate"] == datetime.date(2021, 8, 30),
108129
"(((`t`.`person`.children)[OFFSET(%(param_1:INT64)s)]).bdate)"
130+
" = %(param_2:DATE)s"
131+
if not SQL_2_0
132+
else "((`t`.`person`.children)[OFFSET(%(param_1:INT64)s)]).bdate"
109133
" = %(param_2:DATE)s",
110134
),
111135
(
112136
_col().children[0].bdate == datetime.date(2021, 8, 30),
113137
"(((`t`.`person`.children)[OFFSET(%(param_1:INT64)s)]).bdate)"
138+
" = %(param_2:DATE)s"
139+
if not SQL_2_0
140+
else "((`t`.`person`.children)[OFFSET(%(param_1:INT64)s)]).bdate"
114141
" = %(param_2:DATE)s",
115142
),
116143
],

packages/sqlalchemy-bigquery/tests/unit/test_compliance.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def test_distinct_selectable_in_unions(faux_conn):
5656
s2 = select(table).where(table.c.id == 3).distinct()
5757

5858
u1 = union(s1, s2).limit(2)
59-
assert_result(faux_conn, u1.order_by(u1.c.id), [(2, 2, 3), (3, 3, 4)])
59+
assert_result(
60+
faux_conn, u1.order_by(u1.selected_columns.id), [(2, 2, 3), (3, 3, 4)]
61+
)
6062

6163

6264
def test_limit_offset_aliased_selectable_in_unions(faux_conn):
@@ -79,7 +81,9 @@ def test_limit_offset_aliased_selectable_in_unions(faux_conn):
7981
)
8082

8183
u1 = union(s1, s2).limit(2)
82-
assert_result(faux_conn, u1.order_by(u1.c.id), [(2, 2, 3), (3, 3, 4)])
84+
assert_result(
85+
faux_conn, u1.order_by(u1.selected_columns.id), [(2, 2, 3), (3, 3, 4)]
86+
)
8387

8488

8589
def test_percent_sign_round_trip(faux_conn, metadata):

0 commit comments

Comments
 (0)