From aa28bdd7fe6a6b9c3355a3e5aed041f7c5773a57 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 11 Jul 2024 21:21:02 +0200 Subject: [PATCH] Add tests for instance derivation --- tests/Language/GraphQL/ClassSpec.hs | 58 ++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/tests/Language/GraphQL/ClassSpec.hs b/tests/Language/GraphQL/ClassSpec.hs index c14ca7b..7e6c4cf 100644 --- a/tests/Language/GraphQL/ClassSpec.hs +++ b/tests/Language/GraphQL/ClassSpec.hs @@ -29,6 +29,25 @@ data TwoFieldRecord = TwoFieldRecord $(deriveToGraphQL ''TwoFieldRecord) $(deriveFromGraphQL ''TwoFieldRecord) +data TwoVariantUnion + = FirstVariantUnion TwoFieldRecord + | SecondVariantUnion TwoFieldRecord + deriving (Eq, Show) + +$(deriveToGraphQL ''TwoVariantUnion) + +newtype NewTypeRecord = NewTypeRecord { newTypeField :: Int } + deriving (Eq, Show) + +$(deriveToGraphQL ''NewTypeRecord) +$(deriveFromGraphQL ''NewTypeRecord) + +data TwoFieldEnum = TWO_FIELD_ENUM_1 | TWO_FIELD_ENUM_2 + deriving (Eq, Show) + +$(deriveToGraphQL ''TwoFieldEnum) +$(deriveFromGraphQL ''TwoFieldEnum) + spec :: Spec spec = do describe "ToGraphQL" $ do @@ -82,7 +101,7 @@ spec = do in actual `shouldBe` expected describe "deriveToGraphQL" $ do - it "derives ToGraphQL for a record with multiple fields" $ do + it "derives ToGraphQL for a record with multiple fields" $ let expected = Type.Object $ HashMap.fromList [ ("x", Type.Int 1) , ("y", Type.Boolean True) @@ -94,8 +113,33 @@ spec = do } in toGraphQL given `shouldBe` expected + it "derives ToGraphQL for a union" $ + let expected = Type.Object $ HashMap.fromList + [ ("x", Type.Int 2) + , ("y", Type.Boolean False) + , ("__typename", Type.String "TwoFieldRecord") + ] + given = SecondVariantUnion $ TwoFieldRecord + { x = 2 + , y = False + } + in toGraphQL given `shouldBe` expected + + it "derives ToGraphQL for a newtype record" $ + let expected = Type.Object $ HashMap.fromList + [ ("newTypeField", Type.Int 3) + , ("__typename", Type.String "NewTypeRecord") + ] + given = NewTypeRecord 3 + in toGraphQL given `shouldBe` expected + + it "derives ToGraphQL for an enumeration" $ + let expected = Type.Enum "TWO_FIELD_ENUM_2" + given = TWO_FIELD_ENUM_2 + in toGraphQL given `shouldBe` expected + describe "deriveFromGraphQL" $ do - it "derives FromGraphQL for a record with multiple fields" $ do + it "derives FromGraphQL for a record with multiple fields" $ let given = Type.Object $ HashMap.fromList [ ("x", Type.Int 1) , ("y", Type.Boolean True) @@ -105,3 +149,13 @@ spec = do , y = True } in fromGraphQL given `shouldBe` Just expected + + it "derives FromGraphQL for a newtype record" $ + let given = Type.Object $ HashMap.singleton "newTypeField" (Type.Int 3) + expected = NewTypeRecord 3 + in fromGraphQL given `shouldBe` Just expected + + it "derives FromGraphQL for an enumeration" $ + let given = Type.Enum "TWO_FIELD_ENUM_2" + expected = TWO_FIELD_ENUM_2 + in fromGraphQL given `shouldBe` Just expected