Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 25ab0c4

Browse filesBrowse files
committed
cleanup tests
1 parent 8ec3556 commit 25ab0c4
Copy full SHA for 25ab0c4

File tree

2 files changed

+33
-17
lines changed
Filter options

2 files changed

+33
-17
lines changed

‎sqlalchemy_bigquery/_json.py

Copy file name to clipboardExpand all lines: sqlalchemy_bigquery/_json.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class JSONPathMode(Enum):
8080
class JSONPathType(_FormatTypeMixin, sqltypes.JSON.JSONPathType):
8181
def _mode_prefix(self, mode):
8282
if mode == JSON.JSONPathMode.LAX:
83-
mode_prefix = "lax "
83+
mode_prefix = "lax"
8484
elif mode == JSON.JSONPathMode.LAX_RECURSIVE:
8585
mode_prefix = "lax recursive"
8686
else:
@@ -96,7 +96,7 @@ def _format_value(self, value):
9696
mode_prefix = ""
9797

9898
return "%s$%s" % (
99-
mode_prefix,
99+
mode_prefix + " " if mode_prefix else "",
100100
"".join(
101101
[
102102
"[%s]" % elem if isinstance(elem, int) else '."%s"' % elem

‎tests/unit/test__json.py

Copy file name to clipboardExpand all lines: tests/unit/test__json.py
+31-15Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,29 @@ def test_insert_json(faux_conn, metadata, json_table, json_data):
4646

4747

4848
@pytest.mark.parametrize(
49-
"path,sql,literal_sql",
49+
"path,literal_sql",
5050
(
5151
(
5252
["name"],
53-
"JSON_QUERY(`json_table`.`cart`, %(cart_1:STRING)s)",
5453
"JSON_QUERY(`json_table`.`cart`, '$.\"name\"')",
5554
),
5655
(
5756
["items", 0],
58-
"JSON_QUERY(`json_table`.`cart`, %(cart_1:STRING)s)",
5957
"JSON_QUERY(`json_table`.`cart`, '$.\"items\"[0]')",
6058
),
6159
(
6260
["items", 0, "price"],
63-
"JSON_QUERY(`json_table`.`cart`, %(cart_1:STRING)s)",
6461
'JSON_QUERY(`json_table`.`cart`, \'$."items"[0]."price"\')',
6562
),
6663
),
6764
)
68-
def test_json_query(faux_conn, json_column, path, sql, literal_sql):
65+
def test_json_query(faux_conn, json_column, path, literal_sql):
6966
expr = sqlalchemy.select(json_column[path])
7067

71-
expected_sql = f"SELECT {sql} AS `anon_1` \nFROM `json_table`"
68+
expected_sql = (
69+
"SELECT JSON_QUERY(`json_table`.`cart`, %(cart_1:STRING)s) AS `anon_1` \n"
70+
"FROM `json_table`"
71+
)
7272
expected_literal_sql = f"SELECT {literal_sql} AS `anon_1` \nFROM `json_table`"
7373

7474
actual_sql = expr.compile(faux_conn).string
@@ -85,8 +85,16 @@ def test_json_value(faux_conn, json_column, json_data):
8585
sqlalchemy.func.JSON_VALUE(json_column[["name"]]) == "Alice"
8686
)
8787

88-
expected_sql = "SELECT JSON_QUERY(`json_table`.`cart`, %(cart_1:STRING)s) AS `first_item` \nFROM `json_table` \nWHERE JSON_VALUE(JSON_QUERY(`json_table`.`cart`, %(cart_2:STRING)s)) = %(JSON_VALUE_1:STRING)s"
89-
expected_literal_sql = "SELECT JSON_QUERY(`json_table`.`cart`, '$.\"items\"[0]') AS `first_item` \nFROM `json_table` \nWHERE JSON_VALUE(JSON_QUERY(`json_table`.`cart`, '$.\"name\"')) = 'Alice'"
88+
expected_sql = (
89+
"SELECT JSON_QUERY(`json_table`.`cart`, %(cart_1:STRING)s) AS `first_item` \n"
90+
"FROM `json_table` \n"
91+
"WHERE JSON_VALUE(JSON_QUERY(`json_table`.`cart`, %(cart_2:STRING)s)) = %(JSON_VALUE_1:STRING)s"
92+
)
93+
expected_literal_sql = (
94+
"SELECT JSON_QUERY(`json_table`.`cart`, '$.\"items\"[0]') AS `first_item` \n"
95+
"FROM `json_table` \n"
96+
"WHERE JSON_VALUE(JSON_QUERY(`json_table`.`cart`, '$.\"name\"')) = 'Alice'"
97+
)
9098

9199
actual_sql = expr.compile(faux_conn).string
92100
actual_literal_sql = expr.compile(
@@ -118,12 +126,10 @@ def test_json_literal(faux_conn):
118126
assert expected_literal_sql == actual_literal_sql
119127

120128

121-
@pytest.mark.parametrize("lax", (False, True))
122-
def test_json_casts(faux_conn, json_column, json_data, lax):
129+
@pytest.mark.parametrize("lax,prefix", ((False, ""), (True, "LAX_")))
130+
def test_json_casts(faux_conn, json_column, json_data, lax, prefix):
123131
from sqlalchemy_bigquery import JSON
124132

125-
prefix = "LAX_" if lax else "" # FIXME: Manually parameterize
126-
127133
expr = sqlalchemy.select(1).where(
128134
json_column[["name"]].as_string(lax=lax) == "Alice"
129135
)
@@ -157,13 +163,23 @@ def test_json_casts(faux_conn, json_column, json_data, lax):
157163
)
158164

159165

160-
def test_json_path_mode(faux_conn, json_column):
166+
@pytest.mark.parametrize(
167+
"mode,prefix", ((None, ""), ("LAX", "lax "), ("LAX_RECURSIVE", "lax recursive "))
168+
)
169+
def test_json_path_mode(faux_conn, json_column, mode, prefix):
161170
from sqlalchemy_bigquery import JSON
162171

163-
expr = sqlalchemy.select(json_column[[JSON.JSONPathMode.LAX, "items", "price"]])
172+
if mode == "LAX":
173+
path = [JSON.JSONPathMode.LAX, "items", "price"]
174+
elif mode == "LAX_RECURSIVE":
175+
path = [JSON.JSONPathMode.LAX_RECURSIVE, "items", "price"]
176+
else:
177+
path = ["items", "price"]
178+
179+
expr = sqlalchemy.select(json_column[path])
164180

165181
expected_literal_sql = (
166-
'SELECT JSON_QUERY(`json_table`.`cart`, \'lax $."items"."price"\') AS `anon_1` \n'
182+
f'SELECT JSON_QUERY(`json_table`.`cart`, \'{prefix}$."items"."price"\') AS `anon_1` \n'
167183
"FROM `json_table`"
168184
)
169185
actual_literal_sql = expr.compile(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.