Expanding upon tests.
This commit is contained in:
parent
962cae1268
commit
e5614d9b37
3 changed files with 113 additions and 16 deletions
|
@ -39,7 +39,7 @@ object ConfigName:
|
||||||
* @return
|
* @return
|
||||||
* The underlying string representation of the name.
|
* The underlying string representation of the name.
|
||||||
*/
|
*/
|
||||||
def toRawString(): String = name
|
def unwrap(): String = name
|
||||||
|
|
||||||
/** Express this name as an environment variable.
|
/** Express this name as an environment variable.
|
||||||
*
|
*
|
||||||
|
|
|
@ -21,10 +21,7 @@ final class MemoryConfigSource[F[_]: Applicative](
|
||||||
override def getValue(
|
override def getValue(
|
||||||
key: ConfigKey[?]
|
key: ConfigKey[?]
|
||||||
): F[Option[String]] =
|
): F[Option[String]] =
|
||||||
Applicative[F].pure(
|
Applicative[F].pure(configs.get(key.name.unwrap()))
|
||||||
configs
|
|
||||||
.get(key.name.toRawString())
|
|
||||||
)
|
|
||||||
|
|
||||||
/** @inheritDocs
|
/** @inheritDocs
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,16 +1,23 @@
|
||||||
package gs.config.v0.audit
|
package gs.config.v0.audit
|
||||||
|
|
||||||
import cats.effect.IO
|
import cats.effect.IO
|
||||||
|
import gs.config.v0.AuditedConfiguration
|
||||||
import gs.config.v0.ConfigError
|
import gs.config.v0.ConfigError
|
||||||
import gs.config.v0.ConfigKey
|
import gs.config.v0.ConfigKey
|
||||||
import gs.config.v0.ConfigName
|
import gs.config.v0.ConfigName
|
||||||
|
import gs.config.v0.Configurable
|
||||||
import gs.config.v0.Configuration
|
import gs.config.v0.Configuration
|
||||||
import gs.config.v0.GsSuite
|
import gs.config.v0.GsSuite
|
||||||
import gs.config.v0.source.ConfigSource
|
import gs.config.v0.source.ConfigSource
|
||||||
|
import java.time.Instant
|
||||||
|
import java.time.LocalDate
|
||||||
|
|
||||||
class AuditedConfigurationTests extends GsSuite:
|
class AuditedConfigurationTests extends GsSuite:
|
||||||
import AuditedConfigurationTests.*
|
import AuditedConfigurationTests.*
|
||||||
|
|
||||||
|
given CanEqual[LocalDate, LocalDate] = CanEqual.derived
|
||||||
|
given CanEqual[Instant, Instant] = CanEqual.derived
|
||||||
|
|
||||||
iotest(
|
iotest(
|
||||||
"should not return values, but should record attempts to find, when no config exists"
|
"should not return values, but should record attempts to find, when no config exists"
|
||||||
) {
|
) {
|
||||||
|
@ -18,33 +25,126 @@ class AuditedConfigurationTests extends GsSuite:
|
||||||
config <- Configuration
|
config <- Configuration
|
||||||
.audited(ConfigSource.inMemory[IO](Map.empty))
|
.audited(ConfigSource.inMemory[IO](Map.empty))
|
||||||
.build()
|
.build()
|
||||||
string <- config.getValue(Keys.KString)
|
string <- config.getValue(Keys.KString)
|
||||||
|
int <- config.getValue(Keys.KInt)
|
||||||
|
long <- config.getValue(Keys.KLong)
|
||||||
|
bool <- config.getValue(Keys.KBool)
|
||||||
|
localDate <- config.getValue(Keys.KLocalDate)
|
||||||
|
instant <- config.getValue(Keys.KInstant)
|
||||||
|
manifest <- config.manifest.snapshot()
|
||||||
|
yield
|
||||||
|
assertEquals(string, Left(ConfigError.MissingValue(Names.KString)))
|
||||||
|
assertEquals(int, Left(ConfigError.MissingValue(Names.KInt)))
|
||||||
|
assertEquals(long, Left(ConfigError.MissingValue(Names.KLong)))
|
||||||
|
assertEquals(bool, Left(ConfigError.MissingValue(Names.KBool)))
|
||||||
|
assertEquals(localDate, Left(ConfigError.MissingValue(Names.KLocalDate)))
|
||||||
|
assertEquals(instant, Left(ConfigError.MissingValue(Names.KInstant)))
|
||||||
|
assertMissing(config, Names.KString, manifest)
|
||||||
|
assertMissing(config, Names.KInt, manifest)
|
||||||
|
assertMissing(config, Names.KLong, manifest)
|
||||||
|
assertMissing(config, Names.KBool, manifest)
|
||||||
|
assertMissing(config, Names.KLocalDate, manifest)
|
||||||
|
assertMissing(config, Names.KInstant, manifest)
|
||||||
|
}
|
||||||
|
|
||||||
|
iotest("should find and audit a string value") {
|
||||||
|
testFound(Keys.KString, "test")
|
||||||
|
}
|
||||||
|
|
||||||
|
iotest("should find and audit a boolan value") {
|
||||||
|
testFound(Keys.KBool, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
iotest("should find and audit an integer value") {
|
||||||
|
testFound(Keys.KInt, 11)
|
||||||
|
}
|
||||||
|
|
||||||
|
iotest("should find and audit a long value") {
|
||||||
|
testFound(Keys.KLong, 33L)
|
||||||
|
}
|
||||||
|
|
||||||
|
iotest("should find and audit a local date value") {
|
||||||
|
testFound(Keys.KLocalDate, LocalDate.now())
|
||||||
|
}
|
||||||
|
|
||||||
|
iotest("should find and audit an instant value") {
|
||||||
|
testFound(Keys.KInstant, Instant.now())
|
||||||
|
}
|
||||||
|
|
||||||
|
private def testFound[A: Configurable](
|
||||||
|
key: ConfigKey[A],
|
||||||
|
expectedValue: A
|
||||||
|
): IO[Any] =
|
||||||
|
for
|
||||||
|
config <- Configuration
|
||||||
|
.audited(
|
||||||
|
ConfigSource
|
||||||
|
.inMemory[IO](Map(key.name.unwrap() -> expectedValue.toString()))
|
||||||
|
)
|
||||||
|
.build()
|
||||||
|
value <- config.getValue(key)
|
||||||
manifest <- config.manifest.snapshot()
|
manifest <- config.manifest.snapshot()
|
||||||
yield
|
yield
|
||||||
assert(string == Left(ConfigError.MissingValue(Names.KString)))
|
assertEquals(value, Right(expectedValue))
|
||||||
assert(
|
assertSuccess(config, key.name, manifest, expectedValue.toString())
|
||||||
manifest.get(Names.KString) == Some(
|
|
||||||
List(
|
private def assertMissing(
|
||||||
ConfigQueryResult.Failure(
|
config: AuditedConfiguration[IO],
|
||||||
sources = List(config.sources.head.name),
|
name: ConfigName,
|
||||||
error = ConfigError.MissingValue(Names.KString)
|
manifest: Map[ConfigName, List[ConfigQueryResult]]
|
||||||
)
|
): Unit =
|
||||||
|
assertEquals(
|
||||||
|
manifest.get(name),
|
||||||
|
Some(
|
||||||
|
List(
|
||||||
|
ConfigQueryResult.Failure(
|
||||||
|
sources = List(config.sources.head.name),
|
||||||
|
error = ConfigError.MissingValue(name)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
)
|
||||||
|
|
||||||
|
private def assertSuccess(
|
||||||
|
config: AuditedConfiguration[IO],
|
||||||
|
name: ConfigName,
|
||||||
|
manifest: Map[ConfigName, List[ConfigQueryResult]],
|
||||||
|
expectedRawValue: String
|
||||||
|
): Unit =
|
||||||
|
assertEquals(
|
||||||
|
manifest.get(name),
|
||||||
|
Some(
|
||||||
|
List(
|
||||||
|
ConfigQueryResult.Success(config.sources.head.name, expectedRawValue)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
object AuditedConfigurationTests:
|
object AuditedConfigurationTests:
|
||||||
|
|
||||||
object Names:
|
object Names:
|
||||||
|
|
||||||
val KString: ConfigName = ConfigName("string")
|
val KString: ConfigName = ConfigName("string")
|
||||||
|
val KInt: ConfigName = ConfigName("int")
|
||||||
|
val KLong: ConfigName = ConfigName("long")
|
||||||
|
val KBool: ConfigName = ConfigName("bool")
|
||||||
|
val KLocalDate: ConfigName = ConfigName("localdate")
|
||||||
|
val KInstant: ConfigName = ConfigName("instant")
|
||||||
|
|
||||||
end Names
|
end Names
|
||||||
|
|
||||||
object Keys:
|
object Keys:
|
||||||
|
|
||||||
val KString: ConfigKey[String] = ConfigKey.Required[String](Names.KString)
|
val KString: ConfigKey[String] = ConfigKey.Required[String](Names.KString)
|
||||||
|
val KInt: ConfigKey[Int] = ConfigKey.Required[Int](Names.KInt)
|
||||||
|
val KLong: ConfigKey[Long] = ConfigKey.Required[Long](Names.KLong)
|
||||||
|
val KBool: ConfigKey[Boolean] = ConfigKey.Required[Boolean](Names.KBool)
|
||||||
|
|
||||||
|
val KLocalDate: ConfigKey[LocalDate] =
|
||||||
|
ConfigKey.Required[LocalDate](Names.KLocalDate)
|
||||||
|
|
||||||
|
val KInstant: ConfigKey[Instant] =
|
||||||
|
ConfigKey.Required[Instant](Names.KInstant)
|
||||||
|
|
||||||
end Keys
|
end Keys
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue