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,79 @@ 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
+ def test_upload_data_if_table_exists_replace_dpt_partition (self ):
1101
+ # Issue #47; tests that 'replace' is done by the subsequent call
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
+ # Issue #47; tests that 'append' appends to an existing partition
1134
+ test_dpt_suffix = "20170101"
1135
+ test_size = 10
1136
+ df = make_mixed_dataframe_v2 (test_size )
1137
+
1138
+ dpt_partition = self .destination_date_partitioned_table + '$' + test_dpt_suffix
1139
+
1140
+ result0 = gbq .read_gbq ("SELECT COUNT(*) AS num_rows FROM {0}"
1141
+ .format (dpt_partition ),
1142
+ project_id = _get_project_id (),
1143
+ private_key = _get_private_key_path ())
1144
+ assert result0 ['num_rows' ][0 ] == 5
1145
+
1146
+ gbq .to_gbq (df , dpt_partition ,
1147
+ _get_project_id (), if_exists = 'append' ,
1148
+ private_key = _get_private_key_path ())
1149
+
1150
+ result1 = gbq .read_gbq ("SELECT COUNT(*) AS num_rows FROM {0}"
1151
+ .format (dpt_partition ),
1152
+ project_id = _get_project_id (),
1153
+ private_key = _get_private_key_path ())
1154
+
1155
+ assert result1 ['num_rows' ][0 ] == 15
1156
+
1157
+ sleep (30 )
1158
+
1159
+ # Test whole table
1160
+ result0 = gbq .read_gbq ("SELECT COUNT(*) AS num_rows FROM {0}"
1161
+ .format (self .destination_date_partitioned_table ),
1162
+ project_id = _get_project_id (),
1163
+ private_key = _get_private_key_path ())
1164
+ assert result0 ['num_rows' ][0 ] == 5
1165
+
1166
+ # Test destination partition
1167
+ result1 = gbq .read_gbq ("SELECT COUNT(*) AS num_rows FROM {0}"
1168
+ .format (dpt_partition ),
1169
+ project_id = _get_project_id (),
1170
+ private_key = _get_private_key_path ())
1171
+ assert result1 ['num_rows' ][0 ] == 10
1172
+
1097
1173
def test_upload_data_if_table_exists_raises_value_error (self ):
1098
1174
test_id = "4"
1099
1175
test_size = 10
@@ -1117,7 +1193,7 @@ def test_google_upload_errors_should_raise_exception(self):
1117
1193
with tm .assertRaises (gbq .StreamingInsertError ):
1118
1194
gbq .to_gbq (bad_df , self .destination_table + test_id ,
1119
1195
_get_project_id (), private_key = _get_private_key_path ())
1120
-
1196
+
1121
1197
def test_generate_schema (self ):
1122
1198
df = tm .makeMixedDataFrame ()
1123
1199
schema = gbq ._generate_bq_schema (df )
0 commit comments