forked from zio/zio-sql
-
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.
refactor: remove duplicated code of db-specific TestContainers and Ru…
…nnableSpecs (zio#706)
- Loading branch information
1 parent
55494d5
commit 8cc7f98
Showing
15 changed files
with
160 additions
and
211 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,19 +1,68 @@ | ||
package zio.sql | ||
|
||
import com.dimafeng.testcontainers.JdbcDatabaseContainer | ||
import zio.test.TestEnvironment | ||
import zio.ZLayer | ||
import zio.{ Scope, ZIO, ZLayer } | ||
import zio.test.ZIOSpecDefault | ||
import com.dimafeng.testcontainers.SingleContainer | ||
import java.util.Properties | ||
import zio.test.Spec | ||
|
||
/** | ||
* Base trait for integration-style tests running on Testcontainers. | ||
* Extending classes are expected to provide the container implementation | ||
* this test suite will work on by implementing {@link getContainer}. | ||
* | ||
* Test suite should be implemented in {@link specLayered} and | ||
* particular tests can depend on {@link SQLDriver} in the environment. | ||
*/ | ||
trait JdbcRunnableSpec extends ZIOSpecDefault with Jdbc { | ||
|
||
type JdbcEnvironment = TestEnvironment with SqlDriver | ||
|
||
val poolConfigLayer: ZLayer[Any, Throwable, ConnectionPoolConfig] | ||
def specLayered: Spec[JdbcEnvironment, Object] | ||
|
||
final lazy val jdbcLayer: ZLayer[Any, Any, SqlDriver] = | ||
protected def getContainer: SingleContainer[_] with JdbcDatabaseContainer | ||
|
||
protected val autoCommit = false | ||
|
||
override def spec: Spec[TestEnvironment, Any] = | ||
specLayered.provideCustomShared(jdbcLayer) | ||
|
||
private[this] def connProperties(user: String, password: String): Properties = { | ||
val props = new Properties | ||
props.setProperty("user", user) | ||
props.setProperty("password", password) | ||
props | ||
} | ||
|
||
private[this] val poolConfigLayer: ZLayer[Any, Throwable, ConnectionPoolConfig] = | ||
ZLayer.scoped { | ||
testContainer | ||
.map(a => | ||
ConnectionPoolConfig( | ||
url = a.jdbcUrl, | ||
properties = connProperties(a.username, a.password), | ||
autoCommit = autoCommit | ||
) | ||
) | ||
} | ||
|
||
private[this] final lazy val jdbcLayer: ZLayer[Any, Any, SqlDriver] = | ||
ZLayer.make[SqlDriver]( | ||
poolConfigLayer.orDie, | ||
ConnectionPool.live.orDie, | ||
SqlDriver.live | ||
) | ||
|
||
private[this] def testContainer: ZIO[Scope, Throwable, SingleContainer[_] with JdbcDatabaseContainer] = | ||
ZIO.acquireRelease { | ||
ZIO.attemptBlocking { | ||
val c = getContainer | ||
c.start() | ||
c | ||
} | ||
} { container => | ||
ZIO.attemptBlocking(container.stop()).orDie | ||
} | ||
} |
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,14 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
|
||
<logger name="org.testcontainers" level="INFO"/> | ||
<logger name="com.github.dockerjava" level="WARN"/> | ||
</configuration> |
30 changes: 9 additions & 21 deletions
30
mysql/src/test/scala/zio/sql/mysql/MysqlRunnableSpec.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 |
---|---|---|
@@ -1,29 +1,17 @@ | ||
package zio.sql.mysql | ||
|
||
import java.util.Properties | ||
import zio.sql.{ ConnectionPoolConfig, JdbcRunnableSpec } | ||
import zio.test._ | ||
import zio.ZLayer | ||
import com.dimafeng.testcontainers.{ JdbcDatabaseContainer, MySQLContainer, SingleContainer } | ||
import org.testcontainers.utility.DockerImageName | ||
import zio.sql.JdbcRunnableSpec | ||
|
||
trait MysqlRunnableSpec extends JdbcRunnableSpec with MysqlJdbcModule { | ||
|
||
private def connProperties(user: String, password: String): Properties = { | ||
val props = new Properties | ||
props.setProperty("user", user) | ||
props.setProperty("password", password) | ||
props | ||
} | ||
|
||
val poolConfigLayer: ZLayer[Any, Throwable, ConnectionPoolConfig] = | ||
ZLayer.scoped { | ||
TestContainer | ||
.mysql() | ||
.map(a => ConnectionPoolConfig(a.jdbcUrl, connProperties(a.username, a.password))) | ||
override protected def getContainer: SingleContainer[_] with JdbcDatabaseContainer = | ||
new MySQLContainer( | ||
mysqlImageVersion = Option("mysql").map(DockerImageName.parse) | ||
).configure { a => | ||
a.withInitScript("shop_schema.sql") | ||
() | ||
} | ||
|
||
override def spec: Spec[TestEnvironment, Any] = | ||
specLayered.provideCustomLayerShared(jdbcLayer) | ||
|
||
def specLayered: Spec[JdbcEnvironment, Object] | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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,14 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
|
||
<logger name="org.testcontainers" level="INFO"/> | ||
<logger name="com.github.dockerjava" level="WARN"/> | ||
</configuration> |
37 changes: 10 additions & 27 deletions
37
oracle/src/test/scala/zio/sql/oracle/OracleRunnableSpec.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 |
---|---|---|
@@ -1,34 +1,17 @@ | ||
package zio.sql.oracle | ||
|
||
import zio.ZLayer | ||
import zio.sql.{ ConnectionPoolConfig, JdbcRunnableSpec } | ||
import zio.test.{ Spec, TestEnvironment } | ||
|
||
import java.util.Properties | ||
import com.dimafeng.testcontainers.{ JdbcDatabaseContainer, OracleContainer, SingleContainer } | ||
import org.testcontainers.utility.DockerImageName | ||
import zio.sql.JdbcRunnableSpec | ||
|
||
trait OracleRunnableSpec extends JdbcRunnableSpec with OracleJdbcModule { | ||
|
||
private def connProperties(user: String, password: String): Properties = { | ||
val props = new Properties | ||
props.setProperty("user", user) | ||
props.setProperty("password", password) | ||
props | ||
} | ||
|
||
val poolConfigLayer = ZLayer.scoped { | ||
TestContainer | ||
.oracle() | ||
.map(container => | ||
ConnectionPoolConfig( | ||
url = container.jdbcUrl, | ||
properties = connProperties(container.username, container.password) | ||
) | ||
) | ||
} | ||
|
||
override def spec: Spec[TestEnvironment, Any] = | ||
specLayered.provideCustomShared(jdbcLayer) | ||
|
||
def specLayered: Spec[JdbcEnvironment, Object] | ||
override protected def getContainer: SingleContainer[_] with JdbcDatabaseContainer = | ||
new OracleContainer( | ||
dockerImageName = DockerImageName.parse("gvenzl/oracle-xe") | ||
).configure { container => | ||
container.withInitScript("shop_schema.sql") | ||
() | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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,14 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
|
||
<logger name="org.testcontainers" level="INFO"/> | ||
<logger name="com.github.dockerjava" level="WARN"/> | ||
</configuration> |
39 changes: 10 additions & 29 deletions
39
postgres/src/test/scala/zio/sql/postgresql/PostgresRunnableSpec.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 |
---|---|---|
@@ -1,36 +1,17 @@ | ||
package zio.sql.postgresql | ||
|
||
import zio._ | ||
import zio.test._ | ||
import java.util.Properties | ||
import zio.sql.{ ConnectionPoolConfig, JdbcRunnableSpec } | ||
import com.dimafeng.testcontainers.{ JdbcDatabaseContainer, PostgreSQLContainer, SingleContainer } | ||
import org.testcontainers.utility.DockerImageName | ||
import zio.sql.JdbcRunnableSpec | ||
|
||
trait PostgresRunnableSpec extends JdbcRunnableSpec with PostgresJdbcModule { | ||
|
||
def autoCommit: Boolean = true | ||
|
||
private def connProperties(user: String, password: String): Properties = { | ||
val props = new Properties | ||
props.setProperty("user", user) | ||
props.setProperty("password", password) | ||
props | ||
} | ||
|
||
val poolConfigLayer = ZLayer.scoped { | ||
TestContainer | ||
.postgres() | ||
.map(a => | ||
ConnectionPoolConfig( | ||
url = a.jdbcUrl, | ||
properties = connProperties(a.username, a.password), | ||
autoCommit = autoCommit | ||
) | ||
) | ||
} | ||
|
||
override def spec: Spec[TestEnvironment, Any] = | ||
specLayered.provideCustomShared(jdbcLayer) | ||
|
||
def specLayered: Spec[JdbcEnvironment, Object] | ||
override protected def getContainer: SingleContainer[_] with JdbcDatabaseContainer = | ||
new PostgreSQLContainer( | ||
dockerImageNameOverride = Option("postgres:alpine").map(DockerImageName.parse) | ||
).configure { a => | ||
a.withInitScript("db_schema.sql") | ||
() | ||
} | ||
|
||
} |
25 changes: 0 additions & 25 deletions
25
postgres/src/test/scala/zio/sql/postgresql/TestContainer.scala
This file was deleted.
Oops, something went wrong.
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
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,14 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
|
||
<logger name="org.testcontainers" level="INFO"/> | ||
<logger name="com.github.dockerjava" level="WARN"/> | ||
</configuration> |
Oops, something went wrong.