Skip to content

Commit

Permalink
Change integration tests to use constructor instead of @BeforeClass
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed May 28, 2014
1 parent d7a4381 commit 466524d
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ public ExecutorService getExecutor()
return executor;
}

@Override
public ConnectorSession getDefaultSession()
{
return defaultSession;
}

public void createCatalog(String catalogName, ConnectorFactory connectorFactory, Map<String, String> properties)
{
nodeManager.addCurrentNodeDatasource(catalogName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface QueryRunner

int getNodeCount();

ConnectorSession getDefaultSession();

MaterializedResult execute(@Language("SQL") String sql);

MaterializedResult execute(ConnectorSession session, @Language("SQL") String sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,51 @@
*/
package com.facebook.presto.raptor;

import com.facebook.presto.metadata.QualifiedTableName;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.testing.QueryRunner;
import com.facebook.presto.tests.AbstractTestDistributedQueries;
import com.facebook.presto.tests.DistributedQueryRunner;
import com.facebook.presto.tpch.TpchMetadata;
import com.facebook.presto.tpch.TpchPlugin;
import com.facebook.presto.tpch.testing.SampledTpchPlugin;
import com.google.common.collect.ImmutableMap;
import io.airlift.log.Logger;
import org.intellij.lang.annotations.Language;
import org.testng.annotations.AfterClass;

import java.io.File;
import java.util.Map;

import static com.facebook.presto.spi.type.TimeZoneKey.UTC_KEY;
import static com.facebook.presto.util.Types.checkType;
import static io.airlift.units.Duration.nanosSince;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static java.util.concurrent.TimeUnit.SECONDS;

public class TestRaptorDistributedQueries
extends AbstractTestDistributedQueries
{
private ConnectorSession session;
private ConnectorSession sampledSession;
private static final Logger log = Logger.get("TestQueries");
private static final String TPCH_SAMPLED_SCHEMA = "tpch_sampled";

@Override
public ConnectorSession getSession()
public TestRaptorDistributedQueries()
throws Exception
{
return session;
super(createQueryRunner(), createSession(TPCH_SAMPLED_SCHEMA));
}

@Override
public ConnectorSession getSampledSession()
@AfterClass(alwaysRun = true)
public void destroy()
{
return sampledSession;
queryRunner.close();
}

@Override
protected QueryRunner createQueryRunner()
private static QueryRunner createQueryRunner()
throws Exception
{
session = new ConnectorSession("user", "test", "default", "tpch", UTC_KEY, ENGLISH, null, null);
sampledSession = new ConnectorSession("user", "test", "default", "tpch_sampled", UTC_KEY, ENGLISH, null, null);

DistributedQueryRunner queryRunner = new DistributedQueryRunner(session, 4);
DistributedQueryRunner queryRunner = new DistributedQueryRunner(createSession("tpch"), 4);

queryRunner.installPlugin(new TpchPlugin());
queryRunner.createCatalog("tpch", "tpch");
Expand All @@ -61,17 +66,40 @@ protected QueryRunner createQueryRunner()
queryRunner.createCatalog("tpch_sampled", "tpch_sampled");

queryRunner.installPlugin(new RaptorPlugin());

// install raptor plugin
File baseDir = queryRunner.getCoordinator().getBaseDataDir().toFile();

Map<String, String> raptorProperties = ImmutableMap.<String, String>builder()
.put("metadata.db.type", "h2")
.put("metadata.db.filename", new File(baseDir, "db").getAbsolutePath())
.put("storage.data-directory", new File(baseDir, "data").getAbsolutePath())
.build();

queryRunner.createCatalog(session.getCatalog(), "raptor", raptorProperties);
queryRunner.createCatalog("default", "raptor", raptorProperties);

log.info("Loading data...");
long startTime = System.nanoTime();
distributeData(queryRunner, "tpch", TpchMetadata.TINY_SCHEMA_NAME, createSession("tpch"));
distributeData(queryRunner, "tpch_sampled", TpchMetadata.TINY_SCHEMA_NAME, createSession(TPCH_SAMPLED_SCHEMA));
log.info("Loading complete in %s", nanosSince(startTime).toString(SECONDS));

return queryRunner;
}

private static void distributeData(QueryRunner queryRunner, String catalog, String schema, ConnectorSession session)
throws Exception
{
for (QualifiedTableName table : queryRunner.listTables(session, catalog, schema)) {
if (table.getTableName().equalsIgnoreCase("dual")) {
continue;
}
log.info("Running import for %s", table.getTableName());
@Language("SQL") String sql = format("CREATE TABLE %s AS SELECT * FROM %s", table.getTableName(), table);
long rows = checkType(queryRunner.execute(session, sql).getMaterializedRows().get(0).getField(0), Long.class, "rows");
log.info("Imported %s rows for %s", rows, table.getTableName());
}
}

private static ConnectorSession createSession(String schema)
{
return new ConnectorSession("user", "test", "default", schema, UTC_KEY, ENGLISH, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,85 +13,28 @@
*/
package com.facebook.presto.tests;

import com.facebook.presto.metadata.QualifiedTableName;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.testing.MaterializedResult;
import com.facebook.presto.testing.QueryRunner;
import com.facebook.presto.tpch.TpchMetadata;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import io.airlift.log.Logger;
import org.intellij.lang.annotations.Language;
import org.testng.annotations.Test;

import static com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.INFORMATION_SCHEMA;
import static com.facebook.presto.util.Types.checkType;
import static com.google.common.collect.Iterables.transform;
import static io.airlift.units.Duration.nanosSince;
import static java.lang.String.format;
import static java.util.Collections.nCopies;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public abstract class AbstractTestDistributedQueries
extends AbstractTestSampledQueries
{
private static final Logger log = Logger.get("TestQueries");

private QueryRunner queryRunner;

public abstract ConnectorSession getSampledSession();

@Override
protected ConnectorSession setUpQueryFramework()
throws Exception
{
queryRunner = createQueryRunner();

log.info("Loading data...");
long startTime = System.nanoTime();
distributeData("tpch", TpchMetadata.TINY_SCHEMA_NAME, getSession());
distributeData("tpch_sampled", TpchMetadata.TINY_SCHEMA_NAME, getSampledSession());
log.info("Loading complete in %s", nanosSince(startTime).toString(SECONDS));

return getSession();
}

protected abstract QueryRunner createQueryRunner()
throws Exception;

private void distributeData(String sourceCatalog, String sourceSchema, ConnectorSession session)
throws Exception
{
for (QualifiedTableName table : queryRunner.listTables(session, sourceCatalog, sourceSchema)) {
if (table.getTableName().equalsIgnoreCase("dual")) {
continue;
}
log.info("Running import for %s", table.getTableName());
@Language("SQL") String sql = format("CREATE TABLE %s AS SELECT * FROM %s", table.getTableName(), table);
long rows = checkType(queryRunner.execute(session, sql).getMaterializedRows().get(0).getField(0), Long.class, "rows");
log.info("Imported %s rows for %s", rows, table.getTableName());
}
}

@Override
protected int getNodeCount()
protected AbstractTestDistributedQueries(QueryRunner queryRunner, ConnectorSession defaultSampledSession)
{
return queryRunner.getNodeCount();
}

@Override
protected MaterializedResult computeActual(@Language("SQL") String sql)
{
return queryRunner.execute(getSession(), sql);
}

@Override
protected MaterializedResult computeActualSampled(@Language("SQL") String sql)
{
return queryRunner.execute(getSampledSession(), sql);
super(queryRunner, defaultSampledSession);
}

private void assertCreateTable(String table, @Language("SQL") String query, @Language("SQL") String rowCountQuery)
Expand All @@ -110,14 +53,6 @@ private void assertCreateTable(String table, @Language("SQL") String query, @Lan
assertFalse(queryRunner.tableExists(getSession(), table));
}

@Override
protected void tearDownQueryFramework()
throws Exception
{
queryRunner.close();
queryRunner = null;
}

@Test
public void testCreateSampledTableAsSelectLimit()
throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,26 @@
*/
package com.facebook.presto.tests;

import com.facebook.presto.testing.QueryRunner;
import com.facebook.presto.tests.tpch.TpchIndexSpec;
import com.facebook.presto.tests.tpch.TpchIndexSpec.Builder;
import com.facebook.presto.tpch.TpchMetadata;
import com.google.common.collect.ImmutableSet;
import org.testng.annotations.Test;

public abstract class AbstractTestIndexedQueries
extends AbstractTestQueryFramework
{
private final TpchIndexSpec indexSpec;
// Generate the indexed data sets
public static final TpchIndexSpec INDEX_SPEC = new Builder()
.addIndex("orders", TpchMetadata.TINY_SCALE_FACTOR, ImmutableSet.of("orderkey"))
.addIndex("orders", TpchMetadata.TINY_SCALE_FACTOR, ImmutableSet.of("orderkey", "orderstatus"))
.addIndex("orders", TpchMetadata.TINY_SCALE_FACTOR, ImmutableSet.of("orderkey", "custkey"))
.build();

protected AbstractTestIndexedQueries()
protected AbstractTestIndexedQueries(QueryRunner queryRunner)
{
// Generate the indexed data sets
this.indexSpec = new TpchIndexSpec.Builder()
.addIndex("orders", TpchMetadata.TINY_SCALE_FACTOR, ImmutableSet.of("orderkey"))
.addIndex("orders", TpchMetadata.TINY_SCALE_FACTOR, ImmutableSet.of("orderkey", "orderstatus"))
.addIndex("orders", TpchMetadata.TINY_SCALE_FACTOR, ImmutableSet.of("orderkey", "custkey"))
.build();
}

protected TpchIndexSpec getTpchIndexSpec()
{
return indexSpec;
super(queryRunner);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.testing.MaterializedResult;
import com.facebook.presto.testing.MaterializedRow;
import com.facebook.presto.testing.QueryRunner;
import com.facebook.presto.util.DateTimeZoneIndex;
import com.google.common.base.Function;
import com.google.common.collect.ArrayListMultimap;
Expand Down Expand Up @@ -78,6 +79,11 @@ public abstract class AbstractTestQueries
.scalar(CreateHll.class)
.getFunctions();

public AbstractTestQueries(QueryRunner queryRunner)
{
super(queryRunner);
}

@Test
public void testValues()
throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@
import com.facebook.presto.sql.tree.ExplainType;
import com.facebook.presto.testing.MaterializedResult;
import com.facebook.presto.testing.MaterializedRow;
import com.facebook.presto.testing.QueryRunner;
import com.facebook.presto.type.TypeRegistry;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Iterables;
import io.airlift.log.Logger;
import io.airlift.log.Logging;
import io.airlift.units.Duration;
import org.intellij.lang.annotations.Language;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

import java.util.List;

Expand All @@ -47,47 +46,36 @@

public abstract class AbstractTestQueryFramework
{
private H2QueryRunner h2QueryRunner;
private ConnectorSession session;
private final H2QueryRunner h2QueryRunner;
protected final QueryRunner queryRunner;

@BeforeClass(alwaysRun = true)
public void setupDatabase()
throws Exception
protected AbstractTestQueryFramework(QueryRunner queryRunner)
{
Logging.initialize();
this.queryRunner = queryRunner;
h2QueryRunner = new H2QueryRunner();

session = setUpQueryFramework();
}

@AfterClass(alwaysRun = true)
public void cleanupDatabase()
private void close()
throws Exception
{
try {
tearDownQueryFramework();
}
finally {
h2QueryRunner.close();
}
h2QueryRunner.close();
}

protected ConnectorSession getSession()
{
return session;
return queryRunner.getDefaultSession();
}

protected abstract int getNodeCount();

protected abstract ConnectorSession setUpQueryFramework()
throws Exception;

protected void tearDownQueryFramework()
throws Exception
public final int getNodeCount()
{
return queryRunner.getNodeCount();
}

protected abstract MaterializedResult computeActual(@Language("SQL") String sql);
protected MaterializedResult computeActual(@Language("SQL") String sql)
{
return queryRunner.execute(getSession(), sql).toJdbcTypes();
}

protected void assertQuery(@Language("SQL") String sql)
throws Exception
Expand Down Expand Up @@ -191,6 +179,6 @@ private QueryExplainer getQueryExplainer()
MetadataManager metadata = new MetadataManager(new FeaturesConfig().setExperimentalSyntaxEnabled(true), new TypeRegistry());
FeaturesConfig featuresConfig = new FeaturesConfig().setExperimentalSyntaxEnabled(true);
List<PlanOptimizer> optimizers = new PlanOptimizersFactory(metadata, new SplitManager(), new IndexManager(), featuresConfig).get();
return new QueryExplainer(session, optimizers, metadata, featuresConfig.isExperimentalSyntaxEnabled());
return new QueryExplainer(queryRunner.getDefaultSession(), optimizers, metadata, featuresConfig.isExperimentalSyntaxEnabled());
}
}
Loading

0 comments on commit 466524d

Please sign in to comment.