|
| 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 | +from dateutil.relativedelta import relativedelta |
| 16 | +import pytest |
| 17 | + |
| 18 | +from google.cloud.bigquery.schema import SchemaField |
| 19 | + |
| 20 | + |
| 21 | +def create_field(mode="NULLABLE", type_="IGNORED"): |
| 22 | + return SchemaField("test_field", type_, mode=mode) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def mut(): |
| 27 | + from google.cloud.bigquery import _helpers |
| 28 | + |
| 29 | + return _helpers |
| 30 | + |
| 31 | + |
| 32 | +def test_interval_from_json_w_none_nullable(mut): |
| 33 | + got = mut._interval_from_json(None, create_field()) |
| 34 | + assert got is None |
| 35 | + |
| 36 | + |
| 37 | +def test_interval_from_json_w_none_required(mut): |
| 38 | + with pytest.raises(TypeError): |
| 39 | + mut._interval_from_json(None, create_field(mode="REQUIRED")) |
| 40 | + |
| 41 | + |
| 42 | +def test_interval_from_json_w_invalid_format(mut): |
| 43 | + with pytest.raises(ValueError, match="NOT_AN_INTERVAL"): |
| 44 | + mut._interval_from_json("NOT_AN_INTERVAL", create_field()) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + ("value", "expected"), |
| 49 | + ( |
| 50 | + ("0-0 0 0:0:0", relativedelta()), |
| 51 | + # SELECT INTERVAL X YEAR |
| 52 | + ("-10000-0 0 0:0:0", relativedelta(years=-10000)), |
| 53 | + ("-1-0 0 0:0:0", relativedelta(years=-1)), |
| 54 | + ("1-0 0 0:0:0", relativedelta(years=1)), |
| 55 | + ("10000-0 0 0:0:0", relativedelta(years=10000)), |
| 56 | + # SELECT INTERVAL X MONTH |
| 57 | + ("-0-11 0 0:0:0", relativedelta(months=-11)), |
| 58 | + ("-0-1 0 0:0:0", relativedelta(months=-1)), |
| 59 | + ("0-1 0 0:0:0", relativedelta(months=1)), |
| 60 | + ("0-11 0 0:0:0", relativedelta(months=11)), |
| 61 | + # SELECT INTERVAL X DAY |
| 62 | + ("0-0 -3660000 0:0:0", relativedelta(days=-3660000)), |
| 63 | + ("0-0 -1 0:0:0", relativedelta(days=-1)), |
| 64 | + ("0-0 1 0:0:0", relativedelta(days=1)), |
| 65 | + ("0-0 3660000 0:0:0", relativedelta(days=3660000)), |
| 66 | + # SELECT INTERVAL X HOUR |
| 67 | + ("0-0 0 -87840000:0:0", relativedelta(hours=-87840000)), |
| 68 | + ("0-0 0 -1:0:0", relativedelta(hours=-1)), |
| 69 | + ("0-0 0 1:0:0", relativedelta(hours=1)), |
| 70 | + ("0-0 0 87840000:0:0", relativedelta(hours=87840000)), |
| 71 | + # SELECT INTERVAL X MINUTE |
| 72 | + ("0-0 0 -0:59:0", relativedelta(minutes=-59)), |
| 73 | + ("0-0 0 -0:1:0", relativedelta(minutes=-1)), |
| 74 | + ("0-0 0 0:1:0", relativedelta(minutes=1)), |
| 75 | + ("0-0 0 0:59:0", relativedelta(minutes=59)), |
| 76 | + # SELECT INTERVAL X SECOND |
| 77 | + ("0-0 0 -0:0:59", relativedelta(seconds=-59)), |
| 78 | + ("0-0 0 -0:0:1", relativedelta(seconds=-1)), |
| 79 | + ("0-0 0 0:0:1", relativedelta(seconds=1)), |
| 80 | + ("0-0 0 0:0:59", relativedelta(seconds=59)), |
| 81 | + # SELECT (INTERVAL -1 SECOND) / 1000000 |
| 82 | + ("0-0 0 -0:0:0.000001", relativedelta(microseconds=-1)), |
| 83 | + ("0-0 0 -0:0:59.999999", relativedelta(seconds=-59, microseconds=-999999)), |
| 84 | + ("0-0 0 -0:0:59.999", relativedelta(seconds=-59, microseconds=-999000)), |
| 85 | + ("0-0 0 0:0:59.999", relativedelta(seconds=59, microseconds=999000)), |
| 86 | + ("0-0 0 0:0:59.999999", relativedelta(seconds=59, microseconds=999999)), |
| 87 | + # Test with multiple digits in each section. |
| 88 | + ( |
| 89 | + "32-11 45 67:16:23.987654", |
| 90 | + relativedelta( |
| 91 | + years=32, |
| 92 | + months=11, |
| 93 | + days=45, |
| 94 | + hours=67, |
| 95 | + minutes=16, |
| 96 | + seconds=23, |
| 97 | + microseconds=987654, |
| 98 | + ), |
| 99 | + ), |
| 100 | + ( |
| 101 | + "-32-11 -45 -67:16:23.987654", |
| 102 | + relativedelta( |
| 103 | + years=-32, |
| 104 | + months=-11, |
| 105 | + days=-45, |
| 106 | + hours=-67, |
| 107 | + minutes=-16, |
| 108 | + seconds=-23, |
| 109 | + microseconds=-987654, |
| 110 | + ), |
| 111 | + ), |
| 112 | + # Test with mixed +/- sections. |
| 113 | + ( |
| 114 | + "9999-9 -999999 9999999:59:59.999999", |
| 115 | + relativedelta( |
| 116 | + years=9999, |
| 117 | + months=9, |
| 118 | + days=-999999, |
| 119 | + hours=9999999, |
| 120 | + minutes=59, |
| 121 | + seconds=59, |
| 122 | + microseconds=999999, |
| 123 | + ), |
| 124 | + ), |
| 125 | + # Test with fraction that is not microseconds. |
| 126 | + ("0-0 0 0:0:42.", relativedelta(seconds=42)), |
| 127 | + ("0-0 0 0:0:59.1", relativedelta(seconds=59, microseconds=100000)), |
| 128 | + ("0-0 0 0:0:0.12", relativedelta(microseconds=120000)), |
| 129 | + ("0-0 0 0:0:0.123", relativedelta(microseconds=123000)), |
| 130 | + ("0-0 0 0:0:0.1234", relativedelta(microseconds=123400)), |
| 131 | + # Fractional seconds can cause rounding problems if cast to float. See: |
| 132 | + # https://github.com/googleapis/python-db-dtypes-pandas/issues/18 |
| 133 | + ("0-0 0 0:0:59.876543", relativedelta(seconds=59, microseconds=876543)), |
| 134 | + ( |
| 135 | + "0-0 0 01:01:01.010101", |
| 136 | + relativedelta(hours=1, minutes=1, seconds=1, microseconds=10101), |
| 137 | + ), |
| 138 | + ( |
| 139 | + "0-0 0 09:09:09.090909", |
| 140 | + relativedelta(hours=9, minutes=9, seconds=9, microseconds=90909), |
| 141 | + ), |
| 142 | + ( |
| 143 | + "0-0 0 11:11:11.111111", |
| 144 | + relativedelta(hours=11, minutes=11, seconds=11, microseconds=111111), |
| 145 | + ), |
| 146 | + ( |
| 147 | + "0-0 0 19:16:23.987654", |
| 148 | + relativedelta(hours=19, minutes=16, seconds=23, microseconds=987654), |
| 149 | + ), |
| 150 | + # Nanoseconds are not expected, but should not cause error. |
| 151 | + ("0-0 0 0:0:00.123456789", relativedelta(microseconds=123456)), |
| 152 | + ("0-0 0 0:0:59.87654321", relativedelta(seconds=59, microseconds=876543)), |
| 153 | + ), |
| 154 | +) |
| 155 | +def test_w_string_values(mut, value, expected): |
| 156 | + got = mut._interval_from_json(value, create_field()) |
| 157 | + assert got == expected |
0 commit comments