Skip to content

Commit

Permalink
#43 Adapt to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Nov 22, 2021
1 parent 97cf1a0 commit 1da5169
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 100 deletions.
@@ -1,137 +1,98 @@
package com.exasol.adapter.dialects.postgresql;

import java.sql.*;
import java.sql.Date;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;

import com.exasol.adapter.commontests.scalarfunction.*;
import com.exasol.adapter.commontests.scalarfunction.ScalarFunctionsTestBase;
import com.exasol.adapter.commontests.scalarfunction.TestSetup;
import com.exasol.adapter.commontests.scalarfunction.virtualschematestsetup.*;
import com.exasol.adapter.commontests.scalarfunction.virtualschematestsetup.request.Column;
import com.exasol.adapter.commontests.scalarfunction.virtualschematestsetup.request.TableRequest;
import com.exasol.adapter.metadata.DataType;
import com.exasol.closeafterall.CloseAfterAll;
import com.exasol.closeafterall.CloseAfterAllExtension;
import com.exasol.dbbuilder.dialects.Schema;
import com.exasol.dbbuilder.dialects.Table;
import com.exasol.dbbuilder.dialects.exasol.VirtualSchema;
import com.exasol.dbbuilder.dialects.postgres.PostgreSqlObjectFactory;

@ExtendWith({ CloseAfterAllExtension.class })
class PostgreSQLScalarFunctionsIT extends ScalarFunctionsTestBase {
@CloseAfterAll
private static final PostgresVirtualSchemaIntegrationTestSetup SETUP = new PostgresVirtualSchemaIntegrationTestSetup();

@Override
protected Set<String> getDialectSpecificExcludes() {
return Collections.emptySet();
}

@BeforeAll
static void beforeAll() {
}
protected TestSetup getTestSetup() {
final PostgreSqlObjectFactory postgresFactory = SETUP.getPostgresFactory();
return new TestSetup() {

@Override
protected SingleTableVirtualSchemaTestSetup createVirtualSchemaTableWithExamplesForAllDataTypes() {
return new PostgreSQLSingleTableVirtualSchemaTestSetup() {
@Override
protected Table createTable() {
return this.getPostgresqlSchema().createTableBuilder(getUniqueIdentifier())//
.column("floating_point", "real")//
.column("number", "integer")//
.column("boolean", "boolean")//
.column("string", "VARCHAR(2)")//
.column("date", "DATE")//
.column("timestamp", "TIMESTAMP").build()
.insert(0.5, 2, true, "a", new Date(1000), new Timestamp(1001));
public VirtualSchemaTestSetupProvider getVirtualSchemaTestSetupProvider() {
return (final CreateVirtualSchemaTestSetupRequest request) -> {
final Schema postgresSchema = postgresFactory.createSchema(getUniqueIdentifier());
for (final TableRequest tableRequest : request.getTableRequests()) {
final Table.Builder tableBuilder = postgresSchema.createTableBuilder(tableRequest.getName());
for (final Column column : tableRequest.getColumns()) {
tableBuilder.column(column.getName(), column.getType());
}
final Table table = tableBuilder.build();
for (final List<Object> row : tableRequest.getRows()) {
table.insert(row.toArray());
}
}

final VirtualSchema virtualSchema = SETUP.createVirtualSchema(postgresSchema.getName(),
Map.of("IGNORE_ERRORS", "POSTGRESQL_UPPERCASE_TABLES"));

return new PostgreSQLSingleTableVirtualSchemaTestSetup(virtualSchema, postgresSchema);
};
}
};
}

@Override
protected SingleRowSingleTableVirtualSchemaTestSetup<Timestamp> createDateVirtualSchemaTable() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
return createTestTable("timestamp");
}

@Override
protected SingleRowSingleTableVirtualSchemaTestSetup<Integer> createIntegerVirtualSchemaTable() {
return createTestTable("integer");
}

@Override
protected SingleRowSingleTableVirtualSchemaTestSetup<Double> createDoubleVirtualSchemaTable() {
return createTestTable("real");
}
@Override
public String getExternalTypeFor(final DataType exasolType) {
return exasolType.toString();
}

@Override
protected SingleRowSingleTableVirtualSchemaTestSetup<Boolean> createBooleanVirtualSchemaTable() {
return createTestTable("boolean");
}
@Override
public Set<String> getDialectSpecificExcludes() {
return Collections.emptySet();
}

private <T> SingleRowSingleTableVirtualSchemaTestSetup<T> createTestTable(final String type) {
return new SingleRowPostgreSQLSingleTableVirtualSchemaTestSetup<>() {
@Override
protected Table createTable() {
return this.getPostgresqlSchema().createTableBuilder(getUniqueIdentifier())//
.column("my_column", type).build();
public Connection createExasolConnection() throws SQLException {
return SETUP.getExasolContainer().createConnection();
}
};
}

@Override
protected Connection createExasolConnection() throws SQLException {
return SETUP.getExasolContainer().createConnection();
}

@Override
protected SingleRowSingleTableVirtualSchemaTestSetup<String> createStringVirtualSchemaTable() {
return createTestTable("VARCHAR(500)");
}

private static abstract class PostgreSQLSingleTableVirtualSchemaTestSetup
implements SingleTableVirtualSchemaTestSetup {
private static class PostgreSQLSingleTableVirtualSchemaTestSetup implements VirtualSchemaTestSetup {
private final VirtualSchema virtualSchema;
private final Table table;
private final Schema postgresqlSchema;

public PostgreSQLSingleTableVirtualSchemaTestSetup() {
this.postgresqlSchema = SETUP.getPostgresFactory().createSchema(getUniqueIdentifier());
this.table = createTable();
this.virtualSchema = SETUP.createVirtualSchema(this.postgresqlSchema.getName(), Map.of());
private PostgreSQLSingleTableVirtualSchemaTestSetup(final VirtualSchema virtualSchema,
final Schema postgresqlSchema) {
this.virtualSchema = virtualSchema;
this.postgresqlSchema = postgresqlSchema;
}

protected abstract Table createTable();

@Override
public String getFullyQualifiedName() {
return this.virtualSchema.getFullyQualifiedName() + "." + this.table.getName();
return this.virtualSchema.getFullyQualifiedName();
}

@Override
public void drop() {
public void close() {
this.virtualSchema.drop();
this.table.drop();
this.postgresqlSchema.drop();
}

public Schema getPostgresqlSchema() {
return this.postgresqlSchema;
}

public Table getTable() {
return this.table;
}
}

private static abstract class SingleRowPostgreSQLSingleTableVirtualSchemaTestSetup<T> extends
PostgreSQLSingleTableVirtualSchemaTestSetup implements SingleRowSingleTableVirtualSchemaTestSetup<T> {

@Override
public void truncateTable() throws SQLException {
SETUP.getPostgresqlStatement().executeUpdate("TRUNCATE TABLE " + this.getTable().getFullyQualifiedName());
}

@Override
public void insertValue(final T value) {
getTable().insert(value);
}
@BeforeAll
static void beforeAll() {
}

}
Expand Up @@ -2,8 +2,7 @@

import static com.exasol.dbbuilder.dialects.exasol.AdapterScript.Language.JAVA;

import java.io.Closeable;
import java.io.IOException;
import java.io.*;
import java.nio.file.Path;
import java.sql.*;
import java.util.HashMap;
Expand All @@ -15,6 +14,7 @@
import com.exasol.bucketfs.Bucket;
import com.exasol.bucketfs.BucketAccessException;
import com.exasol.containers.ExasolContainer;
import com.exasol.containers.ExasolService;
import com.exasol.dbbuilder.dialects.exasol.*;
import com.exasol.dbbuilder.dialects.postgres.PostgreSqlObjectFactory;
import com.exasol.errorreporting.ExaError;
Expand All @@ -25,20 +25,21 @@
* This class contains the common integration test setup for all PostgreSQL virtual schemas.
*/
public class PostgresVirtualSchemaIntegrationTestSetup implements Closeable {
private static final String VIRTUAL_SCHEMAS_JAR_NAME_AND_VERSION = "virtual-schema-dist-9.0.1-postgresql-2.0.0.jar";
private static final String VIRTUAL_SCHEMAS_JAR_NAME_AND_VERSION = "virtual-schema-dist-9.0.4-postgresql-2.0.1.jar";
private static final Path PATH_TO_VIRTUAL_SCHEMAS_JAR = Path.of("target", VIRTUAL_SCHEMAS_JAR_NAME_AND_VERSION);
private static final String SCHEMA_EXASOL = "SCHEMA_EXASOL";
private static final String ADAPTER_SCRIPT_EXASOL = "ADAPTER_SCRIPT_EXASOL";
private static final String EXASOL_DOCKER_IMAGE_REFERENCE = "7.0.5";
private static final String POSTGRES_CONTAINER_NAME = "postgres:13.1";
private static final String EXASOL_DOCKER_IMAGE_REFERENCE = "7.1.2";
private static final String POSTGRES_CONTAINER_NAME = "postgres:14.1";
private static final String JDBC_DRIVER_NAME = "postgresql.jar";
static final Path JDBC_DRIVER_PATH = Path.of("target/postgresql-driver/" + JDBC_DRIVER_NAME);
private static final int POSTGRES_PORT = 5432;
private final Statement postgresStatement;
private final PostgreSQLContainer<? extends PostgreSQLContainer<?>> postgresqlContainer = new PostgreSQLContainer<>(
POSTGRES_CONTAINER_NAME);
private final ExasolContainer<? extends ExasolContainer<?>> exasolContainer = new ExasolContainer<>(
EXASOL_DOCKER_IMAGE_REFERENCE).withReuse(true);
EXASOL_DOCKER_IMAGE_REFERENCE).withRequiredServices(ExasolService.BUCKETFS, ExasolService.UDF)
.withReuse(true);
private final Connection exasolConection;
private final Statement exasolStatement;
private final AdapterScript adapterScript;
Expand Down Expand Up @@ -83,7 +84,7 @@ private static void uploadDriverToBucket(final Bucket bucket)
throws InterruptedException, TimeoutException, BucketAccessException {
try {
bucket.uploadFile(JDBC_DRIVER_PATH, JDBC_DRIVER_NAME);
} catch (final BucketAccessException exception) {
} catch (final BucketAccessException | FileNotFoundException exception) {
throw new IllegalStateException(
ExaError.messageBuilder("F-PGVS-8")
.message("An error occurred while uploading the jdbc driver to the bucket.")
Expand All @@ -94,9 +95,12 @@ private static void uploadDriverToBucket(final Bucket bucket)
}
}

private static void uploadVsJarToBucket(final Bucket bucket)
throws InterruptedException, BucketAccessException, TimeoutException {
bucket.uploadFile(PATH_TO_VIRTUAL_SCHEMAS_JAR, VIRTUAL_SCHEMAS_JAR_NAME_AND_VERSION);
private static void uploadVsJarToBucket(final Bucket bucket) {
try {
bucket.uploadFile(PATH_TO_VIRTUAL_SCHEMAS_JAR, VIRTUAL_SCHEMAS_JAR_NAME_AND_VERSION);
} catch (FileNotFoundException | BucketAccessException | TimeoutException exception) {
throw new IllegalStateException("Failed to upload jar to bucket " + bucket, exception);
}
}

private AdapterScript createAdapterScript(final ExasolSchema schema) {
Expand Down

0 comments on commit 1da5169

Please sign in to comment.