forked from googleapis/python-bigquery
/
conftest.py
117 lines (89 loc) · 3.74 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pathlib
import re
import pytest
import test_utils.prefixer
from google.cloud import bigquery
from google.cloud.bigquery import enums
from . import helpers
prefixer = test_utils.prefixer.Prefixer("python-bigquery", "tests/system")
DATA_DIR = pathlib.Path(__file__).parent.parent / "data"
@pytest.fixture(scope="session", autouse=True)
def cleanup_datasets(bigquery_client: bigquery.Client):
for dataset in bigquery_client.list_datasets():
if prefixer.should_cleanup(dataset.dataset_id):
bigquery_client.delete_dataset(
dataset, delete_contents=True, not_found_ok=True
)
@pytest.fixture(scope="session")
def bigquery_client():
return bigquery.Client()
@pytest.fixture(scope="session")
def project_id(bigquery_client: bigquery.Client):
return bigquery_client.project
@pytest.fixture(scope="session")
def bqstorage_client(bigquery_client):
from google.cloud import bigquery_storage
return bigquery_storage.BigQueryReadClient(credentials=bigquery_client._credentials)
@pytest.fixture(scope="session")
def dataset_id(bigquery_client):
dataset_id = prefixer.create_prefix()
bigquery_client.create_dataset(dataset_id)
yield dataset_id
bigquery_client.delete_dataset(dataset_id, delete_contents=True, not_found_ok=True)
@pytest.fixture()
def dataset_client(bigquery_client, dataset_id):
import google.cloud.bigquery.job
return bigquery.Client(
default_query_job_config=google.cloud.bigquery.job.QueryJobConfig(
default_dataset=f"{bigquery_client.project}.{dataset_id}",
)
)
@pytest.fixture
def table_id(dataset_id):
return f"{dataset_id}.table_{helpers.temp_suffix()}"
@pytest.fixture(scope="session")
def scalars_table(bigquery_client: bigquery.Client, project_id: str, dataset_id: str):
schema = bigquery_client.schema_from_json(DATA_DIR / "scalars_schema.json")
job_config = bigquery.LoadJobConfig()
job_config.schema = schema
job_config.source_format = enums.SourceFormat.NEWLINE_DELIMITED_JSON
full_table_id = f"{project_id}.{dataset_id}.scalars"
with open(DATA_DIR / "scalars.jsonl", "rb") as data_file:
job = bigquery_client.load_table_from_file(
data_file, full_table_id, job_config=job_config
)
job.result()
yield full_table_id
bigquery_client.delete_table(full_table_id)
@pytest.fixture(scope="session")
def scalars_extreme_table(
bigquery_client: bigquery.Client, project_id: str, dataset_id: str
):
schema = bigquery_client.schema_from_json(DATA_DIR / "scalars_schema.json")
job_config = bigquery.LoadJobConfig()
job_config.schema = schema
job_config.source_format = enums.SourceFormat.NEWLINE_DELIMITED_JSON
full_table_id = f"{project_id}.{dataset_id}.scalars_extreme"
with open(DATA_DIR / "scalars_extreme.jsonl", "rb") as data_file:
job = bigquery_client.load_table_from_file(
data_file, full_table_id, job_config=job_config
)
job.result()
yield full_table_id
bigquery_client.delete_table(full_table_id)
@pytest.fixture
def test_table_name(request, replace_non_anum=re.compile(r"[^a-zA-Z0-9_]").sub):
return replace_non_anum("_", request.node.name)