Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlFragment::executeWithReturning: Do not discard decoding errors - Add test #187

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion integration/src/test/scala/zio/jdbc/ReturningSpec.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
package zio.jdbc

import zio.Scope
import zio.test.Assertion._
import zio.test.TestAspect.after
import zio.test._

import java.sql.ResultSet
import java.util.UUID

object ReturningSpec extends PgSpec {
final case class User(internalId: UUID, name: String, age: Int)

object User {
implicit val jdbcDecoder: JdbcDecoder[User] =
JdbcDecoder[(UUID, String, Int)].map((User.apply _).tupled)
implicit val jdbcEncoder: JdbcEncoder[User] =
JdbcEncoder[(UUID, String, Int)].contramap(User.unapply(_).get)
}

final case class InvalidDecoderUser(internalId: UUID, name: String, age: Int)
object InvalidDecoderUser {
implicit val jdbcDecoder: JdbcDecoder[InvalidDecoderUser] =
new JdbcDecoder[InvalidDecoderUser] {
override def unsafeDecode(columIndex: Int, rs: ResultSet): (Int, InvalidDecoderUser) =
throw new RuntimeException("Boom!")
}
implicit val jdbcEncoder: JdbcEncoder[InvalidDecoderUser] =
JdbcEncoder[(UUID, String, Int)].contramap(InvalidDecoderUser.unapply(_).get)
}

val genUser: Gen[Any, User] =
for {
uuid <- Gen.uuid
name <- Gen.alphaNumericString.map(_.take(40))
age <- Gen.int(10, 100)
} yield User(internalId = uuid, name = name, age = age)

val genInvalidDecoderUser: Gen[Any, InvalidDecoderUser] =
genUser.map(u => InvalidDecoderUser(internalId = u.internalId, name = u.name, age = u.age))

val spec: Spec[ZConnectionPool with TestEnvironment with Scope, Any] =
suite("Returning")(
test("Inserts returning rows") {
Expand All @@ -38,6 +54,19 @@ object ReturningSpec extends PgSpec {
assert(result.updatedKeys.map(_._1).toSet)(Assertion.hasSameElements(result.updatedKeys.map(_._1)))
}
},
test("Inserts returning rows - propagate decoding errors") {
check(Gen.chunkOf1(genInvalidDecoderUser)) { users =>
val insertResult =
transaction {
(sql"""INSERT INTO users(internalId, name, age)""".values(
users.toChunk
) ++ " RETURNING internalId, name, age")
.insertReturning[InvalidDecoderUser]
}

assertZIO(insertResult.exit)(fails(isSubtype[RuntimeException](hasMessage(equalTo("Boom!")))))
}
} @@ TestAspect.samples(1),
test("Updates returning rows") {
check(Gen.chunkOf1(genUser)) { users =>
for {
Expand Down