Skip to content

Commit

Permalink
add test cases for json[] in json4s/play-json test suite (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
tminglei committed Apr 19, 2015
1 parent 4b12151 commit 4c8e064
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class PgJson4sSupportSuite extends FunSuite {

override val api = new API with JsonImplicits {
implicit val strListTypeMapper = new SimpleArrayJdbcType[String]("text").to(_.toList)
implicit val json4sJsonArrayTypeMapper =
new AdvancedArrayJdbcType[JValue](pgjson,
(s) => utils.SimpleArrayUtils.fromString[JValue](jsonMethods.parse(_))(s).orNull,
(v) => utils.SimpleArrayUtils.mkString[JValue](j=>jsonMethods.compact(jsonMethods.render(j)))(v)
).to(_.toList)
}

val plainAPI = new API with Json4sJsonPlainImplicits
Expand All @@ -31,21 +36,22 @@ class PgJson4sSupportSuite extends FunSuite {

val db = Database.forURL(url = dbUrl, driver = "org.postgresql.Driver")

case class JsonBean(id: Long, json: JValue)
case class JsonBean(id: Long, json: JValue, jsons: List[JValue])

class JsonTestTable(tag: Tag) extends Table[JsonBean](tag, "JsonTest1") {
def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
def json = column[JValue]("json")
def jsons = column[List[JValue]]("jsons")

def * = (id, json) <> (JsonBean.tupled, JsonBean.unapply)
def * = (id, json, jsons) <> (JsonBean.tupled, JsonBean.unapply)
}
val JsonTests = TableQuery[JsonTestTable]

//------------------------------------------------------------------------------

val testRec1 = JsonBean(33L, parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """))
val testRec2 = JsonBean(35L, parse(""" [ {"a":"v1","b":2}, {"a":"v5","b":3} ] """))
val testRec3 = JsonBean(37L, parse(""" ["a", "b"] """))
val testRec1 = JsonBean(33L, parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """), List(parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """)))
val testRec2 = JsonBean(35L, parse(""" [ {"a":"v1","b":2}, {"a":"v5","b":3} ] """), List(parse(""" [ {"a":"v1","b":2}, {"a":"v5","b":3} ] """)))
val testRec3 = JsonBean(37L, parse(""" ["a", "b"] """), Nil)

test("Json4s Lifted support") {
val json1 = parse(""" {"a":"v1","b":2} """)
Expand All @@ -61,6 +67,9 @@ class PgJson4sSupportSuite extends FunSuite {
JsonTests.filter(_.id === testRec2.id.bind).map(_.json).result.head.map(
r => assert(JArray(List(json1,json2)) === r)
),
JsonTests.to[List].result.map(
r => assert(List(testRec1, testRec2, testRec3) === r)
),
// ->>/->
JsonTests.filter(_.json.+>>("a") === "101").map(_.json.+>>("c")).result.head.map(
r => assert("[3,4,5,9]" === r.replace(" ", ""))
Expand Down Expand Up @@ -139,12 +148,14 @@ class PgJson4sSupportSuite extends FunSuite {

//------------------------------------------------------------------------------

case class JsonBean1(id: Long, json: JValue)

test("Json4s Plain SQL support") {
import MyPostgresDriver.plainAPI._

implicit val getJsonBeanResult = GetResult(r => JsonBean(r.nextLong(), r.nextJson()))
implicit val getJsonBeanResult = GetResult(r => JsonBean1(r.nextLong(), r.nextJson()))

val b = JsonBean(34L, parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """))
val b = JsonBean1(34L, parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """))

Await.result(db.run(
DBIO.seq(
Expand All @@ -154,7 +165,7 @@ class PgJson4sSupportSuite extends FunSuite {
""",
///
sqlu""" insert into JsonTest1 values(${b.id}, ${b.json}) """,
sql""" select * from JsonTest1 where id = ${b.id} """.as[JsonBean].head.map(
sql""" select * from JsonTest1 where id = ${b.id} """.as[JsonBean1].head.map(
r => assert(b === r)
),
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class PgPlayJsonSupportSuite extends FunSuite {

override val api = new API with JsonImplicits {
implicit val strListTypeMapper = new SimpleArrayJdbcType[String]("text").to(_.toList)
implicit val json4sJsonArrayTypeMapper =
new AdvancedArrayJdbcType[JsValue](pgjson,
(s) => utils.SimpleArrayUtils.fromString[JsValue](Json.parse(_))(s).orNull,
(v) => utils.SimpleArrayUtils.mkString[JsValue](_.toString())(v)
).to(_.toList)
}

val plainAPI = new API with PlayJsonPlainImplicits
Expand All @@ -27,21 +32,22 @@ class PgPlayJsonSupportSuite extends FunSuite {

val db = Database.forURL(url = dbUrl, driver = "org.postgresql.Driver")

case class JsonBean(id: Long, json: JsValue)
case class JsonBean(id: Long, json: JsValue, jsons: List[JsValue])

class JsonTestTable(tag: Tag) extends Table[JsonBean](tag, "JsonTest2") {
def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
def json = column[JsValue]("json", O.Default(Json.parse(""" {"a":"v1","b":2} """)))
def jsons = column[List[JsValue]]("jsons")

def * = (id, json) <> (JsonBean.tupled, JsonBean.unapply)
def * = (id, json, jsons) <> (JsonBean.tupled, JsonBean.unapply)
}
val JsonTests = TableQuery[JsonTestTable]

//------------------------------------------------------------------------------

val testRec1 = JsonBean(33L, Json.parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """))
val testRec2 = JsonBean(35L, Json.parse(""" [ {"a":"v1","b":2}, {"a":"v5","b":3} ] """))
val testRec3 = JsonBean(37L, Json.parse(""" ["a", "b"] """))
val testRec1 = JsonBean(33L, Json.parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """), List(Json.parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """)))
val testRec2 = JsonBean(35L, Json.parse(""" [ {"a":"v1","b":2}, {"a":"v5","b":3} ] """), List(Json.parse(""" [ {"a":"v1","b":2}, {"a":"v5","b":3} ] """)))
val testRec3 = JsonBean(37L, Json.parse(""" ["a", "b"] """), Nil)

test("Play json Lifted support") {
val json1 = Json.parse(""" {"a":"v1","b":2} """)
Expand All @@ -57,6 +63,9 @@ class PgPlayJsonSupportSuite extends FunSuite {
JsonTests.filter(_.id === testRec2.id.bind).map(_.json).result.head.map(
r => assert(JsArray(List(json1,json2)) === r)
),
JsonTests.to[List].result.map(
r => assert(List(testRec1, testRec2, testRec3) === r)
),
// ->>/->
JsonTests.filter(_.json.+>>("a") === "101").map(_.json.+>>("c")).result.head.map(
r => assert("[3,4,5,9]" === r.replace(" ", ""))
Expand Down Expand Up @@ -135,12 +144,14 @@ class PgPlayJsonSupportSuite extends FunSuite {

//------------------------------------------------------------------------------

case class JsonBean1(id: Long, json: JsValue)

test("Json Plain SQL support") {
import MyPostgresDriver.plainAPI._

implicit val getJsonBeanResult = GetResult(r => JsonBean(r.nextLong(), r.nextJson()))
implicit val getJsonBeanResult = GetResult(r => JsonBean1(r.nextLong(), r.nextJson()))

val b = JsonBean(34L, Json.parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """))
val b = JsonBean1(34L, Json.parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """))

Await.result(db.run(
DBIO.seq(
Expand All @@ -150,7 +161,7 @@ class PgPlayJsonSupportSuite extends FunSuite {
""",
///
sqlu""" insert into JsonTest2 values(${b.id}, ${b.json}) """,
sql""" select * from JsonTest2 where id = ${b.id} """.as[JsonBean].head.map(
sql""" select * from JsonTest2 where id = ${b.id} """.as[JsonBean1].head.map(
r => assert(b === r)
),
///
Expand Down

0 comments on commit 4c8e064

Please sign in to comment.