3
3
import os
4
4
import shutil
5
5
import subprocess
6
- import unittest
7
6
8
7
import numpy as np
9
8
@@ -41,18 +40,18 @@ def GetTableSchema(self,table_dict):
41
40
class FakeApiClient :
42
41
def __init__ (self ):
43
42
self ._fakejobs = FakeJobs ()
44
-
43
+
45
44
46
45
def jobs (self ):
47
46
return self ._fakejobs
48
47
49
48
class FakeJobs :
50
- def __init__ (self ):
49
+ def __init__ (self ):
51
50
self ._fakequeryresults = FakeResults ()
52
51
53
52
def getQueryResults (self , job_id = None , project_id = None ,
54
53
max_results = None , timeout_ms = None , ** kwargs ):
55
- return self ._fakequeryresults
54
+ return self ._fakequeryresults
56
55
57
56
class FakeResults :
58
57
def execute (self ):
@@ -74,7 +73,7 @@ def execute(self):
74
73
75
74
####################################################################################
76
75
77
- class test_gbq ( unittest .TestCase ):
76
+ class TestGbq ( tm .TestCase ):
78
77
def setUp (self ):
79
78
with open (self .fake_job_path , 'r' ) as fin :
80
79
self .fake_job = ast .literal_eval (fin .read ())
@@ -102,7 +101,7 @@ def setUp(self):
102
101
('othello' , 1603 , 'brawl' , 2 ),
103
102
('othello' , 1603 , "'" , 17 ),
104
103
('othello' , 1603 , 'troubled' , 1 )
105
- ],
104
+ ],
106
105
dtype = [('corpus' , 'S16' ),
107
106
('corpus_date' , '<i8' ),
108
107
('word' , 'S16' ),
@@ -137,17 +136,18 @@ def setUp(self):
137
136
'TRUE_BOOLEAN' ,
138
137
'FALSE_BOOLEAN' ,
139
138
'NULL_BOOLEAN' ]]
140
-
139
+
141
140
@classmethod
142
141
def setUpClass (self ):
143
142
# Integration tests require a valid bigquery token
144
143
# be present in the user's home directory. This
145
144
# can be generated with 'bq init' in the command line
145
+ super (TestGbq , cls ).setupClass ()
146
146
self .dirpath = tm .get_data_path ()
147
147
home = os .path .expanduser ("~" )
148
148
self .bq_token = os .path .join (home , '.bigquery.v2.token' )
149
149
self .fake_job_path = os .path .join (self .dirpath , 'gbq_fake_job.txt' )
150
-
150
+
151
151
# If we're using a valid token, make a test dataset
152
152
# Note, dataset functionality is beyond the scope
153
153
# of the module under test, so we rely on the command
@@ -167,7 +167,7 @@ def test_valid_authentication(self):
167
167
# If the user has a token file, they should recieve a client from gbq._authenticate
168
168
if not os .path .exists (self .bq_token ):
169
169
raise nose .SkipTest ('Skipped because authentication information is not available.' )
170
-
170
+
171
171
self .assertTrue (gbq ._authenticate is not None , 'Authentication To GBQ Failed' )
172
172
173
173
@with_connectivity_check
@@ -205,14 +205,14 @@ def test_data_small(self):
205
205
'An element in the result DataFrame didn\' t match the sample set' )
206
206
207
207
def test_index_column (self ):
208
- # A user should be able to specify an index column for return
208
+ # A user should be able to specify an index column for return
209
209
result_frame = gbq ._parse_data (FakeClient (), self .fake_job , index_col = 'word' )
210
210
correct_frame = DataFrame (self .correct_data_small )
211
211
correct_frame .set_index ('word' , inplace = True )
212
212
self .assertTrue (result_frame .index .name == correct_frame .index .name )
213
213
214
214
def test_column_order (self ):
215
- # A User should be able to specify the order in which columns are returned in the dataframe
215
+ # A User should be able to specify the order in which columns are returned in the dataframe
216
216
col_order = ['corpus_date' , 'word_count' , 'corpus' , 'word' ]
217
217
result_frame = gbq ._parse_data (FakeClient (), self .fake_job , col_order = col_order )
218
218
tm .assert_index_equal (result_frame .columns , DataFrame (self .correct_data_small )[col_order ].columns )
@@ -279,8 +279,8 @@ def test_download_all_data_types(self):
279
279
@with_connectivity_check
280
280
def test_table_exists (self ):
281
281
# Given a table name in the format {dataset}.{tablename}, if a table exists,
282
- # the GetTableReference should accurately indicate this.
283
- # This could possibly change in future implementations of bq,
282
+ # the GetTableReference should accurately indicate this.
283
+ # This could possibly change in future implementations of bq,
284
284
# but it is the simplest way to provide users with appropriate
285
285
# error messages regarding schemas.
286
286
if not os .path .exists (self .bq_token ):
@@ -309,7 +309,7 @@ def test_upload_new_table_schema_error(self):
309
309
df = DataFrame (self .correct_data_small )
310
310
with self .assertRaises (gbq .SchemaMissing ):
311
311
gbq .to_gbq (df , 'pandas_testing_dataset.test_database' , schema = None , col_order = None , if_exists = 'fail' )
312
-
312
+
313
313
@with_connectivity_check
314
314
def test_upload_replace_schema_error (self ):
315
315
# Attempting to replace an existing table without specifying a schema should fail
@@ -319,7 +319,7 @@ def test_upload_replace_schema_error(self):
319
319
df = DataFrame (self .correct_data_small )
320
320
with self .assertRaises (gbq .SchemaMissing ):
321
321
gbq .to_gbq (df , 'pandas_testing_dataset.test_database' , schema = None , col_order = None , if_exists = 'replace' )
322
-
322
+
323
323
@with_connectivity_check
324
324
def test_upload_public_data_error (self ):
325
325
# Attempting to upload to a public, read-only, dataset should fail
@@ -432,7 +432,7 @@ def test_upload_replace(self):
432
432
'contributor_ip' ,'contributor_id' ,'contributor_username' ,'timestamp' ,
433
433
'is_minor' ,'is_bot' ,'reversion_id' ,'comment' ,'num_characters' ])
434
434
gbq .to_gbq (df1 , 'pandas_testing_dataset.test_data5' , schema = schema , col_order = None , if_exists = 'fail' )
435
-
435
+
436
436
array2 = [['TESTING_GBQ' , 999999999 , 'hi' , 0 , True , 9999999999 , '00.000.00.000' , 1 , 'hola' ,
437
437
99999999 , False , False , 1 , 'Jedi' , 11210 ]]
438
438
@@ -441,7 +441,7 @@ def test_upload_replace(self):
441
441
'contributor_ip' ,'contributor_id' ,'contributor_username' ,'timestamp' ,
442
442
'is_minor' ,'is_bot' ,'reversion_id' ,'comment' ,'num_characters' ])
443
443
gbq .to_gbq (df2 , 'pandas_testing_dataset.test_data5' , schema = schema , col_order = None , if_exists = 'replace' )
444
-
444
+
445
445
# Read the table and confirm the new data is all that is there
446
446
a = gbq .read_gbq ("SELECT * FROM pandas_testing_dataset.test_data5" )
447
447
self .assertTrue ((a == df2 ).all ().all ())
0 commit comments