Skip to content

Commit 796c1b1

Browse files
committed
Use forked version to enable testing against multiple postgresql versions.
1 parent 922a6f5 commit 796c1b1

File tree

3 files changed

+90
-37
lines changed

3 files changed

+90
-37
lines changed

build.sbt

+59-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ val baseSettings = Seq(
1919
"org.scalatest" %% "scalatest" % "3.0.8" % "test,it",
2020
"org.scalacheck" %% "scalacheck" % "1.14.3" % "test,it",
2121
"org.scalamock" %% "scalamock" % "4.4.0" % "test,it",
22-
"io.circe" %% "circe-testing" % circeTestingVersion(scalaVersion.value) % "test,it",
23-
"com.opentable.components" % "otj-pg-embedded" % "0.13.3" % "test",
22+
"io.circe" %% "circe-testing" % circeTestingVersion(scalaVersion.value) % "test,it"
2423
)
2524
)
2625

@@ -80,13 +79,70 @@ lazy val publishSettings = Seq(
8079
lazy val allSettings = baseSettings ++ buildSettings ++ publishSettings
8180

8281
lazy val shapelessRef = LocalProject("finagle-postgres-shapeless")
82+
lazy val integrationTestRef = LocalProject("finagle-postgres-integration")
8383

8484
lazy val `finagle-postgres` = project
8585
.in(file("."))
8686
.settings(moduleName := "finagle-postgres")
8787
.settings(allSettings)
8888
.configs(IntegrationTest)
89-
.aggregate(shapelessRef)
89+
.aggregate(
90+
shapelessRef,
91+
integrationTestRef
92+
)
93+
94+
lazy val `finagle-postgres-integration` = project
95+
.settings(moduleName := "finagle-postgres-integration")
96+
.settings(allSettings)
97+
.configs(IntegrationTest)
98+
.settings(
99+
libraryDependencies ++= Seq(
100+
"io.zonky.test" % "embedded-postgres" % "1.2.6" % "test"
101+
)
102+
)
103+
.dependsOn(`finagle-postgres` % "test->test")
104+
.aggregate(
105+
test9,
106+
test10,
107+
test11
108+
)
109+
110+
lazy val test9 = integrationTests("9.6.17")
111+
lazy val test10 = integrationTests("10.11.0") // 10.12.0 fails to find libz for some reason
112+
lazy val test11 = integrationTests("11.6.0")
113+
114+
def integrationTests(v: String) = {
115+
val majorVersion = v.split('.') match {
116+
case Array(major, _, _) => major
117+
case _ => sys.error(s"unexpected version number. Expected major.minor.patch, got $v")
118+
}
119+
val id = s"finagle-postgres-test-$majorVersion"
120+
Project(id = id, base = file(id))
121+
.settings(baseSettings ++ buildSettings) // don't publish
122+
.settings(
123+
libraryDependencies ++= Seq(
124+
// TODO
125+
"io.zonky.test.postgres" % "embedded-postgres-binaries-darwin-amd64" % v % "test",
126+
)
127+
)
128+
.settings(
129+
parallelExecution in Test := false,
130+
javaOptions in Test += "-Duser.timezone=UTC" // TODO: investigate and submit a test to demonstrate that timezone handling is broken.
131+
)
132+
.settings(
133+
Test / sourceGenerators += Def.task {
134+
val file = (Test / sourceManaged).value / "com" / "twitter" / "finagle" / "postgres" / "IntegrationSpec.scala"
135+
IO.write(file,
136+
s"""package com.twitter.finagle.postgres
137+
|
138+
|class IntegrationSpec extends BaseIntegrationSpec("$v")
139+
|""".stripMargin)
140+
Seq(file)
141+
}.taskValue
142+
)
143+
.configs(IntegrationTest)
144+
.dependsOn(integrationTestRef % "compile->compile;test->test")
145+
}
90146

91147
lazy val `finagle-postgres-shapeless` = project
92148
.settings(moduleName := "finagle-postgres-shapeless")

src/test/scala/com/twitter/finagle/postgres/EmbeddedPgSqlSpec.scala renamed to finagle-postgres-integration/src/test/scala/com/twitter/finagle/postgres/EmbeddedPgSqlSpec.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.twitter.finagle.postgres
22

3-
import com.opentable.db.postgres.embedded.EmbeddedPostgres
43
import com.twitter.finagle.Postgres
54
import com.twitter.util.Try
5+
import io.zonky.test.db.postgres.embedded.EmbeddedPostgres
66
import org.scalatest.BeforeAndAfterAll
77

8-
class EmbeddedPgSqlSpec extends Spec with BeforeAndAfterAll {
8+
abstract class EmbeddedPgSqlSpec extends Spec with BeforeAndAfterAll {
99

1010
var embeddedPgSql: Option[EmbeddedPostgres] = None
1111

src/test/scala/com/twitter/finagle/postgres/integration/IntegrationSpec.scala renamed to finagle-postgres-integration/src/test/scala/com/twitter/finagle/postgres/IntegrationSpec.scala

+29-32
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
package com.twitter.finagle.postgres.integration
1+
package com.twitter.finagle.postgres
22

33
import java.sql.Timestamp
44
import java.time.Instant
55

6-
import com.twitter.finagle.postgres._
6+
import com.twitter.finagle.{Postgres, Status}
77
import com.twitter.finagle.postgres.codec.ServerError
8-
import com.twitter.finagle.Postgres
9-
import com.twitter.finagle.Status
10-
import com.twitter.util.Await
11-
import com.twitter.util.Duration
8+
import com.twitter.util.{Await, Duration}
129

13-
object IntegrationSpec {
10+
object BaseIntegrationSpec {
1411
val pgTestTable = "finagle_test"
1512
}
1613

17-
class IntegrationSpec extends EmbeddedPgSqlSpec {
14+
abstract class BaseIntegrationSpec(version: String) extends EmbeddedPgSqlSpec {
1815

1916
val queryTimeout = Duration.fromSeconds(2)
2017

@@ -29,7 +26,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
2926
}
3027

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

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

6158
val response = Await.result(insertDataQuery, queryTimeout)
6259

6360
response must equal(OK(4))
6461
}
6562

66-
"A postgres client" should {
63+
s"A postgres client against Postgresql v$version" should {
6764
"insert and select rows" in {
6865
val client = getClient
6966
cleanDb(client)
7067
insertSampleData(client)
7168

7269
val selectQuery = client.select(
73-
"SELECT * FROM %s WHERE str_field='hello' ORDER BY timestamp_field".format(IntegrationSpec.pgTestTable)
70+
"SELECT * FROM %s WHERE str_field='hello' ORDER BY timestamp_field".format(BaseIntegrationSpec.pgTestTable)
7471
)(
7572
identity)
7673

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

106-
IntegrationSpec.pgTestTable)
103+
BaseIntegrationSpec.pgTestTable)
107104
)(identity)
108105

109106
val
@@ -121,7 +118,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
121118

122119
val updateQuery = client.executeUpdate(
123120

124-
"UPDATE %s SET str_field='hello_updated' where int_field=4567".format(IntegrationSpec.pgTestTable)
121+
"UPDATE %s SET str_field='hello_updated' where int_field=4567".format(BaseIntegrationSpec.pgTestTable)
125122
)
126123

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

133130
val selectQuery = client.select(
134131

135-
"SELECT * FROM %s WHERE str_field='hello_updated'".format(IntegrationSpec.pgTestTable)
132+
"SELECT * FROM %s WHERE str_field='hello_updated'".format(BaseIntegrationSpec.pgTestTable)
136133
)(
137134

138135
identity)
@@ -152,15 +149,15 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
152149

153150
val updateQuery = client.executeUpdate(
154151
"DELETE FROM %s WHERE str_field='hello'"
155-
.format(IntegrationSpec.pgTestTable)
152+
.format(BaseIntegrationSpec.pgTestTable)
156153
)
157154

158155
val response = Await.result(updateQuery, queryTimeout)
159156

160157
response must equal(OK(3))
161158

162159
val selectQuery = client.select(
163-
"SELECT * FROM %s".format(IntegrationSpec.pgTestTable)
160+
"SELECT * FROM %s".format(BaseIntegrationSpec.pgTestTable)
164161
)(identity)
165162

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

178175
val preparedQuery = client.prepareAndQuery(
179-
"SELECT * FROM %s WHERE str_field=$1 AND bool_field=$2".format(IntegrationSpec.pgTestTable),
176+
"SELECT * FROM %s WHERE str_field=$1 AND bool_field=$2".format(BaseIntegrationSpec.pgTestTable),
180177
Param("hello"),
181178
Param(true))(identity)
182179

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

201198
val preparedQuery = client.prepareAndExecute(
202-
"UPDATE %s SET str_field = $1 where int_field = 4567".format(IntegrationSpec.pgTestTable),
199+
"UPDATE %s SET str_field = $1 where int_field = 4567".format(BaseIntegrationSpec.pgTestTable),
203200
Param("hello_updated")
204201
)
205202

206203
val numRows = Await.result(preparedQuery)
207204

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

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

220217

221218
val preparedQuery = client.prepareAndExecute(
222-
"UPDATE %s SET str_field = $1 where int_field = 4567".format(IntegrationSpec.pgTestTable),
219+
"UPDATE %s SET str_field = $1 where int_field = 4567".format(BaseIntegrationSpec.pgTestTable),
223220
Some("hello_updated_some")
224221
)
225222

226223
val numRows = Await.result(preparedQuery)
227224

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

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

240237

241238
val preparedQuery = client.prepareAndExecute(
242-
"UPDATE %s SET str_field = $1 where int_field = 4567".format(IntegrationSpec.pgTestTable),
239+
"UPDATE %s SET str_field = $1 where int_field = 4567".format(BaseIntegrationSpec.pgTestTable),
243240
None: Option[String]
244241
)
245242

246243
val numRows = Await.result(preparedQuery)
247244

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

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

260257

261258
val preparedQuery = client.prepareAndQuery(
262-
"UPDATE %s SET str_field = $1 where int_field = 4567 RETURNING *".format(IntegrationSpec.pgTestTable),
259+
"UPDATE %s SET str_field = $1 where int_field = 4567 RETURNING *".format(BaseIntegrationSpec.pgTestTable),
263260
Param("hello_updated")
264261
)(identity)
265262

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

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

282279
val preparedQuery = client.prepareAndQuery (
283-
"DELETE FROM %s where int_field = 9012 RETURNING *".format(IntegrationSpec.pgTestTable)
280+
"DELETE FROM %s where int_field = 9012 RETURNING *".format(BaseIntegrationSpec.pgTestTable)
284281
)(identity)
285282

286283
val resultRows = Await.result(preparedQuery)
@@ -295,7 +292,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
295292
cleanDb(client)
296293
insertSampleData(client)
297294
val preparedQuery = client.prepareAndQuery(
298-
"UPDATE %s SET str_field = $1 where str_field = $2 RETURNING *".format(IntegrationSpec.pgTestTable),
295+
"UPDATE %s SET str_field = $1 where str_field = $2 RETURNING *".format(BaseIntegrationSpec.pgTestTable),
299296
Param("hello_updated"),
300297
Param("xxxx")
301298
)(identity)
@@ -312,7 +309,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
312309
insertSampleData(client)
313310

314311
val preparedQuery = client.prepareAndQuery(
315-
"DELETE FROM %s WHERE str_field=$1".format(IntegrationSpec.pgTestTable),
312+
"DELETE FROM %s WHERE str_field=$1".format(BaseIntegrationSpec.pgTestTable),
316313
Param("xxxx")
317314
)(identity)
318315

@@ -347,7 +344,7 @@ class IntegrationSpec extends EmbeddedPgSqlSpec {
347344
cleanDb(client)
348345

349346
val selectQuery = client.select(
350-
"SELECT * FROM %s WHERE unknown_column='hello_updated'".format(IntegrationSpec.pgTestTable)
347+
"SELECT * FROM %s WHERE unknown_column='hello_updated'".format(BaseIntegrationSpec.pgTestTable)
351348
)(identity)
352349

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

370367
val preparedQuery = client.prepareAndQuery(
371-
"SELECT * FROM %s WHERE str_field=$1 AND bool_field=$2".format(IntegrationSpec.pgTestTable),
368+
"SELECT * FROM %s WHERE str_field=$1 AND bool_field=$2".format(BaseIntegrationSpec.pgTestTable),
372369
Param("hello")
373370
)(identity)
374371

0 commit comments

Comments
 (0)