Skip to content

Instantly share code, notes, and snippets.

@robfletcher
Created August 18, 2011 16:27
Show Gist options
  • Save robfletcher/1154459 to your computer and use it in GitHub Desktop.
Save robfletcher/1154459 to your computer and use it in GitHub Desktop.
Example of data-driving a Spock specification using database metadata
import grails.plugin.spock.IntegrationSpec
import java.sql.Connection
import javax.sql.DataSource
import spock.lang.*
class DatabaseSchemaSpec extends IntegrationSpec {
@Shared def dataSource
@Shared List<String> tableNames
def setupSpec() {
DataSource.mixin(DataSourceCategory)
tableNames = []
dataSource.withConnection {connection ->
def rs = connection.metaData.getTables(null, null, '%', ['TABLE'] as String[])
while (rs.next()) {
tableNames << rs.getString(3)
}
}
}
@Unroll("the #table table has a primary key")
void "all tables have a primary key"() {
expect:
dataSource.withConnection { Connection connection ->
assert connection.metaData.getPrimaryKeys(null, null, table).next()
}
where:
table << tableNames
}
}
@Category(DataSource)
class DataSourceCategory {
static void withConnection(dataSource, Closure closure) {
Connection connection = dataSource.connection
try {
closure(connection)
} finally {
connection?.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment