dk.kosmisk:postgresql-test-datasource

An integration test helper designed for dk.kosmisk:postgresql-maven-plugin. It is able to create a DataSource that connects to a postgresql, given a system-property with a port in it and a database name, or a URI in an environment variable or from PG* environment variables or to a personal PostgreSQL instance, with user/password/database-name which is the same as the user name for the user running the tests.

License

License

Categories

Categories

PostgreSQL Data Databases
GroupId

GroupId

dk.kosmisk
ArtifactId

ArtifactId

postgresql-test-datasource
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

dk.kosmisk:postgresql-test-datasource
An integration test helper designed for dk.kosmisk:postgresql-maven-plugin. It is able to create a DataSource that connects to a postgresql, given a system-property with a port in it and a database name, or a URI in an environment variable or from PG* environment variables or to a personal PostgreSQL instance, with user/password/database-name which is the same as the user name for the user running the tests.
Project URL

Project URL

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

Source Code Management

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

Download postgresql-test-datasource

How to add to project

<!-- https://jarcasting.com/artifacts/dk.kosmisk/postgresql-test-datasource/ -->
<dependency>
    <groupId>dk.kosmisk</groupId>
    <artifactId>postgresql-test-datasource</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/dk.kosmisk/postgresql-test-datasource/
implementation 'dk.kosmisk:postgresql-test-datasource:1.0.2'
// https://jarcasting.com/artifacts/dk.kosmisk/postgresql-test-datasource/
implementation ("dk.kosmisk:postgresql-test-datasource:1.0.2")
'dk.kosmisk:postgresql-test-datasource:jar:1.0.2'
<dependency org="dk.kosmisk" name="postgresql-test-datasource" rev="1.0.2">
  <artifact name="postgresql-test-datasource" type="jar" />
</dependency>
@Grapes(
@Grab(group='dk.kosmisk', module='postgresql-test-datasource', version='1.0.2')
)
libraryDependencies += "dk.kosmisk" % "postgresql-test-datasource" % "1.0.2"
[dk.kosmisk/postgresql-test-datasource "1.0.2"]

Dependencies

compile (2)

Group / Artifact Type Version
org.apache.commons : commons-dbcp2 jar 2.7.0
com.google.code.findbugs : annotations jar 3.0.1

test (2)

Group / Artifact Type Version
org.postgresql : postgresql jar 42.2.8
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

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

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>${some.version}</version>
        <configuration>
            <redirectTestOutputToFile>false</redirectTestOutputToFile>
            <systemPropertyVariables>
                <postgresql.testbase.port>${postgresql.testbase.port}</postgresql.testbase.port>
                <postgresql.dump.folder>${postgresql.dump.folder}</postgresql.dump.folder>
            </systemPropertyVariables>
            <argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    <plugin>
        <groupId>dk.kosmisk</groupId>
        <artifactId>postgresql-maven-plugin</artifactId>
        <version>${some.version}</version>
        <configuration>
            <!-- <version>LATEST</version> -->
        </configuration>
        <executions>
            <execution>
                <id>postgresql-test-database</id>
                <goals>
                    <goal>setup</goal>
                    <goal>startup</goal>
                    <goal>shutdown</goal>
                </goals>
                <configuration>
                    <name>testbase</name>
                    <scripts>
                        <script>${basedir}/src/test/resources/schema.sql</script>
                        <script>${basedir}/src/test/resources/testdata.sql</script>
                    </scripts>
                </configuration>
            </execution>
        </executions>
    </plugin>

...

    <dependency>
        <groupId>dk.kosmisk</groupId>
        <artifactId>postgresql-test-datasource</artifactId>
        <version>${some.version}</version>
        <type>jar</type>
    </dependency>

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()) {

...

            }
        }

Versions

Version
1.0.2
1.0.1
1.0.0