blob: aa07668daa5a5a0f121fa994a49655f23dad6f15 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package xyz.driver.core.testkit
import java.nio.file.{Files, Path}
import scala.concurrent.Await
import scala.concurrent.duration._
import org.scalatest.{BeforeAndAfterEach, Suite}
import slick.basic.BasicProfile
import slick.jdbc.JdbcProfile
import slick.relational.RelationalProfile
trait FixtureDatabase[P <: BasicProfile] { self: Suite =>
val profile: P
val database: P#Backend#DatabaseDef
}
trait CreateAndDropSchemaForEach[P <: RelationalProfile] extends BeforeAndAfterEach {
self: Suite with FixtureDatabase[P] =>
import profile.api._
def schema: profile.SchemaDescription
override protected def beforeEach() = {
Await.result(database.run(schema.create), 5.seconds)
super.beforeEach()
}
override protected def afterEach() = {
try super.afterEach()
finally Await.result(database.run(schema.drop), 5.seconds)
}
}
trait TruncateSchemaAfterEach[P <: RelationalProfile] extends BeforeAndAfterEach {
self: Suite with FixtureDatabase[P] =>
import profile.api._
def schema: profile.SchemaDescription
override protected def afterEach() = {
try super.afterEach()
finally Await.result(database.run(schema.truncate), 5.seconds)
}
}
trait InsertBeforeEach[P <: JdbcProfile] extends BeforeAndAfterEach { self: Suite with FixtureDatabase[P] =>
import profile.api._
val insertsFiles: Set[Path]
def insertTestData(insertsFile: Path): DBIO[Int] = {
val rawInserts = new String(Files.readAllBytes(insertsFile), "UTF-8")
sql"#$rawInserts".asUpdate
}
override protected def beforeEach(): Unit = {
concurrent.Await.result(database.run(DBIO.sequence(insertsFiles.toList.map(insertTestData))), 30.seconds)
super.beforeEach()
}
}
|