Skip to content

Commit 2bf694b

Browse files
authored
Add spanner system tests: (googleapis#4371)
- Bind timestamp. - Bind string array. - Bind empty string array. - Bind string to null. - Query returning array of -Inf, +Inf, NaN.
1 parent c353f2b commit 2bf694b

File tree

1 file changed

+91
-27
lines changed

1 file changed

+91
-27
lines changed

spanner/tests/system/test_system.py

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ class TestSessionAPI(unittest.TestCase, _TestData):
399399
([1], True, BYTES_1, SOME_DATE, 0.0, 19, u'dog', SOME_TIME),
400400
([5, 10], True, BYTES_1, None, 1.25, 99, u'cat', None),
401401
([], False, BYTES_2, None, float('inf'), 107, u'frog', None),
402-
([3, None, 9], False, None, None, float('-inf'), 207, None, None),
403-
([], False, None, None, float('nan'), 1207, None, None),
404-
([], False, None, None, OTHER_NAN, 2000, None, NANO_TIME),
402+
([3, None, 9], False, None, None, float('-inf'), 207, u'bat', None),
403+
([], False, None, None, float('nan'), 1207, u'owl', None),
404+
([], False, None, None, OTHER_NAN, 2000, u'virus', NANO_TIME),
405405
)
406406

407407
@classmethod
@@ -956,8 +956,9 @@ def test_execute_sql_w_manual_consume(self):
956956
self.assertEqual(streamed._current_row, [])
957957
self.assertEqual(streamed._pending_chunk, None)
958958

959-
def _check_sql_results(self, snapshot, sql, params, param_types, expected):
960-
if 'ORDER' not in sql:
959+
def _check_sql_results(
960+
self, snapshot, sql, params, param_types, expected, order=True):
961+
if order and 'ORDER' not in sql:
961962
sql += ' ORDER BY eye_d'
962963
rows = list(snapshot.execute_sql(
963964
sql, params=params, param_types=param_types))
@@ -1041,6 +1042,14 @@ def test_execute_sql_w_query_param(self):
10411042
expected=[(19,)],
10421043
)
10431044

1045+
self._check_sql_results(
1046+
snapshot,
1047+
sql='SELECT eye_d FROM all_types WHERE exactly_hwhen = @hwhen',
1048+
params={'hwhen': self.SOME_TIME},
1049+
param_types={'hwhen': Type(code=TIMESTAMP)},
1050+
expected=[(19,)],
1051+
)
1052+
10441053
self._check_sql_results(
10451054
snapshot,
10461055
sql=('SELECT eye_d FROM all_types WHERE approx_value >= @lower'
@@ -1051,24 +1060,6 @@ def test_execute_sql_w_query_param(self):
10511060
expected=[(None,), (19,)],
10521061
)
10531062

1054-
# Find -inf
1055-
self._check_sql_results(
1056-
snapshot,
1057-
sql='SELECT eye_d FROM all_types WHERE approx_value = @pos_inf',
1058-
params={'pos_inf': float('+inf')},
1059-
param_types={'pos_inf': Type(code=FLOAT64)},
1060-
expected=[(107,)],
1061-
)
1062-
1063-
# Find +inf
1064-
self._check_sql_results(
1065-
snapshot,
1066-
sql='SELECT eye_d FROM all_types WHERE approx_value = @neg_inf',
1067-
params={'neg_inf': float('-inf')},
1068-
param_types={'neg_inf': Type(code=FLOAT64)},
1069-
expected=[(207,)],
1070-
)
1071-
10721063
self._check_sql_results(
10731064
snapshot,
10741065
sql='SELECT description FROM all_types WHERE eye_d = @my_id',
@@ -1093,8 +1084,6 @@ def test_execute_sql_w_query_param(self):
10931084
expected=[(19,)],
10941085
)
10951086

1096-
# NaNs cannot be searched for by equality.
1097-
10981087
self._check_sql_results(
10991088
snapshot,
11001089
sql='SELECT eye_d FROM all_types WHERE exactly_hwhen = @hwhen',
@@ -1103,16 +1092,91 @@ def test_execute_sql_w_query_param(self):
11031092
expected=[(19,)],
11041093
)
11051094

1106-
array_type = Type(code=ARRAY, array_element_type=Type(code=INT64))
1095+
int_array_type = Type(code=ARRAY, array_element_type=Type(code=INT64))
1096+
11071097
self._check_sql_results(
11081098
snapshot,
11091099
sql=('SELECT description FROM all_types '
11101100
'WHERE eye_d in UNNEST(@my_list)'),
11111101
params={'my_list': [19, 99]},
1112-
param_types={'my_list': array_type},
1102+
param_types={'my_list': int_array_type},
11131103
expected=[(u'dog',), (u'cat',)],
11141104
)
11151105

1106+
str_array_type = Type(code=ARRAY, array_element_type=Type(code=STRING))
1107+
1108+
self._check_sql_results(
1109+
snapshot,
1110+
sql=('SELECT eye_d FROM all_types '
1111+
'WHERE description in UNNEST(@my_list)'),
1112+
params={'my_list': []},
1113+
param_types={'my_list': str_array_type},
1114+
expected=[],
1115+
)
1116+
1117+
self._check_sql_results(
1118+
snapshot,
1119+
sql=('SELECT eye_d FROM all_types '
1120+
'WHERE description in UNNEST(@my_list)'),
1121+
params={'my_list': [u'dog', u'cat']},
1122+
param_types={'my_list': str_array_type},
1123+
expected=[(19,), (99,)],
1124+
)
1125+
1126+
self._check_sql_results(
1127+
snapshot,
1128+
sql='SELECT @v',
1129+
params={'v': None},
1130+
param_types={'v': Type(code=STRING)},
1131+
expected=[(None,)],
1132+
order=False,
1133+
)
1134+
1135+
def test_execute_sql_w_query_param_transfinite(self):
1136+
session = self._db.session()
1137+
session.create()
1138+
self.to_delete.append(session)
1139+
1140+
with session.batch() as batch:
1141+
batch.delete(self.ALL_TYPES_TABLE, self.ALL)
1142+
batch.insert(
1143+
self.ALL_TYPES_TABLE,
1144+
self.ALL_TYPES_COLUMNS,
1145+
self.ALL_TYPES_ROWDATA)
1146+
1147+
snapshot = session.snapshot(
1148+
read_timestamp=batch.committed, multi_use=True)
1149+
1150+
# Find -inf
1151+
self._check_sql_results(
1152+
snapshot,
1153+
sql='SELECT eye_d FROM all_types WHERE approx_value = @pos_inf',
1154+
params={'pos_inf': float('+inf')},
1155+
param_types={'pos_inf': Type(code=FLOAT64)},
1156+
expected=[(107,)],
1157+
)
1158+
1159+
# Find +inf
1160+
self._check_sql_results(
1161+
snapshot,
1162+
sql='SELECT eye_d FROM all_types WHERE approx_value = @neg_inf',
1163+
params={'neg_inf': float('-inf')},
1164+
param_types={'neg_inf': Type(code=FLOAT64)},
1165+
expected=[(207,)],
1166+
)
1167+
1168+
rows = list(snapshot.execute_sql(
1169+
'SELECT'
1170+
' [CAST("-inf" AS FLOAT64),'
1171+
' CAST("+inf" AS FLOAT64),'
1172+
' CAST("NaN" AS FLOAT64)]'))
1173+
self.assertEqual(len(rows), 1)
1174+
float_array, = rows[0]
1175+
self.assertEqual(float_array[0], float('-inf'))
1176+
self.assertEqual(float_array[1], float('+inf'))
1177+
# NaNs cannot be searched for by equality.
1178+
self.assertTrue(math.isnan(float_array[2]))
1179+
11161180

11171181
class TestStreamingChunking(unittest.TestCase, _TestData):
11181182

0 commit comments

Comments
 (0)