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.

111 lines
3.5 KiB

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