|
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():
|
@@ -933,6 +934,8 @@ def setup_method(self, method):
|
933 | 934 | private_key=_get_private_key_path())
|
934 | 935 | self.destination_table = "{0}{1}.{2}".format(self.dataset_prefix, "1",
|
935 | 936 | TABLE_ID)
|
| 937 | + self.destination_dpt = "{0}{1}.{2}".format(self.dataset_prefix, "1", |
| 938 | + DPT_TABLE_ID) |
936 | 939 | self.dataset.create(self.dataset_prefix + "1")
|
937 | 940 |
|
938 | 941 | @classmethod
|
@@ -1056,6 +1059,92 @@ def test_upload_data_if_table_exists_replace(self):
|
1056 | 1059 | private_key=_get_private_key_path())
|
1057 | 1060 | assert result['num_rows'][0] == 5
|
1058 | 1061 |
|
| 1062 | + def test_upload_data_if_table_exists_replace_dpt_partition(self): |
| 1063 | + # Issue #47; tests that 'replace' is done by the subsequent call |
| 1064 | + test_dpt_suffix = "20170101" |
| 1065 | + test_size = 10 |
| 1066 | + df = make_mixed_dataframe_v2(test_size) |
| 1067 | + df_different_schema = tm.makeMixedDataFrame() |
| 1068 | + |
| 1069 | + dpt_partition = self.destination_dpt + '$' + test_dpt_suffix |
| 1070 | + self.table.create(self.destination_dpt, gbq._generate_bq_schema(df)) |
| 1071 | + gbq.to_gbq(df, dpt_partition, _get_project_id(), |
| 1072 | + chunksize=10000, private_key=_get_private_key_path()) |
| 1073 | + |
| 1074 | + gbq.to_gbq(df_different_schema, dpt_partition, |
| 1075 | + _get_project_id(), if_exists='replace', |
| 1076 | + private_key=_get_private_key_path()) |
| 1077 | + |
| 1078 | + sleep(30) |
| 1079 | + |
| 1080 | + # Test whole table |
| 1081 | + result0 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}" |
| 1082 | + .format(self.destination_dpt), |
| 1083 | + project_id=_get_project_id(), |
| 1084 | + private_key=_get_private_key_path()) |
| 1085 | + assert result0['num_rows'][0] == 5 |
| 1086 | + |
| 1087 | + # Test destination partition |
| 1088 | + result1 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}" |
| 1089 | + .format(dpt_partition), |
| 1090 | + project_id=_get_project_id(), |
| 1091 | + private_key=_get_private_key_path()) |
| 1092 | + assert result1['num_rows'][0] == 5 |
| 1093 | + |
| 1094 | + def test_upload_data_if_table_exists_append_dpt_partition(self): |
| 1095 | + # Issue #47; tests that 'append' appends to an existing partition |
| 1096 | + test_dpt_suffix = "20170101" |
| 1097 | + test_size = 10 |
| 1098 | + df = make_mixed_dataframe_v2(test_size) |
| 1099 | + |
| 1100 | + dpt_partition = self.destination_dpt + '$' + test_dpt_suffix |
| 1101 | + |
| 1102 | + result0 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}" |
| 1103 | + .format(dpt_partition), |
| 1104 | + project_id=_get_project_id(), |
| 1105 | + private_key=_get_private_key_path()) |
| 1106 | + assert result0['num_rows'][0] == 5 |
| 1107 | + |
| 1108 | + gbq.to_gbq(df, dpt_partition, |
| 1109 | + _get_project_id(), if_exists='append', |
| 1110 | + private_key=_get_private_key_path()) |
| 1111 | + |
| 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 | + |
| 1117 | + assert result1['num_rows'][0] == 15 |
| 1118 | + |
| 1119 | + sleep(30) |
| 1120 | + |
| 1121 | + # Test whole table |
| 1122 | + result0 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}" |
| 1123 | + .format(self.destination_dpt), |
| 1124 | + project_id=_get_project_id(), |
| 1125 | + private_key=_get_private_key_path()) |
| 1126 | + assert result0['num_rows'][0] == 5 |
| 1127 | + |
| 1128 | + # Test destination partition |
| 1129 | + result1 = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}" |
| 1130 | + .format(dpt_partition), |
| 1131 | + project_id=_get_project_id(), |
| 1132 | + private_key=_get_private_key_path()) |
| 1133 | + assert result1['num_rows'][0] == 10 |
| 1134 | + |
| 1135 | + def test_table_creation_error_raised_when_dpt_does_not_exist(self): |
| 1136 | + test_dpt_suffix = "20170101" |
| 1137 | + test_id = "18" |
| 1138 | + test_size = 10 |
| 1139 | + df = make_mixed_dataframe_v2(test_size) |
| 1140 | + dpt_partition = 'dataset.foobar' + '$' + test_dpt_suffix |
| 1141 | + |
| 1142 | + with pytest.raises(gbq.TableCreationError): |
| 1143 | + gbq.to_gbq(df, |
| 1144 | + dpt_partition, |
| 1145 | + project_id=_get_project_id(), |
| 1146 | + private_key=_get_private_key_path()) |
| 1147 | + |
1059 | 1148 | def test_upload_data_if_table_exists_raises_value_error(self):
|
1060 | 1149 | test_id = "4"
|
1061 | 1150 | test_size = 10
|
|
0 commit comments