|
| 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 | +from unittest import mock |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +import bigframes.bigquery.table |
| 20 | +import bigframes.core.sql.table |
| 21 | +import bigframes.session |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def mock_session(): |
| 26 | + return mock.create_autospec(spec=bigframes.session.Session) |
| 27 | + |
| 28 | + |
| 29 | +def test_create_external_table_ddl(): |
| 30 | + sql = bigframes.core.sql.table.create_external_table_ddl( |
| 31 | + "my-project.my_dataset.my_table", |
| 32 | + columns={"col1": "INT64", "col2": "STRING"}, |
| 33 | + options={"format": "CSV", "uris": ["gs://bucket/path*"]}, |
| 34 | + ) |
| 35 | + expected = "CREATE EXTERNAL TABLE my-project.my_dataset.my_table (col1 INT64, col2 STRING) OPTIONS (format = 'CSV', uris = ['gs://bucket/path*'])" |
| 36 | + assert sql == expected |
| 37 | + |
| 38 | + |
| 39 | +def test_create_external_table_ddl_replace(): |
| 40 | + sql = bigframes.core.sql.table.create_external_table_ddl( |
| 41 | + "my-project.my_dataset.my_table", |
| 42 | + replace=True, |
| 43 | + columns={"col1": "INT64", "col2": "STRING"}, |
| 44 | + options={"format": "CSV", "uris": ["gs://bucket/path*"]}, |
| 45 | + ) |
| 46 | + expected = "CREATE OR REPLACE EXTERNAL TABLE my-project.my_dataset.my_table (col1 INT64, col2 STRING) OPTIONS (format = 'CSV', uris = ['gs://bucket/path*'])" |
| 47 | + assert sql == expected |
| 48 | + |
| 49 | + |
| 50 | +def test_create_external_table_ddl_if_not_exists(): |
| 51 | + sql = bigframes.core.sql.table.create_external_table_ddl( |
| 52 | + "my-project.my_dataset.my_table", |
| 53 | + if_not_exists=True, |
| 54 | + columns={"col1": "INT64", "col2": "STRING"}, |
| 55 | + options={"format": "CSV", "uris": ["gs://bucket/path*"]}, |
| 56 | + ) |
| 57 | + expected = "CREATE EXTERNAL TABLE IF NOT EXISTS my-project.my_dataset.my_table (col1 INT64, col2 STRING) OPTIONS (format = 'CSV', uris = ['gs://bucket/path*'])" |
| 58 | + assert sql == expected |
| 59 | + |
| 60 | + |
| 61 | +def test_create_external_table_ddl_partition_columns(): |
| 62 | + sql = bigframes.core.sql.table.create_external_table_ddl( |
| 63 | + "my-project.my_dataset.my_table", |
| 64 | + columns={"col1": "INT64", "col2": "STRING"}, |
| 65 | + partition_columns={"part1": "DATE", "part2": "STRING"}, |
| 66 | + options={"format": "CSV", "uris": ["gs://bucket/path*"]}, |
| 67 | + ) |
| 68 | + expected = "CREATE EXTERNAL TABLE my-project.my_dataset.my_table (col1 INT64, col2 STRING) WITH PARTITION COLUMNS (part1 DATE, part2 STRING) OPTIONS (format = 'CSV', uris = ['gs://bucket/path*'])" |
| 69 | + assert sql == expected |
| 70 | + |
| 71 | + |
| 72 | +def test_create_external_table_ddl_connection(): |
| 73 | + sql = bigframes.core.sql.table.create_external_table_ddl( |
| 74 | + "my-project.my_dataset.my_table", |
| 75 | + columns={"col1": "INT64", "col2": "STRING"}, |
| 76 | + connection_name="my-connection", |
| 77 | + options={"format": "CSV", "uris": ["gs://bucket/path*"]}, |
| 78 | + ) |
| 79 | + expected = "CREATE EXTERNAL TABLE my-project.my_dataset.my_table (col1 INT64, col2 STRING) WITH CONNECTION `my-connection` OPTIONS (format = 'CSV', uris = ['gs://bucket/path*'])" |
| 80 | + assert sql == expected |
| 81 | + |
| 82 | + |
| 83 | +@mock.patch("bigframes.bigquery.table._get_table_metadata") |
| 84 | +def test_create_external_table(get_table_metadata_mock, mock_session): |
| 85 | + bigframes.bigquery.table.create_external_table( |
| 86 | + "my-project.my_dataset.my_table", |
| 87 | + columns={"col1": "INT64", "col2": "STRING"}, |
| 88 | + options={"format": "CSV", "uris": ["gs://bucket/path*"]}, |
| 89 | + session=mock_session, |
| 90 | + ) |
| 91 | + mock_session.read_gbq_query.assert_called_once() |
| 92 | + generated_sql = mock_session.read_gbq_query.call_args[0][0] |
| 93 | + expected = "CREATE EXTERNAL TABLE my-project.my_dataset.my_table (col1 INT64, col2 STRING) OPTIONS (format = 'CSV', uris = ['gs://bucket/path*'])" |
| 94 | + assert generated_sql == expected |
| 95 | + get_table_metadata_mock.assert_called_once() |
0 commit comments