|
| 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