Skip to content

Commit

Permalink
Add supplementary tests to make things sure
Browse files Browse the repository at this point in the history
  • Loading branch information
XiNiHa committed May 11, 2024
1 parent 066a821 commit 7aea83b
Showing 1 changed file with 78 additions and 19 deletions.
97 changes: 78 additions & 19 deletions core/src/test/scala/caliban/schema/SemanticNonNullSchemaSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package caliban.schema

import caliban._
import caliban.introspection.adt.{ __DeprecatedArgs, __Field }
import caliban.introspection.adt.{ __DeprecatedArgs, __Field, __Type, __TypeKind }
import caliban.parsing.adt.Directive
import caliban.schema.Annotations._
import zio._
Expand All @@ -16,34 +16,93 @@ object SemanticNonNullSchemaSpec extends ZIOSpecDefault {
override def spec =
suite("SemanticNonNullSchemaSpec")(
test("effectful field as semanticNonNull") {
assert(effectfulFieldObjectSchema.toType_().fields(__DeprecatedArgs()).toList.flatten.headOption)(
isSome(
hasField[__Field, Option[List[Directive]]](
"directives",
_.directives,
isSome(contains(SchemaUtils.SemanticNonNull))
)
val field = effectfulFieldObjectSchema.toType_().fields(__DeprecatedArgs()).toList.flatten.apply(0)
assert(field)(
hasField[__Field, Option[List[Directive]]](
"directives",
_.directives,
isSome(contains(SchemaUtils.SemanticNonNull))
)
)
assert(field._type)(
hasField[__Type, __TypeKind]("kind", _.kind, equalTo(__TypeKind.SCALAR))
)
},
test("effectful field as non-nullable") {
val field = effectfulFieldObjectSchema.toType_().fields(__DeprecatedArgs()).toList.flatten.apply(1)
assert(field)(
hasField[__Field, Option[List[Directive]]](
"directives",
_.directives.map(_.filter(_ == SchemaUtils.SemanticNonNull)).filter(_.nonEmpty),
isNone
)
)
assert(field._type)(
hasField[__Type, __TypeKind]("kind", _.kind, equalTo(__TypeKind.NON_NULL))
)
},
test("nullable effectful field") {
val field = nullableEffectfulFieldObjectSchema.toType_().fields(__DeprecatedArgs()).toList.flatten.apply(0)
assert(field)(
hasField[__Field, Option[List[Directive]]](
"directives",
_.directives.map(_.filter(_ == SchemaUtils.SemanticNonNull)).filter(_.nonEmpty),
isNone
)
)
assert(field._type)(
hasField[__Type, __TypeKind]("kind", _.kind, equalTo(__TypeKind.SCALAR))
)
},
test("optional effectful field") {
assert(optionalEffectfulFieldObjectSchema.toType_().fields(__DeprecatedArgs()).toList.flatten.headOption)(
isSome(
hasField[__Field, Option[List[Directive]]](
"directives",
_.directives.map(_.filter(_ == SchemaUtils.SemanticNonNull)).filter(_.nonEmpty),
isNone
)
test("nullable effectful field as non-nullable") {
val field = nullableEffectfulFieldObjectSchema.toType_().fields(__DeprecatedArgs()).toList.flatten.apply(1)
assert(field)(
hasField[__Field, Option[List[Directive]]](
"directives",
_.directives,
isNone
)
)
assert(field._type)(
hasField[__Type, __TypeKind]("kind", _.kind, equalTo(__TypeKind.NON_NULL))
)
},
test("infallible effectful field") {
val field = infallibleEffectfulFieldSchema.toType_().fields(__DeprecatedArgs()).toList.flatten.apply(0)
assert(field)(
hasField[__Field, Option[List[Directive]]](
"directives",
_.directives,
isNone
)
)
assert(field._type)(
hasField[__Type, __TypeKind]("kind", _.kind, equalTo(__TypeKind.NON_NULL))
)
},
test("infallible effectful field as nullable") {
val field = infallibleEffectfulFieldSchema.toType_().fields(__DeprecatedArgs()).toList.flatten.apply(1)
assert(field)(
hasField[__Field, Option[List[Directive]]](
"directives",
_.directives,
isNone
)
)
assert(field._type)(
hasField[__Type, __TypeKind]("kind", _.kind, equalTo(__TypeKind.SCALAR))
)
}
)

case class EffectfulFieldObject(q: Task[Int], @GQLNonNullable qAnnotated: Task[Int])
case class OptionalEffectfulFieldObject(q: Task[Option[String]], @GQLNonNullable qAnnotated: Task[Option[String]])
case class NullableEffectfulFieldObject(q: Task[Option[String]], @GQLNonNullable qAnnotated: Task[Option[String]])
case class InfallibleFieldObject(q: UIO[Int], @GQLNullable qAnnotated: UIO[Int])

implicit val effectfulFieldObjectSchema: Schema[Any, EffectfulFieldObject] =
SemanticNonNullSchema.gen[Any, EffectfulFieldObject]
implicit val optionalEffectfulFieldObjectSchema: Schema[Any, OptionalEffectfulFieldObject] =
SemanticNonNullSchema.gen[Any, OptionalEffectfulFieldObject]
implicit val nullableEffectfulFieldObjectSchema: Schema[Any, NullableEffectfulFieldObject] =
SemanticNonNullSchema.gen[Any, NullableEffectfulFieldObject]
implicit val infallibleEffectfulFieldSchema: Schema[Any, InfallibleFieldObject] =
SemanticNonNullSchema.gen[Any, InfallibleFieldObject]
}

0 comments on commit 7aea83b

Please sign in to comment.