Skip to content

Commit

Permalink
Merge pull request #141 from kdhrubo/feature/140_load_multiple_db_scr…
Browse files Browse the repository at this point in the history
…ipts

testcontainer DB loading multiple scripts
  • Loading branch information
kdhrubo authored Jan 14, 2024
2 parents 282f137 + 5e97ebd commit 5571a65
Show file tree
Hide file tree
Showing 6 changed files with 28,616 additions and 28,579 deletions.
40 changes: 30 additions & 10 deletions src/test/java/com/homihq/db2rest/MySQLContainerConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
package com.homihq.db2rest;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.ext.ScriptUtils;
import org.testcontainers.jdbc.JdbcDatabaseDelegate;

import javax.sql.DataSource;
import java.util.Set;

@TestConfiguration(proxyBeanMethods = false)
public class MySQLContainerConfiguration {
@Bean
@ServiceConnection
public MySQLContainer<?> mySQLContainer() {
var mySQLContainer = new MySQLContainer("mysql:8.2");
mySQLContainer.withUsername("mysql")
.withPassword("mysql")
.withInitScript("mysql/mysql-sakila.sql")
.withDatabaseName("sakila").withReuse(true);
return mySQLContainer;

private static final Set<String> mySqlScripts = Set.of("mysql/mysql-sakila.sql",
"mysql/mysql-sakila-data.sql");

private static final MySQLContainer mySQLContainer = (MySQLContainer) new MySQLContainer("mysql:8.2")
.withDatabaseName("sakila")
.withUsername("mysql")
.withPassword("mysql")
.withReuse(true);

static {
mySQLContainer.start();
var containerDelegate = new JdbcDatabaseDelegate(mySQLContainer, "");
mySqlScripts.forEach(initScript -> ScriptUtils.runInitScript(containerDelegate, initScript));
}

@Bean("mySQLDataSource")
public DataSource dataSource() {
var dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("com.mysql.jdbc.Driver");
dataSourceBuilder.url(mySQLContainer.getJdbcUrl());
dataSourceBuilder.username(mySQLContainer.getUsername());
dataSourceBuilder.password(mySQLContainer.getPassword());
return dataSourceBuilder.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
package com.homihq.db2rest;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.ext.ScriptUtils;
import org.testcontainers.jdbc.JdbcDatabaseDelegate;

import javax.sql.DataSource;
import java.util.Set;

@TestConfiguration(proxyBeanMethods = false)
public class PostgreSQLContainerConfiguration {

@Bean
@ServiceConnection
public PostgreSQLContainer<?> postgreSQLContainer() {
PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:15.2-alpine");
postgreSQLContainer.withUsername("postgres")
.withPassword("postgres")
.withInitScript("pg/postgres-sakila.sql")
.withDatabaseName("postgres").withReuse(true);
return postgreSQLContainer;
private static final Set<String> postgresScripts = Set.of("pg/postgres-sakila.sql",
"pg/postgres-sakila-data.sql");

private static final PostgreSQLContainer testPostgres = (PostgreSQLContainer) new PostgreSQLContainer("postgres:15.2-alpine")
.withDatabaseName("postgres")
.withUsername("postgres")
.withPassword("postgres")
.withReuse(true);

static {
testPostgres.start();
var containerDelegate = new JdbcDatabaseDelegate(testPostgres, "");
postgresScripts.forEach(initScript -> ScriptUtils.runInitScript(containerDelegate, initScript));
}

@Bean("postgresDataSource")
public DataSource dataSource() {
var dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.postgresql.Driver");
dataSourceBuilder.url(testPostgres.getJdbcUrl());
dataSourceBuilder.username(testPostgres.getUsername());
dataSourceBuilder.password(testPostgres.getPassword());
return dataSourceBuilder.build();
}

}
Loading

0 comments on commit 5571a65

Please sign in to comment.