You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. package wow.doge.http4sdemo
  2. import com.dimafeng.testcontainers.ContainerDef
  3. import com.dimafeng.testcontainers.PostgreSQLContainer
  4. import com.dimafeng.testcontainers.munit.TestContainerForAll
  5. import com.typesafe.config.ConfigFactory
  6. import monix.bio.IO
  7. import monix.bio.Task
  8. import monix.bio.UIO
  9. import monix.execution.Scheduler
  10. import org.testcontainers.utility.DockerImageName
  11. import slick.jdbc.JdbcBackend
  12. import slick.jdbc.PostgresProfile
  13. import wow.doge.http4sdemo.MonixBioSuite
  14. trait DatabaseIntegrationTestBase
  15. extends MonixBioSuite
  16. with TestContainerForAll {
  17. def databaseName = "testcontainer-scala"
  18. def username = "scala"
  19. def password = "scala"
  20. override val containerDef: ContainerDef = PostgreSQLContainer.Def(
  21. dockerImageName = DockerImageName.parse("postgres:12-alpine"),
  22. databaseName = databaseName,
  23. username = username,
  24. password = password
  25. )
  26. lazy val profile = PostgresProfile
  27. def config(url: String) = ConfigFactory.parseString(s"""|
  28. |testDatabase = {
  29. | url = "$url"
  30. | driver = org.postgresql.Driver
  31. | user = $username
  32. | password = $password
  33. |
  34. | numThreads = 2
  35. |
  36. | queueSize = 10
  37. |
  38. | maxThreads = 2
  39. |
  40. | maxConnections = 2
  41. |
  42. }""".stripMargin)
  43. def withDb[T](url: String)(f: JdbcBackend.DatabaseDef => Task[T]) = Task(
  44. // JdbcBackend.Database.forURL(
  45. // url,
  46. // // user = username,
  47. // // password = password,
  48. // // driver = "org.postgresql.Driver",
  49. // prop = Map(
  50. // "driver" -> "org.postgresql.Driver",
  51. // "user" -> username,
  52. // "password" -> password,
  53. // "numThreads" -> "16",
  54. // "maxThreads" -> "36",
  55. // "queueSize" -> "10",
  56. // "maxConnections" -> "36"
  57. // )
  58. // )
  59. JdbcBackend.Database.forConfig("testDatabase", config(url))
  60. ).bracket(f)(db => UIO(db.close()))
  61. def createSchema(containers: Containers) = {
  62. implicit val s = Scheduler.global
  63. containers match {
  64. case container: PostgreSQLContainer =>
  65. val config = JdbcDatabaseConfig(
  66. container.jdbcUrl,
  67. "org.postgresql.Driver",
  68. Some(username),
  69. Some(password),
  70. "flyway_schema_history",
  71. List("classpath:db/migration/default")
  72. )
  73. // (UIO(println("creating db")) >> dbBracket(container.jdbcUrl)(
  74. // // _.runL(Tables.schema.create)
  75. // _ => DBMigrations.migrate[Task](config)
  76. // ))
  77. DBMigrations.migrate[Task](config).runSyncUnsafe(munitTimeout)
  78. case _ => ()
  79. }
  80. }
  81. // val fixture = ResourceFixture(
  82. // Resource.make(
  83. // Task(
  84. // JdbcBackend.Database.forURL(
  85. // "jdbc:postgresql://localhost:49162/testcontainer-scala?",
  86. // user = username,
  87. // password = password,
  88. // driver = "org.postgresql.Driver"
  89. // )
  90. // )
  91. // )(db => Task(db.close()))
  92. // )
  93. def withContainersIO[A](pf: PartialFunction[Containers, Task[A]]): Task[A] = {
  94. withContainers { containers =>
  95. pf.applyOrElse(
  96. containers,
  97. (c: Containers) =>
  98. IO.terminate(new Exception(s"Unknown container: ${c.toString}"))
  99. )
  100. }
  101. }
  102. }