21
21
22
22
23
23
TABLE_ID = 'new_test'
24
+ DPT_TABLE_ID = 'dpt_test'
24
25
25
26
26
27
_IMPORTS = False
@@ -407,7 +408,7 @@ def test_should_return_bigquery_strings_as_python_strings(self):
407
408
def test_to_gbq_should_fail_if_invalid_table_name_passed (self ):
408
409
with tm .assertRaises (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
413
with tm .assertRaises (TypeError ):
413
414
gbq .to_gbq (DataFrame (), 'dataset.tablename' )
@@ -996,6 +997,8 @@ def setup_method(self, method):
996
997
private_key = _get_private_key_path ())
997
998
self .destination_table = "{0}{1}.{2}" .format (self .dataset_prefix , "1" ,
998
999
TABLE_ID )
1000
+ self .destination_date_partitioned_table = "{0}{1}.{2}" .format (self .dataset_prefix , "1" ,
1001
+ DPT_TABLE_ID )
999
1002
self .dataset .create (self .dataset_prefix + "1" )
1000
1003
1001
1004
@classmethod
@@ -1094,6 +1097,78 @@ def test_upload_data_if_table_exists_replace(self):
1094
1097
private_key = _get_private_key_path ())
1095
1098
assert result ['num_rows' ][0 ] == 5
1096
1099
1100
+ # Issue #47; tests that 'replace' is done by the subsequent call
1101
+ def test_upload_data_if_table_exists_replace_dpt_partition (self ):
1102
+ test_dpt_suffix = "20170101"
1103
+ test_size = 10
1104
+ df = make_mixed_dataframe_v2 (test_size )
1105
+ df_different_schema = tm .makeMixedDataFrame ()
1106
+
1107
+ dpt_partition = self .destination_date_partitioned_table + '$' + test_dpt_suffix
1108
+
1109
+ gbq .to_gbq (df , dpt_partition , _get_project_id (),
1110
+ chunksize = 10000 , private_key = _get_private_key_path ())
1111
+
1112
+ gbq .to_gbq (df_different_schema , dpt_partition ,
1113
+ _get_project_id (), if_exists = 'replace' ,
1114
+ private_key = _get_private_key_path ())
1115
+
1116
+ sleep (30 )
1117
+
1118
+ # Test whole table
1119
+ result0 = gbq .read_gbq ("SELECT COUNT(*) AS num_rows FROM {0}"
1120
+ .format (self .destination_date_partitioned_table ),
1121
+ project_id = _get_project_id (),
1122
+ private_key = _get_private_key_path ())
1123
+ assert result0 ['num_rows' ][0 ] == 5
1124
+
1125
+ # Test destination partition
1126
+ result1 = gbq .read_gbq ("SELECT COUNT(*) AS num_rows FROM {0}"
1127
+ .format (dpt_partition ),
1128
+ project_id = _get_project_id (),
1129
+ private_key = _get_private_key_path ())
1130
+ assert result1 ['num_rows' ][0 ] == 5
1131
+
1132
+ def test_upload_data_if_table_exists_append_dpt_partition (self ):
1133
+ test_dpt_suffix = "20170101"
1134
+ test_size = 10
1135
+ df = make_mixed_dataframe_v2 (test_size )
1136
+
1137
+ dpt_partition = self .destination_date_partitioned_table + '$' + test_dpt_suffix
1138
+
1139
+ result0 = gbq .read_gbq ("SELECT COUNT(*) AS num_rows FROM {0}"
1140
+ .format (dpt_partition ),
1141
+ project_id = _get_project_id (),
1142
+ private_key = _get_private_key_path ())
1143
+ assert result0 ['num_rows' ][0 ] == 5
1144
+
1145
+ gbq .to_gbq (df , dpt_partition ,
1146
+ _get_project_id (), if_exists = 'append' ,
1147
+ private_key = _get_private_key_path ())
1148
+
1149
+ result1 = gbq .read_gbq ("SELECT COUNT(*) AS num_rows FROM {0}"
1150
+ .format (dpt_partition ),
1151
+ project_id = _get_project_id (),
1152
+ private_key = _get_private_key_path ())
1153
+
1154
+ assert result1 ['num_rows' ][0 ] == 15
1155
+
1156
+ sleep (30 )
1157
+
1158
+ # Test whole table
1159
+ result0 = gbq .read_gbq ("SELECT COUNT(*) AS num_rows FROM {0}"
1160
+ .format (self .destination_date_partitioned_table ),
1161
+ project_id = _get_project_id (),
1162
+ private_key = _get_private_key_path ())
1163
+ assert result0 ['num_rows' ][0 ] == 5
1164
+
1165
+ # Test destination partition
1166
+ result1 = gbq .read_gbq ("SELECT COUNT(*) AS num_rows FROM {0}"
1167
+ .format (dpt_partition ),
1168
+ project_id = _get_project_id (),
1169
+ private_key = _get_private_key_path ())
1170
+ assert result1 ['num_rows' ][0 ] == 10
1171
+
1097
1172
def test_upload_data_if_table_exists_raises_value_error (self ):
1098
1173
test_id = "4"
1099
1174
test_size = 10
@@ -1117,7 +1192,7 @@ def test_google_upload_errors_should_raise_exception(self):
1117
1192
with tm .assertRaises (gbq .StreamingInsertError ):
1118
1193
gbq .to_gbq (bad_df , self .destination_table + test_id ,
1119
1194
_get_project_id (), private_key = _get_private_key_path ())
1120
-
1195
+
1121
1196
def test_generate_schema (self ):
1122
1197
df = tm .makeMixedDataFrame ()
1123
1198
schema = gbq ._generate_bq_schema (df )
0 commit comments