Skip to content

Commit 99441d7

Browse files
committed
Simplify RowIterator constructor. Add test comments.
Use getattr instead of protecting with hasattr in the RowIterator constructor. Add comments about intentionally conflicting values for total_rows.
1 parent 73b46b9 commit 99441d7

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

bigquery/google/cloud/bigquery/table.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -1298,17 +1298,13 @@ def __init__(
12981298
page_start=_rows_page_start,
12991299
next_token="pageToken",
13001300
)
1301-
self._schema = schema
13021301
self._field_to_index = _helpers._field_to_index_mapping(schema)
1303-
1304-
self._total_rows = None
1305-
if table is not None and hasattr(table, "num_rows"):
1306-
self._total_rows = table.num_rows
1307-
13081302
self._page_size = page_size
1309-
self._table = table
1310-
self._selected_fields = selected_fields
13111303
self._project = client.project
1304+
self._schema = schema
1305+
self._selected_fields = selected_fields
1306+
self._table = table
1307+
self._total_rows = getattr(table, "num_rows", None)
13121308

13131309
def _get_next_page_response(self):
13141310
"""Requests the next page from the path provided.

bigquery/tests/unit/test_job.py

+4
Original file line numberDiff line numberDiff line change
@@ -4021,6 +4021,8 @@ def test_result(self):
40214021
"totalRows": "2",
40224022
}
40234023
tabledata_resource = {
4024+
# Explicitly set totalRows to be different from the query response.
4025+
# to test update during iteration.
40244026
"totalRows": "1",
40254027
"pageToken": None,
40264028
"rows": [{"f": [{"v": "abc"}]}],
@@ -4038,6 +4040,8 @@ def test_result(self):
40384040
rows = list(result)
40394041
self.assertEqual(len(rows), 1)
40404042
self.assertEqual(rows[0].col1, "abc")
4043+
# Test that the total_rows property has changed during iteration, based
4044+
# on the response from tabledata.list.
40414045
self.assertEqual(result.total_rows, 1)
40424046

40434047
def test_result_w_empty_schema(self):

0 commit comments

Comments
 (0)