Integration Tests

Learn how to test the FlywayDatabaseMigrator and the repository.

Testing the FlywayDatabaseMigrator

To verify the basic behavior of our FlywayDatabaseMigrator, we write a simple test.

Press + to interact
"thedatabaseisnotavailable"must{
"throw an exception" in {
val cfg = DatabaseConfig(
driver = "This is no driver name!",
url = "jdbc://some.host/whatever",
user = "no-user",
pass = "no-password"
)
val migrator: DatabaseMigrator[IO] = new FlywayDatabaseMigrator
val program = migrator.migrate(cfg.url, cfg.user, cfg.pass)
an[FlywayException] must be thrownBy program.unsafeRunSync
}
}

We construct an invalid database configuration within this test and expect that the call to migrate throws an exception. Review lines 3–8. If you remember, we had this issue already and chose not to handle any exceptions but let the calling site do this, for example, via a MonadError instance.

Press + to interact
dbConfig.map { cfg =>
val migrator: DatabaseMigrator[IO] = new FlywayDatabaseMigrator
val program = migrator.migrate(cfg.url, cfg.user, cfg.pass)
program.unsafeRunSync must be > 0
}
// --
dbConfig.map { cfg =>
val migrator: DatabaseMigrator[IO] = new FlywayDatabaseMigrator
val program = migrator.migrate(cfg.url, cfg.user, cfg.pass)
val _ = program.unsafeRunSync
program.unsafeRunSync must be(0)
}

The other two tests are also quite simple: we just expect them to return either zero or the ...