Skip to content

Commit 1227127

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

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

pandas_gbq/tests/test_gbq.py

+77-2
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
@@ -1094,6 +1097,78 @@ def test_upload_data_if_table_exists_replace(self):
10941097
private_key=_get_private_key_path())
10951098
assert result['num_rows'][0] == 5
10961099

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+
10971172
def test_upload_data_if_table_exists_raises_value_error(self):
10981173
test_id = "4"
10991174
test_size = 10
@@ -1117,7 +1192,7 @@ def test_google_upload_errors_should_raise_exception(self):
11171192
with tm.assertRaises(gbq.StreamingInsertError):
11181193
gbq.to_gbq(bad_df, self.destination_table + test_id,
11191194
_get_project_id(), private_key=_get_private_key_path())
1120-
1195+
11211196
def test_generate_schema(self):
11221197
df = tm.makeMixedDataFrame()
11231198
schema = gbq._generate_bq_schema(df)

0 commit comments

Comments
 (0)