Skip to content

Commit ef94d81

Browse files
Merge pull request #1 from busymachines/upgrade/munit
Upgrade: pureharm-testkit w/ munit
2 parents 7215b81 + b7639f4 commit ef94d81

File tree

8 files changed

+145
-137
lines changed

8 files changed

+145
-137
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
# unreleased
99

10+
### breaking changes:
11+
12+
- upgraded to `pureharm-testkit` 0.3.0` which replaces scalatest w/ munit.
13+
- you have to use `testResource.test("name")` in all your tests to be consistent w/ munit handling of resources.
14+
- `DBTestSetup.flywayConfig` now takes an `implicit logger`, should be source compatible. Updated to copy schema name from the db config by default. Since most likely it's wrong if the schema name is different between the two configs.
15+
16+
### dependency upgrades
17+
18+
- [pureharm-core-anomaly](https://github.com/busymachines/pureharm-core/releases) `0.2.0`
19+
- [pureharm-core-sprout](https://github.com/busymachines/pureharm-core/releases) `0.2.0`
20+
- [pureharm-core-identifiable](https://github.com/busymachines/pureharm-core/releases) `0.2.0`
21+
- [pureharm-effects-cats](https://github.com/busymachines/pureharm-effects-cats/releases) `0.4.0`
22+
- [pureharm-testkit](https://github.com/busymachines/pureharm-testkit/releases) `0.3.0`
23+
- [pureharm-db-core](https://github.com/busymachines/pureharm-db-core/releases) `0.4.0`
24+
- [pureharm-db-flyway](https://github.com/busymachines/pureharm-db-flyway/releases) `0.4.0`
25+
1026
# 0.1.0
1127

1228
Split out from [pureharm](https://github.com/busymachines/pureharm) as of version `0.0.7`.
1329

1430
:warning: Breaking changes :warning:
31+
1532
- renamed module maven artifact ID from `pureharm-db-core-testkit` to `pureharm-db-testkit`
1633

1734
- cross compiled to Scala 2.13 -- pending support for scala 3.0.0-RC1

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ We do not even pretend to support anything other than Postgresql.
1111
- [pureharm-core-sprout](https://github.com/busymachines/pureharm-core/releases) `0.1.0`
1212
- [pureharm-core-identifiable](https://github.com/busymachines/pureharm-core/releases) `0.1.0`
1313
- [pureharm-effects-cats](https://github.com/busymachines/pureharm-effects-cats/releases) `0.1.0`
14+
- [pureharm-testkit](https://github.com/busymachines/pureharm-testkit/releases) `0.1.0`
1415
- [pureharm-db-core](https://github.com/busymachines/pureharm-db-core/releases) `0.1.0`
1516
- [pureharm-db-flyway](https://github.com/busymachines/pureharm-db-flyway/releases) `0.1.0`
1617
- `"com.busymachines" %% s"pureharm-db-test-data" % "0.1.0"`

build.sbt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ val Scala3RC1 = "3.0.0-RC1"
3232
//see: https://github.com/xerial/sbt-sonatype#buildsbt
3333
ThisBuild / sonatypeCredentialHost := "s01.oss.sonatype.org"
3434

35-
ThisBuild / baseVersion := "0.1.0"
35+
ThisBuild / baseVersion := "0.2"
3636
ThisBuild / organization := "com.busymachines"
3737
ThisBuild / organizationName := "BusyMachines"
3838
ThisBuild / homepage := Option(url("https://github.com/busymachines/pureharm-db-testkit"))
@@ -84,14 +84,14 @@ ThisBuild / versionIntroduced := Map(
8484
ThisBuild / resolvers += Resolver.sonatypeRepo("releases")
8585
ThisBuild / resolvers += Resolver.sonatypeRepo("snapshots")
8686

87-
val pureharmCoreV = "0.1.0" //https://github.com/busymachines/pureharm-core/releases
88-
val pureharmEffectsV = "0.1.0" //https://github.com/busymachines/pureharm-effects-cats/releases
89-
val pureharmDBCoreV = "0.1.0" //https://github.com/busymachines/pureharm-db-core/releases
90-
val pureharmDBFlywayV = "0.1.0" //https://github.com/busymachines/pureharm-db-flyway/releases
87+
// format: off
88+
val pureharmCoreV = "0.2.0" //https://github.com/busymachines/pureharm-core/releases
89+
val pureharmEffectsV = "0.4.0" //https://github.com/busymachines/pureharm-effects-cats/releases
90+
val pureharmDBCoreV = "0.4.0" //https://github.com/busymachines/pureharm-db-core/releases
91+
val pureharmDBFlywayV = "0.4.0" //https://github.com/busymachines/pureharm-db-flyway/releases
92+
val pureharmTestkitV = "0.3.0" //https://github.com/busymachines/pureharm-testkit/releases
93+
// format: on
9194

92-
//for testing
93-
val pureharmTestkitV = "0.1.0" //https://github.com/busymachines/pureharm-testkit/releases
94-
val log4catsV = "1.2.0" //https://github.com/typelevel/log4cats/releases
9595
//=============================================================================
9696
//============================== Project details ==============================
9797
//=============================================================================
@@ -111,16 +111,15 @@ lazy val `db-testkit` = project
111111
.settings(
112112
name := "pureharm-db-testkit",
113113
libraryDependencies ++= Seq(
114-
"com.busymachines" %% "pureharm-core-identifiable" % pureharmCoreV withSources(),
115-
"com.busymachines" %% "pureharm-core-anomaly" % pureharmCoreV withSources(),
116-
"com.busymachines" %% "pureharm-core-sprout" % pureharmCoreV withSources(),
117-
"com.busymachines" %% "pureharm-effects-cats" % pureharmEffectsV withSources(),
118-
"com.busymachines" %% "pureharm-testkit" % pureharmTestkitV withSources(),
119-
120-
"com.busymachines" %% "pureharm-db-core" % pureharmDBCoreV withSources(),
121-
"com.busymachines" %% "pureharm-db-flyway" % pureharmDBFlywayV withSources(),
122-
123-
"org.typelevel" %% "log4cats-slf4j" % log4catsV % Test withSources(),
114+
// format: off
115+
"com.busymachines" %% "pureharm-core-identifiable" % pureharmCoreV withSources(),
116+
"com.busymachines" %% "pureharm-core-anomaly" % pureharmCoreV withSources(),
117+
"com.busymachines" %% "pureharm-core-sprout" % pureharmCoreV withSources(),
118+
"com.busymachines" %% "pureharm-effects-cats" % pureharmEffectsV withSources(),
119+
"com.busymachines" %% "pureharm-testkit" % pureharmTestkitV withSources(),
120+
"com.busymachines" %% "pureharm-db-core" % pureharmDBCoreV withSources(),
121+
"com.busymachines" %% "pureharm-db-flyway" % pureharmDBFlywayV withSources(),
122+
// format: on
124123
),
125124
).settings(
126125
javaOptions ++= Seq("-source", "1.8", "-target", "1.8")

db-testkit/src/main/scala/busymachines/pureharm/db/testkit/DBTest.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@
1717
package busymachines.pureharm.db.testkit
1818

1919
import busymachines.pureharm.effects._
20-
import busymachines.pureharm.testkit.PureharmTestWithResource
20+
import busymachines.pureharm.testkit._
21+
22+
abstract class DBTest[Trans] extends PureharmTest {
23+
type ResourceType
2124

22-
abstract class DBTest[Trans] extends PureharmTestWithResource {
2325
def setup: DBTestSetup[Trans]
2426

25-
def resource(meta: MetaData, trans: Trans): Resource[IO, ResourceType]
27+
def resource(meta: TestOptions, trans: Trans): Resource[IO, ResourceType]
2628

27-
override def resource(meta: MetaData): Resource[IO, ResourceType] =
29+
def testResource: SyncIO[FunFixture[ResourceType]] = ResourceFixture { testOptions =>
2830
for {
29-
trans <- setup.transactor(meta)
30-
fix <- resource(meta, trans)
31+
trans <- setup.transactor(testOptions)
32+
fix <- resource(testOptions, trans)
3133
} yield fix
34+
}
35+
3236
}

db-testkit/src/main/scala/busymachines/pureharm/db/testkit/DBTestSetup.scala

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,25 @@ import busymachines.pureharm.db.flyway.FlywayConfig
2222
import busymachines.pureharm.effects._
2323
import busymachines.pureharm.effects.implicits._
2424
import busymachines.pureharm.testkit._
25-
import busymachines.pureharm.testkit.util.{MDCKeys, PureharmTestRuntime}
26-
import org.scalatest.TestData
25+
import busymachines.pureharm.testkit.util._
2726

2827
/** @author Lorand Szakacs, https://github.com/lorandszakacs
2928
* @since 25 Jun 2020
3029
*/
31-
trait DBTestSetup[DBTransactor] {
30+
trait DBTestSetup[DBTransactor] extends PureharmTestRuntimeLazyConversions {
3231
final type RT = PureharmTestRuntime
3332

3433
implicit class TestSetupClassName(config: DBConnectionConfig) {
3534

3635
/** @see schemaName
3736
*/
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)))
4039

4140
/** @see schemaName
4241
*/
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)))
4544
}
4645

4746
/** Should be overridden to create a connection config appropriate for the test
@@ -50,31 +49,37 @@ trait DBTestSetup[DBTransactor] {
5049
* TestSetupClassName.withSchemaFromClassAndTest
5150
* or the explicit variants schemaName
5251
*/
53-
def dbConfig(meta: TestData)(implicit logger: TestLogger): DBConnectionConfig
52+
def dbConfig(testOptions: TestOptions)(implicit logger: TestLogger): DBConnectionConfig
5453

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)))
5756

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]
5960

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] =
6162
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")
6465
_ <-
6566
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")
6768
.to[Resource[IO, *]]
68-
_ <- _cleanDB(meta)
69-
_ <- _initDB(meta)
70-
fixture <- dbTransactorInstance(meta)
69+
_ <- _cleanDB(testOptions)
70+
_ <- _initDB(testOptions)
71+
fixture <- dbTransactorInstance(testOptions)
7172
} yield fixture
7273

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] =
7575
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, *]]
7883
_ <- (migs <= 0).ifTrueRaise[Resource[IO, *]](
7984
InconsistentStateCatastrophe(
8085
"""
@@ -91,44 +96,35 @@ trait DBTestSetup[DBTransactor] {
9196
)
9297
)
9398

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, *]]
95100
} yield ()
96101

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] =
99103
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, *]]
101105
_ <- 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, *]]
103107
} yield ()
104108

105109
/** @return
106110
* The schema name in the format of:
107-
* {getClass.SimpleName()_{testLineNumber Fallback to testName hash if line number not available}}
111+
* $${getClass.SimpleName()_$${testLineNumber}}
108112
*/
109-
def schemaName(meta: TestData): SchemaName =
110-
truncateSchemaName(SchemaName(s"${schemaNameFromClassAndLineNumber(meta)}"))
113+
def schemaName(testOptions: TestOptions): SchemaName =
114+
truncateSchemaName(SchemaName(s"${schemaNameFromClassAndLineNumber(testOptions)}"))
111115

112116
/** @return
113117
* 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}}
115119
*/
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)}"))
118122

119123
protected def truncateSchemaName(s: SchemaName): SchemaName = SchemaName(s.takeRight(63))
120124

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}")
123127

124128
protected def schemaNameFromClass: String =
125129
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}")
134130
}

0 commit comments

Comments
 (0)