DirectQuery Testing

Testing

To perform unit tests on the DirectQuery databases, the test section of the common library provides test templates that you can either copy or include in your tests.

All the test helpers are derived from the interface IDirectQueryTestContainer that provides :

  • The IToolbox object.
  • The use database statement.
  • The create and use schema statement.
  • The execute SQL statement and script.

Example

Here is an example of using the templates to test a feature on different databases.

The common test part

public abstract class SessionTest {

    protected static void setupDatabase(IDirectQueryTestContainer<?> server) {
        final INamingConvention locToDbNaming = DirectQueryUtils.converterToNameMapping(DirectQueryUtils::camelToSnake);
        var generator = new SQLGenerator(locToDbNaming, server.createToolbox());
        server.executeSqlQueries(generator.createTable(MY_BASE_STORE));
    }

    protected abstract IDirectQueryTestContainer<?> getServer();

    @Test
    protected void testDynamic() {
        Registry.setContributionProvider(new ClasspathContributionProvider());
        IToolbox toolbox = getServer().createToolbox();
        var table = toolbox.getSession().discoverTable(MY_BASE_STORE.getName());
        //...
    }
}

Database-specific part

@Testcontainers
class MSSQLSessionTestDQ extends SessionTest {

    private static final String DATABASE_NAME = "snippets";

    @Container
    private static final DirectqueryMSSQLContainer SERVER = new DirectqueryMSSQLContainer();

    @BeforeAll
    public static void setupDatabase() {
        SERVER.createDatabase(DATABASE_NAME);
        setupDatabase(SERVER);
    }

    @Override
    protected IDirectQueryTestContainer<?> getServer() {
        return SERVER;
    }
}
@Testcontainers
class ClickHouseSessionTestDQ extends SessionTest {

    private static final String DATABASE_NAME = "snippets";

    @Container
    private static final DirectqueryClickhouseContainer SERVER = new DirectqueryClickhouseContainer();

    @BeforeAll
    public static void setupDatabase() {
        SERVER.createDatabase(DATABASE_NAME);
        setupDatabase(SERVER);
    }

    @Override
    protected IDirectQueryTestContainer<?> getServer() {
        return SERVER;
    }
}

As it is a real database connection, you must provide:

  • DATABASE_NAME
  • SCHEMA_NAME
  • WHAREHOUSE_NAME
  • Username as a system property SNOWFLAKE_USERNAME
  • User password as a system property SNOWFLAKE_PASSWORD
@Disabled("Real DB on the back ...")
class SnowflakeSessionTestDQ extends SessionTest {

    private static final SnowFlakeTestHelper SERVER = new SnowFlakeTestHelper(DATABASE_NAME, SCHEMA_NAME, WHAREHOUSE_NAME);

    @BeforeAll
    public static void setupDatabase() {
        setupDatabase(SERVER);
    }

    @Override
    protected IDirectQueryTestContainer<?> getServer() {
        return SERVER;
    }
}