Skip to content

Commit

Permalink
atsh
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanAtDealEngine committed Sep 2, 2023
1 parent 2106396 commit 74bef46
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions quill-finagle-postgres/src/test/scala/io/getquill/TestSkunk.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.getquill

import cats.effect.{IO, Resource}
import com.twitter.finagle.{Postgres, Status}
import com.twitter.finagle.postgres.{
OK,
Param,
PostgresClient,
PostgresClientImpl,
QueryResponse,
Row
}
import com.twitter.util.Future
import com.typesafe.config.Config
import weaver.*
import skunk.*
import skunk.codec.all.*
import natchez.Trace.Implicits.noop

import scala.util.Try

object UtilsSkunk {
def getClient: Option[PostgresClient] =
for {
host <- sys.env.get("PG_HOST")
port = sys.env
.get("PG_PORT")
.flatMap { x =>
Try { x.toInt }.toOption
}
.getOrElse(5432)
user <- sys.env.get("PG_USER")
password = sys.env.get("PG_PASSWORD")
dbname <- sys.env.get("PG_DBNAME")
} yield {
new finagle_postgres.skunk.PostgresClientImpl(
Session.single(
host = host,
port = port,
user = user,
database = dbname,
password = password
)
)
}
}

object TestSkunk extends MutableTwitterFutureSuite {

override type Res = FinaglePostgresContext[SnakeCase.type]
override def sharedResource: Resource[IO, Res] =
Resource.make {
IO.fromOption(UtilsSkunk.getClient)(new Exception()).map { client =>
val ctx = new FinaglePostgresContext[SnakeCase.type](SnakeCase, client)
val _ = {
import ctx.*
ctx.run(sql"DROP TABLE IF EXISTS table_test".as[Insert[Unit]])
ctx.run(
sql"CREATE TABLE table_test (id integer, name text)"
.as[Insert[Unit]]
)
}
ctx
}
} { client =>
IO.blocking(client.close()).void
}

future("Can make insert and fetch") { ctx =>
import ctx.*
val results = ctx.transaction {
for {
_ <- ctx.run(query[TableTest].insertValue(TableTest(0, "hola")))
result <- ctx.run(query[TableTest].filter(_.id == 0))
} yield result
}
results.map(res => expect(res.head == TableTest(0, "hola")))
}
}

0 comments on commit 74bef46

Please sign in to comment.