prepopulated, reloadable in-memory H2 database? For testing.

52 views
Skip to first unread message

Andrzej Skalski

unread,
Jul 30, 2015, 6:58:18 PM7/30/15
to Lift
Hi,

I started to use specs2 seriously for my project, testing database. I had to update to "specs2-core" % "3.6.3" to get anything working, but that's another story.

Anyway, there are several scenarios in which I use some initial values, that are computed basing on several hard-drive files. The process takes 4-5 seconds, so if it was done once, it wouldn't be bad.

Unfortunately, there are several tests, where I really need to have the database clean (to see for instance if duplication elimination mechanisms work correctly). This gives me a multiplier, and my tests are getting 20 seconds longer pretty much everyday.

Is there a way to initialize database once, and then copy-and-paste it to other tests?

Right now I do:

trait WithSomeMatapDB extends BeforeAfterEach {

def admin = User.findUserByUniqueId(DefaultValues.getAdminUserId).get

override def before = {
DBHelper.initSquerylRecordWithInMemoryDB()
DBHelper.createSchema
DBHelper.putBasicTemplates
}

override def after = {
DBHelper.dropSchema
DBHelper.closeSession
}
}

where

def initSquerylRecordWithInMemoryDB() {
Class.forName("org.h2.Driver")
SquerylRecord.initWithSquerylSession {
val session = Session.create(DriverManager.getConnection("jdbc:h2:mem:testSquerylRecordDB;DB_CLOSE_DELAY=-1", "sa", ""), new H2Adapter)
// session.setLogger(statement => println(statement))
session
}
}

what I'd prefer to do is to read the h2database with some initial contents from hard drive (or even memory singleton), do a test, and reload it again to initial contents "BeforeAfterEach". It's "putBasicTemplates" that consumes several seconds, it communicates with outside services.

Any ideas?

Diego Medina

unread,
Aug 2, 2015, 12:57:15 AM8/2/15
to Lift
you may want to try sending this email to the specs2 mailing list (if you haven't already) because your question doesn't just apply to running tests on a Lift app.

Thanks

Diego

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Diego Medina
Lift/Scala consultant
di...@fmpwizard.com
http://blog.fmpwizard.com/

Richard Dallaway

unread,
Aug 4, 2015, 3:44:59 AM8/4/15
to lif...@googlegroups.com
With h2 there's a parameter called INIT you can use to supply a .sql. So  another option might be to generate your set-up as a SQL script, which will be executed when a connection is made.

http://www.h2database.com/html/features.html#execute_sql_on_connection

Richard

--
Reply all
Reply to author
Forward
0 new messages