From f2130cb87ca84e3a6b22953ac4be79d4db165630 Mon Sep 17 00:00:00 2001 From: Ivar Ness Date: Thu, 5 Sep 2024 11:56:08 +0200 Subject: [PATCH] Added test for validation of graphql schema --- .../test-compile/build.sbt | 3 +- .../src/test/resources/postservice.graphql | 54 +++++++++++++++++++ .../src/test/scala/ValidateGraphQlSpec.scala | 24 +++++++++ .../compiletime-codegen/test-compile/test | 2 + 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/resources/postservice.graphql create mode 100644 codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/scala/ValidateGraphQlSpec.scala diff --git a/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/build.sbt b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/build.sbt index d9b19bc620..cbfa21bba6 100644 --- a/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/build.sbt +++ b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/build.sbt @@ -38,6 +38,7 @@ lazy val sttp = Seq( "com.softwaremill.sttp.client3" %% "zio" % "3.9.8" ) +lazy val zioTest = Seq("dev.zio" %% "zio-test" % "2.1.9" % Test) // ### App Modules ### /** @@ -103,7 +104,7 @@ lazy val posts = ) ) ) - .settings(libraryDependencies ++= calibanLib) + .settings(libraryDependencies ++= calibanLib ++ zioTest) lazy val potatoes = project diff --git a/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/resources/postservice.graphql b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/resources/postservice.graphql new file mode 100644 index 0000000000..71122441c4 --- /dev/null +++ b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/resources/postservice.graphql @@ -0,0 +1,54 @@ +schema { + query: Query + mutation: Mutation + subscription: Subscription +} +scalar Unit + +input AuthorNameInput { + name: String! +} + +input PostContentInput { + content: String! +} + +input PostTitleInput { + title: String! +} + +type AuthorName { + name: String! +} + +type Mutation { + createPost(authorName: AuthorNameInput!, title: PostTitleInput!, content: PostContentInput!): Post + deletePost(id: ID!): Unit +} + +type Post { + id: PostId! + author: AuthorName! + title: PostTitle! + content: PostContent! +} + +type PostContent { + content: String! +} + +type PostId { + id: ID! +} + +type PostTitle { + title: String! +} + +type Query { + postById(id: ID!): Post +} + +type Subscription { + allPostsByAuthor(name: String!): Post +} \ No newline at end of file diff --git a/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/scala/ValidateGraphQlSpec.scala b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/scala/ValidateGraphQlSpec.scala new file mode 100644 index 0000000000..4d688e3337 --- /dev/null +++ b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/scala/ValidateGraphQlSpec.scala @@ -0,0 +1,24 @@ +import poc.caliban.posts.GraphQLApi + +import scala.io.Source +import java.io.File +import java.nio.file.{Files, Paths} +import zio.test.Assertion._ +import zio.test._ + +class ValidateGraphQlSpec extends ZIOSpecDefault { + + override def spec = + suite("Validate Postservice")( + test("Render postservice as earlier") { + val filename = "postservice.graphql" + val expectedGraphQL: String = Source.fromResource(filename).getLines().mkString("\n") + val gqlApi = GraphQLApi.api + val renderContent: String = s"${gqlApi.render}" + + //Files.writeString(Paths.get(File(s"/tmp/$filename").toURI), renderContent) + + assertTrue(expectedGraphQL == renderContent) + } + ) +} \ No newline at end of file diff --git a/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/test b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/test index e063c05659..1ad20505c5 100644 --- a/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/test +++ b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/test @@ -201,3 +201,5 @@ $ exists modules/potatoes-clients/src/main/scala/poc/caliban/client/generated/po -$ newer modules/posts/target/ctCalibanServer/touch modules/posts/target/ctCalibanServer/touch_old # TODO: This should be newer, sadly I don't find how to make it work. -$ newer modules/posts-clients/target/scala-2.12/src_managed/main/poc/caliban/client/generated/posts/CalibanClient.scala modules/posts-clients/tmp/CalibanClient.scala.old + +> reload; test \ No newline at end of file