Skip to content

Commit

Permalink
switch integration tests to use h2 where possible (DAT-16015) (#5417)
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenMassaro committed Jan 5, 2024
1 parent e1fa277 commit f463402
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,29 @@ import liquibase.TagVersionEnum
import liquibase.command.util.CommandUtil
import liquibase.exception.CommandExecutionException
import liquibase.exception.RollbackFailedException
import liquibase.exception.UnexpectedLiquibaseException
import liquibase.extension.testing.testsystem.DatabaseTestSystem
import liquibase.extension.testing.testsystem.TestSystemFactory
import liquibase.extension.testing.testsystem.spock.LiquibaseIntegrationTest
import liquibase.logging.core.BufferedLogService
import liquibase.resource.SearchPathResourceAccessor
import liquibase.ui.ConsoleUIService
import spock.lang.Shared
import spock.lang.Specification

import java.util.logging.Level

@LiquibaseIntegrationTest
class RollbackIntegrationTest extends Specification {
@Shared
private DatabaseTestSystem postgres = (DatabaseTestSystem) Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getTestSystem("postgresql")
private DatabaseTestSystem h2 = (DatabaseTestSystem) Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getTestSystem("h2")

def "Should rollback correctly with duplicate tags"() {
given:
def changelogFile = 'target/test-classes/changelogs/pgsql/rollback/rollback-to-tag-changelog.xml'
CommandUtil.runUpdate(postgres, changelogFile)
CommandUtil.runUpdate(h2, changelogFile)
ConsoleUIService console = Scope.getCurrentScope().getUI() as ConsoleUIService
def outputStream = new ByteArrayOutputStream()
console.setOutputStream(new PrintStream(outputStream))

when:
CommandUtil.runRollback(new SearchPathResourceAccessor("."), postgres, changelogFile, "version_2.0", TagVersionEnum.NEWEST)
CommandUtil.runRollback(new SearchPathResourceAccessor("."), h2, changelogFile, "version_2.0", TagVersionEnum.NEWEST)
String outputString = outputStream.toString()

then:
Expand All @@ -41,10 +37,10 @@ class RollbackIntegrationTest extends Specification {
assert outputString.contains("Rolling Back Changeset: target/test-classes/changelogs/pgsql/rollback/rollback-to-tag-changelog.xml::13.2::testuser")

when:
CommandUtil.runUpdate(postgres, changelogFile)
CommandUtil.runUpdate(h2, changelogFile)
outputStream = new ByteArrayOutputStream()
console.setOutputStream(new PrintStream(outputStream))
CommandUtil.runRollback(new SearchPathResourceAccessor("."), postgres, changelogFile, "version_2.0", TagVersionEnum.OLDEST)
CommandUtil.runRollback(new SearchPathResourceAccessor("."), h2, changelogFile, "version_2.0", TagVersionEnum.OLDEST)
outputString = outputStream.toString()

then:
Expand All @@ -55,7 +51,7 @@ class RollbackIntegrationTest extends Specification {
when:
outputStream = new ByteArrayOutputStream()
console.setOutputStream(new PrintStream(outputStream))
CommandUtil.runRollback(new SearchPathResourceAccessor("."), postgres, changelogFile, "version_2.0")
CommandUtil.runRollback(new SearchPathResourceAccessor("."), h2, changelogFile, "version_2.0")
outputString = outputStream.toString()

then:
Expand All @@ -64,6 +60,6 @@ class RollbackIntegrationTest extends Specification {
assert e.getMessage() == "liquibase.exception.RollbackFailedException: Could not find tag 'version_2.0' in the database"

cleanup:
CommandUtil.runDropAll(postgres)
CommandUtil.runDropAll(h2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,8 @@ VALUES('2', 'fl', '$changesetFilepath', '2023-09-29 14:33:39.112', 2, 'EXECUTED'
def ranChangeSets = changeLogService.getRanChangeSets()
ranChangeSets.size() == 1
ranChangeSets.get(0).getLastCheckSum().toString() == "7:72c7eea8dda3c3582e3cfb39eec12033"

cleanup:
CommandUtil.runDropAll(h2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ import spock.lang.Specification
class DiffIntegrationTest extends Specification {

@Shared
private DatabaseTestSystem postgres =
(DatabaseTestSystem) Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getTestSystem("postgresql")
private DatabaseTestSystem h2 =
(DatabaseTestSystem) Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getTestSystem("h2")

def "Diff with excludes that reference objects on target should work" () {
when:
CommandUtil.runDropAll(postgres)
CommandUtil.runSnapshot(postgres, "target/test-classes/snapshot.json")
CommandUtil.runTag(postgres, "1.0.0")
CommandUtil.runDropAll(h2)
CommandUtil.runSnapshot(h2, "target/test-classes/snapshot.json")
CommandUtil.runTag(h2, "1.0.0")
def diffFile = "target/test-classes/diff.json"
def url = "offline:postgresql?snapshot=target/test-classes/snapshot.json"
Database targetDatabase =
DatabaseFactory.getInstance().openDatabase(postgres.getConnectionUrl(), postgres.getUsername(), postgres.getPassword(), null, new SearchPathResourceAccessor("."))
DatabaseFactory.getInstance().openDatabase(h2.getConnectionUrl(), h2.getUsername(), h2.getPassword(), null, new SearchPathResourceAccessor("."))

def refUrl = "offline:postgresql?snapshot=target/test-classes/snapshot.json"
Database refDatabase =
Expand Down Expand Up @@ -72,8 +72,8 @@ class DiffIntegrationTest extends Specification {
} catch (Exception ignored) {

}
CommandUtil.runDropAll(postgres)
postgres.getConnection().close()
CommandUtil.runDropAll(h2)
h2.getConnection().close()
refDatabase.close()
targetDatabase.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ import java.util.logging.Level
@LiquibaseIntegrationTest
class ShowSummaryUtilCommandTest extends Specification {
@Shared
private DatabaseTestSystem postgres = (DatabaseTestSystem) Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getTestSystem("postgresql")
private DatabaseTestSystem h2 = (DatabaseTestSystem) Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getTestSystem("h2")

def "Should show summary output when run multiple times"() {
given:
String outputFile = "target/test-classes/output.txt"

when:
new File(outputFile).delete()
CommandUtil.runUpdate(postgres,'changelogs/pgsql/update/showSummaryWithLabels.xml', "testtable1", "none", outputFile)
CommandUtil.runUpdate(h2,'changelogs/pgsql/update/showSummaryWithLabels.xml', "testtable1", "none", outputFile)

then:
new File(outputFile).getText("UTF-8").contains("Run: 2")
new File(outputFile).getText("UTF-8").contains("Filtered out: 4")

when:
new File(outputFile).delete()
CommandUtil.runUpdate(postgres,'changelogs/pgsql/update/showSummaryWithLabels.xml', "testtable1", "none", outputFile)
CommandUtil.runUpdate(h2,'changelogs/pgsql/update/showSummaryWithLabels.xml', "testtable1", "none", outputFile)

then:
new File(outputFile).getText("UTF-8").contains("Run: 0")
new File(outputFile).getText("UTF-8").contains("Filtered out: 4")

cleanup:
CommandUtil.runDropAll(postgres)
if (postgres.getConnection() != null) {
postgres.getConnection().close()
CommandUtil.runDropAll(h2)
if (h2.getConnection() != null) {
h2.getConnection().close()
}
}

Expand All @@ -60,9 +60,9 @@ class ShowSummaryUtilCommandTest extends Specification {
@Override
void run() throws Exception {
CommandScope commandScope = new CommandScope(UpdateCommandStep.COMMAND_NAME)
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.URL_ARG, postgres.getConnectionUrl())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.USERNAME_ARG, postgres.getUsername())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.PASSWORD_ARG, postgres.getPassword())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.URL_ARG, h2.getConnectionUrl())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.USERNAME_ARG, h2.getUsername())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.PASSWORD_ARG, h2.getPassword())
commandScope.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, "changelogs/pgsql/update/showSummaryWithLabels.xml")
commandScope.addArgumentValue(UpdateCommandStep.LABEL_FILTER_ARG, "testtable1")
commandScope.addArgumentValue(UpdateCommandStep.CONTEXTS_ARG, null)
Expand All @@ -88,9 +88,9 @@ class ShowSummaryUtilCommandTest extends Specification {
!outputStream.toString().contains("UPDATE SUMMARY")

cleanup:
CommandUtil.runDropAll(postgres)
if (postgres.getConnection() != null) {
postgres.getConnection().close()
CommandUtil.runDropAll(h2)
if (h2.getConnection() != null) {
h2.getConnection().close()
}
}

Expand All @@ -105,9 +105,9 @@ class ShowSummaryUtilCommandTest extends Specification {
@Override
void run() throws Exception {
CommandScope commandScope = new CommandScope(UpdateCommandStep.COMMAND_NAME)
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.URL_ARG, postgres.getConnectionUrl())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.USERNAME_ARG, postgres.getUsername())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.PASSWORD_ARG, postgres.getPassword())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.URL_ARG, h2.getConnectionUrl())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.USERNAME_ARG, h2.getUsername())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.PASSWORD_ARG, h2.getPassword())
commandScope.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, "changelogs/pgsql/update/showSummaryWithLabels.xml")
commandScope.addArgumentValue(UpdateCommandStep.LABEL_FILTER_ARG, "testtable1")
commandScope.addArgumentValue(UpdateCommandStep.CONTEXTS_ARG, null)
Expand Down Expand Up @@ -135,9 +135,9 @@ class ShowSummaryUtilCommandTest extends Specification {
!logContent.contains("UPDATE SUMMARY")

cleanup:
CommandUtil.runDropAll(postgres)
if (postgres.getConnection() != null) {
postgres.getConnection().close()
CommandUtil.runDropAll(h2)
if (h2.getConnection() != null) {
h2.getConnection().close()
}
}

Expand All @@ -152,9 +152,9 @@ class ShowSummaryUtilCommandTest extends Specification {
@Override
void run() throws Exception {
CommandScope commandScope = new CommandScope(UpdateCommandStep.COMMAND_NAME)
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.URL_ARG, postgres.getConnectionUrl())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.USERNAME_ARG, postgres.getUsername())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.PASSWORD_ARG, postgres.getPassword())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.URL_ARG, h2.getConnectionUrl())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.USERNAME_ARG, h2.getUsername())
commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.PASSWORD_ARG, h2.getPassword())
commandScope.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, "changelogs/pgsql/update/showSummaryWithLabels.xml")
commandScope.addArgumentValue(UpdateCommandStep.LABEL_FILTER_ARG, "testtable1")
commandScope.addArgumentValue(UpdateCommandStep.CONTEXTS_ARG, null)
Expand Down Expand Up @@ -190,9 +190,9 @@ class ShowSummaryUtilCommandTest extends Specification {
streamContent.contains("DBMS mismatch: 1")

cleanup:
CommandUtil.runDropAll(postgres)
if (postgres.getConnection() != null) {
postgres.getConnection().close()
CommandUtil.runDropAll(h2)
if (h2.getConnection() != null) {
h2.getConnection().close()
}
}

Expand Down

0 comments on commit f463402

Please sign in to comment.