Giter Club home page Giter Club logo

test-db-builder-java's Introduction

Test Database Builder Java

Build Status Maven Central โ€“ Test Database Builder for Java

Quality Gate Status

Security Rating Reliability Rating Maintainability Rating Technical Debt

Code Smells Coverage Duplicated Lines (%) Lines of Code

Overview

Exasol's Test Database Builder for Java (TDBJ) is a library that makes writing integration tests for database applications easier.

The main design goals are to make the code of the integration test compact and readable at the same time.

In a Nutshell

import com.exasol.dbbuilder.dialects.DatabaseObjectFactory;

class OnlineShopIT {
    final static DatabaseObjectFactory factory;

    @BeforeAll

    static void beforeAll() {
        // ... get a JDBC connection and store it in variable "connection"
        factory = new ExasolObjectFactory(connection);
    }

    @Test
    void testShopItemList() {
        // Test preparation in the database:
        final Schema schema = factory.createSchema("ONLINESHOP");
        final Table table = schema.createTable("ITEMS", "PRODUCT_ID", "DECIMAL(18,0)", "NAME", "VARCHAR(40)")
                .insert("1", "Cat food")
                .insert("2", "Toy mouse");
        final User user = factory.createUser("KIMIKO")
                .grant(CREATE_SESSION)
                .grant(table, SELECT, UDPATE);
        // ... the actual test
    }
}

For more details, please refer to the user guide.

What it is

This module is designed to be used in test code. It helps you write tests, quickly, easily while still focusing on readability.

What it isn't

TDBJ is not suited for production code. We sacrifice speed and features for compactness and ease-of-use. If you are looking for code that helps writing production code, please refer to

Supported Databases

  • Exasol
  • MySQL
  • PostgreSQL

Features

  • Create: schemas, tables, adapter scripts, users, connection definitions, virtual schemas
  • Grant privileges to users
  • Insert data into tables

Customer Support

This is an open source project which is written by enthusiasts at Exasol and not officially supported. We will still try to help you as much as possible. So please create GitHub issue tickets when you want to request features or report bugs.

Table of Contents

Information for Users

test-db-builder-java's People

Contributors

anastasiiasergienko avatar ckunki avatar dependabot[bot] avatar jakobbraun avatar kaklakariada avatar morazow avatar pj-spoelders avatar priyank0910 avatar redcatbear avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

test-db-builder-java's Issues

Fix the `DatabaseObjectWriter` hierarchy

At the moment, we have some members of the DatabaseObjectWriter hierarchy that restrict the features of the super classes, as in
https://github.com/exasol/test-db-builder-java/blob/main/src/main/java/com/exasol/dbbuilder/dialects/postgres/PostgreSqlImmediateDatabaseObjectWriter.java#L33

A class should extend another one if:

  • the subclass is a superclass
  • the subclass can do everything the superclass can

Extending a class restricting the super class is usually not a good idea, as it breaks the Liskov substitution principle and will eventually lead to a hierarchy hard to maintain, extend and change.

We should fix this.

Deploy to Central Repository

Situation

Since the TDDB is a library that you usually include in your projects via Maven or Gradle, we need to deploy to the Central Repository.

Acceptance Criteria

  1. Version 0.1.0 deployed on Central Repository

Remove R as Virtual Schema language

The database does currently not support R adapter scripts. For that reason, R should be removed from the list of possible languages for adapter scripts.

Mark deleted objects

Add a flag to deleted DaabaseObjects that marks them as deleted and causes TDBJ to refuse operations on them

Support connections with empty username but password

The unified connection definition requires connections like:

CREATE OR REPLACE CONNECTION S3_CONNECTION
TO ''
USER ''
IDENTIFIED BY '{
  "awsAccessKeyId": "<AWS_ACCESS_KEY>",
  "awsSecretAccessKey": "<AWS_SECRET_KEY>",
  "awsRegion": "<SESSION_TOKEN>",
  "s3Bucket": "<BUCKET_NAME>"
}';

Currently, TDBJ can't create this connection since it can not create connections with a password but no username.

Write user guide

Situation

The first implementation comes without a user guide. This is required for completeness.

Acceptance Criteria

  1. User guide exists.

Features for RLS tests

Situation

We need this test framework in our row-level-security integration tests.

Acceptance Criteria

  • Can create: Schema, Virtual Schema, Table, User, Connection
  • Can fill: Table
  • Can grant: SESSION, all object privilege types

Unify the interface for creating database objects

Most of the database objects are instantiated with and com.exasol.db.Identifier, as in https://github.com/exasol/test-db-builder-java/blob/main/src/main/java/com/exasol/dbbuilder/dialects/postgres/PostgreSqlUser.java#L18

Some other objects do the same but change the API, as in https://github.com/exasol/test-db-builder-java/blob/main/src/main/java/com/exasol/dbbuilder/dialects/postgres/PostgreSqlSchema.java#L18

This makes the API harder to use. We should unify this.

Create a dialect for DB2 database

Problem

We have an integration test for DB2 Virtual Schema. The test would be more readable if we use db-builder with DB2 dialect in the test.

Support replacing Exasol database object

Situation

As a tester sometimes I want to replace existing database objects.

Acceptance Criteria

  • Udfscripts, tables can be replaced

We can create all objects using CREATE OR REPLACE by default and in addition add api for the replacing existing ones.

Make datbase objects `AutoClosable`

Situation

In cases where you create transient database objects in test cases, you want to make sure that the are dropped after the test case.
The cleanest way to get rid of test-case-specific objects would be to initialize them in a try-with-resources and let Java clean them up. This also works if the test fails.

Acceptance Criteria

  • Database objects that support drop() also implement AutoClosable

Add UDF object creation example to user guide

Situation

TDDB support Exasol User defined functions (UDF) object. But user guide does not mention it.

Acceptance Criteria

  • Added short description and example of UDF creation to user guide

Add requirements and design

Situation

For the first set of functions requirements and design are still missing.

Acceptance Criteria

  1. All existing functions covered in requirements and design

Make `script.execute` robust against quotes

Situation

Sometimes you want to try out how your SQL scripts behave if you feed them parameters that contain single or double quotes.
While double quotes seem to work fine, single quotes produce an error.

script.execute(parameters)

Produces

syntax error, unexpected IDENTIFIER_LIST_, expecting ',' or ')' [line 1, column 114]

This is caused by the fact that the prepared statement does not use placeholders, so the parameters are not properly quoted. That results in constructs like this:

EXECUTE SCRIPT "REMOVE_USER_FROM_GROUP_SCHEMA"."REMOVE_USER_FROM_GROUP" ('THE_USER',  ARRAY('GROUP_A', 'CONTAINS'SINGLE_QUOTE', 'GROUP_C'))

Acceptance Criteria

  • Script parameters are properly quoted.

Use existing database objects

Situation

Sometimes you need to manipulate database objects in test that already exist. For example if they were created by your implementation and you need to modify them for a white-box test. Or if they are imported from a SQL file.

In those cases the TDDB must let you attach to these existing database objects. For starters we are covering scripts, but the concept can be expanded to other types too.

Acceptance criteria

  • Users can get a control object for an existing script.

Add bulk insert for Tables

Current state:
It is only possible to insert data row by row. This is comparably slow.

Desired:
Fast insert method.

JAVA adapter script builder

Situation

With the basic implementation we can create all kinds of adapter scripts. But especially for Java adapter scripts, a specialized builder could increase code compactness, readability and user convenience.

Acceptance Criteria

  1. Convenience builder for Java Adapter scripts added.
  2. Builder accepts JVM parameters (e.g. for configuring a debugger, profiler or SONAR agent)
  3. Builder accepts JAR references
  4. Builder accepts mail class reference

Execute scripts without wrapping exceptions

Situation

If you want to check whether your scripts produce the expected exceptions, it is more convenient to not have them wrapped in a runtime exception โ€” or to be more precise a DatabaseObjectException.

Acceptance Criteria

  1. Scripts can optionally be executed without wrapping the SqlException.

Create scripts

Situation

Currently the TDDB can create adapter scripts. But for testing the administration scripts in RLS we need support for creating LUA scripts too.

Acceptance Criteria

  1. TDDB can create LUA scripts

Add JUnit extension that isolates tests

Add a JUnit extension that isolates a test case. Isolates mean that all objects that were created during a test will be deleted afterwards.

Ideas for the API:

@TestDbBuilder
private static ExasolObjectFactory testDbBuilder = ...;

@Tests
void test(@IsolatedTestDbBuilder ExasolObjectFactory factory){
 factory.createSchema("test");
}

or

@TestDbBuilder
private static ExasolObjectFactory testDbBuilder = ...;

@Tests
@WithTestDbCleanup
void test(){
  factory.createSchema("test");
}

Expected behavior:

  • TDB deletes the schema test after the test execution.
  • Objects created in outside the test (for example in beforeAll) are not neleted

Move `DatabaseObjectWriter.write(object)` calls away from constructors

At the moment, every time we are instantiating a new object to be save in the database, we do the saving inside the constructor as in https://github.com/exasol/test-db-builder-java/blob/main/src/main/java/com/exasol/dbbuilder/dialects/postgres/PostgreSqlSchema.java#L21.
This might be dangerous as the objects are not fully created at that point, specially if we keep instantiating fields after this call.

At the same time, we are providing the user of the API a clear entry point for creating these objects (https://github.com/exasol/test-db-builder-java/blob/main/src/main/java/com/exasol/dbbuilder/dialects/postgres/PostgreSqlObjectFactory.java).

We should move this behavior outside the constructor, and probably add it to the factories that create these objects after they are fully initialized.

Another approach might be to provide a builder for each of these objects, and call this behavior in in the buildes, and have the factories call the builders instead of the constructors.

In any case, the constructors should not be part of the public API, as we are providing already a clear entry point for creating objects.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.