|
20 | 20 |
|
21 | 21 |
|
22 | 22 | TABLE_ID = 'new_test'
|
| 23 | +DPT_TABLE_ID = 'dpt_test' |
23 | 24 |
|
24 | 25 |
|
25 | 26 | def _skip_if_no_project_id():
|
@@ -946,6 +947,8 @@ def setup_method(self, method):
|
946 | 947 | private_key=_get_private_key_path())
|
947 | 948 | self.destination_table = "{0}{1}.{2}".format(self.dataset_prefix, "1",
|
948 | 949 | TABLE_ID)
|
| 950 | + self.destination_dpt = "{0}{1}.{2}".format(self.dataset_prefix, "1", |
| 951 | + DPT_TABLE_ID) |
949 | 952 | self.dataset.create(self.dataset_prefix + "1")
|
950 | 953 |
|
951 | 954 | @classmethod
|
@@ -1080,6 +1083,94 @@ def test_upload_data_if_table_exists_replace(self):
|
1080 | 1083 | private_key=_get_private_key_path())
|
1081 | 1084 | assert result['num_rows'][0] == 5
|
1082 | 1085 |
|
| 1086 | + def test_upload_data_if_table_exists_replace_dpt_partition(self): |
| 1087 | + # Issue #47; tests that 'replace' is done by the subsequent call |
| 1088 | + test_dpt_suffix = "20170101" |
| 1089 | + test_size = 10 |
| 1090 | + df = make_mixed_dataframe_v2(test_size) |
| 1091 | + df_different_schema = tm.makeMixedDataFrame() |
| 1092 | + |
| 1093 | + dpt_partition = self.destination_dpt + '$' + test_dpt_suffix |
| 1094 | + self.table.create(DPT_TABLE_ID, gbq._generate_bq_schema(df)) |
| 1095 | + gbq.to_gbq(df, dpt_partition, _get_project_id(), |
| 1096 | + private_key=_get_private_key_path()) |
| 1097 | + |
| 1098 | + gbq.to_gbq(df_different_schema, dpt_partition, |
| 1099 | + _get_project_id(), if_exists='replace', |
| 1100 | + private_key=_get_private_key_path()) |
| 1101 | + |
| 1102 | + sleep(30) |
| 1103 | + |
| 1104 | + # Test whole table |
| 1105 | + result0 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}" |
| 1106 | + .format(self.destination_dpt), |
| 1107 | + project_id=_get_project_id(), |
| 1108 | + private_key=_get_private_key_path()) |
| 1109 | + assert result0['num_rows'][0] == 5 |
| 1110 | + |
| 1111 | + # Test destination partition |
| 1112 | + result1 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}" |
| 1113 | + .format(dpt_partition), |
| 1114 | + project_id=_get_project_id(), |
| 1115 | + private_key=_get_private_key_path()) |
| 1116 | + assert result1['num_rows'][0] == 5 |
| 1117 | + self.table.delete(DPT_TABLE_ID) |
| 1118 | + |
| 1119 | + def test_upload_data_if_table_exists_append_dpt_partition(self): |
| 1120 | + # Issue #47; tests that 'append' appends to an existing partition |
| 1121 | + test_dpt_suffix = "20170101" |
| 1122 | + test_size = 10 |
| 1123 | + df = make_mixed_dataframe_v2(test_size) |
| 1124 | + |
| 1125 | + dpt_partition = self.destination_dpt + '$' + test_dpt_suffix |
| 1126 | + self.table.create(DPT_TABLE_ID, gbq._generate_bq_schema(df)) |
| 1127 | + |
| 1128 | + result0 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}" |
| 1129 | + .format(dpt_partition), |
| 1130 | + project_id=_get_project_id(), |
| 1131 | + private_key=_get_private_key_path()) |
| 1132 | + assert result0['num_rows'][0] == 5 |
| 1133 | + |
| 1134 | + gbq.to_gbq(df, dpt_partition, |
| 1135 | + _get_project_id(), if_exists='append', |
| 1136 | + private_key=_get_private_key_path()) |
| 1137 | + |
| 1138 | + result1 = 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 | + |
| 1143 | + assert result1['num_rows'][0] == 15 |
| 1144 | + |
| 1145 | + sleep(30) |
| 1146 | + |
| 1147 | + # Test whole table |
| 1148 | + result0 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}" |
| 1149 | + .format(self.destination_dpt), |
| 1150 | + project_id=_get_project_id(), |
| 1151 | + private_key=_get_private_key_path()) |
| 1152 | + assert result0['num_rows'][0] == 5 |
| 1153 | + |
| 1154 | + # Test destination partition |
| 1155 | + result1 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}" |
| 1156 | + .format(dpt_partition), |
| 1157 | + project_id=_get_project_id(), |
| 1158 | + private_key=_get_private_key_path()) |
| 1159 | + assert result1['num_rows'][0] == 10 |
| 1160 | + self.table.delete(DPT_TABLE_ID) |
| 1161 | + |
| 1162 | + def test_table_creation_error_raised_when_dpt_does_not_exist(self): |
| 1163 | + test_dpt_suffix = "20170101" |
| 1164 | + test_size = 10 |
| 1165 | + df = make_mixed_dataframe_v2(test_size) |
| 1166 | + dpt_partition = 'dataset.foobar' + '$' + test_dpt_suffix |
| 1167 | + |
| 1168 | + with pytest.raises(gbq.TableCreationError): |
| 1169 | + gbq.to_gbq(df, |
| 1170 | + dpt_partition, |
| 1171 | + project_id=_get_project_id(), |
| 1172 | + private_key=_get_private_key_path()) |
| 1173 | + |
1083 | 1174 | def test_upload_data_if_table_exists_raises_value_error(self):
|
1084 | 1175 | test_id = "4"
|
1085 | 1176 | test_size = 10
|
|
0 commit comments