9
9
import logging
10
10
11
11
import numpy as np
12
+ import pytest
12
13
13
14
from distutils .version import StrictVersion
14
15
from pandas import compat
@@ -403,7 +404,7 @@ def test_import_google_api_python_client(self):
403
404
"using google-api-python-client==1.2" )
404
405
405
406
if compat .PY2 :
406
- with tm . assertRaises (ImportError ):
407
+ with pytest . raises (ImportError ):
407
408
from googleapiclient .discovery import build # noqa
408
409
from googleapiclient .errors import HttpError # noqa
409
410
from apiclient .discovery import build # noqa
@@ -433,15 +434,15 @@ def test_should_return_bigquery_strings_as_python_strings(self):
433
434
tm .assert_equal (result , 'STRING' )
434
435
435
436
def test_to_gbq_should_fail_if_invalid_table_name_passed (self ):
436
- with tm . assertRaises (gbq .NotFoundException ):
437
+ with pytest . raises (gbq .NotFoundException ):
437
438
gbq .to_gbq (DataFrame (), 'invalid_table_name' , project_id = "1234" )
438
439
439
440
def test_to_gbq_with_no_project_id_given_should_fail (self ):
440
- with tm . assertRaises (TypeError ):
441
+ with pytest . raises (TypeError ):
441
442
gbq .to_gbq (DataFrame (), 'dataset.tablename' )
442
443
443
444
def test_read_gbq_with_no_project_id_given_should_fail (self ):
444
- with tm . assertRaises (TypeError ):
445
+ with pytest . raises (TypeError ):
445
446
gbq .read_gbq ('SELECT 1' )
446
447
447
448
def test_that_parse_data_works_properly (self ):
@@ -454,29 +455,29 @@ def test_that_parse_data_works_properly(self):
454
455
tm .assert_frame_equal (test_output , correct_output )
455
456
456
457
def test_read_gbq_with_invalid_private_key_json_should_fail (self ):
457
- with tm . assertRaises (gbq .InvalidPrivateKeyFormat ):
458
+ with pytest . raises (gbq .InvalidPrivateKeyFormat ):
458
459
gbq .read_gbq ('SELECT 1' , project_id = 'x' , private_key = 'y' )
459
460
460
461
def test_read_gbq_with_empty_private_key_json_should_fail (self ):
461
- with tm . assertRaises (gbq .InvalidPrivateKeyFormat ):
462
+ with pytest . raises (gbq .InvalidPrivateKeyFormat ):
462
463
gbq .read_gbq ('SELECT 1' , project_id = 'x' , private_key = '{}' )
463
464
464
465
def test_read_gbq_with_private_key_json_wrong_types_should_fail (self ):
465
- with tm . assertRaises (gbq .InvalidPrivateKeyFormat ):
466
+ with pytest . raises (gbq .InvalidPrivateKeyFormat ):
466
467
gbq .read_gbq (
467
468
'SELECT 1' , project_id = 'x' ,
468
469
private_key = '{ "client_email" : 1, "private_key" : True }' )
469
470
470
471
def test_read_gbq_with_empty_private_key_file_should_fail (self ):
471
472
with tm .ensure_clean () as empty_file_path :
472
- with tm . assertRaises (gbq .InvalidPrivateKeyFormat ):
473
+ with pytest . raises (gbq .InvalidPrivateKeyFormat ):
473
474
gbq .read_gbq ('SELECT 1' , project_id = 'x' ,
474
475
private_key = empty_file_path )
475
476
476
477
def test_read_gbq_with_corrupted_private_key_json_should_fail (self ):
477
478
_skip_if_no_private_key_contents ()
478
479
479
- with tm . assertRaises (gbq .InvalidPrivateKeyFormat ):
480
+ with pytest . raises (gbq .InvalidPrivateKeyFormat ):
480
481
gbq .read_gbq (
481
482
'SELECT 1' , project_id = 'x' ,
482
483
private_key = re .sub ('[a-z]' , '9' , _get_private_key_contents ()))
@@ -753,7 +754,7 @@ def test_read_gbq_raises_invalid_column_order(self):
753
754
col_order = ['string_aaa' , 'string_1' , 'string_2' ]
754
755
755
756
# Column string_aaa does not exist. Should raise InvalidColumnOrder
756
- with tm . assertRaises (gbq .InvalidColumnOrder ):
757
+ with pytest . raises (gbq .InvalidColumnOrder ):
757
758
gbq .read_gbq (query , project_id = _get_project_id (),
758
759
col_order = col_order ,
759
760
private_key = _get_private_key_path ())
@@ -775,24 +776,24 @@ def test_read_gbq_raises_invalid_index_column(self):
775
776
col_order = ['string_3' , 'string_2' ]
776
777
777
778
# Column string_bbb does not exist. Should raise InvalidIndexColumn
778
- with tm . assertRaises (gbq .InvalidIndexColumn ):
779
+ with pytest . raises (gbq .InvalidIndexColumn ):
779
780
gbq .read_gbq (query , project_id = _get_project_id (),
780
781
index_col = 'string_bbb' , col_order = col_order ,
781
782
private_key = _get_private_key_path ())
782
783
783
784
def test_malformed_query (self ):
784
- with tm . assertRaises (gbq .GenericGBQException ):
785
+ with pytest . raises (gbq .GenericGBQException ):
785
786
gbq .read_gbq ("SELCET * FORM [publicdata:samples.shakespeare]" ,
786
787
project_id = _get_project_id (),
787
788
private_key = _get_private_key_path ())
788
789
789
790
def test_bad_project_id (self ):
790
- with tm . assertRaises (gbq .GenericGBQException ):
791
+ with pytest . raises (gbq .GenericGBQException ):
791
792
gbq .read_gbq ("SELECT 1" , project_id = '001' ,
792
793
private_key = _get_private_key_path ())
793
794
794
795
def test_bad_table_name (self ):
795
- with tm . assertRaises (gbq .GenericGBQException ):
796
+ with pytest . raises (gbq .GenericGBQException ):
796
797
gbq .read_gbq ("SELECT * FROM [publicdata:samples.nope]" ,
797
798
project_id = _get_project_id (),
798
799
private_key = _get_private_key_path ())
@@ -828,7 +829,7 @@ def test_legacy_sql(self):
828
829
829
830
# Test that a legacy sql statement fails when
830
831
# setting dialect='standard'
831
- with tm . assertRaises (gbq .GenericGBQException ):
832
+ with pytest . raises (gbq .GenericGBQException ):
832
833
gbq .read_gbq (legacy_sql , project_id = _get_project_id (),
833
834
dialect = 'standard' ,
834
835
private_key = _get_private_key_path ())
@@ -846,7 +847,7 @@ def test_standard_sql(self):
846
847
847
848
# Test that a standard sql statement fails when using
848
849
# the legacy SQL dialect (default value)
849
- with tm . assertRaises (gbq .GenericGBQException ):
850
+ with pytest . raises (gbq .GenericGBQException ):
850
851
gbq .read_gbq (standard_sql , project_id = _get_project_id (),
851
852
private_key = _get_private_key_path ())
852
853
@@ -862,7 +863,7 @@ def test_invalid_option_for_sql_dialect(self):
862
863
"`publicdata.samples.wikipedia` LIMIT 10"
863
864
864
865
# Test that an invalid option for `dialect` raises ValueError
865
- with tm . assertRaises (ValueError ):
866
+ with pytest . raises (ValueError ):
866
867
gbq .read_gbq (sql_statement , project_id = _get_project_id (),
867
868
dialect = 'invalid' ,
868
869
private_key = _get_private_key_path ())
@@ -902,7 +903,7 @@ def test_query_with_parameters(self):
902
903
}
903
904
# Test that a query that relies on parameters fails
904
905
# when parameters are not supplied via configuration
905
- with tm . assertRaises (ValueError ):
906
+ with pytest . raises (ValueError ):
906
907
gbq .read_gbq (sql_statement , project_id = _get_project_id (),
907
908
private_key = _get_private_key_path ())
908
909
@@ -924,7 +925,7 @@ def test_query_inside_configuration(self):
924
925
}
925
926
# Test that it can't pass query both
926
927
# inside config and as parameter
927
- with tm . assertRaises (ValueError ):
928
+ with pytest . raises (ValueError ):
928
929
gbq .read_gbq (query_no_use , project_id = _get_project_id (),
929
930
private_key = _get_private_key_path (),
930
931
configuration = config )
@@ -952,7 +953,7 @@ def test_configuration_without_query(self):
952
953
}
953
954
# Test that only 'query' configurations are supported
954
955
# nor 'copy','load','extract'
955
- with tm . assertRaises (ValueError ):
956
+ with pytest . raises (ValueError ):
956
957
gbq .read_gbq (sql_statement , project_id = _get_project_id (),
957
958
private_key = _get_private_key_path (),
958
959
configuration = config )
@@ -1062,12 +1063,12 @@ def test_upload_data_if_table_exists_fail(self):
1062
1063
self .table .create (TABLE_ID + test_id , gbq ._generate_bq_schema (df ))
1063
1064
1064
1065
# Test the default value of if_exists is 'fail'
1065
- with tm . assertRaises (gbq .TableCreationError ):
1066
+ with pytest . raises (gbq .TableCreationError ):
1066
1067
gbq .to_gbq (df , self .destination_table + test_id , _get_project_id (),
1067
1068
private_key = _get_private_key_path ())
1068
1069
1069
1070
# Test the if_exists parameter with value 'fail'
1070
- with tm . assertRaises (gbq .TableCreationError ):
1071
+ with pytest . raises (gbq .TableCreationError ):
1071
1072
gbq .to_gbq (df , self .destination_table + test_id , _get_project_id (),
1072
1073
if_exists = 'fail' , private_key = _get_private_key_path ())
1073
1074
@@ -1094,7 +1095,7 @@ def test_upload_data_if_table_exists_append(self):
1094
1095
assert result ['num_rows' ][0 ] == test_size * 2
1095
1096
1096
1097
# Try inserting with a different schema, confirm failure
1097
- with tm . assertRaises (gbq .InvalidSchema ):
1098
+ with pytest . raises (gbq .InvalidSchema ):
1098
1099
gbq .to_gbq (df_different_schema , self .destination_table + test_id ,
1099
1100
_get_project_id (), if_exists = 'append' ,
1100
1101
private_key = _get_private_key_path ())
@@ -1128,7 +1129,7 @@ def test_upload_data_if_table_exists_raises_value_error(self):
1128
1129
df = make_mixed_dataframe_v2 (test_size )
1129
1130
1130
1131
# Test invalid value for if_exists parameter raises value error
1131
- with tm . assertRaises (ValueError ):
1132
+ with pytest . raises (ValueError ):
1132
1133
gbq .to_gbq (df , self .destination_table + test_id , _get_project_id (),
1133
1134
if_exists = 'xxxxx' , private_key = _get_private_key_path ())
1134
1135
@@ -1142,7 +1143,7 @@ def test_google_upload_errors_should_raise_exception(self):
1142
1143
'times' : [test_timestamp , test_timestamp ]},
1143
1144
index = range (2 ))
1144
1145
1145
- with tm . assertRaises (gbq .StreamingInsertError ):
1146
+ with pytest . raises (gbq .StreamingInsertError ):
1146
1147
gbq .to_gbq (bad_df , self .destination_table + test_id ,
1147
1148
_get_project_id (), private_key = _get_private_key_path ())
1148
1149
0 commit comments