62 lines
1.7 KiB
Scala
62 lines
1.7 KiB
Scala
package gs.predicate.v0.json
|
|
|
|
import cats.effect.IO
|
|
import cats.effect.std.MapRef
|
|
import gs.datagen.v0.Gen
|
|
import gs.datagen.v0.generators.Size
|
|
import gs.predicate.v0.api.Predicate
|
|
import gs.predicate.v0.json.query.JsonQuery
|
|
import io.circe.Json
|
|
import support.IOSuite
|
|
|
|
class JsonQueryExistsTests extends IOSuite:
|
|
|
|
import JsonQueryExistsTests.Data
|
|
import JsonQueryExistsTests.newProvider
|
|
|
|
iotest("should find a match for an existing key") {
|
|
val key = Data.keyGen.gen()
|
|
val value = Data.strValGen.gen()
|
|
val query = key
|
|
val blob = Json.obj(
|
|
key -> value
|
|
)
|
|
|
|
newProvider(Map(key -> blob)).flatMap { provider =>
|
|
given JsonProvider[IO] = provider
|
|
val p = JsonQueryExists[IO](key, JsonQuery.compile(query))
|
|
p.eval().map(result => assertEquals(result, Predicate.Result.matched()))
|
|
}
|
|
}
|
|
|
|
iotest("should fail to match against some non-existing key") {
|
|
val key = Data.keyGen.gen()
|
|
val value = Data.strValGen.gen()
|
|
val query = "somethingelse"
|
|
val blob = Json.obj(
|
|
key -> value
|
|
)
|
|
|
|
newProvider(Map(key -> blob)).flatMap { provider =>
|
|
given JsonProvider[IO] = provider
|
|
val p = JsonQueryExists[IO](key, JsonQuery.compile(query))
|
|
p.eval().map(result => assertEquals(result, Predicate.Result.missed()))
|
|
}
|
|
}
|
|
|
|
object JsonQueryExistsTests:
|
|
|
|
object Data:
|
|
|
|
val keyGen: Gen[String] = Gen.string.alphaNumeric(Size.between(4, 16))
|
|
|
|
val strValGen: Gen[Json] =
|
|
Gen.string.uppercaseAlpha(Size.fixed(8)).map(Json.fromString)
|
|
|
|
end Data
|
|
|
|
def newProvider(data: Map[String, Json]): IO[JsonProvider[IO]] =
|
|
for map <- MapRef.ofSingleImmutableMap[IO, String, Json](data)
|
|
yield new MemoryMapJsonProvider(map)
|
|
|
|
end JsonQueryExistsTests
|