@@ -22,26 +22,25 @@ import busymachines.pureharm.db.flyway.FlywayConfig
22
22
import busymachines .pureharm .effects ._
23
23
import busymachines .pureharm .effects .implicits ._
24
24
import busymachines .pureharm .testkit ._
25
- import busymachines .pureharm .testkit .util .{MDCKeys , PureharmTestRuntime }
26
- import org .scalatest .TestData
25
+ import busymachines .pureharm .testkit .util ._
27
26
28
27
/** @author Lorand Szakacs, https://github.com/lorandszakacs
29
28
* @since 25 Jun 2020
30
29
*/
31
- trait DBTestSetup [DBTransactor ] {
30
+ trait DBTestSetup [DBTransactor ] extends PureharmTestRuntimeLazyConversions {
32
31
final type RT = PureharmTestRuntime
33
32
34
33
implicit class TestSetupClassName (config : DBConnectionConfig ) {
35
34
36
35
/** @see schemaName
37
36
*/
38
- def withSchemaFromClassAndTest (meta : TestData ): DBConnectionConfig =
39
- config.copy(schema = Option (schemaName(meta )))
37
+ def withSchemaFromClassAndTest (testOptions : TestOptions ): DBConnectionConfig =
38
+ config.copy(schema = Option (schemaName(testOptions )))
40
39
41
40
/** @see schemaName
42
41
*/
43
- def withSchemaFromClassAndTest (prefix : String , meta : TestData ): DBConnectionConfig =
44
- config.copy(schema = Option (schemaName(prefix, meta )))
42
+ def withSchemaFromClassAndTest (prefix : String , testOptions : TestOptions ): DBConnectionConfig =
43
+ config.copy(schema = Option (schemaName(prefix, testOptions )))
45
44
}
46
45
47
46
/** Should be overridden to create a connection config appropriate for the test
@@ -50,31 +49,37 @@ trait DBTestSetup[DBTransactor] {
50
49
* TestSetupClassName.withSchemaFromClassAndTest
51
50
* or the explicit variants schemaName
52
51
*/
53
- def dbConfig (meta : TestData )(implicit logger : TestLogger ): DBConnectionConfig
52
+ def dbConfig (testOptions : TestOptions )(implicit logger : TestLogger ): DBConnectionConfig
54
53
55
- // hack to remove unused param error
56
- def flywayConfig ( meta : TestData ) : Option [ FlywayConfig ] = Option (meta) >> Option .empty
54
+ def flywayConfig ( testOptions : TestOptions )( implicit logger : TestLogger ) : Option [ FlywayConfig ] =
55
+ dbConfig(testOptions).schema.map(dbSchema => FlywayConfig .defaultConfig.copy(schemas = List (dbSchema)))
57
56
58
- protected def dbTransactorInstance (meta : TestData )(implicit rt : RT , logger : TestLogger ): Resource [IO , DBTransactor ]
57
+ protected def dbTransactorInstance (
58
+ testOptions : TestOptions
59
+ )(implicit rt : RT , logger : TestLogger ): Resource [IO , DBTransactor ]
59
60
60
- def transactor (meta : TestData )(implicit rt : RT , logger : TestLogger ): Resource [IO , DBTransactor ] =
61
+ def transactor (testOptions : TestOptions )(implicit rt : RT , logger : TestLogger ): Resource [IO , DBTransactor ] =
61
62
for {
62
- _ <- logger.info(MDCKeys (meta ))(" SETUP — init" ).to[Resource [IO , * ]]
63
- schema = dbConfig(meta ).schema.getOrElse(" public" )
63
+ _ <- logger.info(MDCKeys .testSetup(testOptions ))(" SETUP — init" ).to[Resource [IO , * ]]
64
+ schema = dbConfig(testOptions ).schema.getOrElse(" public" )
64
65
_ <-
65
66
logger
66
- .info(MDCKeys (meta ) ++ Map (" schema" -> schema))(s " SETUP — schema name for test: $schema" )
67
+ .info(MDCKeys .testSetup(testOptions ) ++ Map (" schema" -> schema))(s " SETUP — schema name for test: $schema" )
67
68
.to[Resource [IO , * ]]
68
- _ <- _cleanDB(meta )
69
- _ <- _initDB(meta )
70
- fixture <- dbTransactorInstance(meta )
69
+ _ <- _cleanDB(testOptions )
70
+ _ <- _initDB(testOptions )
71
+ fixture <- dbTransactorInstance(testOptions )
71
72
} yield fixture
72
73
73
- @ scala.annotation.nowarn
74
- protected def _initDB (meta : TestData )(implicit rt : RT , logger : TestLogger ): Resource [IO , Unit ] =
74
+ protected def _initDB (testOptions : TestOptions )(implicit rt : RT , logger : TestLogger ): Resource [IO , Unit ] =
75
75
for {
76
- _ <- logger.info(MDCKeys (meta))(" SETUP — preparing DB" ).to[Resource [IO , * ]]
77
- migs <- flyway.Flyway .migrate[IO ](dbConfig = dbConfig(meta), flywayConfig(meta)).to[Resource [IO , * ]]
76
+ _ <- logger.info(MDCKeys .testSetup(testOptions))(" SETUP — preparing DB" ).to[Resource [IO , * ]]
77
+ att <- flyway.Flyway
78
+ .migrate[IO ](dbConfig = dbConfig(testOptions), flywayConfig(testOptions))
79
+ .timedAttempt()
80
+ .to[Resource [IO , * ]]
81
+ (duration, migsAtt) = att
82
+ migs <- migsAtt.liftTo[Resource [IO , * ]]
78
83
_ <- (migs <= 0 ).ifTrueRaise[Resource [IO , * ]](
79
84
InconsistentStateCatastrophe (
80
85
"""
@@ -91,44 +96,35 @@ trait DBTestSetup[DBTransactor] {
91
96
)
92
97
)
93
98
94
- _ <- logger.info(MDCKeys (meta ))(" SETUP — done preparing DB" ).to[Resource [IO , * ]]
99
+ _ <- logger.info(MDCKeys .testSetup(testOptions, duration ))(" SETUP — done preparing DB" ).to[Resource [IO , * ]]
95
100
} yield ()
96
101
97
- @ scala.annotation.nowarn
98
- protected def _cleanDB (meta : TestData )(implicit rt : RT , logger : TestLogger ): Resource [IO , Unit ] =
102
+ protected def _cleanDB (meta : TestOptions )(implicit rt : RT , logger : TestLogger ): Resource [IO , Unit ] =
99
103
for {
100
- _ <- logger.info(MDCKeys (meta))(" SETUP — cleaning DB for a clean slate" ).to[Resource [IO , * ]]
104
+ _ <- logger.info(MDCKeys .testSetup (meta))(" SETUP — cleaning DB for a clean slate" ).to[Resource [IO , * ]]
101
105
_ <- flyway.Flyway .clean[IO ](dbConfig(meta)).to[Resource [IO , * ]]
102
- _ <- logger.info(MDCKeys (meta))(" SETUP — done cleaning DB" ).to[Resource [IO , * ]]
106
+ _ <- logger.info(MDCKeys .testSetup (meta))(" SETUP — done cleaning DB" ).to[Resource [IO , * ]]
103
107
} yield ()
104
108
105
109
/** @return
106
110
* The schema name in the format of:
107
- * {getClass.SimpleName()_{testLineNumber Fallback to testName hash if line number not available }}
111
+ * $$ {getClass.SimpleName()_$$ {testLineNumber}}
108
112
*/
109
- def schemaName (meta : TestData ): SchemaName =
110
- truncateSchemaName(SchemaName (s " ${schemaNameFromClassAndLineNumber(meta )}" ))
113
+ def schemaName (testOptions : TestOptions ): SchemaName =
114
+ truncateSchemaName(SchemaName (s " ${schemaNameFromClassAndLineNumber(testOptions )}" ))
111
115
112
116
/** @return
113
117
* The schema name in the format of:
114
- * prefix_{getClass.SimpleName()_{testLineNumber Fallback to testName hash if line number not available }}
118
+ * prefix_{getClass.SimpleName()_{testLineNumber}}
115
119
*/
116
- def schemaName (prefix : String , meta : TestData ): SchemaName =
117
- truncateSchemaName(SchemaName (s " ${prefix}_ ${schemaNameFromClassAndLineNumber(meta )}" ))
120
+ def schemaName (prefix : String , testOptions : TestOptions ): SchemaName =
121
+ truncateSchemaName(SchemaName (s " ${prefix}_ ${schemaNameFromClassAndLineNumber(testOptions )}" ))
118
122
119
123
protected def truncateSchemaName (s : SchemaName ): SchemaName = SchemaName (s.takeRight(63 ))
120
124
121
- protected def schemaNameFromClassAndLineNumber (meta : TestData ): SchemaName =
122
- SchemaName (s " ${schemaNameFromClass}_ ${lineNumberOrTestNameHash( meta) }" )
125
+ protected def schemaNameFromClassAndLineNumber (meta : TestOptions ): SchemaName =
126
+ SchemaName (s " ${schemaNameFromClass}_ ${meta.location.line.toString }" )
123
127
124
128
protected def schemaNameFromClass : String =
125
129
getClass.getSimpleName.replace(" $" , " " ).toLowerCase
126
-
127
- /** When creating a schema we discriminate using the line number when defined,
128
- * otherwise using the hash of the test name, we print it out to the console,
129
- * so no worries, you can still identify the test easily.
130
- * @return
131
- */
132
- protected def lineNumberOrTestNameHash (meta : TestData ): String =
133
- meta.pos.map(_.lineNumber.toString).getOrElse(s " ${meta.name.hashCode.toString}" )
134
130
}
0 commit comments