|
22 | 22 | import org.apache.seatunnel.api.table.catalog.CatalogTableUtil;
|
23 | 23 | import org.apache.seatunnel.api.table.catalog.InMemoryCatalog;
|
24 | 24 | import org.apache.seatunnel.api.table.catalog.InMemoryCatalogFactory;
|
| 25 | +import org.apache.seatunnel.api.table.catalog.TablePath; |
25 | 26 | import org.apache.seatunnel.api.table.type.BasicType;
|
26 | 27 | import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
|
27 | 28 | import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
|
| 29 | +import org.apache.seatunnel.common.exception.SeaTunnelRuntimeException; |
28 | 30 |
|
29 | 31 | import org.junit.jupiter.api.BeforeEach;
|
30 | 32 | import org.junit.jupiter.api.Test;
|
31 | 33 |
|
32 | 34 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
| 35 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
33 | 36 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
| 37 | +import static org.mockito.ArgumentMatchers.any; |
| 38 | +import static org.mockito.ArgumentMatchers.eq; |
| 39 | +import static org.mockito.Mockito.mock; |
| 40 | +import static org.mockito.Mockito.times; |
| 41 | +import static org.mockito.Mockito.verify; |
| 42 | +import static org.mockito.Mockito.when; |
34 | 43 |
|
35 | 44 | public class DefaultSaveModeHandlerTest {
|
36 | 45 |
|
@@ -115,6 +124,61 @@ public void shouldNotTruncateRecreatedTable() {
|
115 | 124 | "Should not truncate data for recreated table");
|
116 | 125 | }
|
117 | 126 |
|
| 127 | + @Test |
| 128 | + public void handlesErrorWhenSchemaNotExist() { |
| 129 | + Catalog catalog = mock(Catalog.class); |
| 130 | + CatalogTable catalogTable = createCatalogTable("notExistsTable"); |
| 131 | + when(catalog.tableExists(any(TablePath.class))).thenReturn(false); |
| 132 | + DefaultSaveModeHandler handler = |
| 133 | + new DefaultSaveModeHandler( |
| 134 | + SchemaSaveMode.ERROR_WHEN_SCHEMA_NOT_EXIST, |
| 135 | + DataSaveMode.APPEND_DATA, |
| 136 | + catalog, |
| 137 | + catalogTable, |
| 138 | + null); |
| 139 | + |
| 140 | + assertThrows(SeaTunnelRuntimeException.class, handler::handleSchemaSaveModeWithRestore); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void createsSchemaWhenNotExist() { |
| 145 | + CatalogTable catalogTable = createCatalogTable("notExistsTable"); |
| 146 | + |
| 147 | + Catalog catalog = mock(Catalog.class); |
| 148 | + when(catalog.tableExists(any(TablePath.class))).thenReturn(false); |
| 149 | + DefaultSaveModeHandler handler = |
| 150 | + new DefaultSaveModeHandler( |
| 151 | + SchemaSaveMode.CREATE_SCHEMA_WHEN_NOT_EXIST, |
| 152 | + DataSaveMode.APPEND_DATA, |
| 153 | + catalog, |
| 154 | + catalogTable, |
| 155 | + null); |
| 156 | + |
| 157 | + handler.handleSchemaSaveModeWithRestore(); |
| 158 | + |
| 159 | + verify(catalog, times(1)) |
| 160 | + .createTable(any(TablePath.class), any(CatalogTable.class), eq(true)); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void recreatesSchemaWhenNotExist() { |
| 165 | + CatalogTable catalogTable = createCatalogTable("notExistsTable"); |
| 166 | + Catalog catalog = mock(Catalog.class); |
| 167 | + when(catalog.tableExists(any(TablePath.class))).thenReturn(false); |
| 168 | + DefaultSaveModeHandler handler = |
| 169 | + new DefaultSaveModeHandler( |
| 170 | + SchemaSaveMode.RECREATE_SCHEMA, |
| 171 | + DataSaveMode.APPEND_DATA, |
| 172 | + catalog, |
| 173 | + catalogTable, |
| 174 | + null); |
| 175 | + |
| 176 | + handler.handleSchemaSaveModeWithRestore(); |
| 177 | + |
| 178 | + verify(catalog, times(1)) |
| 179 | + .createTable(any(TablePath.class), any(CatalogTable.class), eq(true)); |
| 180 | + } |
| 181 | + |
118 | 182 | private CatalogTable createCatalogTable(String tableName) {
|
119 | 183 | return CatalogTableUtil.getCatalogTable("", "st", "public", tableName, rowType);
|
120 | 184 | }
|
|
0 commit comments