Reputation: 580
In order to make isolated tests, it would be interesting to drop and create tables or clean them before each test. Any easy way to do it with SORM?
Upvotes: 3
Views: 279
Reputation: 580
My solution was to define the reload method to drop and recreate tables without reconnecting. My Instance definition:
class DatabaseTest extends Instance(...) {
def reload = {
connector.withConnection { connection =>
connection.dropAllTables()
Create.tables(mappings.values).foreach {
t => connection.createTable(t)
}
}
}
}
Upvotes: 2
Reputation: 43310
You can create new instances for each test with initMode
set to DropAllCreate and free all instance's resources after, using the close() method.
A custom function like the following might be helpful to you:
def withDb ( f : Instance => () ) {
val db = new Instance ( ..., initMode = InitMode.DropAllCreate )
f(db)
db.close()
}
Using it it'll be easy to always work in a context of a freshly created instance:
withDb { db =>
db.save(...)
db.query[...](...)
}
Upvotes: 2