summaryrefslogtreecommitdiff
path: root/tests/Language/GraphQL/ClassSpec.hs
blob: 7e6c4cf56f81bdccff40300dadecdab557d821c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{- This Source Code Form is subject to the terms of the Mozilla Public License,
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
   obtain one at https://mozilla.org/MPL/2.0/. -}

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Language.GraphQL.ClassSpec
    ( spec
    ) where

import Data.Text (Text)
import Data.Time (UTCTime(..))
import Data.Time.Calendar.OrdinalDate (fromOrdinalDate)
import qualified Language.GraphQL.Type as Type
import Language.GraphQL.Class
    ( FromGraphQL(..)
    , ToGraphQL(..)
    , deriveFromGraphQL
    , deriveToGraphQL
    )
import Test.Hspec (Spec, describe, it, shouldBe)
import qualified Data.HashMap.Strict as HashMap

data TwoFieldRecord = TwoFieldRecord
    { x :: Int
    , y :: Bool
    } deriving (Eq, Show)

$(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
        it "converts integers" $
            toGraphQL (5 :: Int) `shouldBe` Type.Int 5

        it "converts text" $
            toGraphQL ("String" :: Text) `shouldBe` Type.String "String"

        it "converts booleans" $
            toGraphQL True `shouldBe` Type.Boolean True

        it "converts Nothing to Null" $
            toGraphQL (Nothing :: Maybe Int) `shouldBe` Type.Null

        it "converts singleton lists" $
            toGraphQL [True] `shouldBe` Type.List [Type.Boolean True]

        it "converts UTCTime" $
            let given = UTCTime
                    { utctDay = fromOrdinalDate 2023 5
                    , utctDayTime = 90
                    }
                actual = toGraphQL given
                expected = Type.String "2023-01-05T00:01:30Z"
             in actual `shouldBe` expected

    describe "FromGraphQL" $ do
        it "converts integers" $
            fromGraphQL (Type.Int 5) `shouldBe` Just (5 :: Int)

        it "converts text" $
            fromGraphQL (Type.String "String") `shouldBe` Just ("String" :: Text)

        it "converts booleans" $
            fromGraphQL (Type.Boolean True) `shouldBe` Just True

        it "converts Null to Nothing" $
            fromGraphQL Type.Null `shouldBe` Just (Nothing :: Maybe Int)

        it "converts singleton lists" $
            fromGraphQL (Type.List [Type.Boolean True]) `shouldBe` Just [True]

        it "converts UTCTime" $
            let given = Type.String "2023-01-05T00:01:30Z"
                expected = Just $ UTCTime
                    { utctDay = fromOrdinalDate 2023 5
                    , utctDayTime = 90
                    }
                actual = fromGraphQL given
             in actual `shouldBe` expected

    describe "deriveToGraphQL" $ do
        it "derives ToGraphQL for a record with multiple fields" $
            let expected = Type.Object $ HashMap.fromList
                    [ ("x", Type.Int 1)
                    , ("y", Type.Boolean True)
                    , ("__typename", Type.String "TwoFieldRecord")
                    ]
                given = TwoFieldRecord
                    { x = 1
                    , y = True
                    }
             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" $
            let given = Type.Object $ HashMap.fromList
                    [ ("x", Type.Int 1)
                    , ("y", Type.Boolean True)
                    ]
                expected = TwoFieldRecord
                    { x = 1
                    , 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