From 6784705eab7df3e705e073bce4b4cf45923ca252 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 +- .../test/resources/postservice_scala3.graphql | 54 +++++++++++++++++++ .../src/test/scala/ValidateGraphQLTest.scala | 22 ++++++++ .../compiletime-codegen/test-compile/test | 2 + 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/resources/postservice_scala3.graphql create mode 100644 codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/scala/ValidateGraphQLTest.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 b886ad57f8..05150c6325 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 munit = Seq("org.scalameta" %% "munit" % "1.0.0" % Test) // ### App Modules ### /** @@ -103,7 +104,7 @@ lazy val posts = ) ) ) - .settings(libraryDependencies ++= calibanLib) + .settings(libraryDependencies ++= calibanLib ++ munit) lazy val potatoes = project diff --git a/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/resources/postservice_scala3.graphql b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/resources/postservice_scala3.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_scala3.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/ValidateGraphQLTest.scala b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/scala/ValidateGraphQLTest.scala new file mode 100644 index 0000000000..2e1b03d22c --- /dev/null +++ b/codegen-sbt/src/sbt-test/compiletime-codegen/test-compile/modules/posts/src/test/scala/ValidateGraphQLTest.scala @@ -0,0 +1,22 @@ +import poc.caliban.posts.GraphQLApi + +import scala.io.Source +import munit.FunSuite +import java.io.File +import java.nio.file.{Files, Paths} + +class ValidateGraphQLTest extends FunSuite { + + if (scala.util.Properties.versionNumberString.startsWith("3.")) { + test("validate postservice.graphql for scala 3") { + val filename = "postservice_scala3.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) + + assertEquals(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