Permalink
Browse files

Adding overloaded sequence method for Option, simple test - #1192

  • Loading branch information...
1 parent a0eb1ee commit 97758946c18fa1224b1228c4a79b84656054c1e3 Grenville Wilson committed Jul 12, 2016
@@ -1,6 +1,8 @@
package com.typesafe.slick.testkit.tests
-import com.typesafe.slick.testkit.util.{StandardTestDBs, RelationalTestDB, AsyncTest}
+import com.typesafe.slick.testkit.util.{AsyncTest, RelationalTestDB, StandardTestDBs}
+import slick.dbio.DBIOAction
+import slick.dbio.Effect.Read
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.Future
@@ -110,6 +112,30 @@ class ActionTest extends AsyncTest[RelationalTestDB] {
)
} else DBIO.successful(())
+ def testOptionSequence = {
+ class T(tag: Tag) extends Table[Option[Int]](tag, u"t") {
+ def a = column[Int]("a")
+ def * = a.?
+ }
+ val ts = TableQuery[T]
+
+ val aSetup = ts.schema.create
+
+ val a1 = LiteralColumn(Option(1)).result
+ val a2 = DBIO.sequence(Option(LiteralColumn(1).result))
+ val a3 = DBIO.sequence(Option.empty[DBIO[Int]])
+
+ for {
+ _ <- aSetup
+ b1 <- a1
+ b2 <- a2
+ b3 <- a3
+ } yield {
+ b1 shouldBe b2
+ b2 shouldNotBe b3
+ }
+ }
+
def testFlatten = {
class T(tag: Tag) extends Table[Int](tag, u"t") {
def a = column[Int]("a")
@@ -170,6 +170,12 @@ object DBIOAction {
total.result()
}
+ /** Transform a `Option[ DBIO[R] ]` into a `DBIO[ Option[R] ]`. */
+ def sequence[R, E <: Effect](in: Option[DBIOAction[R, NoStream, E]]): DBIOAction[Option[R], NoStream, E] = {
+ implicit val ec = DBIO.sameThreadExecutionContext
+ sequence(in.toList).map(_.headOption)
+ }
+
/** Transform a `TraversableOnce[ DBIO[R] ]` into a `DBIO[ TraversableOnce[R] ]`. */
def sequence[R, M[+_] <: TraversableOnce[_], E <: Effect](in: M[DBIOAction[R, NoStream, E]])(implicit cbf: CanBuildFrom[M[DBIOAction[R, NoStream, E]], R, M[R]]): DBIOAction[M[R], NoStream, E] = {
implicit val ec = DBIO.sameThreadExecutionContext

0 comments on commit 9775894

Please sign in to comment.