reset database completely after each test so that tests cannot impact each other

This commit is contained in:
Athou
2023-12-24 10:52:09 +01:00
parent d09cabb8c6
commit b2bd386e9c

View File

@@ -1,10 +1,7 @@
package com.commafeed;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import javax.sql.DataSource;
@@ -15,9 +12,6 @@ import io.dropwizard.testing.junit5.DropwizardAppExtension;
public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<CommaFeedConfiguration> {
private static final List<String> TABLES = Arrays.asList("FEEDENTRYSTATUSES", "FEEDENTRYTAGS", "FEEDENTRIES", "FEEDENTRYCONTENTS",
"FEEDSUBSCRIPTIONS", "FEEDS", "FEEDCATEGORIES");
public CommaFeedDropwizardAppExtension() {
super(CommaFeedApplication.class, ResourceHelpers.resourceFilePath("config.test.yml"));
}
@@ -29,10 +23,7 @@ public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<Comm
// clean database after each test
DataSource dataSource = getConfiguration().getDataSourceFactory().build(new MetricRegistry(), "cleanup");
try (Connection connection = dataSource.getConnection()) {
for (String table : TABLES) {
PreparedStatement statement = connection.prepareStatement("DELETE FROM " + table);
statement.executeUpdate();
}
connection.prepareStatement("DROP ALL OBJECTS").executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("could not cleanup database", e);
}