@@ -1258,6 +1258,49 @@ def test_verify_schema_ignores_field_mode(self):
1258
1258
assert self .sut .verify_schema (
1259
1259
self .dataset_prefix + "1" , TABLE_ID + test_id , test_schema_2 )
1260
1260
1261
+ # Issue #46; tests test scenarios with user-provided
1262
+ # schemas
1263
+
1264
+ def test_upload_data_with_valid_user_schema (self ):
1265
+ df = tm .makeMixedDataFrame ()
1266
+ test_id = "15"
1267
+ test_schema = [{'name' : 'A' , 'type' : 'FLOAT' },
1268
+ {'name' : 'B' , 'type' : 'FLOAT' },
1269
+ {'name' : 'C' , 'type' : 'STRING' },
1270
+ {'name' : 'D' , 'type' : 'TIMESTAMP' }]
1271
+ destination_table = self .destination_table + test_id
1272
+ gbq .to_gbq (df , destination_table , _get_project_id (),
1273
+ private_key = _get_private_key_path (),
1274
+ table_schema = test_schema )
1275
+ dataset , table = destination_table .split ('.' )
1276
+ assert self .table .verify_schema (dataset , table ,
1277
+ dict (fields = test_schema ))
1278
+
1279
+ def test_upload_data_with_invalid_user_schema_raises_error (self ):
1280
+ df = tm .makeMixedDataFrame ()
1281
+ test_id = "16"
1282
+ test_schema = [{'name' : 'A' , 'type' : 'FLOAT' },
1283
+ {'name' : 'B' , 'type' : 'FLOAT' },
1284
+ {'name' : 'C' , 'type' : 'FLOAT' },
1285
+ {'name' : 'D' , 'type' : 'FLOAT' }]
1286
+ destination_table = self .destination_table + test_id
1287
+ with tm .assertRaises (gbq .StreamingInsertError ):
1288
+ gbq .to_gbq (df , destination_table , _get_project_id (),
1289
+ private_key = _get_private_key_path (),
1290
+ table_schema = test_schema )
1291
+
1292
+ def test_upload_data_with_missing_schema_fields_raises_error (self ):
1293
+ df = tm .makeMixedDataFrame ()
1294
+ test_id = "16"
1295
+ test_schema = [{'name' : 'A' , 'type' : 'FLOAT' },
1296
+ {'name' : 'B' , 'type' : 'FLOAT' },
1297
+ {'name' : 'C' , 'type' : 'FLOAT' }]
1298
+ destination_table = self .destination_table + test_id
1299
+ with tm .assertRaises (gbq .StreamingInsertError ):
1300
+ gbq .to_gbq (df , destination_table , _get_project_id (),
1301
+ private_key = _get_private_key_path (),
1302
+ table_schema = test_schema )
1303
+
1261
1304
def test_list_dataset (self ):
1262
1305
dataset_id = self .dataset_prefix + "1"
1263
1306
assert dataset_id in self .dataset .datasets ()
0 commit comments