Skip to content

Commit fce0fa2

Browse files
authored
refactor: split pandas system tests to new module (#548)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Follow-up to googleapis/python-bigquery#448 Towards #366
1 parent 5156077 commit fce0fa2

4 files changed

Lines changed: 969 additions & 918 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
# https://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 pytest
16+
17+
from . import helpers
18+
19+
20+
@pytest.fixture(scope="session")
21+
def bigquery_client():
22+
from google.cloud import bigquery
23+
24+
return bigquery.Client()
25+
26+
27+
@pytest.fixture(scope="session")
28+
def bqstorage_client(bigquery_client):
29+
from google.cloud import bigquery_storage
30+
31+
return bigquery_storage.BigQueryReadClient(credentials=bigquery_client._credentials)
32+
33+
34+
@pytest.fixture
35+
def dataset_id(bigquery_client):
36+
dataset_id = f"bqsystem_{helpers.temp_suffix()}"
37+
bigquery_client.create_dataset(dataset_id)
38+
yield dataset_id
39+
bigquery_client.delete_dataset(dataset_id, delete_contents=True)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
# https://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+
import uuid
18+
19+
import google.api_core.exceptions
20+
import test_utils.retry
21+
22+
from google.cloud._helpers import UTC
23+
24+
25+
_naive = datetime.datetime(2016, 12, 5, 12, 41, 9)
26+
_naive_microseconds = datetime.datetime(2016, 12, 5, 12, 41, 9, 250000)
27+
_stamp = "%s %s" % (_naive.date().isoformat(), _naive.time().isoformat())
28+
_stamp_microseconds = _stamp + ".250000"
29+
_zoned = _naive.replace(tzinfo=UTC)
30+
_zoned_microseconds = _naive_microseconds.replace(tzinfo=UTC)
31+
_numeric = decimal.Decimal("123456789.123456789")
32+
33+
34+
# Examples of most data types to test with query() and DB-API.
35+
STANDARD_SQL_EXAMPLES = [
36+
("SELECT 1", 1),
37+
("SELECT 1.3", 1.3),
38+
("SELECT TRUE", True),
39+
('SELECT "ABC"', "ABC"),
40+
('SELECT CAST("foo" AS BYTES)', b"foo"),
41+
('SELECT TIMESTAMP "%s"' % (_stamp,), _zoned),
42+
('SELECT TIMESTAMP "%s"' % (_stamp_microseconds,), _zoned_microseconds,),
43+
('SELECT DATETIME(TIMESTAMP "%s")' % (_stamp,), _naive),
44+
('SELECT DATETIME(TIMESTAMP "%s")' % (_stamp_microseconds,), _naive_microseconds,),
45+
('SELECT DATE(TIMESTAMP "%s")' % (_stamp,), _naive.date()),
46+
('SELECT TIME(TIMESTAMP "%s")' % (_stamp,), _naive.time()),
47+
('SELECT NUMERIC "%s"' % (_numeric,), _numeric),
48+
("SELECT (1, 2)", {"_field_1": 1, "_field_2": 2}),
49+
(
50+
"SELECT ((1, 2), (3, 4), 5)",
51+
{
52+
"_field_1": {"_field_1": 1, "_field_2": 2},
53+
"_field_2": {"_field_1": 3, "_field_2": 4},
54+
"_field_3": 5,
55+
},
56+
),
57+
("SELECT [1, 2, 3]", [1, 2, 3]),
58+
(
59+
"SELECT ([1, 2], 3, [4, 5])",
60+
{"_field_1": [1, 2], "_field_2": 3, "_field_3": [4, 5]},
61+
),
62+
(
63+
"SELECT [(1, 2, 3), (4, 5, 6)]",
64+
[
65+
{"_field_1": 1, "_field_2": 2, "_field_3": 3},
66+
{"_field_1": 4, "_field_2": 5, "_field_3": 6},
67+
],
68+
),
69+
(
70+
"SELECT [([1, 2, 3], 4), ([5, 6], 7)]",
71+
[{"_field_1": [1, 2, 3], "_field_2": 4}, {"_field_1": [5, 6], "_field_2": 7}],
72+
),
73+
("SELECT ARRAY(SELECT STRUCT([1, 2]))", [{"_field_1": [1, 2]}]),
74+
("SELECT ST_GeogPoint(1, 2)", "POINT(1 2)"),
75+
]
76+
77+
78+
def temp_suffix():
79+
now = datetime.datetime.now()
80+
return f"{now.strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}"
81+
82+
83+
def _rate_limit_exceeded(forbidden):
84+
"""Predicate: pass only exceptions with 'rateLimitExceeded' as reason."""
85+
return any(error["reason"] == "rateLimitExceeded" for error in forbidden._errors)
86+
87+
88+
# We need to wait to stay within the rate limits.
89+
# The alternative outcome is a 403 Forbidden response from upstream, which
90+
# they return instead of the more appropriate 429.
91+
# See https://cloud.google.com/bigquery/quota-policy
92+
retry_403 = test_utils.retry.RetryErrors(
93+
google.api_core.exceptions.Forbidden, error_predicate=_rate_limit_exceeded,
94+
)

0 commit comments

Comments
 (0)