Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 90115a3

Browse files
committed
add unit test
1 parent ab099c2 commit 90115a3

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

tests/unit/job/test_query.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,73 @@ def test_result_with_start_index(self):
16821682
tabledata_list_request[1]["query_params"]["maxResults"], page_size
16831683
)
16841684

1685+
def test_result_with_start_index_multi_page(self):
1686+
# When there are multiple pages of response and the user has set
1687+
# start_index, we should supply start_index to the server in the first
1688+
# request. However, in the subsequent requests, we will pass only
1689+
# page_token but not start_index, because the server only allows one
1690+
# of them.
1691+
from google.cloud.bigquery.table import RowIterator
1692+
1693+
query_resource = {
1694+
"jobComplete": True,
1695+
"jobReference": {"projectId": self.PROJECT, "jobId": self.JOB_ID},
1696+
"schema": {"fields": [{"name": "col1", "type": "STRING"}]},
1697+
"totalRows": "7",
1698+
}
1699+
tabledata_resource_1 = {
1700+
"totalRows": "7",
1701+
"pageToken": "page_token_1",
1702+
"rows": [
1703+
{"f": [{"v": "abc"}]},
1704+
{"f": [{"v": "def"}]},
1705+
{"f": [{"v": "ghi"}]},
1706+
],
1707+
}
1708+
tabledata_resource_2 = {
1709+
"totalRows": "7",
1710+
"pageToken": None,
1711+
"rows": [
1712+
{"f": [{"v": "jkl"}]},
1713+
{"f": [{"v": "mno"}]},
1714+
{"f": [{"v": "pqe"}]},
1715+
],
1716+
}
1717+
1718+
connection = make_connection(query_resource, tabledata_resource_1, tabledata_resource_2)
1719+
client = _make_client(self.PROJECT, connection=connection)
1720+
resource = self._make_resource(ended=True)
1721+
job = self._get_target_class().from_api_repr(resource, client)
1722+
1723+
start_index = 1
1724+
page_size = 3
1725+
1726+
result = job.result(page_size=page_size, start_index=start_index)
1727+
1728+
self.assertIsInstance(result, RowIterator)
1729+
self.assertEqual(result.total_rows, 7)
1730+
1731+
rows = list(result)
1732+
1733+
self.assertEqual(len(rows), 6)
1734+
self.assertEqual(len(connection.api_request.call_args_list), 3)
1735+
1736+
# First call has both startIndex and maxResults.
1737+
tabledata_list_request_1 = connection.api_request.call_args_list[1]
1738+
self.assertEqual(
1739+
tabledata_list_request_1[1]["query_params"]["startIndex"], start_index
1740+
)
1741+
self.assertEqual(
1742+
tabledata_list_request_1[1]["query_params"]["maxResults"], page_size
1743+
)
1744+
1745+
# Second call only has maxResults.
1746+
tabledata_list_request_2 = connection.api_request.call_args_list[2]
1747+
self.assertFalse("startIndex" in tabledata_list_request_2[1]["query_params"])
1748+
self.assertEqual(
1749+
tabledata_list_request_2[1]["query_params"]["maxResults"], page_size
1750+
)
1751+
16851752
def test_result_error(self):
16861753
from google.cloud import exceptions
16871754

0 commit comments

Comments
 (0)