An open API service indexing awesome lists of open source software.

https://github.com/kosmisk-dk/postgresql-test-datasource

DataSource Provider for Integation Testing with PostgreSQL databases
https://github.com/kosmisk-dk/postgresql-test-datasource

integration-testing java postgresql

Last synced: 3 months ago
JSON representation

DataSource Provider for Integation Testing with PostgreSQL databases

Awesome Lists containing this project

README

          

# PostgreSQL DataSource Integration Test Helper

A helper class for PostgreSQL integration testing.

It allows for postgresql connections made from

* System properties (give name of property that defines the port and the name of the database)
* Environment Variables (give name of environment variable, that contain a connect URI)
* fallback
* from PG* Environment variables
* from ${user.name} (as user, password and database)

It also implements a number of helper methods, to manipulate the database.

Functions to:

* wipe a schema
* dump/truncate and restore tables either by name from a list or all tables in foreign key respecting order

## Usage

A typical use case is outlined below:

### Typical Test Environment


org.apache.maven.plugins
maven-failsafe-plugin
${some.version}

false

${postgresql.testbase.port}
${postgresql.dump.folder}

-Dfile.encoding=${project.build.sourceEncoding}




integration-test
verify




dk.kosmisk
postgresql-maven-plugin
${some.version}





postgresql-test-database

setup
startup
shutdown


testbase

${basedir}/src/test/resources/schema.sql
${basedir}/src/test/resources/testdata.sql




...


dk.kosmisk
postgresql-test-datasource
${some.version}
jar

### Typical Test

public class EntityTest {

private PostgresITDataSource dataSource;

@Before
public void setup() throws Exception {
dataSource = PostgresITDataSource.builder()
.fromProperty("testbase", "postgresql.testbase.port")
.fromEnvironment("MY_PGTEST_URL")
.withFallback()
.build();
dataSource.copyAllTablesToDisk();
}

@After
public void cleanup() throws Exception {
dataSource.truncateAllTables();
dataSource.copyAllTablesFromDisk();
}

@Test
public void testSomething() throws Exception {
System.out.println("Something");
try(Connection connection = dataSource.getConnection()) {

...

}
}