use of org.apache.flink.table.utils.TableEnvironmentMock in project flink by apache.
the class TableEnvironmentTest method innerTestManagedTableFromDescriptor.
private void innerTestManagedTableFromDescriptor(boolean ignoreIfExists, boolean isTemporary) {
final TableEnvironmentMock tEnv = TableEnvironmentMock.getStreamingInstance();
final String catalog = tEnv.getCurrentCatalog();
final String database = tEnv.getCurrentDatabase();
final Schema schema = Schema.newBuilder().column("f0", DataTypes.INT()).build();
final String tableName = UUID.randomUUID().toString();
ObjectIdentifier identifier = ObjectIdentifier.of(catalog, database, tableName);
// create table
MANAGED_TABLES.put(identifier, new AtomicReference<>());
CreateTableOperation createOperation = new CreateTableOperation(identifier, TableDescriptor.forManaged().schema(schema).option("a", "Test").build().toCatalogTable(), ignoreIfExists, isTemporary);
tEnv.executeInternal(createOperation);
// test ignore: create again
if (ignoreIfExists) {
tEnv.executeInternal(createOperation);
} else {
assertThatThrownBy(() -> tEnv.executeInternal(createOperation), isTemporary ? "already exists" : "Could not execute CreateTable");
}
// lookup table
boolean isInCatalog = tEnv.getCatalog(catalog).orElseThrow(AssertionError::new).tableExists(new ObjectPath(database, tableName));
if (isTemporary) {
assertThat(isInCatalog).isFalse();
} else {
assertThat(isInCatalog).isTrue();
}
final Optional<ContextResolvedTable> lookupResult = tEnv.getCatalogManager().getTable(identifier);
assertThat(lookupResult.isPresent()).isTrue();
final CatalogBaseTable catalogTable = lookupResult.get().getTable();
assertThat(catalogTable instanceof CatalogTable).isTrue();
assertThat(catalogTable.getUnresolvedSchema()).isEqualTo(schema);
assertThat(catalogTable.getOptions().get("a")).isEqualTo("Test");
assertThat(catalogTable.getOptions().get(ENRICHED_KEY)).isEqualTo(ENRICHED_VALUE);
AtomicReference<Map<String, String>> reference = MANAGED_TABLES.get(identifier);
assertThat(reference.get()).isNotNull();
assertThat(reference.get().get("a")).isEqualTo("Test");
assertThat(reference.get().get(ENRICHED_KEY)).isEqualTo(ENRICHED_VALUE);
DropTableOperation dropOperation = new DropTableOperation(identifier, ignoreIfExists, isTemporary);
tEnv.executeInternal(dropOperation);
assertThat(MANAGED_TABLES.get(identifier).get()).isNull();
// test ignore: drop again
if (ignoreIfExists) {
tEnv.executeInternal(dropOperation);
} else {
assertThatThrownBy(() -> tEnv.executeInternal(dropOperation), "does not exist");
}
MANAGED_TABLES.remove(identifier);
}
use of org.apache.flink.table.utils.TableEnvironmentMock in project flink by apache.
the class TableEnvironmentTest method testCreateTemporaryTableFromDescriptor.
@Test
public void testCreateTemporaryTableFromDescriptor() {
final TableEnvironmentMock tEnv = TableEnvironmentMock.getStreamingInstance();
final String catalog = tEnv.getCurrentCatalog();
final String database = tEnv.getCurrentDatabase();
final Schema schema = Schema.newBuilder().column("f0", DataTypes.INT()).build();
tEnv.createTemporaryTable("T", TableDescriptor.forConnector("fake").schema(schema).option("a", "Test").build());
assertThat(tEnv.getCatalog(catalog).orElseThrow(AssertionError::new).tableExists(new ObjectPath(database, "T"))).isFalse();
final Optional<ContextResolvedTable> lookupResult = tEnv.getCatalogManager().getTable(ObjectIdentifier.of(catalog, database, "T"));
assertThat(lookupResult.isPresent()).isTrue();
final CatalogBaseTable catalogTable = lookupResult.get().getTable();
assertThat(catalogTable instanceof CatalogTable).isTrue();
assertThat(catalogTable.getUnresolvedSchema()).isEqualTo(schema);
assertThat(catalogTable.getOptions().get("connector")).isEqualTo("fake");
assertThat(catalogTable.getOptions().get("a")).isEqualTo("Test");
}
use of org.apache.flink.table.utils.TableEnvironmentMock in project flink by apache.
the class TableEnvironmentTest method testTableFromDescriptor.
@Test
public void testTableFromDescriptor() {
final TableEnvironmentMock tEnv = TableEnvironmentMock.getStreamingInstance();
final Schema schema = Schema.newBuilder().column("f0", DataTypes.INT()).build();
final TableDescriptor descriptor = TableDescriptor.forConnector("fake").schema(schema).build();
final Table table = tEnv.from(descriptor);
assertThat(Schema.newBuilder().fromResolvedSchema(table.getResolvedSchema()).build()).isEqualTo(schema);
assertThat(table.getQueryOperation()).asInstanceOf(type(SourceQueryOperation.class)).extracting(SourceQueryOperation::getContextResolvedTable).satisfies(crs -> {
assertThat(crs.isAnonymous()).isTrue();
assertThat(crs.getIdentifier().toList()).hasSize(1);
assertThat(crs.getTable().getOptions()).containsEntry("connector", "fake");
});
assertThat(tEnv.getCatalogManager().listTables()).isEmpty();
}
use of org.apache.flink.table.utils.TableEnvironmentMock in project flink by apache.
the class GenericInMemoryCatalogTest method testRegisterCatalog.
@Test
public void testRegisterCatalog() {
final TableEnvironmentMock tableEnv = TableEnvironmentMock.getStreamingInstance();
try {
tableEnv.registerCatalog(TEST_CATALOG_NAME, new MyCatalog(TEST_CATALOG_NAME));
} catch (CatalogException e) {
}
assertThat(tableEnv.getCatalog(TEST_CATALOG_NAME).isPresent(), equalTo(false));
}
use of org.apache.flink.table.utils.TableEnvironmentMock in project flink by apache.
the class TableEnvironmentTest method testCreateTableFromDescriptor.
@Test
public void testCreateTableFromDescriptor() throws Exception {
final TableEnvironmentMock tEnv = TableEnvironmentMock.getStreamingInstance();
final String catalog = tEnv.getCurrentCatalog();
final String database = tEnv.getCurrentDatabase();
final Schema schema = Schema.newBuilder().column("f0", DataTypes.INT()).build();
tEnv.createTable("T", TableDescriptor.forConnector("fake").schema(schema).option("a", "Test").build());
final ObjectPath objectPath = new ObjectPath(database, "T");
assertThat(tEnv.getCatalog(catalog).orElseThrow(AssertionError::new).tableExists(objectPath)).isTrue();
final CatalogBaseTable catalogTable = tEnv.getCatalog(catalog).orElseThrow(AssertionError::new).getTable(objectPath);
assertThat(catalogTable).isInstanceOf(CatalogTable.class);
assertThat(catalogTable.getUnresolvedSchema()).isEqualTo(schema);
assertThat(catalogTable.getOptions()).contains(entry("connector", "fake"), entry("a", "Test"));
}
Aggregations