|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import datetime |
| 16 | +import decimal |
| 17 | + |
| 18 | +from google.cloud import bigquery |
| 19 | +from google.cloud.bigquery import enums |
| 20 | + |
| 21 | + |
| 22 | +def test_list_rows_empty_table(bigquery_client: bigquery.Client, table_id: str): |
| 23 | + from google.cloud.bigquery.table import RowIterator |
| 24 | + |
| 25 | + table = bigquery_client.create_table(table_id) |
| 26 | + |
| 27 | + # It's a bit silly to list rows for an empty table, but this does |
| 28 | + # happen as the result of a DDL query from an IPython magic command. |
| 29 | + rows = bigquery_client.list_rows(table) |
| 30 | + assert isinstance(rows, RowIterator) |
| 31 | + assert tuple(rows) == () |
| 32 | + |
| 33 | + |
| 34 | +def test_list_rows_page_size(bigquery_client: bigquery.Client, table_id: str): |
| 35 | + num_items = 7 |
| 36 | + page_size = 3 |
| 37 | + num_pages, num_last_page = divmod(num_items, page_size) |
| 38 | + |
| 39 | + to_insert = [{"string_col": "item%d" % i, "rowindex": i} for i in range(num_items)] |
| 40 | + bigquery_client.load_table_from_json(to_insert, table_id).result() |
| 41 | + |
| 42 | + df = bigquery_client.list_rows( |
| 43 | + table_id, |
| 44 | + selected_fields=[bigquery.SchemaField("string_col", enums.SqlTypeNames.STRING)], |
| 45 | + page_size=page_size, |
| 46 | + ) |
| 47 | + pages = df.pages |
| 48 | + |
| 49 | + for i in range(num_pages): |
| 50 | + page = next(pages) |
| 51 | + assert page.num_items == page_size |
| 52 | + page = next(pages) |
| 53 | + assert page.num_items == num_last_page |
| 54 | + |
| 55 | + |
| 56 | +def test_list_rows_scalars(bigquery_client: bigquery.Client, scalars_table: str): |
| 57 | + rows = sorted( |
| 58 | + bigquery_client.list_rows(scalars_table), key=lambda row: row["rowindex"] |
| 59 | + ) |
| 60 | + row = rows[0] |
| 61 | + assert row["bool_col"] # True |
| 62 | + assert row["bytes_col"] == b"Hello, World!" |
| 63 | + assert row["date_col"] == datetime.date(2021, 7, 21) |
| 64 | + assert row["datetime_col"] == datetime.datetime(2021, 7, 21, 11, 39, 45) |
| 65 | + assert row["geography_col"] == "POINT(-122.0838511 37.3860517)" |
| 66 | + assert row["int64_col"] == 123456789 |
| 67 | + assert row["numeric_col"] == decimal.Decimal("1.23456789") |
| 68 | + assert row["bignumeric_col"] == decimal.Decimal("10.111213141516171819") |
| 69 | + assert row["float64_col"] == 1.25 |
| 70 | + assert row["string_col"] == "Hello, World!" |
| 71 | + assert row["time_col"] == datetime.time(11, 41, 43, 76160) |
| 72 | + assert row["timestamp_col"] == datetime.datetime( |
| 73 | + 2021, 7, 21, 17, 43, 43, 945289, tzinfo=datetime.timezone.utc |
| 74 | + ) |
| 75 | + |
| 76 | + nullrow = rows[1] |
| 77 | + for column, value in nullrow.items(): |
| 78 | + if column == "rowindex": |
| 79 | + assert value == 1 |
| 80 | + else: |
| 81 | + assert value is None |
| 82 | + |
| 83 | + |
| 84 | +def test_list_rows_scalars_extreme( |
| 85 | + bigquery_client: bigquery.Client, scalars_extreme_table: str |
| 86 | +): |
| 87 | + rows = sorted( |
| 88 | + bigquery_client.list_rows(scalars_extreme_table), |
| 89 | + key=lambda row: row["rowindex"], |
| 90 | + ) |
| 91 | + row = rows[0] |
| 92 | + assert row["bool_col"] # True |
| 93 | + assert row["bytes_col"] == b"\r\n" |
| 94 | + assert row["date_col"] == datetime.date(9999, 12, 31) |
| 95 | + assert row["datetime_col"] == datetime.datetime(9999, 12, 31, 23, 59, 59, 999999) |
| 96 | + assert row["geography_col"] == "POINT(-135 90)" |
| 97 | + assert row["int64_col"] == 9223372036854775807 |
| 98 | + assert row["numeric_col"] == decimal.Decimal(f"9.{'9' * 37}E+28") |
| 99 | + assert row["bignumeric_col"] == decimal.Decimal(f"9.{'9' * 75}E+37") |
| 100 | + assert row["float64_col"] == float("Inf") |
| 101 | + assert row["string_col"] == "Hello, World" |
| 102 | + assert row["time_col"] == datetime.time(23, 59, 59, 999999) |
| 103 | + assert row["timestamp_col"] == datetime.datetime( |
| 104 | + 9999, 12, 31, 23, 59, 59, 999999, tzinfo=datetime.timezone.utc |
| 105 | + ) |
| 106 | + |
| 107 | + nullrow = rows[4] |
| 108 | + for column, value in nullrow.items(): |
| 109 | + if column == "rowindex": |
| 110 | + assert value == 4 |
| 111 | + else: |
| 112 | + assert value is None |
0 commit comments