@@ -892,6 +892,8 @@ def test_tokyo(self, tokyo_dataset, tokyo_table, private_key_path):
892
892
class TestToGBQIntegration (object ):
893
893
@pytest .fixture (autouse = True , scope = "function" )
894
894
def setup (self , project , credentials , random_dataset_id ):
895
+ from google .cloud import bigquery
896
+
895
897
# - PER-TEST FIXTURES -
896
898
# put here any instruction you want to be run *BEFORE* *EVERY* test is
897
899
# executed.
@@ -900,6 +902,9 @@ def setup(self, project, credentials, random_dataset_id):
900
902
)
901
903
self .destination_table = "{}.{}" .format (random_dataset_id , TABLE_ID )
902
904
self .credentials = credentials
905
+ self .bqclient = bigquery .Client (
906
+ project = project , credentials = credentials
907
+ )
903
908
904
909
def test_upload_data (self , project_id ):
905
910
test_id = "1"
@@ -926,7 +931,6 @@ def test_upload_data(self, project_id):
926
931
927
932
def test_upload_empty_data (self , project_id ):
928
933
test_id = "data_with_0_rows"
929
- test_size = 0
930
934
df = DataFrame ()
931
935
932
936
gbq .to_gbq (
@@ -936,15 +940,31 @@ def test_upload_empty_data(self, project_id):
936
940
credentials = self .credentials ,
937
941
)
938
942
939
- result = gbq .read_gbq (
940
- "SELECT COUNT(*) AS num_rows FROM {0}" .format (
941
- self .destination_table + test_id
942
- ),
943
- project_id = project_id ,
943
+ table = self .bqclient .get_table (self .destination_table + test_id )
944
+ assert table .num_rows == 0
945
+ assert len (table .schema ) == 0
946
+
947
+ def test_upload_empty_data_with_schema (self , project_id ):
948
+ test_id = "data_with_0_rows"
949
+ df = DataFrame (
950
+ {
951
+ "a" : pandas .Series (dtype = "int64" ),
952
+ "b" : pandas .Series (dtype = "object" ),
953
+ }
954
+ )
955
+
956
+ gbq .to_gbq (
957
+ df ,
958
+ self .destination_table + test_id ,
959
+ project_id ,
944
960
credentials = self .credentials ,
945
- dialect = "legacy" ,
946
961
)
947
- assert result ["num_rows" ][0 ] == test_size
962
+
963
+ table = self .bqclient .get_table (self .destination_table + test_id )
964
+ assert table .num_rows == 0
965
+ schema = table .schema
966
+ assert schema [0 ].field_type == "INTEGER"
967
+ assert schema [1 ].field_type == "STRING"
948
968
949
969
def test_upload_data_if_table_exists_fail (self , project_id ):
950
970
test_id = "2"
0 commit comments