|
| 1 | +# Copyright 2025 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 db_dtypes # type: ignore |
| 16 | +import pyarrow as pa # type: ignore |
| 17 | +import pytest |
| 18 | +import shapely.geometry # type: ignore |
| 19 | + |
| 20 | +import bigframes.dtypes |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + ["python_type", "expected_dtype"], |
| 25 | + [ |
| 26 | + (bool, bigframes.dtypes.BOOL_DTYPE), |
| 27 | + (int, bigframes.dtypes.INT_DTYPE), |
| 28 | + (str, bigframes.dtypes.STRING_DTYPE), |
| 29 | + (shapely.geometry.Point, bigframes.dtypes.GEO_DTYPE), |
| 30 | + (shapely.geometry.Polygon, bigframes.dtypes.GEO_DTYPE), |
| 31 | + (shapely.geometry.base.BaseGeometry, bigframes.dtypes.GEO_DTYPE), |
| 32 | + ], |
| 33 | +) |
| 34 | +def test_bigframes_type_supports_python_types(python_type, expected_dtype): |
| 35 | + got_dtype = bigframes.dtypes.bigframes_type(python_type) |
| 36 | + assert got_dtype == expected_dtype |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize( |
| 40 | + ["scalar", "expected_dtype"], |
| 41 | + [ |
| 42 | + (pa.scalar(1_000_000_000, type=pa.int64()), bigframes.dtypes.INT_DTYPE), |
| 43 | + (pa.scalar(True, type=pa.bool_()), bigframes.dtypes.BOOL_DTYPE), |
| 44 | + (pa.scalar("hello", type=pa.string()), bigframes.dtypes.STRING_DTYPE), |
| 45 | + # Support NULL scalars. |
| 46 | + (pa.scalar(None, type=pa.int64()), bigframes.dtypes.INT_DTYPE), |
| 47 | + (pa.scalar(None, type=pa.bool_()), bigframes.dtypes.BOOL_DTYPE), |
| 48 | + (pa.scalar(None, type=pa.string()), bigframes.dtypes.STRING_DTYPE), |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_infer_literal_type_arrow_scalar(scalar, expected_dtype): |
| 52 | + assert bigframes.dtypes.infer_literal_type(scalar) == expected_dtype |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize( |
| 56 | + ["type_", "expected"], |
| 57 | + [ |
| 58 | + (pa.int64(), False), |
| 59 | + (db_dtypes.JSONArrowType(), True), |
| 60 | + (pa.struct([("int", pa.int64()), ("str", pa.string())]), False), |
| 61 | + (pa.struct([("int", pa.int64()), ("json", db_dtypes.JSONArrowType())]), True), |
| 62 | + (pa.list_(pa.int64()), False), |
| 63 | + (pa.list_(db_dtypes.JSONArrowType()), True), |
| 64 | + ( |
| 65 | + pa.list_( |
| 66 | + pa.struct([("int", pa.int64()), ("json", db_dtypes.JSONArrowType())]) |
| 67 | + ), |
| 68 | + True, |
| 69 | + ), |
| 70 | + ], |
| 71 | +) |
| 72 | +def test_contains_db_dtypes_json_arrow_type(type_, expected): |
| 73 | + assert bigframes.dtypes.contains_db_dtypes_json_arrow_type(type_) == expected |
0 commit comments