3

I try to test my play 2 application with scalatest. To test the model I use test database. Each time I run tests the data inserts again. So I need to delete data after test or even drop all the tables. My code is:

import models.User
import org.joda.time.DateTime
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import org.squeryl.adapters.PostgreSqlAdapter
import org.squeryl.{Session, SessionFactory}
import play.api.db.DB
import play.api.{Application, GlobalSettings}
import play.api.test.FakeApplication
class UserSpec extends PlaySpec with OneAppPerSuite with BeforeAndAfterAll {
  implicit override lazy val app: FakeApplication = {
  FakeApplication(
    additionalConfiguration = Map(
      "db.default.driver" -> "org.postgresql.Driver",
      "db.default.url" -> "jdbc:postgresql://localhost:5432/test",
      "db.default.user" -> "test",
      "db.default.password" -> "test"),
    withGlobal = Some(new GlobalSettings {
      override def onStart(app: Application) = {
        SessionFactory.concreteFactory = Some(() => Session.create(DB.getConnection()(app), new PostgreSqlAdapter))
      }}
    )
  )}

  override protected def afterAll(): Unit = {
  }

  "User" should {
    "be creatable and saved to DB" in {
      val user = User.createUser(
      User("example1@example.com",
      "John",
      "Doe",
      new DateTime(),
      "afc677037be3d92324fa6597d6c1506b534e306b" // sha1("123456aA")
      ))
      user.isPersisted mustBe true
    }
  }

}

Is it possible to apply Downs evolution after the tests?

pimezone
  • 113
  • 1
  • 1
  • 5

0 Answers0