Skip to content
Permalink
Browse files
fix: handle Unmergable errors when merging struct responses (#152)
Co-authored-by: larkee <larkee@users.noreply.github.com>
  • Loading branch information
larkee and larkee committed Oct 13, 2020
1 parent ff97fad commit d132409dd4300cb2dca7c4bc7dbdd4d429d2fa7c
Showing with 27 additions and 1 deletion.
  1. +7 −1 google/cloud/spanner_v1/streamed.py
  2. +20 −0 tests/unit/test_streamed.py
@@ -286,7 +286,13 @@ def _merge_struct(lhs, rhs, type_):
lhs.append(first)
else:
last = lhs.pop()
lhs.append(_merge_by_type(last, first, candidate_type))
try:
merged = _merge_by_type(last, first, candidate_type)
except Unmergeable:
lhs.append(last)
lhs.append(first)
else:
lhs.append(merged)
return Value(list_value=ListValue(values=lhs + rhs))


@@ -448,6 +448,26 @@ def test__merge_chunk_array_of_struct_unmergeable(self):
self.assertEqual(merged, expected)
self.assertIsNone(streamed._pending_chunk)

def test__merge_chunk_array_of_struct_unmergeable_split(self):
iterator = _MockCancellableIterator()
streamed = self._make_one(iterator)
struct_type = self._make_struct_type(
[("name", "STRING"), ("height", "FLOAT64"), ("eye_color", "STRING")]
)
FIELDS = [self._make_array_field("test", element_type=struct_type)]
streamed._metadata = self._make_result_set_metadata(FIELDS)
partial = self._make_list_value([u"Phred Phlyntstone", 1.65])
streamed._pending_chunk = self._make_list_value(value_pbs=[partial])
rest = self._make_list_value(["brown"])
chunk = self._make_list_value(value_pbs=[rest])

merged = streamed._merge_chunk(chunk)

struct = self._make_list_value([u"Phred Phlyntstone", 1.65, "brown"])
expected = self._make_list_value(value_pbs=[struct])
self.assertEqual(merged, expected)
self.assertIsNone(streamed._pending_chunk)

def test_merge_values_empty_and_empty(self):
iterator = _MockCancellableIterator()
streamed = self._make_one(iterator)

0 comments on commit d132409

Please sign in to comment.