Skip to content

Commit 5dc3708

Browse files
author
Lukasz Myslinski
committed
Working sample PSQL container test
1 parent 2e3c5d8 commit 5dc3708

11 files changed

Lines changed: 248 additions & 151 deletions

File tree

modules/postgresql/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
description = "Testcontainers :: JDBC :: PostgreSQL"
22

3+
repositories {
4+
mavenCentral()
5+
maven {
6+
url("https://repo.spring.io/milestone")
7+
}
8+
}
9+
310
dependencies {
411
compile project(':jdbc')
12+
compile project(':r2dbc')
513

614
testCompile 'org.postgresql:postgresql:42.2.5'
715
testCompile 'commons-dbutils:commons-dbutils:1.7'
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.testcontainers.containers;
2+
3+
import org.testcontainers.containers.wait.LogMessageWaitStrategy;
4+
import org.testcontainers.r2dbc.R2dbcConnectionParams;
5+
import org.testcontainers.r2dbc.R2dbcDatabaseContainer;
6+
7+
import java.time.Duration;
8+
9+
import static java.time.temporal.ChronoUnit.SECONDS;
10+
11+
// The container should expose the R2dbc connection (factory?) to the test so that we can construct a driver instance outside of it.
12+
public class R2dbcPostgreSQLContainer<SELF extends R2dbcPostgreSQLContainer<SELF>> extends R2dbcDatabaseContainer<SELF> {
13+
public static final String NAME = "postgresql";
14+
public static final String IMAGE = "postgres";
15+
public static final String DEFAULT_TAG = "9.6.8";
16+
17+
public static final Integer POSTGRESQL_PORT = 5432;
18+
private String databaseName = "test";
19+
private String username = "test";
20+
private String password = "test";
21+
private String host = "localhost";
22+
23+
@Override
24+
protected void configure() {
25+
addExposedPort(POSTGRESQL_PORT);
26+
addEnv("POSTGRES_DB", databaseName);
27+
addEnv("POSTGRES_USER", username);
28+
addEnv("POSTGRES_HOST", host);
29+
addEnv("POSTGRES_PASSWORD", password);
30+
setCommand("postgres");
31+
}
32+
33+
public R2dbcPostgreSQLContainer() {
34+
this(IMAGE + ":" + DEFAULT_TAG);
35+
}
36+
37+
public R2dbcPostgreSQLContainer(final String dockerImageName) {
38+
super(dockerImageName);
39+
this.waitStrategy = new LogMessageWaitStrategy()
40+
.withRegEx(".*database system is ready to accept connections.*\\s")
41+
.withTimes(2)
42+
.withStartupTimeout(Duration.of(60, SECONDS));
43+
}
44+
45+
@Override
46+
public R2dbcConnectionParams getR2dbcConnectionParams(){
47+
return R2dbcConnectionParams.builder()
48+
.host(host)
49+
.database(databaseName)
50+
.username(username)
51+
.password(password)
52+
.port(getMappedPort(POSTGRESQL_PORT))
53+
.build();
54+
}
55+
56+
@Override
57+
protected String getTestQueryString() {
58+
return "SELECT 1";
59+
}
60+
}

modules/r2dbc-test/build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
repositories {
2+
maven {
3+
url "https://maven.atlassian.com/3rdparty/"
4+
}
5+
maven {
6+
url("https://repo.spring.io/milestone")
7+
}
8+
}
9+
10+
dependencies {
11+
compile project(':r2dbc')
12+
compile project(':database-commons')
13+
compile project(':postgresql')
14+
15+
testCompile 'com.google.guava:guava:18.0'
16+
testCompile 'org.postgresql:postgresql:42.0.0'
17+
18+
testCompile 'commons-dbutils:commons-dbutils:1.6'
19+
20+
testCompile 'com.googlecode.junit-toolbox:junit-toolbox:2.4'
21+
22+
testCompile 'io.r2dbc:r2dbc-client:1.0.0.M6'
23+
testCompile 'io.r2dbc:r2dbc-postgresql:1.0.0.M6'
24+
testCompile 'io.r2dbc:r2dbc-spi:1.0.0.M6'
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import io.r2dbc.client.R2dbc;
2+
import org.junit.Test;
3+
import org.testcontainers.containers.R2dbcPostgreSQLContainer;
4+
5+
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
6+
7+
/**
8+
* @author humblehound
9+
*/
10+
public class SimplePostgreSQLTest {
11+
12+
@Test
13+
public void testSimple() {
14+
R2dbcPostgreSQLContainer postgres = new R2dbcPostgreSQLContainer<>();
15+
postgres.start();
16+
R2dbc r2dbc = postgres.getR2dbc();
17+
Integer result = r2dbc.inTransaction(handle -> handle.createUpdate("SELECT 1").execute()).blockFirst();
18+
assertEquals("A basic SELECT query succeeds", 1, result);
19+
postgres.stop();
20+
}
21+
22+
// @Test
23+
// public void testExplicitInitScript() throws SQLException {
24+
// try (R2dbcPostgreSQLContainer postgres = new R2dbcPostgreSQLContainer().withInitScript("somepath/init_postgresql.sql")) {
25+
// postgres.start();
26+
//
27+
// ResultSet resultSet = performQuery(postgres, "SELECT foo FROM bar");
28+
//
29+
// String firstColumnValue = resultSet.getString(1);
30+
// assertEquals("Value from init script should equal real value", "hello world", firstColumnValue);
31+
// }
32+
// }
33+
34+
}

modules/r2dbc/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
description = "Testcontainers :: r2dbc"
22

33
dependencies {
4-
compile project(':testcontainers')
4+
compile project(':database-commons')
55
compile 'io.r2dbc:r2dbc-client:1.0.0.M6'
6+
compile 'io.r2dbc:r2dbc-postgresql:1.0.0.M6'
67
compile 'io.r2dbc:r2dbc-spi:1.0.0.M6'
78
}
89

modules/r2dbc/src/main/java/org/testcontainers/containers/R2dbcConnectionParams.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

modules/r2dbc/src/main/java/org/testcontainers/containers/R2dbcDatabaseContainer.java

Lines changed: 0 additions & 140 deletions
This file was deleted.

modules/r2dbc/src/main/java/org/testcontainers/containers/R2dbcDatabaseContainerProvider.java

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.testcontainers.r2dbc;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
@Data
7+
@Builder
8+
public class R2dbcConnectionParams {
9+
private final String host;
10+
private final int port;
11+
private final String database;
12+
private final String username;
13+
private final String password;
14+
}

0 commit comments

Comments
 (0)