1
+ from contextlib import ExitStack as does_not_raise
1
2
from datetime import datetime
2
3
import os
3
4
import platform
5
+ import random
6
+ import string
4
7
5
8
import numpy as np
6
9
import pytest
18
21
PRIVATE_KEY_JSON_PATH = None
19
22
PRIVATE_KEY_JSON_CONTENTS = None
20
23
21
- DATASET_ID = "pydata_pandas_bq_testing_py3"
22
-
23
- TABLE_ID = "new_test"
24
- DESTINATION_TABLE = "{0}.{1}" .format (DATASET_ID + "1" , TABLE_ID )
25
-
26
24
VERSION = platform .python_version ()
27
25
28
26
@@ -149,34 +147,33 @@ def mock_read_gbq(sql, **kwargs):
149
147
150
148
@pytest .mark .single
151
149
class TestToGBQIntegrationWithServiceAccountKeyPath :
152
- @classmethod
153
- def setup_class (cls ):
154
- # - GLOBAL CLASS FIXTURES -
155
- # put here any instruction you want to execute only *ONCE* *BEFORE*
156
- # executing *ALL* tests described below.
157
-
150
+ @pytest .fixture ()
151
+ def gbq_dataset (self ):
152
+ # Setup Dataset
158
153
_skip_if_no_project_id ()
159
154
_skip_if_no_private_key_path ()
160
155
161
- cls .client = _get_client ()
162
- cls .dataset = cls .client .dataset (DATASET_ID + "1" )
156
+ dataset_id = "pydata_pandas_bq_testing_py31"
157
+
158
+ self .client = _get_client ()
159
+ self .dataset = self .client .dataset (dataset_id )
163
160
try :
164
161
# Clean-up previous test runs.
165
- cls .client .delete_dataset (cls .dataset , delete_contents = True )
162
+ self .client .delete_dataset (self .dataset , delete_contents = True )
166
163
except api_exceptions .NotFound :
167
164
pass # It's OK if the dataset doesn't already exist.
168
165
169
- cls .client .create_dataset (bigquery .Dataset (cls .dataset ))
166
+ self .client .create_dataset (bigquery .Dataset (self .dataset ))
167
+
168
+ table_name = "" .join (random .choices (string .ascii_lowercase , k = 10 ))
169
+ destination_table = f"{ dataset_id } .{ table_name } "
170
+ yield destination_table
170
171
171
- @classmethod
172
- def teardown_class (cls ):
173
- # - GLOBAL CLASS FIXTURES -
174
- # put here any instruction you want to execute only *ONCE* *AFTER*
175
- # executing all tests.
176
- cls .client .delete_dataset (cls .dataset , delete_contents = True )
172
+ # Teardown Dataset
173
+ self .client .delete_dataset (self .dataset , delete_contents = True )
177
174
178
- def test_roundtrip (self ):
179
- destination_table = DESTINATION_TABLE + "1"
175
+ def test_roundtrip (self , gbq_dataset ):
176
+ destination_table = gbq_dataset
180
177
181
178
test_size = 20001
182
179
df = make_mixed_dataframe_v2 (test_size )
@@ -189,21 +186,26 @@ def test_roundtrip(self):
189
186
)
190
187
191
188
result = pd .read_gbq (
192
- "SELECT COUNT(*) AS num_rows FROM {0}" . format ( destination_table ) ,
189
+ f "SELECT COUNT(*) AS num_rows FROM { destination_table } " ,
193
190
project_id = _get_project_id (),
194
191
credentials = _get_credentials (),
195
192
dialect = "standard" ,
196
193
)
197
194
assert result ["num_rows" ][0 ] == test_size
198
195
199
- @pytest .mark .xfail (reason = "Test breaking master" , strict = False )
200
196
@pytest .mark .parametrize (
201
- "if_exists, expected_num_rows" ,
202
- [("append" , 300 ), ("fail" , 200 ), ("replace" , 100 )],
197
+ "if_exists, expected_num_rows, expectation" ,
198
+ [
199
+ ("append" , 300 , does_not_raise ()),
200
+ ("fail" , 200 , pytest .raises (pandas_gbq .gbq .TableCreationError )),
201
+ ("replace" , 100 , does_not_raise ()),
202
+ ],
203
203
)
204
- def test_gbq_if_exists (self , if_exists , expected_num_rows ):
204
+ def test_gbq_if_exists (
205
+ self , if_exists , expected_num_rows , expectation , gbq_dataset
206
+ ):
205
207
# GH 29598
206
- destination_table = DESTINATION_TABLE + "2"
208
+ destination_table = gbq_dataset
207
209
208
210
test_size = 200
209
211
df = make_mixed_dataframe_v2 (test_size )
@@ -215,13 +217,14 @@ def test_gbq_if_exists(self, if_exists, expected_num_rows):
215
217
credentials = _get_credentials (),
216
218
)
217
219
218
- df .iloc [:100 ].to_gbq (
219
- destination_table ,
220
- _get_project_id (),
221
- if_exists = if_exists ,
222
- chunksize = None ,
223
- credentials = _get_credentials (),
224
- )
220
+ with expectation :
221
+ df .iloc [:100 ].to_gbq (
222
+ destination_table ,
223
+ _get_project_id (),
224
+ if_exists = if_exists ,
225
+ chunksize = None ,
226
+ credentials = _get_credentials (),
227
+ )
225
228
226
229
result = pd .read_gbq (
227
230
f"SELECT COUNT(*) AS num_rows FROM { destination_table } " ,
0 commit comments