diff --git a/scripts/load_test_data.sh b/scripts/load_test_data.sh deleted file mode 100755 index a6e975f8..00000000 --- a/scripts/load_test_data.sh +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright (c) 2017 The PyBigQuery Authors -# -# Permission is hereby granted, free of charge, to any person obtaining a copy of -# this software and associated documentation files (the "Software"), to deal in -# the Software without restriction, including without limitation the rights to -# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -# the Software, and to permit persons to whom the Software is furnished to do so, -# subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -bq mk test_pybigquery -bq mk --data_location=asia-northeast1 test_pybigquery_location -bq mk test_pybigquery_alt - -bq rm -f -t test_pybigquery.sample -bq rm -f -t test_pybigquery_alt.sample_alt -bq rm -f -t test_pybigquery.sample_one_row -bq rm -f -t test_pybigquery.sample_dml -bq rm -f -t test_pybigquery.sample_view -bq rm -f -t test_pybigquery_location.sample_one_row - -bq mk --table --schema=$(dirname $0)/schema.json --time_partitioning_field timestamp --clustering_fields integer,string --description 'A sample table containing most data types.' test_pybigquery.sample -bq mk --table --schema=$(dirname $0)/schema.json --time_partitioning_field timestamp --clustering_fields integer,string test_pybigquery_alt.sample_alt -bq load --source_format=NEWLINE_DELIMITED_JSON --schema=$(dirname $0)/schema.json test_pybigquery.sample $(dirname $0)/sample.json - -bq mk --table --schema=$(dirname $0)/schema.json --time_partitioning_field timestamp --clustering_fields integer,string test_pybigquery.sample_one_row -bq load --source_format=NEWLINE_DELIMITED_JSON --schema=$(dirname $0)/schema.json test_pybigquery.sample_one_row $(dirname $0)/sample_one_row.json - -bq --location=asia-northeast1 load --source_format=NEWLINE_DELIMITED_JSON --schema=$(dirname $0)/schema.json test_pybigquery_location.sample_one_row $(dirname $0)/sample_one_row.json -bq mk --schema=$(dirname $0)/schema.json -t test_pybigquery.sample_dml - -bq mk --use_legacy_sql=false --view 'SELECT string FROM test_pybigquery.sample' test_pybigquery.sample_view diff --git a/tests/system/conftest.py b/tests/system/conftest.py new file mode 100644 index 00000000..5f694133 --- /dev/null +++ b/tests/system/conftest.py @@ -0,0 +1,111 @@ +# Copyright 2021 The PyBigQuery Authors +# +# Use of this source code is governed by an MIT-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/MIT. + +import pathlib + +import pytest +from google.cloud import bigquery + +from typing import List + + +DATA_DIR = pathlib.Path(__file__).parent / "data" + + +def load_sample_data( + full_table_id: str, + bigquery_client: bigquery.Client, + bigquery_schema: List[bigquery.SchemaField], + filename: str = "sample.json", +): + # Delete the table first. Even though we can use WRITE_TRUNCATE, the load + # job fails if properties such as table description do not match. + bigquery_client.delete_table(full_table_id, not_found_ok=True) + sample_config = bigquery.LoadJobConfig() + sample_config.destination_table_description = ( + "A sample table containing most data types." + ) + sample_config.schema = bigquery_schema + sample_config.time_partitioning = bigquery.TimePartitioning(field="timestamp") + sample_config.clustering_fields = ["integer", "string"] + sample_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON + sample_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE + + with open(DATA_DIR / filename, "rb") as data_file: + return bigquery_client.load_table_from_file( + data_file, full_table_id, job_config=sample_config, + ) + + +@pytest.fixture(scope="session") +def bigquery_client(): + return bigquery.Client() + + +@pytest.fixture(scope="session") +def bigquery_schema(bigquery_client: bigquery.Client): + return bigquery_client.schema_from_json(DATA_DIR / "schema.json") + + +@pytest.fixture(scope="session", autouse=True) +def bigquery_dataset( + bigquery_client: bigquery.Client, bigquery_schema: List[bigquery.SchemaField] +): + project_id = bigquery_client.project + dataset_id = "test_pybigquery" + dataset = bigquery.Dataset(f"{project_id}.{dataset_id}") + dataset = bigquery_client.create_dataset(dataset, exists_ok=True) + empty_table = bigquery.Table( + f"{project_id}.{dataset_id}.sample_dml", schema=bigquery_schema + ) + view = bigquery.Table(f"{project_id}.{dataset_id}.sample_view",) + view.view_query = f"SELECT string FROM `{dataset_id}.sample`" + job1 = load_sample_data( + f"{project_id}.{dataset_id}.sample", bigquery_client, bigquery_schema + ) + job2 = load_sample_data( + f"{project_id}.{dataset_id}.sample_one_row", + bigquery_client, + bigquery_schema, + filename="sample_one_row.json", + ) + bigquery_client.create_table(empty_table, exists_ok=True) + job1.result() + job2.result() + bigquery_client.create_table(view, exists_ok=True) + return dataset_id + + +@pytest.fixture(scope="session", autouse=True) +def bigquery_alt_dataset( + bigquery_client: bigquery.Client, bigquery_schema: List[bigquery.SchemaField] +): + project_id = bigquery_client.project + dataset_id = "test_pybigquery_alt" + dataset = bigquery.Dataset(f"{project_id}.{dataset_id}") + dataset = bigquery_client.create_dataset(dataset, exists_ok=True) + job = load_sample_data( + f"{project_id}.{dataset_id}.sample_alt", bigquery_client, bigquery_schema + ) + job.result() + return dataset_id + + +@pytest.fixture(scope="session", autouse=True) +def bigquery_regional_dataset(bigquery_client, bigquery_schema): + project_id = bigquery_client.project + dataset_id = "test_pybigquery_location" + dataset = bigquery.Dataset(f"{project_id}.{dataset_id}") + dataset.location = "asia-northeast1" + dataset = bigquery_client.create_dataset(dataset, exists_ok=True) + job = load_sample_data( + f"{project_id}.{dataset_id}.sample_one_row", + bigquery_client, + bigquery_schema, + filename="sample_one_row.json", + ) + job.result() + return dataset_id diff --git a/scripts/sample.json b/tests/system/data/sample.json similarity index 100% rename from scripts/sample.json rename to tests/system/data/sample.json diff --git a/scripts/sample_one_row.json b/tests/system/data/sample_one_row.json similarity index 100% rename from scripts/sample_one_row.json rename to tests/system/data/sample_one_row.json diff --git a/scripts/schema.json b/tests/system/data/schema.json similarity index 100% rename from scripts/schema.json rename to tests/system/data/schema.json