代码之家  ›  专栏  ›  技术社区  ›  Synesso

在ScalikeJDBC内存测试数据库中应用进化

  •  0
  • Synesso  · 技术社区  · 5 年前

    我正在使用ScalikeJDBC进行播放。我想为我的Specs2测试将evolutions应用到内存数据库中。

    import org.specs2.mutable.Specification
    import org.specs2.specification.BeforeAfterAll
    import play.api.db.{Database, Databases}
    import play.api.db.evolutions.Evolutions
    import scalikejdbc.ConnectionPool.DEFAULT_NAME
    import scalikejdbc.{ConnectionPool, DataSourceConnectionPool}
    
    import scalikejdbc.{AutoSession, _}
    
    class PaymentRepoSpec extends Specification with BeforeAfterAll {
    
      private var database: Option[Database] = None
    
      def beforeAll(): Unit = {
        database = Some(Databases.inMemory(
          name = "payment-repo",
          urlOptions = Map("MODE" -> "PostgreSQL", "DATABASE_TO_UPPER" -> "FALSE"),
          config = Map()
        ))
        database.foreach(Evolutions.applyEvolutions(_))
        database.foreach(db => ConnectionPool.add(DEFAULT_NAME, new DataSourceConnectionPool(db.dataSource)))
      }
    
      def afterAll(): Unit = {
        database.foreach(Evolutions.cleanupEvolutions(_))
        database.foreach(_.shutdown())
      }
    
      "fruitcakes" should {
        "be delicious" >> {
    
          import scalikejdbc._
    
          DB.readOnly { implicit s =>
            sql"""select id from payments"""
              .map(_.int(1)).list().apply().foreach(println)
          }
          ok
        }
      }
    }
    

    org.h2.jdbc.JdbcSQLException: Table "payments" not found; SQL statement:
    select id from payments [42102-192]
    

    包括

    modules.enabled += "scalikejdbc.PlayModule"
    modules.disabled += "play.api.db.DBModule"
    

    evolutions.default/1版本.sql语句

    CREATE TABLE payments(
        id                  SERIAL PRIMARY KEY,
        source              CHAR(56) NOT NULL,
        destination         CHAR(56) NOT NULL,
        code                VARCHAR(12) NOT NULL,
        issuer              CHAR(56),
        units               NUMERIC NOT NULL,
        received            TIMESTAMP NOT NULL,
        scheduled           TIMESTAMP NOT NULL,
        submitted           TIMESTAMP,
        status              VARCHAR(9) NOT NULL CHECK (status IN ('pending', 'submitted', 'failed', 'succeeded')),
        op_result           VARCHAR(64)
    );
    

    (这在运行应用程序时有效)

    包括

    libraryDependencies ++= Seq(
      guice, evolutions, jdbc, specs2 % Test,
      "io.github.synesso" %% "scala-stellar-sdk" % "0.5.1",
      "com.nrinaudo" %% "kantan.csv-generic" % "0.4.0",
      "com.h2database" % "h2" % "1.4.192",
      "org.postgresql" % "postgresql" % "42.2.5",
      "org.scalikejdbc" %% "scalikejdbc" % "3.3.0",
      "org.scalikejdbc" %% "scalikejdbc-config" % "3.3.0",
      "org.scalikejdbc" %% "scalikejdbc-play-initializer" % "2.6.0-scalikejdbc-3.3",
      "org.webjars.npm" % "bulma" % "0.7.2",
      "org.webjars" % "font-awesome" % "5.6.3",
      "org.specs2" %% "specs2-scalacheck" % "4.3.6" % Test
    )
    

    如何让测试对ScalikeJDBC数据库运行演进?

    0 回复  |  直到 5 年前
        1
  •  0
  •   Synesso    5 年前

    参数 name Databases.inMemory 必须与下的文件夹匹配 evolutions .

    例如,如果进化 evolutions/default/*.sql ,那么你必须打电话 Databases.inMemory(name="default", db) .

    推荐文章