Skip to content

Commit

Permalink
unit tests use PostgreSQL if DIGDAG_TEST_POSTGRESQL is set
Browse files Browse the repository at this point in the history
  • Loading branch information
frsyuki committed Mar 25, 2016
1 parent 7acc7a8 commit c63578f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
*/build/
*.pyc
/dist/*.tar.gz
/config/test_postgresql.properties
MANIFEST
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,22 @@ $ curl -v -4 "http://localhost:9090/api/repositories"
$ curl -v -4 "http://localhost:9090/api/sessions"
```
# Development
## Running test
```
$ ./gradlew check
```
Test coverage report is generated at `didgag-*/build/reports/jacoco/test/html/index.html`.
Findbugs report is generated at `digdag-*/build/reports/findbugs/main.html`.
## Testing with PostgreSQL
Test uses in-memory H2 database by default. To use PostgreSQL, set following environment variables:
```
$ export DIGDAG_TEST_POSTGRESQL="$(cat config/test_postgresql.properties)"
```
5 changes: 5 additions & 0 deletions config/test_postgresql.properties.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
host = localhost
port = 5432
user = pg
password =
database = digdag_test
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;
import java.io.IOException;
import java.io.StringReader;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.base.Optional;
Expand All @@ -20,26 +23,66 @@ public class DatabaseTestingUtils
{
private DatabaseTestingUtils() { }

public static DatabaseConfig getEnvironmentDatabaseConfig()
{
String pg = System.getenv("DIGDAG_TEST_POSTGRESQL");
if (pg != null && !pg.isEmpty()) {
Properties props = new Properties();
try (StringReader reader = new StringReader(pg)) {
props.load(reader);
}
catch (IOException ex) {
throw Throwables.propagate(ex);
}

return DatabaseConfig.builder()
.type("postgresql")
.path(Optional.absent())
.remoteDatabaseConfig(
RemoteDatabaseConfig.builder()
.user(props.getProperty("user"))
.password(props.getProperty("password", ""))
.host(props.getProperty("host"))
.port(Integer.parseInt(props.getProperty("port")))
.database(props.getProperty("database"))
.loginTimeout(10)
.socketTimeout(60)
.ssl(false)
.build())
.options(ImmutableMap.of())
.expireLockInterval(10)
.autoMigrate(true)
.connectionTimeout(30)
.idleTimeout(600)
.validationTimeout(5)
.maximumPoolSize(10)
.build();
}
else {
return DatabaseConfig.builder()
.type("h2")
.path(Optional.absent())
.remoteDatabaseConfig(Optional.absent())
.options(ImmutableMap.of())
.expireLockInterval(10)
.autoMigrate(true)
.connectionTimeout(30)
.idleTimeout(600)
.validationTimeout(5)
.maximumPoolSize(10)
.build();
}
}

public static DatabaseFactory setupDatabase()
{
DatabaseConfig config = DatabaseConfig.builder()
.type("h2")
.path(Optional.absent())
.remoteDatabaseConfig(Optional.absent())
.options(ImmutableMap.of())
.expireLockInterval(10)
.autoMigrate(true)
.connectionTimeout(30)
.idleTimeout(600)
.validationTimeout(5)
.maximumPoolSize(10)
.build();
DatabaseConfig config = getEnvironmentDatabaseConfig();
PooledDataSourceProvider dsp = new PooledDataSourceProvider(config);

DBI dbi = new DBI(dsp.get());
new DatabaseMigrator(dbi, config).migrate();

cleanDatabase(dbi);
cleanDatabase(config.getType(), dbi);

return new DatabaseFactory(dbi, dsp, config);
}
Expand All @@ -64,16 +107,25 @@ public static DatabaseFactory setupDatabase()
"queued_task_locks",
};

public static void cleanDatabase(IDBI dbi)
public static void cleanDatabase(String databaseType, IDBI dbi)
{
try (Handle handle = dbi.open()) {
// h2 database can't truncate tables with references if REFERENTIAL_INTEGRITY is true (default)
handle.createStatement("SET REFERENTIAL_INTEGRITY FALSE").execute();
for (String name : Lists.reverse(Arrays.asList(ALL_TABLES))) {
handle.createStatement("TRUNCATE TABLE " + name).execute();
//handle.createStatement("TRUNCATE " + name + " CASCADE").execute();
switch (databaseType) {
case "h2":
// h2 database can't truncate tables with references if REFERENTIAL_INTEGRITY is true (default)
handle.createStatement("SET REFERENTIAL_INTEGRITY FALSE").execute();
for (String name : Lists.reverse(Arrays.asList(ALL_TABLES))) {
handle.createStatement("TRUNCATE TABLE " + name).execute();
}
handle.createStatement("SET REFERENTIAL_INTEGRITY TRUE").execute();
break;
default:
// postgresql needs "CASCADE" option to TRUNCATE
for (String name : Lists.reverse(Arrays.asList(ALL_TABLES))) {
handle.createStatement("TRUNCATE " + name + " CASCADE").execute();
}
break;
}
handle.createStatement("SET REFERENTIAL_INTEGRITY TRUE").execute();
}
}

Expand Down

0 comments on commit c63578f

Please sign in to comment.