Skip to content

Commit 605fdc7

Browse files
committed
add tests for streaming into a date-partitioned table
1 parent 771337e commit 605fdc7

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed

pandas_gbq/tests/test_gbq.py

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
TABLE_ID = 'new_test'
24+
DPT_TABLE_ID = 'dpt_test'
2425

2526

2627
_IMPORTS = False
@@ -407,7 +408,7 @@ def test_should_return_bigquery_strings_as_python_strings(self):
407408
def test_to_gbq_should_fail_if_invalid_table_name_passed(self):
408409
with tm.assertRaises(gbq.NotFoundException):
409410
gbq.to_gbq(DataFrame(), 'invalid_table_name', project_id="1234")
410-
411+
411412
def test_to_gbq_with_no_project_id_given_should_fail(self):
412413
with tm.assertRaises(TypeError):
413414
gbq.to_gbq(DataFrame(), 'dataset.tablename')
@@ -996,6 +997,8 @@ def setup_method(self, method):
996997
private_key=_get_private_key_path())
997998
self.destination_table = "{0}{1}.{2}".format(self.dataset_prefix, "1",
998999
TABLE_ID)
1000+
self.destination_date_partitioned_table = "{0}{1}.{2}".format(self.dataset_prefix, "1",
1001+
DPT_TABLE_ID)
9991002
self.dataset.create(self.dataset_prefix + "1")
10001003

10011004
@classmethod
@@ -1093,6 +1096,79 @@ def test_upload_data_if_table_exists_replace(self):
10931096
project_id=_get_project_id(),
10941097
private_key=_get_private_key_path())
10951098
assert result['num_rows'][0] == 5
1099+
1100+
def test_upload_data_if_table_exists_replace_dpt_partition(self):
1101+
test_dpt_suffix = "20170101"
1102+
test_size = 10
1103+
df = make_mixed_dataframe_v2(test_size)
1104+
df_different_schema = tm.makeMixedDataFrame()
1105+
1106+
dpt_partition = self.destination_date_partitioned_table + '$' + test_dpt_suffix
1107+
1108+
gbq.to_gbq(df, dpt_partition, _get_project_id(),
1109+
chunksize=10000, private_key=_get_private_key_path())
1110+
1111+
gbq.to_gbq(df_different_schema, dpt_partition,
1112+
_get_project_id(), if_exists='replace',
1113+
private_key=_get_private_key_path())
1114+
1115+
sleep(30)
1116+
1117+
# Test whole table
1118+
result0 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}"
1119+
.format(self.destination_date_partitioned_table),
1120+
project_id=_get_project_id(),
1121+
private_key=_get_private_key_path())
1122+
assert result0['num_rows'][0] == 5
1123+
1124+
# Test destination partition
1125+
result1 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}"
1126+
.format(dpt_partition),
1127+
project_id=_get_project_id(),
1128+
private_key=_get_private_key_path())
1129+
assert result1['num_rows'][0] == 5
1130+
1131+
def test_upload_data_if_table_exists_append_dpt_partition(self):
1132+
test_dpt_suffix = "20170101"
1133+
test_size = 10
1134+
df = make_mixed_dataframe_v2(test_size)
1135+
1136+
dpt_partition = self.destination_date_partitioned_table + '$' + test_dpt_suffix
1137+
1138+
result0 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}"
1139+
.format(dpt_partition),
1140+
project_id=_get_project_id(),
1141+
private_key=_get_private_key_path())
1142+
assert result0['num_rows'][0] == 5
1143+
1144+
gbq.to_gbq(df, dpt_partition,
1145+
_get_project_id(), if_exists='append',
1146+
private_key=_get_private_key_path())
1147+
1148+
result1 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}"
1149+
.format(dpt_partition),
1150+
project_id=_get_project_id(),
1151+
private_key=_get_private_key_path())
1152+
1153+
assert result1['num_rows'][0] == 15
1154+
1155+
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
10961172

10971173
def test_upload_data_if_table_exists_raises_value_error(self):
10981174
test_id = "4"
@@ -1117,7 +1193,7 @@ def test_google_upload_errors_should_raise_exception(self):
11171193
with tm.assertRaises(gbq.StreamingInsertError):
11181194
gbq.to_gbq(bad_df, self.destination_table + test_id,
11191195
_get_project_id(), private_key=_get_private_key_path())
1120-
1196+
11211197
def test_generate_schema(self):
11221198
df = tm.makeMixedDataFrame()
11231199
schema = gbq._generate_bq_schema(df)

0 commit comments

Comments
 (0)