Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 132 additions & 1 deletion spanner/tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ class TestSessionAPI(unittest.TestCase, _TestData):
([3, None, 9], False, None, None, float('-inf'), 207, u'bat', None),
([], False, None, None, float('nan'), 1207, u'owl', None),
([], False, None, None, OTHER_NAN, 2000, u'virus', NANO_TIME),
(None, None, None, None, None, 5432, u'algae', None),
)

@classmethod
Expand Down Expand Up @@ -1674,15 +1675,129 @@ def test_execute_sql_w_query_param(self):
expected=[(19,), (99,)],
)

def test_execute_sql_w_query_param_no_table(self):

self._db.snapshot(multi_use=True)

# Hello, world query
self._check_sql_results(
self._db,
sql='SELECT 1',
params=None,
param_types=None,
expected=[(1,)],
order=False,
)

def _bind_test_helper(
self,
type_name,
single_value,
array_value,
expected_array_value=None,
):

self._db.snapshot(multi_use=True)

# Bind a non-null <type_name>
self._check_sql_results(
self._db,
sql='SELECT @v',
params={'v': single_value},
param_types={'v': Type(code=type_name)},
expected=[(single_value,)],
order=False,
)

# Bind a null <type_name>
self._check_sql_results(
self._db,
sql='SELECT @v',
params={'v': None},
param_types={'v': Type(code=STRING)},
param_types={'v': Type(code=type_name)},
expected=[(None,)],
order=False,
)

# Bind an array of <type_name>
array_type = Type(
code=ARRAY, array_element_type=Type(code=type_name))

if expected_array_value is None:
expected_array_value = array_value

self._check_sql_results(
self._db,
sql='SELECT @v',
params={'v': array_value},
param_types={'v': array_type},
expected=[(expected_array_value,)],
order=False,
)

# Bind an empty array of <type_name>
self._check_sql_results(
self._db,
sql='SELECT @v',
params={'v': []},
param_types={'v': array_type},
expected=[([],)],
order=False,
)

# Bind a null array of <type_name>
self._check_sql_results(
self._db,
sql='SELECT @v',
params={'v': None},
param_types={'v': array_type},
expected=[(None,)],
order=False,
)

def test_execute_sql_w_string_bindings(self):
self._bind_test_helper(STRING, 'Phred', ['Phred', 'Bharney'])

def test_execute_sql_w_bool_bindings(self):
self._bind_test_helper(BOOL, True, [True, False, True])

def test_execute_sql_w_int64_bindings(self):
self._bind_test_helper(INT64, 42, [123, 456, 789])

def test_execute_sql_w_float64_bindings(self):
self._bind_test_helper(FLOAT64, 42.3, [12.3, 456.0, 7.89])

def test_execute_sql_w_bytes_bindings(self):
self._bind_test_helper(BYTES, b'DEADBEEF', [b'FACEDACE', b'DEADBEEF'])

def test_execute_sql_w_timestamp_bindings(self):
import pytz
from google.api_core.datetime_helpers import DatetimeWithNanoseconds

timestamp_1 = DatetimeWithNanoseconds(
1989, 1, 17, 17, 59, 12, nanosecond=345612789)

timestamp_2 = DatetimeWithNanoseconds(
1989, 1, 17, 17, 59, 13, nanosecond=456127893)

timestamps = [timestamp_1, timestamp_2]

# In round-trip, timestamps acquire a timezone value.
expected_timestamps = [
timestamp.replace(tzinfo=pytz.UTC) for timestamp in timestamps]

self._bind_test_helper(
TIMESTAMP, timestamp_1, timestamps, expected_timestamps)

def test_execute_sql_w_date_bindings(self):
import datetime

dates = [
self.SOME_DATE,
self.SOME_DATE + datetime.timedelta(days=1),
]
self._bind_test_helper(DATE, self.SOME_DATE, dates)

def test_execute_sql_w_query_param_transfinite(self):
with self._db.batch() as batch:
batch.delete(self.ALL_TYPES_TABLE, self.ALL)
Expand All @@ -1709,6 +1824,22 @@ def test_execute_sql_w_query_param_transfinite(self):
expected=[(107,)],
)

# Query returning -inf, +inf, NaN as column values
with self._db.snapshot(
read_timestamp=batch.committed,
multi_use=True) as snapshot:
rows = list(snapshot.execute_sql(
'SELECT '
'CAST("-inf" AS FLOAT64), '
'CAST("+inf" AS FLOAT64), '
'CAST("NaN" AS FLOAT64)'))
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0][0], float('-inf'))
self.assertEqual(rows[0][1], float('+inf'))
# NaNs cannot be compared by equality.
self.assertTrue(math.isnan(rows[0][2]))

# Query returning array of -inf, +inf, NaN as one column
with self._db.snapshot(
read_timestamp=batch.committed,
multi_use=True) as snapshot:
Expand Down