Skip to content

Instantly share code, notes, and snippets.

@deusaquilus
Created April 27, 2021 00:40
Show Gist options
  • Save deusaquilus/62e42af0e9d7c40bc8599df3fb12c9c5 to your computer and use it in GitHub Desktop.
Save deusaquilus/62e42af0e9d7c40bc8599df3fb12c9c5 to your computer and use it in GitHub Desktop.
Postgres Async Encoding Spec with Large Encoding Entity
package io.getquill.context.sql
import java.time.LocalDate
import java.util.{ Date, UUID }
import io.getquill.context.Context
import io.getquill._
//case class EncodingTestType(value: String)
case class Number(value: String) extends AnyVal
object Number {
def withValidation(value: String): Option[Number] =
if (value.forall(_.isDigit))
Some(Number(value))
else
None
}
trait EncodingSpec extends Spec {
val context: SqlContext[_, _] //with TestEncoders with TestDecoders
import context._
case class SimpleEncodingTestEntity(
v1: String,
v2: BigDecimal,
v3: Boolean,
v4: Byte,
v5: Short,
v6: Int,
v7: Long,
v8: Float,
v9: Double,
o1: Option[String],
o2: Option[BigDecimal],
o3: Option[Boolean],
av9: Double,
ao1: Option[String],
ao2: Option[BigDecimal],
ao3: Option[Boolean],
o4: Option[Byte],
o5: Option[Short],
o6: Option[Int],
o7: Option[Long],
o8: Option[Float],
o9: Option[Double],
vv1: String,
vv2: BigDecimal,
vv3: Boolean,
vv4: Byte,
vv5: Short,
vv6: Int,
vv7: Long,
blah: Int,
vv9: Double,
vo1: Option[String],
vo2: Option[BigDecimal],
vo3: Option[Boolean],
vo4: Option[Byte],
vo5: Option[Short],
vo6: Option[Int],
vo7: Option[Long],
vo8: Option[Float],
vo9: Option[Double],
bv1: String,
bv2: BigDecimal,
bv3: Boolean,
bv4: Byte,
bv5: Short,
bv6: Int,
bv7: Long,
bv8: Float,
bv9: Double,
bo1: Option[String],
bo2: Option[BigDecimal],
bo3: Option[Boolean],
bav9: Double,
bao1: Option[String],
bao2: Option[BigDecimal],
bao3: Option[Boolean],
bo4: Option[Byte],
bo5: Option[Short],
bo6: Option[Int],
bo7: Option[Long],
bo8: Option[Float],
bo9: Option[Double],
bvv1: String,
bvv2: BigDecimal,
bvv3: Boolean,
bvv4: Byte,
bvv5: Short,
bvv6: Int,
bvv7: Long,
bblah: Int,
bvv9: Double,
bvo1: Option[String],
bvo2: Option[BigDecimal],
bvo3: Option[Boolean],
bvo4: Option[Byte],
bvo5: Option[Short],
bvo6: Option[Int],
bvo7: Option[Long],
bvo8: Option[Float],
bvo9: Option[Double],
cbv1: String,
cbv2: BigDecimal,
cbv3: Boolean,
cbv4: Byte,
cbv5: Short,
cbv6: Int,
cbv7: Long,
cbv8: Float,
cbv9: Double,
cbo1: Option[String],
cbo2: Option[BigDecimal],
cbo3: Option[Boolean],
cbav9: Double,
cbao1: Option[String],
cbao2: Option[BigDecimal],
cbao3: Option[Boolean],
cbo4: Option[Byte],
cbo5: Option[Short],
cbo6: Option[Int],
cbo7: Option[Long],
cbo8: Option[Float],
cbo9: Option[Double],
cbvv1: String,
cbvv2: BigDecimal,
cbvv3: Boolean,
cbvv4: Byte,
cbvv5: Short,
cbvv6: Int,
cbvv7: Long,
cbblah: Int,
cbvv9: Double,
cbvo1: Option[String],
cbvo2: Option[BigDecimal],
cbvo3: Option[Boolean],
cbvo4: Option[Byte],
cbvo5: Option[Short],
cbvo6: Option[Int],
cbvo7: Option[Long],
cbvo8: Option[Float],
cbvo9: Option[Double],
dcbv1: String,
dcbv2: BigDecimal,
dcbv3: Boolean,
dcbv4: Byte,
dcbv5: Short,
dcbv6: Int,
dcbv7: Long,
dcbv8: Float,
dcbv9: Double,
dcbo1: Option[String],
dcbo2: Option[BigDecimal],
dcbo3: Option[Boolean],
dcbav9: Double,
dcbao1: Option[String],
dcbao2: Option[BigDecimal],
dcbao3: Option[Boolean],
dcbo4: Option[Byte],
dcbo5: Option[Short],
dcbo6: Option[Int],
dcbo7: Option[Long],
dcbo8: Option[Float],
dcbo9: Option[Double],
dcbvv1: String,
dcbvv2: BigDecimal,
dcbvv3: Boolean,
dcbvv4: Byte,
dcbvv5: Short,
dcbvv6: Int,
dcbvv7: Long,
dcbblah: Int,
dcbvv9: Double,
dcbvo1: Option[String],
dcbvo2: Option[BigDecimal],
dcbvo3: Option[Boolean],
dcbvo4: Option[Byte],
dcbvo5: Option[Short],
dcbvo6: Option[Int],
dcbvo7: Option[Long],
dcbvo8: Option[Float],
dcbvo9: Option[Double]
)
case class EncodingTestEntity(
v1: String,
v2: BigDecimal,
v3: Boolean,
v4: Byte,
v5: Short,
v6: Int,
v7: Long,
v8: Float,
v9: Double,
v10: Array[Byte],
v11: Date,
//v12: EncodingTestType,
v13: LocalDate,
v14: UUID,
o1: Option[String],
o2: Option[BigDecimal],
o3: Option[Boolean],
o4: Option[Byte],
o5: Option[Short],
o6: Option[Int],
o7: Option[Long],
o8: Option[Float],
o9: Option[Double],
o10: Option[Array[Byte]],
o11: Option[Date],
//o12: Option[EncodingTestType],
o13: Option[LocalDate],
o14: Option[UUID],
o15: Option[Number]
)
inline def delete = quote {
query[EncodingTestEntity].delete
}
inline def insert = quote {
(e: EncodingTestEntity) => query[EncodingTestEntity].insert(e)
}
inline def insertValues =
Seq(
EncodingTestEntity(
"s",
BigDecimal(1.1),
true,
11.toByte,
23.toShort,
33,
431L,
34.4f,
42d,
Array(1.toByte, 2.toByte),
new Date(31200000),
//EncodingTestType("s"),
LocalDate.of(2013, 11, 23),
UUID.randomUUID(),
Some("s"),
Some(BigDecimal(1.1)),
Some(true),
Some(11.toByte),
Some(23.toShort),
Some(33),
Some(431L),
Some(34.4f),
Some(42d),
Some(Array(1.toByte, 2.toByte)),
Some(new Date(31200000)),
//Some(EncodingTestType("s")),
Some(LocalDate.of(2013, 11, 23)),
Some(UUID.randomUUID()),
Some(Number("0"))
),
EncodingTestEntity(
"",
BigDecimal(0),
false,
0.toByte,
0.toShort,
0,
0L,
0F,
0D,
Array[Byte](), // In ProtoQuill, Dotty requires this to be typed
new Date(0),
//EncodingTestType(""),
LocalDate.ofEpochDay(0),
UUID.randomUUID(),
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
//None,
None,
None
)
)
def verify(result: List[EncodingTestEntity]) = {
result.size mustEqual insertValues.size
result.zip(insertValues).foreach {
case (e1, e2) =>
e1.v1 mustEqual e2.v1
e1.v2 mustEqual e2.v2
e1.v3 mustEqual e2.v3
e1.v4 mustEqual e2.v4
e1.v5 mustEqual e2.v5
e1.v6 mustEqual e2.v6
e1.v7 mustEqual e2.v7
e1.v8 mustEqual e2.v8
e1.v9 mustEqual e2.v9
e1.v10 mustEqual e2.v10
e1.v11 mustEqual e2.v11
//e1.v12 mustEqual e2.v12
e1.v13 mustEqual e2.v13
e1.v14 mustEqual e2.v14
e1.o1 mustEqual e2.o1
e1.o2 mustEqual e2.o2
e1.o3 mustEqual e2.o3
e1.o4 mustEqual e2.o4
e1.o5 mustEqual e2.o5
e1.o6 mustEqual e2.o6
e1.o7 mustEqual e2.o7
e1.o8 mustEqual e2.o8
e1.o9 mustEqual e2.o9
// For Protoquill, array needs to be explicitly typed as [Byte] or "No ClassTag available for Nothing" error occurs
e1.o10.getOrElse(Array[Byte]()) mustEqual e2.o10.getOrElse(Array[Byte]())
e1.o11 mustEqual e2.o11
//e1.o12 mustEqual e2.o12
e1.o13 mustEqual e2.o13
e1.o14 mustEqual e2.o14
e1.o15 mustEqual e2.o15
}
}
case class BarCode(description: String, uuid: Option[UUID] = None)
// No ReturningGenerated in Protoquill yet
//val insertBarCode = quote((b: BarCode) => query[BarCode].insert(b).returningGenerated(_.uuid))
val barCodeEntry = BarCode("returning UUID")
inline def findBarCodeByUuid(uuid: UUID) = quote(query[BarCode].filter(_.uuid.forall(_ == lift(uuid))))
def verifyBarcode(barCode: BarCode) = barCode.description mustEqual "returning UUID"
}
package io.getquill.context.jasync.postgres
import java.time.{ LocalDate, LocalDateTime, ZonedDateTime }
import io.getquill.context.sql.EncodingSpec
import org.joda.time.{ DateTime => JodaDateTime, LocalDate => JodaLocalDate, LocalDateTime => JodaLocalDateTime }
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import java.util.Date
import java.util.UUID
import io.getquill._
class PostgresAsyncEncodingSpec extends EncodingSpec {
val context = testContext
import testContext._
"encodes and decodes types" in {
// val r =
// for {
// _ <- testContext.run(delete) //hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
// _ <- testContext.run(liftQuery(insertValues).foreach(e => insert(e)))
// result <- testContext.run(query[EncodingTestEntity])
// } yield result
// val q = quote { liftQuery(insertValues).foreach(e => insert(e)) }
//val result = testContext.run { liftQuery(insertValues).foreach(e => insert(e)) } //hellooooooooooooo
//val result = testContext.run()
val e =
new SimpleEncodingTestEntity(
"0": String,
BigDecimal(0): BigDecimal,
true: Boolean,
0: Byte,
0: Short,
0: Int,
0: Long,
0: Float,
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
Option(0: Byte): Option[Byte],
Option(0: Short): Option[Short],
Option(0: Int): Option[Int],
Option(0: Long): Option[Long],
Option(0: Float): Option[Float],
Option(0: Double): Option[Double],
"0": String,
BigDecimal(0): BigDecimal,
true: Boolean,
0: Byte,
0: Short,
0: Int,
0: Long,
0: Int,
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
Option(0: Byte): Option[Byte],
Option(0: Short): Option[Short],
Option(0: Int): Option[Int],
Option(0: Long): Option[Long],
Option(0: Float): Option[Float],
Option(0: Double): Option[Double],
"0": String,
BigDecimal(0): BigDecimal,
true: Boolean,
0: Byte,
0: Short,
0: Int,
0: Long,
0: Float,
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
Option(0: Byte): Option[Byte],
Option(0: Short): Option[Short],
Option(0: Int): Option[Int],
Option(0: Long): Option[Long],
Option(0: Float): Option[Float],
Option(0: Double): Option[Double],
"0": String,
BigDecimal(0): BigDecimal,
true: Boolean,
0: Byte,
0: Short,
0: Int,
0: Long,
0: Int,
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
Option(0: Byte): Option[Byte],
Option(0: Short): Option[Short],
Option(0: Int): Option[Int],
Option(0: Long): Option[Long],
Option(0: Float): Option[Float],
Option(0: Double): Option[Double],
"0": String,
BigDecimal(0): BigDecimal,
true: Boolean,
0: Byte,
0: Short,
0: Int,
0: Long,
0: Float,
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
Option(0: Byte): Option[Byte],
Option(0: Short): Option[Short],
Option(0: Int): Option[Int],
Option(0: Long): Option[Long],
Option(0: Float): Option[Float],
Option(0: Double): Option[Double],
"0": String,
BigDecimal(0): BigDecimal,
true: Boolean,
0: Byte,
0: Short,
0: Int,
0: Long,
0: Int,
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
Option(0: Byte): Option[Byte],
Option(0: Short): Option[Short],
Option(0: Int): Option[Int],
Option(0: Long): Option[Long],
Option(0: Float): Option[Float],
Option(0: Double): Option[Double],
"0": String,
BigDecimal(0): BigDecimal,
true: Boolean,
0: Byte,
0: Short,
0: Int,
0: Long,
0: Float,
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
Option(0: Byte): Option[Byte],
Option(0: Short): Option[Short],
Option(0: Int): Option[Int],
Option(0: Long): Option[Long],
Option(0: Float): Option[Float],
Option(0: Double): Option[Double],
"0": String,
BigDecimal(0): BigDecimal,
true: Boolean,
0: Byte,
0: Short,
0: Int,
0: Long,
0: Int,
0: Double,
Option("0"): Option[String],
Option(BigDecimal(0)): Option[BigDecimal],
Option(true): Option[Boolean],
Option(0: Byte): Option[Byte],
Option(0: Short): Option[Short],
Option(0: Int): Option[Int],
Option(0: Long): Option[Long],
Option(0: Float): Option[Float],
Option(0: Double): Option[Double]
)
val result = testContext.run(query[SimpleEncodingTestEntity].insert(
lift(e))
)
//val result = testContext.run(query[SimpleEncodingTestEntity]) //hello
println(result)
//println(io.getquill.util.Messages.qprint(q))
//verify(Await.result(r, Duration.Inf).toList)
}
// "encodes and decodes uuids" in {
// case class EncodingUUIDTestEntity(v1: UUID)
// val testUUID = UUID.fromString("e5240c08-6ee7-474a-b5e4-91f79c48338f")
// //delete old values
// val q0 = quote(query[EncodingUUIDTestEntity].delete)
// val rez0 = Await.result(testContext.run(q0), Duration.Inf)
// //insert new uuid
// val rez1 = Await.result(testContext.run(query[EncodingUUIDTestEntity].insert(lift(EncodingUUIDTestEntity(testUUID)))), Duration.Inf)
// //verify you can get the uuid back from the db
// val q2 = quote(query[EncodingUUIDTestEntity].map(p => p.v1))
// val rez2 = Await.result(testContext.run(q2), Duration.Inf)
// rez2 mustEqual List(testUUID)
// }
// "fails if the column has the wrong type" - {
// "numeric" in {
// Await.result(testContext.run(liftQuery(insertValues).foreach(e => insert(e))), Duration.Inf)
// case class EncodingTestEntity(v1: Int)
// val e = intercept[IllegalStateException] {
// Await.result(testContext.run(query[EncodingTestEntity]), Duration.Inf)
// }
// }
// "non-numeric" in {
// Await.result(testContext.run(liftQuery(insertValues).foreach(e => insert(e))), Duration.Inf)
// case class EncodingTestEntity(v1: Date)
// val e = intercept[IllegalStateException] {
// Await.result(testContext.run(query[EncodingTestEntity]), Duration.Inf)
// }
// }
// }
// // "encodes sets" in {
// // val q = quote {
// // (set: Query[Int]) =>
// // query[EncodingTestEntity].filter(t => set.contains(t.v6))
// // }
// // val fut =
// // for {
// // _ <- testContext.run(query[EncodingTestEntity].delete)
// // _ <- testContext.run(liftQuery(insertValues).foreach(e => query[EncodingTestEntity].insert(e)))
// // r <- testContext.run(q(liftQuery(insertValues.map(_.v6))))
// // } yield {
// // r
// // }
// // verify(Await.result(fut, Duration.Inf).toList)
// // }
// // "returning UUID" in {
// // val success = for {
// // uuid <- Await.result(testContext.run(insertBarCode(lift(barCodeEntry))), Duration.Inf)
// // barCode <- Await.result(testContext.run(findBarCodeByUuid(uuid)), Duration.Inf).headOption
// // } yield {
// // verifyBarcode(barCode)
// // }
// // success must not be empty
// // }
// "decodes joda DateTime, LocalDate and LocalDateTime types" in {
// case class DateEncodingTestEntity(v1: JodaLocalDate, v2: JodaLocalDateTime, v3: JodaDateTime)
// val entity = DateEncodingTestEntity(JodaLocalDate.now, JodaLocalDateTime.now, JodaDateTime.now)
// val r = for {
// _ <- testContext.run(query[DateEncodingTestEntity].delete)
// _ <- testContext.run(query[DateEncodingTestEntity].insert(lift(entity)))
// result <- testContext.run(query[DateEncodingTestEntity])
// } yield result
// Await.result(r, Duration.Inf) mustBe Seq(entity)
// }
// "decodes ZonedDateTime, LocalDate and LocalDateTime types" in {
// case class DateEncodingTestEntity(v1: LocalDate, v2: LocalDateTime, v3: ZonedDateTime)
// val entity = DateEncodingTestEntity(LocalDate.now, LocalDateTime.now, ZonedDateTime.now)
// val r = for {
// _ <- testContext.run(query[DateEncodingTestEntity].delete)
// _ <- testContext.run(query[DateEncodingTestEntity].insert(lift(entity)))
// result <- testContext.run(query[DateEncodingTestEntity])
// } yield result
// Await.result(r, Duration.Inf) mustBe Seq(entity)
// }
// "encodes custom type inside singleton object" in {
// object Singleton {
// def apply()(implicit c: TestContext) = {
// import c._
// for {
// _ <- c.run(query[EncodingTestEntity].delete)
// result <- c.run(liftQuery(insertValues).foreach(e => query[EncodingTestEntity].insert(e)))
// } yield result
// }
// }
// implicit val c = testContext
// Await.result(Singleton(), Duration.Inf)
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment