This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
forked from finagle/finagle-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2106396
commit 040ab1e
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
quill-finagle-postgres/src/test/scala/io/getquill/TestSkunk.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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"))) | ||
} | ||
} |