Expanding upon tests.

This commit is contained in:
Pat Garrity 2024-05-01 14:07:20 -05:00
parent 962cae1268
commit e5614d9b37
Signed by: pfm
GPG key ID: 5CA5D21BAB7F3A76
3 changed files with 113 additions and 16 deletions

View file

@ -39,7 +39,7 @@ object ConfigName:
* @return
* The underlying string representation of the name.
*/
def toRawString(): String = name
def unwrap(): String = name
/** Express this name as an environment variable.
*

View file

@ -21,10 +21,7 @@ final class MemoryConfigSource[F[_]: Applicative](
override def getValue(
key: ConfigKey[?]
): F[Option[String]] =
Applicative[F].pure(
configs
.get(key.name.toRawString())
)
Applicative[F].pure(configs.get(key.name.unwrap()))
/** @inheritDocs
*/

View file

@ -1,16 +1,23 @@
package gs.config.v0.audit
import cats.effect.IO
import gs.config.v0.AuditedConfiguration
import gs.config.v0.ConfigError
import gs.config.v0.ConfigKey
import gs.config.v0.ConfigName
import gs.config.v0.Configurable
import gs.config.v0.Configuration
import gs.config.v0.GsSuite
import gs.config.v0.source.ConfigSource
import java.time.Instant
import java.time.LocalDate
class AuditedConfigurationTests extends GsSuite:
import AuditedConfigurationTests.*
given CanEqual[LocalDate, LocalDate] = CanEqual.derived
given CanEqual[Instant, Instant] = CanEqual.derived
iotest(
"should not return values, but should record attempts to find, when no config exists"
) {
@ -19,32 +26,125 @@ class AuditedConfigurationTests extends GsSuite:
.audited(ConfigSource.inMemory[IO](Map.empty))
.build()
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
assert(string == Left(ConfigError.MissingValue(Names.KString)))
assert(
manifest.get(Names.KString) == Some(
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()
yield
assertEquals(value, Right(expectedValue))
assertSuccess(config, key.name, manifest, expectedValue.toString())
private def assertMissing(
config: AuditedConfiguration[IO],
name: ConfigName,
manifest: Map[ConfigName, List[ConfigQueryResult]]
): Unit =
assertEquals(
manifest.get(name),
Some(
List(
ConfigQueryResult.Failure(
sources = List(config.sources.head.name),
error = ConfigError.MissingValue(Names.KString)
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 Names:
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
object Keys:
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