我正在使用Acolyte来模拟JDBC驱动程序并在Scala中测试我的数据库层。 没关系,但我找不到任何方法来测试事务行为(提交,回滚......)。
基本上我有一些模拟UnitOfWork的东西,如果适当调用commit和rollback命令,我想测试一下:
我有类似的东西
def executeUnitOfWorkNoResult(sqlInstructions: List[String])
(connectionFactory:()=>Connection): Try[Unit] = {
import implicits.TryImplicits._
var connection: Connection = null
Try {
connection = connectionFactory()
connection.setAutoCommit(false)
val execs = sqlInstructions.foldLeft(Try[Unit]())((tryInstance, sql)=> {
tryInstance.flatMap(t => {
val stmt:Statement = null
Try {
val stmt = connection.createStatement()
stmt.execute(sql)
()
}doFinally {
Try{stmt.close}
}
})
})
execs match {
case Success(_) => connection.commit()
case Failure(e) => {
connection.rollback()
throw e
}
}
} doFinally {
if (connection != null) {
Try(connection.close)
}
}
}
测试基本内容很好,例如测试执行命令:
"executeUnitOfWork" should " execute commands" in {
var insStmtCmt = 0;
val handlerA = AcolyteDSL.handleStatement.withUpdateHandler { e =>
if(e.sql.toLowerCase.startsWith("insert")){
insStmtCmt = insStmtCmt+1
}
UpdateResult.One
}
def acquireMock() = AcolyteDSL.connection(handlerA)
val res = SqlUtils.executeUnitOfWorkNoResult(List(
"insert into MetricDescription values('blaf','','','')",
"insert into MetricDescription values('blaf2','','','')"))(acquireMock)
res.isSuccess shouldBe true
insStmtCmt should equal(2)
}
但是与交易相关的任何内容似乎都不受支持。
在文档中写道,有一个伪支持,但我找不到它。
有什么想法吗?