Skip to content

Commit

Permalink
Simplify / optimize QueryLogSuite
Browse files Browse the repository at this point in the history
  • Loading branch information
jatcwang committed Feb 1, 2025
1 parent 00d98e4 commit d9bcde2
Showing 1 changed file with 89 additions and 65 deletions.
154 changes: 89 additions & 65 deletions modules/core/src/test/scala/doobie/util/QueryLogSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,45 @@ class QueryLogSuite extends munit.CatsEffectSuite with QueryLogSuitePlatform {

test("simple") {
val q: query.Query0[(Int, Int)] = sql"select 1, 2".query[(Int, Int)]
val succEvents: List[IO[Success]] = List(
List(
successEventForCIO(q.to[List]),
successEventForCIO(q.toMap[Int, Int]),
successEventForCIO(q.accumulate[List]),
successEventForCIO(q.unique),
successEventForCIO(q.option),
successEventForCIO(q.nel)
)
.sequence
.map { succEvents =>
succEvents.foreach { succ =>
assertEquals(succ.sql, "select 1, 2")
assertEquals(succ.params, NonBatch(Nil))
assertEquals(succ.label, "unlabeled")
}
}

succEvents.map { succ =>
assertIO(succ.map(_.sql), "select 1, 2") *>
assertIO(succ.map(_.params), NonBatch(Nil)) *>
assertIO(succ.map(_.label), "unlabeled")
}.sequence_
}

test("With params and label") {
val q = sql"select ${1}, ${"2"}".queryWithLabel[(Int, String)]("mylabel")
val succEvents = List(
List(
successEventForCIO(q.to[List]),
successEventForCIO(q.toMap[Int, String]),
successEventForCIO(q.accumulate[List]),
successEventForCIO(q.unique),
successEventForCIO(q.option),
successEventForCIO(q.nel)
)
succEvents.map { succ =>
assertIO(succ.map(_.sql), "select ?, ?") *>
assertIO(succ.map(_.params), NonBatch(List(1, "2"))) *>
assertIO(succ.map(_.label), "mylabel") *>
assertIOBoolean(succ.map(_.exec.toNanos > 0L)) *>
assertIOBoolean(succ.map(_.processing.toNanos > 0L))
}.sequence_
.sequence
.map { succEvents =>
succEvents.foreach { succ =>
assertEquals(succ.sql, "select ?, ?")
assertEquals(succ.params, NonBatch(List(1, "2")))
assertEquals(succ.label, "mylabel")
assert(succ.exec.toNanos > 0L)
assert(succ.processing.toNanos > 0L)
}
}
}

test("execution failure (Error during PreparedStatement construction)") {
Expand All @@ -109,13 +115,17 @@ class QueryLogSuite extends munit.CatsEffectSuite with QueryLogSuitePlatform {
execFailureEventForCIO(q.unique),
execFailureEventForCIO(q.option),
execFailureEventForCIO(q.nel)
).map { ev =>
assertIO(ev.map(_.sql), "select bad_column") *>
assertIO(ev.map(_.params), Parameters.nonBatchEmpty) *>
assertIO(ev.map(_.label), "unlabeled") *>
assertIO(ev.map(_.exec.toNanos), 0L) *>
assertIOBoolean(ev.map(_.failure.getMessage.contains("not found")))
}.sequence
)
.sequence
.map { failEvents =>
failEvents.foreach { ev =>
assertEquals(ev.sql, "select bad_column")
assertEquals(ev.params, Parameters.nonBatchEmpty)
assertEquals(ev.label, "unlabeled")
assertEquals(ev.exec.toNanos, 0L)
assert(ev.failure.getMessage.contains("not found"))
}
}
}

test("execution failure") {
Expand All @@ -127,15 +137,18 @@ class QueryLogSuite extends munit.CatsEffectSuite with QueryLogSuitePlatform {
execFailureEventForCIO(q.unique("not_int")),
execFailureEventForCIO(q.option("not_int")),
execFailureEventForCIO(q.nel("not_int"))
).map { ev =>
assertIO(ev.map(_.sql), "select ? :: Int") *>
assertIO(ev.map(_.params), NonBatch(List("not_int"))) *>
assertIO(ev.map(_.label), "unlabeled") *>
assertIOBoolean(ev.map(_.exec.toNanos > 0L)) *>
assertIOBoolean(ev.map(_.failure.getMessage.contains("Data conversion error")))
}.sequence
)
.sequence
.map { failEvents =>
failEvents.foreach { ev =>
assertEquals(ev.sql, "select ? :: Int")
assertEquals(ev.params, NonBatch(List("not_int")))
assertEquals(ev.label, "unlabeled")
assert(ev.exec.toNanos > 0L)
assert(ev.failure.getMessage.contains("Data conversion error"))
}
}
}

test("processing failure") {
val q = sql"select 'not_int'".query[(Int, String)]
List(
Expand All @@ -145,65 +158,76 @@ class QueryLogSuite extends munit.CatsEffectSuite with QueryLogSuitePlatform {
processFailureEventForCIO(q.unique),
processFailureEventForCIO(q.option),
processFailureEventForCIO(q.nel)
).map { ev =>
assertIO(ev.map(_.sql), "select 'not_int'") *>
assertIO(ev.map(_.params), Parameters.nonBatchEmpty) *>
assertIO(ev.map(_.label), "unlabeled") *>
assertIOBoolean(ev.map(_.exec.toNanos > 0L)) *>
assertIOBoolean(ev.map(_.failure.getMessage.contains("Data conversion error")))
}.sequence_
)
.sequence
.map { failEvents =>
failEvents.foreach { ev =>
assertEquals(ev.sql, "select 'not_int'")
assertEquals(ev.params, Parameters.nonBatchEmpty)
assertEquals(ev.label, "unlabeled")
assert(ev.exec.toNanos > 0L)
assert(ev.failure.getMessage.contains("Data conversion error"))
}
}
}

test("stream") {
val sql = "select * from values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)"
val succ = successEventForCIO(
successEventForCIO(
Query[Unit, Int](sql).stream(()).compile.toList
)
assertIO(succ.map(_.sql), sql) *>
assertIO(succ.map(_.params), NonBatch(Nil)) *>
assertIO(succ.map(_.label), "unlabeled") *>
assertIOBoolean(succ.map(_.exec.toNanos > 0L)) *>
assertIO(succ.map(_.processing.toNanos), 0L)
).map { ev =>
assertEquals(ev.sql, sql)
assertEquals(ev.params, NonBatch(Nil))
assertEquals(ev.label, "unlabeled")
assert(ev.exec.toNanos > 0L)
assertEquals(ev.processing.toNanos, 0L)
}
}

test("streamWithChunkSize") {
val sql = "select * from values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)"
val succ = successEventForCIO(
successEventForCIO(
Query[Unit, Int](sql).streamWithChunkSize((), 5).compile.toList
)
assertIO(succ.map(_.sql), sql) *>
assertIO(succ.map(_.params), NonBatch(Nil)) *>
assertIO(succ.map(_.label), "unlabeled") *>
assertIOBoolean(succ.map(_.exec.toNanos > 0L)) *>
assertIO(succ.map(_.processing.toNanos), 0L)
).map { ev =>
assertEquals(ev.sql, sql)
assertEquals(ev.params, NonBatch(Nil))
assertEquals(ev.label, "unlabeled")
assert(ev.exec.toNanos > 0L)
assertEquals(ev.processing.toNanos, 0L)
}
}

test("stream: Log ExecFailure on failed PreparedStatement construction") {
val q0 = sql"select bad_column".query[Int]
List(
execFailureEventForCIO(q0.stream.compile.toList),
execFailureEventForCIO(q0.streamWithChunkSize(1).compile.toList)
).map { ev =>
assertIO(ev.map(_.sql), "select bad_column") *>
assertIO(ev.map(_.params), Parameters.nonBatchEmpty) *>
assertIO(ev.map(_.label), "unlabeled") *>
assertIO(ev.map(_.exec.toNanos), 0L) *>
assertIOBoolean(ev.map(_.failure.getMessage.contains("not found")))
}.sequence_
).sequence
.map { failEvents =>
failEvents.foreach { ev =>
assertEquals(ev.sql, "select bad_column")
assertEquals(ev.params, Parameters.nonBatchEmpty)
assertEquals(ev.label, "unlabeled")
assertEquals(ev.exec.toNanos, 0L)
assert(ev.failure.getMessage.contains("not found"))
}
}
}

test("stream: Log ExecFailure on failed PreparedStatement execution") {
val q0 = Query[String, (Int, String)]("select ? :: Int")
List(
execFailureEventForCIO(q0.stream("not_int").compile.toList),
execFailureEventForCIO(q0.streamWithChunkSize("not_int", 1).compile.toList)
).map { ev =>
assertIO(ev.map(_.sql), "select ? :: Int") *>
assertIO(ev.map(_.params), NonBatch(List("not_int"))) *>
assertIO(ev.map(_.label), "unlabeled") *>
assertIOBoolean(ev.map(_.exec.toNanos > 0L)) *>
assertIOBoolean(ev.map(_.failure.getMessage.contains("Data conversion error")))
}.sequence_
).sequence
.map { failEvents =>
failEvents.foreach { ev =>
assertEquals(ev.sql, "select ? :: Int")
assertEquals(ev.params, NonBatch(List("not_int")))
assertEquals(ev.label, "unlabeled")
assert(ev.exec.toNanos > 0L)
assert(ev.failure.getMessage.contains("Data conversion error"))
}
}
}

}

0 comments on commit d9bcde2

Please sign in to comment.