Skip to content

Commit

Permalink
Use forked version to enable testing against multiple postgresql vers…
Browse files Browse the repository at this point in the history
…ions.
  • Loading branch information
plaflamme committed Apr 10, 2020
1 parent 922a6f5 commit 796c1b1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 37 deletions.
62 changes: 59 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ val baseSettings = Seq(
"org.scalatest" %% "scalatest" % "3.0.8" % "test,it",
"org.scalacheck" %% "scalacheck" % "1.14.3" % "test,it",
"org.scalamock" %% "scalamock" % "4.4.0" % "test,it",
"io.circe" %% "circe-testing" % circeTestingVersion(scalaVersion.value) % "test,it",
"com.opentable.components" % "otj-pg-embedded" % "0.13.3" % "test",
"io.circe" %% "circe-testing" % circeTestingVersion(scalaVersion.value) % "test,it"
)
)

Expand Down Expand Up @@ -80,13 +79,70 @@ lazy val publishSettings = Seq(
lazy val allSettings = baseSettings ++ buildSettings ++ publishSettings

lazy val shapelessRef = LocalProject("finagle-postgres-shapeless")
lazy val integrationTestRef = LocalProject("finagle-postgres-integration")

lazy val `finagle-postgres` = project
.in(file("."))
.settings(moduleName := "finagle-postgres")
.settings(allSettings)
.configs(IntegrationTest)
.aggregate(shapelessRef)
.aggregate(
shapelessRef,
integrationTestRef
)

lazy val `finagle-postgres-integration` = project
.settings(moduleName := "finagle-postgres-integration")
.settings(allSettings)
.configs(IntegrationTest)
.settings(
libraryDependencies ++= Seq(
"io.zonky.test" % "embedded-postgres" % "1.2.6" % "test"
)
)
.dependsOn(`finagle-postgres` % "test->test")
.aggregate(
test9,
test10,
test11
)

lazy val test9 = integrationTests("9.6.17")
lazy val test10 = integrationTests("10.11.0") // 10.12.0 fails to find libz for some reason
lazy val test11 = integrationTests("11.6.0")

def integrationTests(v: String) = {
val majorVersion = v.split('.') match {
case Array(major, _, _) => major
case _ => sys.error(s"unexpected version number. Expected major.minor.patch, got $v")
}
val id = s"finagle-postgres-test-$majorVersion"
Project(id = id, base = file(id))
.settings(baseSettings ++ buildSettings) // don't publish
.settings(
libraryDependencies ++= Seq(
// TODO
"io.zonky.test.postgres" % "embedded-postgres-binaries-darwin-amd64" % v % "test",
)
)
.settings(
parallelExecution in Test := false,
javaOptions in Test += "-Duser.timezone=UTC" // TODO: investigate and submit a test to demonstrate that timezone handling is broken.
)
.settings(
Test / sourceGenerators += Def.task {
val file = (Test / sourceManaged).value / "com" / "twitter" / "finagle" / "postgres" / "IntegrationSpec.scala"
IO.write(file,
s"""package com.twitter.finagle.postgres
|
|class IntegrationSpec extends BaseIntegrationSpec("$v")
|""".stripMargin)
Seq(file)
}.taskValue
)
.configs(IntegrationTest)
.dependsOn(integrationTestRef % "compile->compile;test->test")
}

lazy val `finagle-postgres-shapeless` = project
.settings(moduleName := "finagle-postgres-shapeless")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.twitter.finagle.postgres

import com.opentable.db.postgres.embedded.EmbeddedPostgres
import com.twitter.finagle.Postgres
import com.twitter.util.Try
import io.zonky.test.db.postgres.embedded.EmbeddedPostgres
import org.scalatest.BeforeAndAfterAll

class EmbeddedPgSqlSpec extends Spec with BeforeAndAfterAll {
abstract class EmbeddedPgSqlSpec extends Spec with BeforeAndAfterAll {

var embeddedPgSql: Option[EmbeddedPostgres] = None

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.twitter.finagle.postgres.integration
package com.twitter.finagle.postgres

import java.sql.Timestamp
import java.time.Instant

import com.twitter.finagle.postgres._
import com.twitter.finagle.{Postgres, Status}
import com.twitter.finagle.postgres.codec.ServerError
import com.twitter.finagle.Postgres
import com.twitter.finagle.Status
import com.twitter.util.Await
import com.twitter.util.Duration
import com.twitter.util.{Await, Duration}

object IntegrationSpec {
object BaseIntegrationSpec {
val pgTestTable = "finagle_test"
}

class IntegrationSpec extends EmbeddedPgSqlSpec {
abstract class BaseIntegrationSpec(version: String) extends EmbeddedPgSqlSpec {

val queryTimeout = Duration.fromSeconds(2)

Expand All @@ -29,7 +26,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
}

def cleanDb(client: PostgresClient): Unit = {
val dropQuery = client.executeUpdate("DROP TABLE IF EXISTS %s".format(IntegrationSpec.pgTestTable))
val dropQuery = client.executeUpdate("DROP TABLE IF EXISTS %s".format(BaseIntegrationSpec.pgTestTable))
val response = Await.result(dropQuery, queryTimeout)

response must equal(OK(1))
Expand All @@ -43,7 +40,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
| timestamp_field TIMESTAMP WITH TIME ZONE,
| bool_field BOOLEAN
|)
""".stripMargin.format(IntegrationSpec.pgTestTable))
""".stripMargin.format(BaseIntegrationSpec.pgTestTable))
val response2 = Await.result(createTableQuery, queryTimeout)
response2 must equal(OK(1))
}
Expand All @@ -56,21 +53,21 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
| ('hello', 5557, -4.51, '2015-01-08 12:55:12-0800', TRUE),
| ('hello', 7787, -42.51, '2013-12-24 07:01:00-0800', FALSE),
| ('goodbye', 4567, 15.8, '2015-01-09 16:55:12+0500', FALSE)
""".stripMargin.format(IntegrationSpec.pgTestTable))
""".stripMargin.format(BaseIntegrationSpec.pgTestTable))

val response = Await.result(insertDataQuery, queryTimeout)

response must equal(OK(4))
}

"A postgres client" should {
s"A postgres client against Postgresql v$version" should {
"insert and select rows" in {
val client = getClient
cleanDb(client)
insertSampleData(client)

val selectQuery = client.select(
"SELECT * FROM %s WHERE str_field='hello' ORDER BY timestamp_field".format(IntegrationSpec.pgTestTable)
"SELECT * FROM %s WHERE str_field='hello' ORDER BY timestamp_field".format(BaseIntegrationSpec.pgTestTable)
)(
identity)

Expand Down Expand Up @@ -103,7 +100,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
"SELECT * FROM %s WHERE str_field='xxxx' ORDER BY timestamp_field".
format(

IntegrationSpec.pgTestTable)
BaseIntegrationSpec.pgTestTable)
)(identity)

val
Expand All @@ -121,7 +118,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {

val updateQuery = client.executeUpdate(

"UPDATE %s SET str_field='hello_updated' where int_field=4567".format(IntegrationSpec.pgTestTable)
"UPDATE %s SET str_field='hello_updated' where int_field=4567".format(BaseIntegrationSpec.pgTestTable)
)

val response = Await.
Expand All @@ -132,7 +129,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {

val selectQuery = client.select(

"SELECT * FROM %s WHERE str_field='hello_updated'".format(IntegrationSpec.pgTestTable)
"SELECT * FROM %s WHERE str_field='hello_updated'".format(BaseIntegrationSpec.pgTestTable)
)(

identity)
Expand All @@ -152,15 +149,15 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {

val updateQuery = client.executeUpdate(
"DELETE FROM %s WHERE str_field='hello'"
.format(IntegrationSpec.pgTestTable)
.format(BaseIntegrationSpec.pgTestTable)
)

val response = Await.result(updateQuery, queryTimeout)

response must equal(OK(3))

val selectQuery = client.select(
"SELECT * FROM %s".format(IntegrationSpec.pgTestTable)
"SELECT * FROM %s".format(BaseIntegrationSpec.pgTestTable)
)(identity)

val resultRows = Await.result(selectQuery, queryTimeout)
Expand All @@ -176,7 +173,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
insertSampleData(client)

val preparedQuery = client.prepareAndQuery(
"SELECT * FROM %s WHERE str_field=$1 AND bool_field=$2".format(IntegrationSpec.pgTestTable),
"SELECT * FROM %s WHERE str_field=$1 AND bool_field=$2".format(BaseIntegrationSpec.pgTestTable),
Param("hello"),
Param(true))(identity)

Expand All @@ -199,14 +196,14 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
insertSampleData(client)

val preparedQuery = client.prepareAndExecute(
"UPDATE %s SET str_field = $1 where int_field = 4567".format(IntegrationSpec.pgTestTable),
"UPDATE %s SET str_field = $1 where int_field = 4567".format(BaseIntegrationSpec.pgTestTable),
Param("hello_updated")
)

val numRows = Await.result(preparedQuery)

val resultRows = Await.result(client.select(
"SELECT * from %s WHERE str_field = 'hello_updated' AND int_field = 4567".format(IntegrationSpec.pgTestTable)
"SELECT * from %s WHERE str_field = 'hello_updated' AND int_field = 4567".format(BaseIntegrationSpec.pgTestTable)
)(identity))

resultRows.size must equal(numRows)
Expand All @@ -219,14 +216,14 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {


val preparedQuery = client.prepareAndExecute(
"UPDATE %s SET str_field = $1 where int_field = 4567".format(IntegrationSpec.pgTestTable),
"UPDATE %s SET str_field = $1 where int_field = 4567".format(BaseIntegrationSpec.pgTestTable),
Some("hello_updated_some")
)

val numRows = Await.result(preparedQuery)

val resultRows = Await.result(client.select(
"SELECT * from %s WHERE str_field = 'hello_updated_some' AND int_field = 4567".format(IntegrationSpec.pgTestTable)
"SELECT * from %s WHERE str_field = 'hello_updated_some' AND int_field = 4567".format(BaseIntegrationSpec.pgTestTable)
)(identity))

resultRows.size must equal(numRows)
Expand All @@ -239,14 +236,14 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {


val preparedQuery = client.prepareAndExecute(
"UPDATE %s SET str_field = $1 where int_field = 4567".format(IntegrationSpec.pgTestTable),
"UPDATE %s SET str_field = $1 where int_field = 4567".format(BaseIntegrationSpec.pgTestTable),
None: Option[String]
)

val numRows = Await.result(preparedQuery)

val resultRows = Await.result(client.select(
"SELECT * from %s WHERE str_field IS NULL AND int_field = 4567".format(IntegrationSpec.pgTestTable)
"SELECT * from %s WHERE str_field IS NULL AND int_field = 4567".format(BaseIntegrationSpec.pgTestTable)
)(identity))

resultRows.size must equal(numRows)
Expand All @@ -259,7 +256,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {


val preparedQuery = client.prepareAndQuery(
"UPDATE %s SET str_field = $1 where int_field = 4567 RETURNING *".format(IntegrationSpec.pgTestTable),
"UPDATE %s SET str_field = $1 where int_field = 4567 RETURNING *".format(BaseIntegrationSpec.pgTestTable),
Param("hello_updated")
)(identity)

Expand All @@ -275,12 +272,12 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
insertSampleData(client)

Await.result(client.prepareAndExecute(
s"""INSERT INTO ${IntegrationSpec.pgTestTable}
s"""INSERT INTO ${BaseIntegrationSpec.pgTestTable}
VALUES ('delete', 9012, 15.8, '2015-01-09 16:55:12+0500', FALSE)"""
))

val preparedQuery = client.prepareAndQuery (
"DELETE FROM %s where int_field = 9012 RETURNING *".format(IntegrationSpec.pgTestTable)
"DELETE FROM %s where int_field = 9012 RETURNING *".format(BaseIntegrationSpec.pgTestTable)
)(identity)

val resultRows = Await.result(preparedQuery)
Expand All @@ -295,7 +292,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
cleanDb(client)
insertSampleData(client)
val preparedQuery = client.prepareAndQuery(
"UPDATE %s SET str_field = $1 where str_field = $2 RETURNING *".format(IntegrationSpec.pgTestTable),
"UPDATE %s SET str_field = $1 where str_field = $2 RETURNING *".format(BaseIntegrationSpec.pgTestTable),
Param("hello_updated"),
Param("xxxx")
)(identity)
Expand All @@ -312,7 +309,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
insertSampleData(client)

val preparedQuery = client.prepareAndQuery(
"DELETE FROM %s WHERE str_field=$1".format(IntegrationSpec.pgTestTable),
"DELETE FROM %s WHERE str_field=$1".format(BaseIntegrationSpec.pgTestTable),
Param("xxxx")
)(identity)

Expand Down Expand Up @@ -347,7 +344,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
cleanDb(client)

val selectQuery = client.select(
"SELECT * FROM %s WHERE unknown_column='hello_updated'".format(IntegrationSpec.pgTestTable)
"SELECT * FROM %s WHERE unknown_column='hello_updated'".format(BaseIntegrationSpec.pgTestTable)
)(identity)

a[ServerError] must be thrownBy {
Expand All @@ -368,7 +365,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
cleanDb(client)

val preparedQuery = client.prepareAndQuery(
"SELECT * FROM %s WHERE str_field=$1 AND bool_field=$2".format(IntegrationSpec.pgTestTable),
"SELECT * FROM %s WHERE str_field=$1 AND bool_field=$2".format(BaseIntegrationSpec.pgTestTable),
Param("hello")
)(identity)

Expand Down

0 comments on commit 796c1b1

Please sign in to comment.