-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testcontainers for Java: Add examples for init scripts and function
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CREATE TABLE alltypes ( | ||
name STRING PRIMARY KEY, | ||
date TIMESTAMP, | ||
datetime_tz TIMESTAMP WITH TIME ZONE, | ||
datetime_notz TIMESTAMP WITHOUT TIME ZONE, | ||
nullable_datetime TIMESTAMP, | ||
nullable_date TIMESTAMP, | ||
kind STRING, | ||
flag BOOLEAN, | ||
position INTEGER, | ||
description STRING, | ||
details ARRAY(OBJECT), | ||
attributes OBJECT(DYNAMIC), | ||
coordinates GEO_POINT | ||
) WITH (number_of_replicas=0); |
68 changes: 68 additions & 0 deletions
68
...ing/testcontainers/java/src/test/java/io/crate/example/testing/TestSqlInitialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.crate.example.testing; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.sql.SQLException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
|
||
/** | ||
* Database containers launched with SQL initialization/provisioning script/routine. | ||
* <p> | ||
* <a href="https://www.testcontainers.org/modules/databases/jdbc/#database-containers-launched-via-jdbc-url-scheme"/> | ||
* <a href="https://www.testcontainers.org/features/reuse/"/> | ||
* </p> | ||
*/ | ||
public class TestSqlInitialization { | ||
|
||
/** | ||
* Launch container with CrateDB 5.2, using `TC_INITSCRIPT` to address a file in Java's CLASSPATH. | ||
* <a href="https://www.testcontainers.org/modules/databases/jdbc/#using-a-classpath-init-script"/> | ||
*/ | ||
@Test | ||
public void testTcInitClasspathFile() throws SQLException, IOException { | ||
String connectionUrl = "jdbc:tc:cratedb:5.2://localhost/doc?user=crate&TC_REUSABLE=true&TC_INITSCRIPT=init.sql"; | ||
System.out.printf("Connecting to %s%n", connectionUrl); | ||
|
||
// Invoke `SHOW CREATE TABLE ...` query. | ||
Application app = new Application(connectionUrl); | ||
var results= app.showCreateTable("alltypes"); | ||
assertThat(results.metaData().getColumnCount()).isEqualTo(1); | ||
assertThat(results.rows()).hasSize(1); | ||
} | ||
|
||
/** | ||
* Launch container with CrateDB 5.2, using `TC_INITSCRIPT` to address an arbitrary file on the filesystem. | ||
* <a href="https://www.testcontainers.org/modules/databases/jdbc/#using-an-init-script-from-a-file"/> | ||
*/ | ||
@Test | ||
public void testTcInitArbitraryFile() throws SQLException, IOException { | ||
String connectionUrl = "jdbc:tc:cratedb:5.2://localhost/doc?user=crate&TC_REUSABLE=true&TC_INITSCRIPT=file:src/main/resources/init.sql"; | ||
System.out.printf("Connecting to %s%n", connectionUrl); | ||
|
||
// Invoke `SHOW CREATE TABLE ...` query. | ||
Application app = new Application(connectionUrl); | ||
var results= app.showCreateTable("alltypes"); | ||
assertThat(results.metaData().getColumnCount()).isEqualTo(1); | ||
assertThat(results.rows()).hasSize(1); | ||
} | ||
|
||
/** | ||
* Launch container with CrateDB 5.2, using an init function. | ||
* <a href="https://www.testcontainers.org/modules/databases/jdbc/#using-an-init-script-from-a-file"/> | ||
*/ | ||
@Test | ||
public void testTcInitFunction() throws SQLException, IOException { | ||
String connectionUrl = "jdbc:tc:cratedb:5.2://localhost/doc?user=crate&TC_INITFUNCTION=io.crate.example.testing.utils.TestingHelpers::sqlInitFunction"; | ||
System.out.printf("Connecting to %s%n", connectionUrl); | ||
|
||
// Invoke `SHOW CREATE TABLE ...` query. | ||
Application app = new Application(connectionUrl); | ||
var results= app.showCreateTable("foobar_init"); | ||
assertThat(results.metaData().getColumnCount()).isEqualTo(1); | ||
assertThat(results.rows()).hasSize(1); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters