Home > Back-end >  DBIOAction cleanUp or asTry do not lead to internal execution
DBIOAction cleanUp or asTry do not lead to internal execution

Time:05-24

I'm trying to test whether side effects get executed or not. Neither

DBIOAction.successful(()).cleanUp(_.fold {
  println("yeay!")
  DBIOAction.successful(())
} { _ => 
  println("aww.")
  DBIOAction.successful(())
})

nor

DBIOAction.successful(()).asTry.map {
  case Succeed(_) => println("yeay!")
  case Failure(_) => println("aww.")
}

print anything. I am not too familiar w/ Slick but it may need to place run somewhere. Is there a way to provide a lightweight runtime for testing purposes?

CodePudding user response:

Consulting the documentation of I/O actions:

Operations that can be executed on a database are called database I/O actions (DBIOAction). Several operations on queries and tables create I/O actions, for example myQuery.result, myQuery.result.headOption, myQuery = data or myTable.schema.create. Actions can be composed with combinators like andThen, flatMap, DBIO.seq or transactionally.

Just like a query, an I/O action is only a description of an operation. Creating or composing actions does not execute anything on a database. Combined actions always consist of strictly linear sequences of other actions. Parts of an action never run concurrently.

and about results:

Any action can be run on a database to obtain the results (or perform side effects such as updating the database). Execution is always asynchronous, i.e. it does not block the caller thread. Any kind of action can be run to obtain a Future that is eventually completed with a result when the execution is finished (myDatabase.run(myAction)). Actions that produce a sequence of values usually support streaming results as well. Such an action can be combined with a database to produce a Reactive Streams Publisher (myDatabase.stream(myAction)). The action is executed when a consumer subscribes to the Publisher.

you have to database.run(ioAction) to have any side effect (including println) evaluated.

  • Related