简体   繁体   English

如何为我的Scala Play2 CRUD应用程序设置内存测试数据库?

[英]How to set an in memory test database for my Scala Play2 CRUD application?

I'm continuing my exploration of the Play framework and its related components. 我将继续探索Play框架及其相关组件。 I used the template for CRUD application with a connection to a PostgreSQL database to begin with. 首先,我将CRUD应用程序的模板与PostgreSQL数据库连接使用。 It splits the application in models, repositories, controllers and views. 它将应用程序分为模型,存储库,控制器和视图。 This works well. 这很好。 Now, I'm trying to create some tests for this application with Specs2. 现在,我正在尝试使用Specs2为此应用程序创建一些测试。 More precisely, I'm trying to test the repository. 更准确地说,我正在尝试测试存储库。 It is defined as follow: 它的定义如下:

package dal
import javax.inject.{ Inject, Singleton }
import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile

import models.Cat
import scala.concurrent.{ Future, ExecutionContext }

@Singleton
class CatRepository @Inject() (dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
...
}

I would like to set an in memory database which would be created (schema, evolutions) before all tests, destroyed after all tests, populated (data, probably with direct SQL) and flushed around each test. 我想设置一个内存数据库,该数据库将在所有测试之前创建(架构,演变),在所有测试之后销毁,填充(数据,可能使用直接SQL)并在每个测试前后进行刷新。 I would like to pass it on to my repository instance which I would then use to perform my test. 我想将其传递给我的存储库实例,然后将其用于执行测试。 Like: 喜欢:

val repo = new CatRepository(inMem_DB)

So how do I go about: 1) creating this db and applying the evolutions? 那么我该怎么做:1)创建该数据库并应用演变? maybe: 也许:

trait TestDB extends BeforeAfterAll {
    var database: Option[Database] = None

    def before = {
        database = Some(Databases.inMemory(name = "test_db"))
        database match {
            case Some(con) => Evolutions.applyEvolutions(con)
            case _ => None
        }
        println("DB READY")
    }

    def after = {
        database match {
            case Some(con) => con.shutdown()
            case _ => None
        }
    }
}

Using a var and always making a "match/case" when I need to use the db isn't convenient. 使用var并在需要使用db时总是进行“匹配/大小写”并不方便。 I guess there is a much better to do this... 我想这样做有更好的选择...

2) Populate and flush around each test? 2)在每个测试周围填充并冲洗? Shall I create a trait extending Around, just as with BeforeAfterAll? 我是否应该像BeforeAfterAll一样创建一个周围延伸的特征?

3) Create on of these play.api.db.slick.DatabaseConfigProvider from the database? 3)从数据库中创建这些play.api.db.slick.DatabaseConfigProvider吗? Any link showing how to do this? 任何链接显示如何执行此操作?

I found few examples which where covering this with running a FakeApplication, but I assume there is a way to pass somehow the db to such repository object outside of a running application..? 我发现了几个示例,这些示例通过运行FakeApplication进行了介绍,但是我认为有一种方法可以将数据库以某种方式传递给正在运行的应用程序之外的此类存储库对象。

Thank you for helping. 感谢您的帮助。 Cheers! 干杯!

You can use a lazy val and AfterAll for the database setup/teardown, then BeforeAfterEach for each example: 您可以使用lazy valAfterAll进行数据库设置/拆卸,然后对每个示例使用BeforeAfterEach

trait TestDB extends AfterAll with BeforeAfterEach {
  lazy val database: Database = 
    Databases.inMemory(name = "test_db")

  def afterAll = 
    database.shutdown


  def before = 
    database.populate

  def after =
    datbase.clean
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM