|
22 | 22 | import operator |
23 | 23 | import os |
24 | 24 | import pathlib |
| 25 | +import random |
| 26 | +import string |
25 | 27 | import time |
26 | 28 | import unittest |
27 | 29 | import uuid |
28 | | -import random |
29 | | -import string |
30 | 30 | from typing import Optional |
31 | 31 |
|
32 | | -from google.api_core.exceptions import PreconditionFailed |
33 | | -from google.api_core.exceptions import BadRequest |
34 | | -from google.api_core.exceptions import ClientError |
35 | | -from google.api_core.exceptions import Conflict |
36 | | -from google.api_core.exceptions import GoogleAPICallError |
37 | | -from google.api_core.exceptions import NotFound |
38 | | -from google.api_core.exceptions import InternalServerError |
39 | | -from google.api_core.exceptions import ServiceUnavailable |
40 | | -from google.api_core.exceptions import TooManyRequests |
41 | | -from google.cloud import bigquery |
42 | | -from google.cloud.bigquery.dataset import Dataset |
43 | | -from google.cloud.bigquery.dataset import DatasetReference |
44 | | -from google.cloud.bigquery.table import Table |
| 32 | +import psutil |
| 33 | +import pytest |
| 34 | +from google.api_core.exceptions import ( |
| 35 | + BadRequest, |
| 36 | + ClientError, |
| 37 | + Conflict, |
| 38 | + GoogleAPICallError, |
| 39 | + InternalServerError, |
| 40 | + NotFound, |
| 41 | + PreconditionFailed, |
| 42 | + ServiceUnavailable, |
| 43 | + TooManyRequests, |
| 44 | +) |
| 45 | +from google.cloud import bigquery, storage |
45 | 46 | from google.cloud._helpers import UTC |
46 | 47 | from google.cloud.bigquery import dbapi, enums |
47 | | -from google.cloud import storage |
48 | | -from google.cloud.datacatalog_v1 import types as datacatalog_types |
| 48 | +from google.cloud.bigquery.dataset import Dataset, DatasetReference |
| 49 | +from google.cloud.bigquery.table import Table |
49 | 50 | from google.cloud.datacatalog_v1 import PolicyTagManagerClient |
50 | | -from google.cloud.resourcemanager_v3 import types as resourcemanager_types |
| 51 | +from google.cloud.datacatalog_v1 import types as datacatalog_types |
51 | 52 | from google.cloud.resourcemanager_v3 import TagKeysClient, TagValuesClient |
52 | | -import psutil |
53 | | -import pytest |
54 | | -from test_utils.retry import RetryErrors |
55 | | -from test_utils.retry import RetryInstanceState |
56 | | -from test_utils.retry import RetryResult |
| 53 | +from google.cloud.resourcemanager_v3 import types as resourcemanager_types |
| 54 | +from test_utils.retry import RetryErrors, RetryInstanceState, RetryResult |
57 | 55 | from test_utils.system import unique_resource_id |
58 | 56 |
|
59 | 57 | from . import helpers |
@@ -254,8 +252,13 @@ def test_close_releases_open_sockets(self): |
254 | 252 | import gc |
255 | 253 |
|
256 | 254 | gc.collect() |
257 | | - conn_end = current_process.net_connections() |
258 | | - conn_count_end = len(conn_end) |
| 255 | + for _ in range(30): # Wait up to 3 seconds |
| 256 | + conn_end = current_process.net_connections() |
| 257 | + conn_count_end = len(conn_end) |
| 258 | + if conn_count_end <= conn_count_start: |
| 259 | + break |
| 260 | + time.sleep(0.1) |
| 261 | + |
259 | 262 | self.assertLessEqual(conn_count_end, conn_count_start) |
260 | 263 |
|
261 | 264 | def test_create_dataset(self): |
@@ -635,8 +638,7 @@ def test_create_table_with_default_value_expression(self): |
635 | 638 | self.assertEqual("FOO", row_2.get("username")) |
636 | 639 |
|
637 | 640 | def test_create_table_w_time_partitioning_w_clustering_fields(self): |
638 | | - from google.cloud.bigquery.table import TimePartitioning |
639 | | - from google.cloud.bigquery.table import TimePartitioningType |
| 641 | + from google.cloud.bigquery.table import TimePartitioning, TimePartitioningType |
640 | 642 |
|
641 | 643 | dataset = self.temp_dataset(_make_dataset_id("create_table_tp_cf")) |
642 | 644 | table_id = "test_table" |
@@ -964,12 +966,12 @@ def test_update_table_clustering_configuration(self): |
964 | 966 | self.assertIsNone(table3.clustering_fields, None) |
965 | 967 |
|
966 | 968 | def test_update_table_constraints(self): |
967 | | - from google.cloud.bigquery.table import TableConstraints |
968 | 969 | from google.cloud.bigquery.table import ( |
969 | | - PrimaryKey, |
| 970 | + ColumnReference, |
970 | 971 | ForeignKey, |
| 972 | + PrimaryKey, |
| 973 | + TableConstraints, |
971 | 974 | TableReference, |
972 | | - ColumnReference, |
973 | 975 | ) |
974 | 976 |
|
975 | 977 | dataset = self.temp_dataset(_make_dataset_id("update_table")) |
@@ -1128,8 +1130,7 @@ def test_insert_rows_then_dump_table(self): |
1128 | 1130 | self.assertEqual(sorted(row_tuples, key=by_age), sorted(ROWS, key=by_age)) |
1129 | 1131 |
|
1130 | 1132 | def test_load_table_from_local_avro_file_then_dump_table(self): |
1131 | | - from google.cloud.bigquery.job import SourceFormat |
1132 | | - from google.cloud.bigquery.job import WriteDisposition |
| 1133 | + from google.cloud.bigquery.job import SourceFormat, WriteDisposition |
1133 | 1134 |
|
1134 | 1135 | TABLE_NAME = "test_table_avro" |
1135 | 1136 | ROWS = [ |
@@ -1169,8 +1170,7 @@ def test_load_table_from_local_avro_file_then_dump_table(self): |
1169 | 1170 |
|
1170 | 1171 | def test_load_table_from_local_parquet_file_decimal_types(self): |
1171 | 1172 | from google.cloud.bigquery.enums import DecimalTargetType |
1172 | | - from google.cloud.bigquery.job import SourceFormat |
1173 | | - from google.cloud.bigquery.job import WriteDisposition |
| 1173 | + from google.cloud.bigquery.job import SourceFormat, WriteDisposition |
1174 | 1174 |
|
1175 | 1175 | TABLE_NAME = "test_table_parquet" |
1176 | 1176 |
|
@@ -1359,9 +1359,11 @@ def test_load_table_from_csv_w_picosecond_timestamp(self): |
1359 | 1359 | self.assertEqual(table.num_rows, 3) |
1360 | 1360 |
|
1361 | 1361 | def test_load_avro_from_uri_then_dump_table(self): |
1362 | | - from google.cloud.bigquery.job import CreateDisposition |
1363 | | - from google.cloud.bigquery.job import SourceFormat |
1364 | | - from google.cloud.bigquery.job import WriteDisposition |
| 1362 | + from google.cloud.bigquery.job import ( |
| 1363 | + CreateDisposition, |
| 1364 | + SourceFormat, |
| 1365 | + WriteDisposition, |
| 1366 | + ) |
1365 | 1367 |
|
1366 | 1368 | table_name = "test_table" |
1367 | 1369 | rows = [ |
@@ -1399,9 +1401,11 @@ def test_load_avro_from_uri_then_dump_table(self): |
1399 | 1401 | ) |
1400 | 1402 |
|
1401 | 1403 | def test_load_table_from_uri_then_dump_table(self): |
1402 | | - from google.cloud.bigquery.job import CreateDisposition |
1403 | | - from google.cloud.bigquery.job import SourceFormat |
1404 | | - from google.cloud.bigquery.job import WriteDisposition |
| 1404 | + from google.cloud.bigquery.job import ( |
| 1405 | + CreateDisposition, |
| 1406 | + SourceFormat, |
| 1407 | + WriteDisposition, |
| 1408 | + ) |
1405 | 1409 |
|
1406 | 1410 | TABLE_ID = "test_table" |
1407 | 1411 | GS_URL = self._write_csv_to_storage( |
@@ -2202,15 +2206,22 @@ def test_dbapi_connection_does_not_leak_sockets(self): |
2202 | 2206 | import gc |
2203 | 2207 |
|
2204 | 2208 | gc.collect() |
2205 | | - conn_end = current_process.net_connections() |
2206 | | - conn_count_end = len(conn_end) |
| 2209 | + for _ in range(30): # Wait up to 3 seconds |
| 2210 | + conn_end = current_process.net_connections() |
| 2211 | + conn_count_end = len(conn_end) |
| 2212 | + if conn_count_end <= conn_count_start: |
| 2213 | + break |
| 2214 | + time.sleep(0.1) |
| 2215 | + |
2207 | 2216 | self.assertLessEqual(conn_count_end, conn_count_start) |
2208 | 2217 |
|
2209 | 2218 | def _load_table_for_dml(self, rows, dataset_id, table_id): |
2210 | 2219 | from google.cloud._testing import _NamedTemporaryFile |
2211 | | - from google.cloud.bigquery.job import CreateDisposition |
2212 | | - from google.cloud.bigquery.job import SourceFormat |
2213 | | - from google.cloud.bigquery.job import WriteDisposition |
| 2220 | + from google.cloud.bigquery.job import ( |
| 2221 | + CreateDisposition, |
| 2222 | + SourceFormat, |
| 2223 | + WriteDisposition, |
| 2224 | + ) |
2214 | 2225 |
|
2215 | 2226 | dataset = self.temp_dataset(dataset_id) |
2216 | 2227 | greeting = bigquery.SchemaField("greeting", "STRING", mode="NULLABLE") |
@@ -2732,8 +2743,7 @@ def test_nested_table_to_arrow(self): |
2732 | 2743 | bigquery_storage = pytest.importorskip("google.cloud.bigquery_storage") |
2733 | 2744 | pyarrow = pytest.importorskip("pyarrow") |
2734 | 2745 | pyarrow.types = pytest.importorskip("pyarrow.types") |
2735 | | - from google.cloud.bigquery.job import SourceFormat |
2736 | | - from google.cloud.bigquery.job import WriteDisposition |
| 2746 | + from google.cloud.bigquery.job import SourceFormat, WriteDisposition |
2737 | 2747 |
|
2738 | 2748 | SF = bigquery.SchemaField |
2739 | 2749 | schema = [ |
@@ -2865,8 +2875,7 @@ def test_parameterized_types_round_trip(dataset_id: str): |
2865 | 2875 |
|
2866 | 2876 |
|
2867 | 2877 | def test_table_snapshots(dataset_id: str): |
2868 | | - from google.cloud.bigquery import CopyJobConfig |
2869 | | - from google.cloud.bigquery import OperationType |
| 2878 | + from google.cloud.bigquery import CopyJobConfig, OperationType |
2870 | 2879 |
|
2871 | 2880 | client = Config.CLIENT |
2872 | 2881 |
|
@@ -2936,8 +2945,7 @@ def test_table_snapshots(dataset_id: str): |
2936 | 2945 |
|
2937 | 2946 |
|
2938 | 2947 | def test_table_clones(dataset_id: str): |
2939 | | - from google.cloud.bigquery import CopyJobConfig |
2940 | | - from google.cloud.bigquery import OperationType |
| 2948 | + from google.cloud.bigquery import CopyJobConfig, OperationType |
2941 | 2949 |
|
2942 | 2950 | client = Config.CLIENT |
2943 | 2951 |
|
|
0 commit comments