|
Play Framework/Scala example source code file (RowSpec.scala)
The RowSpec.scala Play Framework example source code
package anorm
import acolyte.jdbc.AcolyteDSL.{ connection, handleQuery }
import acolyte.jdbc.{ QueryResult, RowLists }
import RowLists.{ rowList2, rowList1, stringList }
import acolyte.jdbc.Implicits._
object RowSpec extends org.specs2.mutable.Specification {
"Row" title
"List of column values" should {
"be expected one" in withQueryResult(rowList2(
classOf[String] -> "foo", classOf[Int] -> "bar") :+ ("row1", 100)) {
implicit c =>
SQL("SELECT * FROM test").map(_.asList).single.
aka("column list") must_== List("row1", 100)
}
"keep null if not nullable" in withQueryResult(stringList :+ null) {
implicit c =>
SQL("SELECT 1").map(_.asList).single.
aka("column list") must_== List(null)
}
"turn null into None if nullable" in withQueryResult(
stringList.withNullable(1, true) :+ null) { implicit c =>
SQL("SELECT 1").map(_.asList).single.
aka("column list") must_== List(None)
}
"turn value into Some(X) if nullable" in withQueryResult(
stringList.withNullable(1, true) :+ "str") { implicit c =>
SQL("SELECT 1").map(_.asList).single.
aka("column list") must_== List(Some("str"))
}
}
"Column dictionary" should {
"be expected one" in withQueryResult(rowList2(
classOf[String] -> "foo", classOf[Int] -> "bar") :+ ("row1", 100)) {
implicit c =>
SQL("SELECT * FROM test").map(_.asMap).single.
aka("column map") must_== Map(".foo" -> "row1", ".bar" -> 100)
}
"keep null if not nullable" in withQueryResult(
rowList1(classOf[String] -> "foo") :+ null) { implicit c =>
SQL("SELECT 1").map(_.asMap).single.
aka("column map") must_== Map(".foo" -> null)
}
"turn null into None if nullable" in withQueryResult(
rowList1(classOf[String] -> "foo").withNullable(1, true) :+ null) {
implicit c =>
SQL("SELECT 1").map(_.asMap).single.
aka("column map") must_== Map(".foo" -> None)
}
"turn value into Some(X) if nullable" in withQueryResult(
rowList1(classOf[String] -> "foo").withNullable(1, true) :+ "str") {
implicit c =>
SQL("SELECT 1").map(_.asMap).single.
aka("column map") must_== Map(".foo" -> Some("str"))
}
}
// ---
def withQueryResult[A](r: QueryResult)(f: java.sql.Connection => A): A =
f(connection(handleQuery { _ => r }))
}
Other Play Framework source code examplesHere is a short list of links related to this Play Framework RowSpec.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.