forked from zio-archive/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.
Merge branch 'master' into 639_hikaricp
- Loading branch information
Showing
22 changed files
with
491 additions
and
215 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
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
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> |
Oops, something went wrong.