From 07c69edc82c902a016502fc11b826b0a62ec908b Mon Sep 17 00:00:00 2001 From: Astha Mohta Date: Wed, 16 Aug 2023 14:09:09 +0530 Subject: [PATCH 1/3] changes --- google/cloud/spanner_v1/streamed.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/google/cloud/spanner_v1/streamed.py b/google/cloud/spanner_v1/streamed.py index 80a452d558..195c7eb583 100644 --- a/google/cloud/spanner_v1/streamed.py +++ b/google/cloud/spanner_v1/streamed.py @@ -189,7 +189,12 @@ def one_or_none(self): raise ValueError("Expected one result; got more.") except StopIteration: return answer - + + def fetch_as_dictionary_list(self): + rows = [] + for row in self: + rows.append({column: value for column, value in zip([column.name for column in self._metadata.row_type.fields], row)}) + return rows class Unmergeable(ValueError): """Unable to merge two values. From 6a864c65d8874bbf49be86653356bbae46628b08 Mon Sep 17 00:00:00 2001 From: Astha Mohta Date: Thu, 17 Aug 2023 11:56:00 +0530 Subject: [PATCH 2/3] adding tests --- google/cloud/spanner_v1/streamed.py | 12 ++++++++++-- tests/system/test_session_api.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/google/cloud/spanner_v1/streamed.py b/google/cloud/spanner_v1/streamed.py index 195c7eb583..fdab8a29e0 100644 --- a/google/cloud/spanner_v1/streamed.py +++ b/google/cloud/spanner_v1/streamed.py @@ -189,13 +189,21 @@ def one_or_none(self): raise ValueError("Expected one result; got more.") except StopIteration: return answer - + def fetch_as_dictionary_list(self): rows = [] for row in self: - rows.append({column: value for column, value in zip([column.name for column in self._metadata.row_type.fields], row)}) + rows.append( + { + column: value + for column, value in zip( + [column.name for column in self._metadata.row_type.fields], row + ) + } + ) return rows + class Unmergeable(ValueError): """Unable to merge two values. diff --git a/tests/system/test_session_api.py b/tests/system/test_session_api.py index 7d58324b04..ad3a9a5ffc 100644 --- a/tests/system/test_session_api.py +++ b/tests/system/test_session_api.py @@ -1918,6 +1918,19 @@ def test_execute_sql_w_manual_consume(sessions_database): assert streamed._pending_chunk is None +def test_execute_sql_w_fetch_as_dictionary_list(sessions_database): + sd = _sample_data + row_count = 40 + _set_up_table(sessions_database, row_count) + + with sessions_database.snapshot() as snapshot: + rows = snapshot.execute_sql(sd.SQL).fetch_as_dictionary_list() + all_data_rows = list(_row_data(row_count)) + row_data = [list(row.values()) for row in rows] + sd._check_row_data(row_data, all_data_rows) + assert all(set(row.keys()) == set(sd.COLUMNS) for row in rows) + + def _check_sql_results( database, sql, From 4a435124d3b41b27aa7c00da19ed5c33fbd068e0 Mon Sep 17 00:00:00 2001 From: Astha Mohta Date: Fri, 18 Aug 2023 14:17:23 +0530 Subject: [PATCH 3/3] comment changes --- google/cloud/spanner_v1/streamed.py | 10 +++++++++- tests/system/test_session_api.py | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/google/cloud/spanner_v1/streamed.py b/google/cloud/spanner_v1/streamed.py index fdab8a29e0..ac8fc71ce6 100644 --- a/google/cloud/spanner_v1/streamed.py +++ b/google/cloud/spanner_v1/streamed.py @@ -190,7 +190,15 @@ def one_or_none(self): except StopIteration: return answer - def fetch_as_dictionary_list(self): + def to_dict_list(self): + """Return the result of a query as a list of dictionaries. + In each dictionary the key is the column name and the value is the + value of the that column in a given row. + + :rtype: + :class:`list of dict` + :returns: result rows as a list of dictionaries + """ rows = [] for row in self: rows.append( diff --git a/tests/system/test_session_api.py b/tests/system/test_session_api.py index ad3a9a5ffc..4438b1459e 100644 --- a/tests/system/test_session_api.py +++ b/tests/system/test_session_api.py @@ -1918,13 +1918,13 @@ def test_execute_sql_w_manual_consume(sessions_database): assert streamed._pending_chunk is None -def test_execute_sql_w_fetch_as_dictionary_list(sessions_database): +def test_execute_sql_w_to_dict_list(sessions_database): sd = _sample_data row_count = 40 _set_up_table(sessions_database, row_count) with sessions_database.snapshot() as snapshot: - rows = snapshot.execute_sql(sd.SQL).fetch_as_dictionary_list() + rows = snapshot.execute_sql(sd.SQL).to_dict_list() all_data_rows = list(_row_data(row_count)) row_data = [list(row.values()) for row in rows] sd._check_row_data(row_data, all_data_rows)