Skip to content

Commit

Permalink
Removed Postgres specific from doobie-core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
milessabin committed Oct 6, 2024
1 parent 9bd0b28 commit d430f80
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 53 deletions.
5 changes: 2 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ lazy val commonSettings = Seq(
"org.typelevel" %%% "munit-cats-effect" % munitCatsEffectVersion % "test",
"io.circe" %%% "circe-literal" % circeVersion % "test",
"io.circe" %%% "circe-jawn" % circeVersion % "test",
"io.circe" %%% "circe-parser" % circeVersion % "test",
) ++ Seq(
compilerPlugin("org.typelevel" %% "kind-projector" % kindProjectorVersion cross CrossVersion.full),
).filterNot(_ => tlIsScala3.value),
Expand Down Expand Up @@ -234,9 +235,7 @@ lazy val doobiecore = project
libraryDependencies ++= Seq(
"org.tpolecat" %% "doobie-core" % doobieVersion,
"org.typelevel" %% "log4cats-core" % log4catsVersion,
"ch.qos.logback" % "logback-classic" % logbackVersion % "test",
// TODO: Needed temporarily for Metas in DoobieDatabaseSuite
"org.tpolecat" %% "doobie-postgres-circe" % doobieVersion % "test"
"ch.qos.logback" % "logback-classic" % logbackVersion % "test"
)
)

Expand Down
16 changes: 2 additions & 14 deletions modules/doobie-core/src/test/scala/DoobieDatabaseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@
package grackle.doobie
package test

import java.time.{Duration, LocalDate, LocalTime, OffsetDateTime}
import java.util.UUID
import java.time.Duration

// TODO: Postgres-specifics will have to move
import doobie.postgres.implicits._
import doobie.postgres.circe.jsonb.implicits._
import doobie.{Get, Meta, Put}
import io.circe.Json
import doobie.Meta
import munit.CatsEffectSuite

import grackle.sql.test._
Expand All @@ -36,7 +31,6 @@ trait DoobieDatabaseSuite extends CatsEffectSuite {
def bool: TestCodec[Boolean] = (Meta[Boolean], false)
def text: TestCodec[String] = (Meta[String], false)
def varchar: TestCodec[String] = (Meta[String], false)
def nvarchar: TestCodec[String] = (Meta[String], false)
def bpchar(len: Int): TestCodec[String] = (Meta[String], false)
def int2: TestCodec[Int] = (Meta[Int], false)
def int4: TestCodec[Int] = (Meta[Int], false)
Expand All @@ -45,14 +39,8 @@ trait DoobieDatabaseSuite extends CatsEffectSuite {
def float8: TestCodec[Double] = (Meta[Double], false)
def numeric(precision: Int, scale: Int): TestCodec[BigDecimal] = (Meta[BigDecimal], false)

def uuid: TestCodec[UUID] = (Meta[UUID], false)
def localDate: TestCodec[LocalDate] = (Meta[LocalDate], false)
def localTime: TestCodec[LocalTime] = (Meta[LocalTime], false)
def offsetDateTime: TestCodec[OffsetDateTime] = (Meta[OffsetDateTime], false)
def duration: TestCodec[Duration] = (Meta[Long].timap(Duration.ofMillis)(_.toMillis), false)

def jsonb: TestCodec[Json] = (new Meta(Get[Json], Put[Json]), false)

def nullable[T](c: TestCodec[T]): TestCodec[T] = (c._1, true)

def list[T](c: TestCodec[T]): TestCodec[List[T]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package grackle.doobie.oracle
package test

import java.sql.Timestamp
import java.time.{LocalTime, OffsetDateTime, ZoneId}
import java.time.{LocalDate, LocalTime, OffsetDateTime, ZoneId}
import java.time.format.DateTimeFormatter
import java.util.UUID
import scala.util.Try
Expand All @@ -28,6 +28,7 @@ import cats.syntax.all._
import doobie.{Meta, Transactor}
import doobie.enumerated.JdbcType
import doobie.util.Put
import doobie.util.meta.MetaConstructors.Basic
import io.circe.Json
import io.circe.parser.parse
import munit.catseffect._
Expand All @@ -40,43 +41,40 @@ import grackle.sql.test._
trait DoobieOracleDatabaseSuite extends DoobieDatabaseSuite {
abstract class DoobieOracleTestMapping[F[_]: Sync](transactor: Transactor[F], monitor: DoobieMonitor[F] = DoobieMonitor.noopMonitor[IO])
extends DoobieOracleMapping[F](transactor, monitor) with DoobieTestMapping[F] with SqlTestMapping[F] {
override val uuid: TestCodec[UUID] = (
Meta[String].tiemap(s => Try(UUID.fromString(s)).toEither.leftMap(_.getMessage))(_.toString),
false
)
def mkTestCodec[T](meta: Meta[T]): TestCodec[T] = (meta, false)

val localTimeFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("0 H:m:s.S")
override val localTime: TestCodec[LocalTime] = (
Meta[String].tiemap(s => Try(LocalTime.parse(s, localTimeFormat)).toEither.leftMap(_.getMessage))(_.format(localTimeFormat)),
false
)
val uuid: TestCodec[UUID] =
mkTestCodec(Meta[String].tiemap(s => Try(UUID.fromString(s)).toEither.leftMap(_.getMessage))(_.toString))

// Forget precise time zone for compatibility with Postgres. Nb. this is specific to this test suite.
override val offsetDateTime: TestCodec[OffsetDateTime] = (
Meta[Timestamp].timap(t => OffsetDateTime.ofInstant(t.toInstant, ZoneId.of("UTC")))(o => Timestamp.from(o.toInstant)),
false
)
val localTime: TestCodec[LocalTime] = {
val localTimeFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("0 H:m:s.S")
mkTestCodec(Meta[String].tiemap(s => Try(LocalTime.parse(s, localTimeFormat)).toEither.leftMap(_.getMessage))(_.format(localTimeFormat)))
}

val nvarcharMeta: Meta[String] = {
import JdbcType._
val oldGet = Meta[String].get
val oldPut = Meta[String].put
val newTargets = NonEmptyList.of(NChar, NVarChar, LongnVarChar)
val newPut =
Put.Basic(oldPut.typeStack, newTargets, oldPut.put, oldPut.update, oldPut.vendorTypeNames.headOption)
val localDate: TestCodec[LocalDate] =
(Basic.oneObject(JdbcType.Date, None, classOf[LocalDate]), false)

new Meta[String](oldGet, newPut)
// Forget precise time zone for compatibility with Postgres. Nb. this is specific to this test suite.
val offsetDateTime: TestCodec[OffsetDateTime] =
mkTestCodec(Meta[Timestamp].timap(t => OffsetDateTime.ofInstant(t.toInstant, ZoneId.of("UTC")))(o => Timestamp.from(o.toInstant)))

val nvarchar: TestCodec[String] = {
val nvarcharMeta: Meta[String] = {
import JdbcType._
val oldGet = Meta[String].get
val oldPut = Meta[String].put
val newTargets = NonEmptyList.of(NChar, NVarChar, LongnVarChar)
val newPut =
Put.Basic(oldPut.typeStack, newTargets, oldPut.put, oldPut.update, oldPut.vendorTypeNames.headOption)

new Meta[String](oldGet, newPut)
}

mkTestCodec(nvarcharMeta)
}

override val nvarchar: TestCodec[String] = (
nvarcharMeta,
false
)

override val jsonb: TestCodec[Json] = (
Meta[String].tiemap(s => parse(s).leftMap(_.getMessage))(_.noSpaces),
false
)
val jsonb: TestCodec[Json] =
mkTestCodec(Meta[String].tiemap(s => parse(s).leftMap(_.getMessage))(_.noSpaces))
}

case class OracleConnectionInfo(host: String, port: Int) {
Expand All @@ -86,11 +84,13 @@ trait DoobieOracleDatabaseSuite extends DoobieDatabaseSuite {
val username = "test"
val password = "test"
}

object OracleConnectionInfo {
val DefaultPort = 1521
}

val oracleConnectionInfo: OracleConnectionInfo = OracleConnectionInfo("localhost", OracleConnectionInfo.DefaultPort)
val oracleConnectionInfo: OracleConnectionInfo =
OracleConnectionInfo("localhost", OracleConnectionInfo.DefaultPort)

def transactorResource: Resource[IO, Transactor[IO]] = {
val connInfo = oracleConnectionInfo
Expand Down
17 changes: 15 additions & 2 deletions modules/doobie-pg/src/test/scala/DoobiePgDatabaseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
package grackle.doobie.postgres
package test

import java.time.{LocalDate, LocalTime, OffsetDateTime}
import java.util.UUID

import cats.effect.{IO, Resource, Sync}
import doobie.Transactor
import doobie.{Get, Meta, Put, Transactor}
import doobie.postgres.implicits._
import doobie.postgres.circe.jsonb.implicits._
import io.circe.Json
import munit.catseffect.IOFixture

import grackle.doobie.DoobieMonitor
Expand All @@ -27,7 +33,14 @@ import grackle.sqlpg.test._

trait DoobiePgDatabaseSuite extends DoobieDatabaseSuite with SqlPgDatabaseSuite {
abstract class DoobiePgTestMapping[F[_]: Sync](transactor: Transactor[F], monitor: DoobieMonitor[F] = DoobieMonitor.noopMonitor[IO])
extends DoobiePgMapping[F](transactor, monitor) with DoobieTestMapping[F] with SqlTestMapping[F]
extends DoobiePgMapping[F](transactor, monitor) with DoobieTestMapping[F] with SqlTestMapping[F] {
def uuid: TestCodec[UUID] = (Meta[UUID], false)
def localDate: TestCodec[LocalDate] = (Meta[LocalDate], false)
def localTime: TestCodec[LocalTime] = (Meta[LocalTime], false)
def offsetDateTime: TestCodec[OffsetDateTime] = (Meta[OffsetDateTime], false)
def jsonb: TestCodec[Json] = (new Meta(Get[Json], Put[Json]), false)
def nvarchar: TestCodec[String] = (Meta[String], false)
}

def transactorResource: Resource[IO, Transactor[IO]] = {
val connInfo = postgresConnectionInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ trait SqlPgDatabaseSuite extends CatsEffectSuite {
val DefaultPort = 5432
}

val postgresConnectionInfo: PostgresConnectionInfo = PostgresConnectionInfo("localhost", PostgresConnectionInfo.DefaultPort)
val postgresConnectionInfo: PostgresConnectionInfo =
PostgresConnectionInfo("localhost", PostgresConnectionInfo.DefaultPort)
}

0 comments on commit d430f80

Please sign in to comment.