Add tests for instance derivation

This commit is contained in:
Eugen Wissner 2024-07-11 21:21:02 +02:00
parent cf029961e8
commit aa28bdd7fe
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0

View File

@ -29,6 +29,25 @@ data TwoFieldRecord = TwoFieldRecord
$(deriveToGraphQL ''TwoFieldRecord) $(deriveToGraphQL ''TwoFieldRecord)
$(deriveFromGraphQL ''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 :: Spec
spec = do spec = do
describe "ToGraphQL" $ do describe "ToGraphQL" $ do
@ -82,7 +101,7 @@ spec = do
in actual `shouldBe` expected in actual `shouldBe` expected
describe "deriveToGraphQL" $ do 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 let expected = Type.Object $ HashMap.fromList
[ ("x", Type.Int 1) [ ("x", Type.Int 1)
, ("y", Type.Boolean True) , ("y", Type.Boolean True)
@ -94,8 +113,33 @@ spec = do
} }
in toGraphQL given `shouldBe` expected 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 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 let given = Type.Object $ HashMap.fromList
[ ("x", Type.Int 1) [ ("x", Type.Int 1)
, ("y", Type.Boolean True) , ("y", Type.Boolean True)
@ -105,3 +149,13 @@ spec = do
, y = True , y = True
} }
in fromGraphQL given `shouldBe` Just expected 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