Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Untyped param #1001

Merged
merged 13 commits into from
Feb 13, 2024
Prev Previous commit
Next Next commit
tests
  • Loading branch information
asthamohta committed Aug 17, 2023
commit e01d9b4f6ab8f63a85ff85f1aa8949b167b24021
50 changes: 29 additions & 21 deletions tests/system/test_session_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
"jsonb_array",
)

QUERY_ALL_TYPES_COLUMNS = LIVE_ALL_TYPES_COLUMNS[1:17:2]

AllTypesRowData = collections.namedtuple("AllTypesRowData", LIVE_ALL_TYPES_COLUMNS)
AllTypesRowData.__new__.__defaults__ = tuple([None for colum in LIVE_ALL_TYPES_COLUMNS])
EmulatorAllTypesRowData = collections.namedtuple(
Expand Down Expand Up @@ -211,6 +213,17 @@
PostGresAllTypesRowData(pkey=309, jsonb_array=[JSON_1, JSON_2, None]),
)

QUERY_ALL_TYPES_DATA = (
123,
False,
BYTES_1,
SOME_DATE,
1.4142136,
"VALUE",
SOME_TIME,
NUMERIC_1,
)

if _helpers.USE_EMULATOR:
ALL_TYPES_COLUMNS = EMULATOR_ALL_TYPES_COLUMNS
ALL_TYPES_ROWDATA = EMULATOR_ALL_TYPES_ROWDATA
Expand Down Expand Up @@ -476,6 +489,22 @@ def test_batch_insert_or_update_then_query(sessions_database):
sd._check_rows_data(rows)


def test_batch_insert_then_read_wo_param_types(sessions_database, database_dialect, not_emulator):
sd = _sample_data

with sessions_database.batch() as batch:
batch.delete(ALL_TYPES_TABLE, sd.ALL)
batch.insert(ALL_TYPES_TABLE, ALL_TYPES_COLUMNS, ALL_TYPES_ROWDATA)

with sessions_database.snapshot(multi_use=True) as snapshot:
for column_type, value in list(zip(QUERY_ALL_TYPES_COLUMNS, QUERY_ALL_TYPES_DATA)):
placeholder = "$1" if database_dialect == DatabaseDialect.POSTGRESQL else f"@value"
sql = 'SELECT * FROM `' + ALL_TYPES_TABLE + '` WHERE ' + column_type + ' = ' + placeholder
param = {"p1": value} if database_dialect == DatabaseDialect.POSTGRESQL else {"value": value}
rows = list(snapshot.execute_sql(sql,params=param))
assert len(rows) == 1


def test_batch_insert_w_commit_timestamp(sessions_database, not_postgres):
table = "users_history"
columns = ["id", "commit_ts", "name", "email", "deleted"]
Expand Down Expand Up @@ -2027,7 +2056,6 @@ def _bind_test_helper(
array_value,
expected_array_value=None,
recurse_into_lists=True,
test_untyped_param=False,
):
database.snapshot(multi_use=True)

Expand Down Expand Up @@ -2097,17 +2125,6 @@ def _bind_test_helper(
recurse_into_lists=recurse_into_lists,
)

# Bind without paramtype of <type_name>
if test_untyped_param:
_check_sql_results(
database,
sql=f"SELECT {placeholder}",
params={key: single_value},
expected=[(single_value,)],
order=False,
recurse_into_lists=recurse_into_lists,
)


def test_execute_sql_w_string_bindings(sessions_database, database_dialect):
_bind_test_helper(
Expand All @@ -2116,7 +2133,6 @@ def test_execute_sql_w_string_bindings(sessions_database, database_dialect):
spanner_v1.param_types.STRING,
"Phred",
["Phred", "Bharney"],
test_untyped_param=True,
)


Expand All @@ -2127,7 +2143,6 @@ def test_execute_sql_w_bool_bindings(sessions_database, database_dialect):
spanner_v1.param_types.BOOL,
True,
[True, False, True],
test_untyped_param=True,
)


Expand All @@ -2138,7 +2153,6 @@ def test_execute_sql_w_int64_bindings(sessions_database, database_dialect):
spanner_v1.param_types.INT64,
42,
[123, 456, 789],
test_untyped_param=True,
)


Expand All @@ -2149,7 +2163,6 @@ def test_execute_sql_w_float64_bindings(sessions_database, database_dialect):
spanner_v1.param_types.FLOAT64,
42.3,
[12.3, 456.0, 7.89],
test_untyped_param=True,
)


Expand Down Expand Up @@ -2187,7 +2200,6 @@ def test_execute_sql_w_bytes_bindings(sessions_database, database_dialect):
spanner_v1.param_types.BYTES,
b"DEADBEEF",
[b"FACEDACE", b"DEADBEEF"],
test_untyped_param=True,
)


Expand All @@ -2214,7 +2226,6 @@ def test_execute_sql_w_timestamp_bindings(sessions_database, database_dialect):
timestamps,
expected_timestamps,
recurse_into_lists=False,
test_untyped_param=True,
)


Expand All @@ -2226,7 +2237,6 @@ def test_execute_sql_w_date_bindings(sessions_database, not_postgres, database_d
spanner_v1.param_types.DATE,
SOME_DATE,
dates,
test_untyped_param=True,
)


Expand All @@ -2240,7 +2250,6 @@ def test_execute_sql_w_numeric_bindings(
spanner_v1.param_types.PG_NUMERIC,
NUMERIC_1,
[NUMERIC_1, NUMERIC_2],
test_untyped_param=True,
)
else:
_bind_test_helper(
Expand All @@ -2249,7 +2258,6 @@ def test_execute_sql_w_numeric_bindings(
spanner_v1.param_types.NUMERIC,
NUMERIC_1,
[NUMERIC_1, NUMERIC_2],
test_untyped_param=True,
)


Expand Down