Note that there are some explanatory texts on larger screens.

plurals
  1. POPlay2 and Scala, How should I configure my integration tests to run with proper DB
    primarykey
    data
    text
    <p>I'm trying to figure out how to write my db integration tests in my Play2 app.</p> <p>In my conf file I have specified two databases, xxx_test for regular use and h2 db for testing:</p> <pre><code>db.xxx_test.driver=com.mysql.jdbc.Driver db.xxx_test.url="jdbc:mysql://localhost/xxx_test?characterEncoding=UTF-8" db.xxx_test.user="root" db.xxx_test.password="" db.h2.driver=org.h2.Driver db.h2.url="jdbc:h2:mem:play" db.h2.user=sa db.h2.password="" </code></pre> <p>In my User object I have specified <code>xxx_test</code> to be used when I run the application. </p> <pre><code>def createUser(user: User): Option[User] = { DB.withConnection("xxx_test") { implicit connection =&gt; SQL("insert into users(first_name, last_name, email, email_validated, last_login, created, modified, active) values({first_name},{last_name},{email},{email_validated},{last_login}, {created}, {modified}, {active})").on( 'first_name -&gt; user.firstName, 'last_name -&gt; user.lastName, 'email -&gt; user.email, 'email_validated -&gt; user.emailValidated, 'last_login -&gt; user.lastLogin, 'created -&gt; user.created, 'modified -&gt; user.modified, 'active -&gt; true ).executeInsert().map(id =&gt; { return Some(User(new Id[Long](id), user.firstName, user.lastName, user.email, user.emailValidated, user.lastLogin, user.created, user.modified, true)) } ) } None } </code></pre> <p>In my test I create a new inMemoryDatabase and then use the User to create and get my object for testing.</p> <pre><code>class DBEvolutionsTest extends Specification { "The Database" should { "persist data properly" in { running(FakeApplication(additionalConfiguration = inMemoryDatabase())) { User.create(User(Id[Long](1L), "jakob", "aa", "aaa", true, DateTime.now(), DateTime.now(), DateTime.now(), true)) val newUser = User.findBy(Id[Long](1L)) newUser.get.firstName must beEqualTo("jakob") } } } } </code></pre> <p>This of course is not the correct way since the User object uses the <code>xxx_test</code> and not the <code>h2</code> db. The test will create a User in the real db and not the one in memory since I have specified the db in the User(<code>DB.withConnection("xxx_test")</code>) object. I guess there is some smart way of doing this, I do not want to pass the db name around inte the application like <code>User.create(User(...), "xxx_test")</code> </p> <p>How have you solved this problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload