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
@@ -375,7 +376,7 @@ def test_import_google_api_python_client(self):
375
376
"using google-api-python-client==1.2" )
376
377
377
378
if compat .PY2 :
378
- with tm . assertRaises (ImportError ):
379
+ with pytest . raises (ImportError ):
379
380
from googleapiclient .discovery import build # noqa
380
381
from googleapiclient .errors import HttpError # noqa
381
382
from apiclient .discovery import build # noqa
@@ -405,15 +406,15 @@ def test_should_return_bigquery_strings_as_python_strings(self):
405
406
tm .assert_equal (result , 'STRING' )
406
407
407
408
def test_to_gbq_should_fail_if_invalid_table_name_passed (self ):
408
- with tm . assertRaises (gbq .NotFoundException ):
409
+ with pytest . raises (gbq .NotFoundException ):
409
410
gbq .to_gbq (DataFrame (), 'invalid_table_name' , project_id = "1234" )
410
411
411
412
def test_to_gbq_with_no_project_id_given_should_fail (self ):
412
- with tm . assertRaises (TypeError ):
413
+ with pytest . raises (TypeError ):
413
414
gbq .to_gbq (DataFrame (), 'dataset.tablename' )
414
415
415
416
def test_read_gbq_with_no_project_id_given_should_fail (self ):
416
- with tm . assertRaises (TypeError ):
417
+ with pytest . raises (TypeError ):
417
418
gbq .read_gbq ('SELECT 1' )
418
419
419
420
def test_that_parse_data_works_properly (self ):
@@ -426,29 +427,29 @@ def test_that_parse_data_works_properly(self):
426
427
tm .assert_frame_equal (test_output , correct_output )
427
428
428
429
def test_read_gbq_with_invalid_private_key_json_should_fail (self ):
429
- with tm . assertRaises (gbq .InvalidPrivateKeyFormat ):
430
+ with pytest . raises (gbq .InvalidPrivateKeyFormat ):
430
431
gbq .read_gbq ('SELECT 1' , project_id = 'x' , private_key = 'y' )
431
432
432
433
def test_read_gbq_with_empty_private_key_json_should_fail (self ):
433
- with tm . assertRaises (gbq .InvalidPrivateKeyFormat ):
434
+ with pytest . raises (gbq .InvalidPrivateKeyFormat ):
434
435
gbq .read_gbq ('SELECT 1' , project_id = 'x' , private_key = '{}' )
435
436
436
437
def test_read_gbq_with_private_key_json_wrong_types_should_fail (self ):
437
- with tm . assertRaises (gbq .InvalidPrivateKeyFormat ):
438
+ with pytest . raises (gbq .InvalidPrivateKeyFormat ):
438
439
gbq .read_gbq (
439
440
'SELECT 1' , project_id = 'x' ,
440
441
private_key = '{ "client_email" : 1, "private_key" : True }' )
441
442
442
443
def test_read_gbq_with_empty_private_key_file_should_fail (self ):
443
444
with tm .ensure_clean () as empty_file_path :
444
- with tm . assertRaises (gbq .InvalidPrivateKeyFormat ):
445
+ with pytest . raises (gbq .InvalidPrivateKeyFormat ):
445
446
gbq .read_gbq ('SELECT 1' , project_id = 'x' ,
446
447
private_key = empty_file_path )
447
448
448
449
def test_read_gbq_with_corrupted_private_key_json_should_fail (self ):
449
450
_skip_if_no_private_key_contents ()
450
451
451
- with tm . assertRaises (gbq .InvalidPrivateKeyFormat ):
452
+ with pytest . raises (gbq .InvalidPrivateKeyFormat ):
452
453
gbq .read_gbq (
453
454
'SELECT 1' , project_id = 'x' ,
454
455
private_key = re .sub ('[a-z]' , '9' , _get_private_key_contents ()))
@@ -725,7 +726,7 @@ def test_read_gbq_raises_invalid_column_order(self):
725
726
col_order = ['string_aaa' , 'string_1' , 'string_2' ]
726
727
727
728
# Column string_aaa does not exist. Should raise InvalidColumnOrder
728
- with tm . assertRaises (gbq .InvalidColumnOrder ):
729
+ with pytest . raises (gbq .InvalidColumnOrder ):
729
730
gbq .read_gbq (query , project_id = _get_project_id (),
730
731
col_order = col_order ,
731
732
private_key = _get_private_key_path ())
@@ -747,24 +748,24 @@ def test_read_gbq_raises_invalid_index_column(self):
747
748
col_order = ['string_3' , 'string_2' ]
748
749
749
750
# Column string_bbb does not exist. Should raise InvalidIndexColumn
750
- with tm . assertRaises (gbq .InvalidIndexColumn ):
751
+ with pytest . raises (gbq .InvalidIndexColumn ):
751
752
gbq .read_gbq (query , project_id = _get_project_id (),
752
753
index_col = 'string_bbb' , col_order = col_order ,
753
754
private_key = _get_private_key_path ())
754
755
755
756
def test_malformed_query (self ):
756
- with tm . assertRaises (gbq .GenericGBQException ):
757
+ with pytest . raises (gbq .GenericGBQException ):
757
758
gbq .read_gbq ("SELCET * FORM [publicdata:samples.shakespeare]" ,
758
759
project_id = _get_project_id (),
759
760
private_key = _get_private_key_path ())
760
761
761
762
def test_bad_project_id (self ):
762
- with tm . assertRaises (gbq .GenericGBQException ):
763
+ with pytest . raises (gbq .GenericGBQException ):
763
764
gbq .read_gbq ("SELECT 1" , project_id = '001' ,
764
765
private_key = _get_private_key_path ())
765
766
766
767
def test_bad_table_name (self ):
767
- with tm . assertRaises (gbq .GenericGBQException ):
768
+ with pytest . raises (gbq .GenericGBQException ):
768
769
gbq .read_gbq ("SELECT * FROM [publicdata:samples.nope]" ,
769
770
project_id = _get_project_id (),
770
771
private_key = _get_private_key_path ())
@@ -800,7 +801,7 @@ def test_legacy_sql(self):
800
801
801
802
# Test that a legacy sql statement fails when
802
803
# setting dialect='standard'
803
- with tm . assertRaises (gbq .GenericGBQException ):
804
+ with pytest . raises (gbq .GenericGBQException ):
804
805
gbq .read_gbq (legacy_sql , project_id = _get_project_id (),
805
806
dialect = 'standard' ,
806
807
private_key = _get_private_key_path ())
@@ -818,7 +819,7 @@ def test_standard_sql(self):
818
819
819
820
# Test that a standard sql statement fails when using
820
821
# the legacy SQL dialect (default value)
821
- with tm . assertRaises (gbq .GenericGBQException ):
822
+ with pytest . raises (gbq .GenericGBQException ):
822
823
gbq .read_gbq (standard_sql , project_id = _get_project_id (),
823
824
private_key = _get_private_key_path ())
824
825
@@ -834,7 +835,7 @@ def test_invalid_option_for_sql_dialect(self):
834
835
"`publicdata.samples.wikipedia` LIMIT 10"
835
836
836
837
# Test that an invalid option for `dialect` raises ValueError
837
- with tm . assertRaises (ValueError ):
838
+ with pytest . raises (ValueError ):
838
839
gbq .read_gbq (sql_statement , project_id = _get_project_id (),
839
840
dialect = 'invalid' ,
840
841
private_key = _get_private_key_path ())
@@ -874,7 +875,7 @@ def test_query_with_parameters(self):
874
875
}
875
876
# Test that a query that relies on parameters fails
876
877
# when parameters are not supplied via configuration
877
- with tm . assertRaises (ValueError ):
878
+ with pytest . raises (ValueError ):
878
879
gbq .read_gbq (sql_statement , project_id = _get_project_id (),
879
880
private_key = _get_private_key_path ())
880
881
@@ -896,7 +897,7 @@ def test_query_inside_configuration(self):
896
897
}
897
898
# Test that it can't pass query both
898
899
# inside config and as parameter
899
- with tm . assertRaises (ValueError ):
900
+ with pytest . raises (ValueError ):
900
901
gbq .read_gbq (query_no_use , project_id = _get_project_id (),
901
902
private_key = _get_private_key_path (),
902
903
configuration = config )
@@ -924,7 +925,7 @@ def test_configuration_without_query(self):
924
925
}
925
926
# Test that only 'query' configurations are supported
926
927
# nor 'copy','load','extract'
927
- with tm . assertRaises (ValueError ):
928
+ with pytest . raises (ValueError ):
928
929
gbq .read_gbq (sql_statement , project_id = _get_project_id (),
929
930
private_key = _get_private_key_path (),
930
931
configuration = config )
@@ -1034,12 +1035,12 @@ def test_upload_data_if_table_exists_fail(self):
1034
1035
self .table .create (TABLE_ID + test_id , gbq ._generate_bq_schema (df ))
1035
1036
1036
1037
# Test the default value of if_exists is 'fail'
1037
- with tm . assertRaises (gbq .TableCreationError ):
1038
+ with pytest . raises (gbq .TableCreationError ):
1038
1039
gbq .to_gbq (df , self .destination_table + test_id , _get_project_id (),
1039
1040
private_key = _get_private_key_path ())
1040
1041
1041
1042
# Test the if_exists parameter with value 'fail'
1042
- with tm . assertRaises (gbq .TableCreationError ):
1043
+ with pytest . raises (gbq .TableCreationError ):
1043
1044
gbq .to_gbq (df , self .destination_table + test_id , _get_project_id (),
1044
1045
if_exists = 'fail' , private_key = _get_private_key_path ())
1045
1046
@@ -1066,7 +1067,7 @@ def test_upload_data_if_table_exists_append(self):
1066
1067
assert result ['num_rows' ][0 ] == test_size * 2
1067
1068
1068
1069
# Try inserting with a different schema, confirm failure
1069
- with tm . assertRaises (gbq .InvalidSchema ):
1070
+ with pytest . raises (gbq .InvalidSchema ):
1070
1071
gbq .to_gbq (df_different_schema , self .destination_table + test_id ,
1071
1072
_get_project_id (), if_exists = 'append' ,
1072
1073
private_key = _get_private_key_path ())
@@ -1100,7 +1101,7 @@ def test_upload_data_if_table_exists_raises_value_error(self):
1100
1101
df = make_mixed_dataframe_v2 (test_size )
1101
1102
1102
1103
# Test invalid value for if_exists parameter raises value error
1103
- with tm . assertRaises (ValueError ):
1104
+ with pytest . raises (ValueError ):
1104
1105
gbq .to_gbq (df , self .destination_table + test_id , _get_project_id (),
1105
1106
if_exists = 'xxxxx' , private_key = _get_private_key_path ())
1106
1107
@@ -1114,7 +1115,7 @@ def test_google_upload_errors_should_raise_exception(self):
1114
1115
'times' : [test_timestamp , test_timestamp ]},
1115
1116
index = range (2 ))
1116
1117
1117
- with tm . assertRaises (gbq .StreamingInsertError ):
1118
+ with pytest . raises (gbq .StreamingInsertError ):
1118
1119
gbq .to_gbq (bad_df , self .destination_table + test_id ,
1119
1120
_get_project_id (), private_key = _get_private_key_path ())
1120
1121
0 commit comments