Skip to content

Commit fe9df29

Browse files
committed
Simplified error code tests
1 parent 566ceed commit fe9df29

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

tests/TestSuite.hs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,47 +60,38 @@ testEncodeErrorWithData = fromByteString (encode err) @?= Just testError
6060
errorData = ('\x03BB', [True], ())
6161

6262
testInvalidJson :: Assertion
63-
testInvalidJson = do
64-
(rspToErrCode =<< rsp) @?= Just (-32700)
65-
rspId <$> rsp @?= Just idNull
63+
testInvalidJson = removeErrMsg <$> rsp @?= Just (errResponse idNull (-32700))
6664
where rsp = callSubtractMethods ("5" :: String)
6765

6866
testInvalidJsonRpc :: Assertion
69-
testInvalidJsonRpc = do
70-
(rspToErrCode =<< rsp) @?= Just (-32600)
71-
rspId <$> rsp @?= Just idNull
67+
testInvalidJsonRpc = removeErrMsg <$> rsp @?= Just (errResponse idNull (-32600))
7268
where rsp = callSubtractMethods $ object ["id" .= (10 :: Int)]
7369

7470
testEmptyBatchCall :: Assertion
75-
testEmptyBatchCall = do
76-
(rspToErrCode =<< rsp) @?= Just (-32600)
77-
rspId <$> rsp @?= Just idNull
71+
testEmptyBatchCall = removeErrMsg <$> rsp @?= Just (errResponse idNull (-32600))
7872
where rsp = callSubtractMethods emptyArray
7973

8074
testInvalidBatchElement :: Assertion
81-
testInvalidBatchElement = do
82-
length <$> rsp @?= Just 1
83-
(rspToErrCode . head =<< rsp) @?= Just (-32600)
84-
rspId . head <$> rsp @?= Just idNull
75+
testInvalidBatchElement = map removeErrMsg <$> rsp @?= Just [errResponse idNull (-32600)]
8576
where rsp = callSubtractMethods [True]
8677

8778
testWrongVersion :: Assertion
88-
testWrongVersion = checkResponseWithSubtract (encode requestWrongVersion) idNull (-32600)
89-
where requestWrongVersion = Object $ H.insert versionKey (String "1") hm
90-
Object hm = toJSON $ subtractRequestNamed [("a1", Number 4)] (idNumber 10)
79+
testWrongVersion = removeErrMsg <$> rsp @?= Just (errResponse idNull (-32600))
80+
where rsp = callSubtractMethods $ Object $ H.insert versionKey (String "1") hm
81+
Object hm = toJSON $ subtractRequestNamed [("x", Number 4)] defaultId
9182

9283
testMethodNotFound :: Assertion
93-
testMethodNotFound = (rspToErrCode =<< callSubtractMethods req) @?= Just (-32601)
94-
where req = TestRequest "ad" (Just [1, 2 :: Int]) (Just defaultId)
84+
testMethodNotFound = removeErrMsg <$> rsp @?= Just (errResponse defaultId (-32601))
85+
where rsp = callSubtractMethods $ TestRequest "ad" (Just defaultArgs) (Just defaultId)
9586

9687
testWrongMethodNameCapitalization :: Assertion
97-
testWrongMethodNameCapitalization = (rspToErrCode =<< callSubtractMethods req) @?= Just (-32601)
98-
where req = TestRequest "Add" (Just [Number 1, Number 2]) (Just defaultId)
88+
testWrongMethodNameCapitalization = removeErrMsg <$> rsp @?= Just (errResponse defaultId (-32601))
89+
where rsp = callSubtractMethods $ TestRequest "Add" (Just defaultArgs) (Just defaultId)
9990

10091
testMissingRequiredNamedArg :: Assertion
101-
testMissingRequiredNamedArg = checkResponseWithSubtract (encode request) i (-32602)
102-
where request = subtractRequestNamed [("A1", Number 1), ("a2", Number 20)] i
103-
i = idNumber 2
92+
testMissingRequiredNamedArg = removeErrMsg <$> rsp @?= Just (errResponse defaultId (-32602))
93+
where rsp = callSubtractMethods $ subtractRequestNamed args defaultId
94+
args = [("X", Number 1), ("y", Number 20)]
10495

10596
testMissingRequiredUnnamedArg :: Assertion
10697
testMissingRequiredUnnamedArg = checkResponseWithSubtract (encode request) i (-32602)
@@ -109,7 +100,7 @@ testMissingRequiredUnnamedArg = checkResponseWithSubtract (encode request) i (-3
109100

110101
testWrongArgType :: Assertion
111102
testWrongArgType = checkResponseWithSubtract (encode request) i (-32602)
112-
where request = subtractRequestNamed [("a1", Number 1), ("a2", Bool True)] i
103+
where request = subtractRequestNamed [("x", Number 1), ("y", Bool True)] i
113104
i = idString "ABC"
114105

115106
testDisallowExtraUnnamedArg :: Assertion
@@ -127,7 +118,7 @@ testBatch = sortBy (compare `on` fromIntId) (fromJust (fromByteString =<< runIde
127118
where expected = [TestResponse i1 (Right $ Number 2), TestResponse i2 (Right $ Number 4)]
128119
response = call (toMethods [subtractMethod]) $ encode request
129120
request = [subtractRequestNamed (toArgs 10 8) i1, subtractRequestNamed (toArgs 24 20) i2]
130-
toArgs x y = [("a1", Number x), ("a2", Number y)]
121+
toArgs x y = [("x", Number x), ("y", Number y)]
131122
i1 = idNumber 1
132123
i2 = idNumber 2
133124
fromIntId rsp = (fromNumId $ rspId rsp) :: Maybe Int
@@ -140,22 +131,22 @@ testBatchNotifications = runState response 0 @?= (Nothing, 10)
140131
testAllowMissingVersion :: Assertion
141132
testAllowMissingVersion = (fromByteString =<< runIdentity response) @?= (Just $ TestResponse i (Right $ Number 1))
142133
where requestNoVersion = Object $ H.delete versionKey hm
143-
Object hm = toJSON $ subtractRequestNamed [("a1", Number 1)] i
134+
Object hm = toJSON $ subtractRequestNamed [("x", Number 1)] i
144135
response = call (toMethods [subtractMethod]) $ encode requestNoVersion
145136
i = idNumber (-1)
146137

147138
testAllowExtraNamedArg :: Assertion
148139
testAllowExtraNamedArg = (fromByteString =<< runIdentity response) @?= (Just $ TestResponse i (Right $ Number (-10)))
149140
where response = call (toMethods [subtractMethod]) $ encode request
150141
request = subtractRequestNamed args i
151-
args = [("a1", Number 10), ("a2", Number 20), ("a3", String "extra")]
142+
args = [("x", Number 10), ("y", Number 20), ("z", String "extra")]
152143
i = idString "ID"
153144

154145
testDefaultNamedArg :: Assertion
155146
testDefaultNamedArg = (fromByteString =<< runIdentity response) @?= (Just $ TestResponse i (Right $ Number 1000))
156147
where response = call (toMethods [subtractMethod]) $ encode request
157148
request = subtractRequestNamed args i
158-
args = [("a", Number 500), ("a1", Number 1000)]
149+
args = [("x1", Number 500), ("x", Number 1000)]
159150
i = idNumber 3
160151

161152
testDefaultUnnamedArg :: Assertion
@@ -168,7 +159,7 @@ testNullId :: Assertion
168159
testNullId = (fromByteString =<< runIdentity response) @?= (Just $ TestResponse idNull (Right $ Number (-80)))
169160
where response = call (toMethods [subtractMethod]) $ encode request
170161
request = subtractRequestNamed args idNull
171-
args = [("a2", Number 70), ("a1", Number (-10))]
162+
args = [("y", Number 70), ("x", Number (-10))]
172163

173164
testNoArgs :: Assertion
174165
testNoArgs = compareGetTimeResult Nothing
@@ -221,7 +212,7 @@ getErrorCode (TestResponse _ (Left (TestRpcError code _ _))) = Just code
221212
getErrorCode _ = Nothing
222213

223214
subtractMethod :: Method Identity
224-
subtractMethod = toMethod "subtract 1" sub (Required "a1" :+: Optional "a2" 0 :+: ())
215+
subtractMethod = toMethod "subtract 1" sub (Required "x" :+: Optional "y" 0 :+: ())
225216
where sub :: Int -> Int -> RpcResult Identity Int
226217
sub x y = return (x - y)
227218

@@ -238,9 +229,16 @@ getTimeMethod = toMethod "get_time_seconds" getTime ()
238229
getTestTime :: IO Integer
239230
getTestTime = return 100
240231

241-
rspToErrCode :: TestResponse -> Maybe Int
242-
rspToErrCode (TestResponse _ (Left (TestRpcError code _ _))) = Just code
243-
rspToErrCode _ = Nothing
232+
removeErrMsg :: TestResponse -> TestResponse
233+
removeErrMsg (TestResponse i (Left (TestRpcError code _ _)))
234+
= TestResponse i (Left (TestRpcError code "" Nothing))
235+
removeErrMsg rsp = rsp
236+
237+
errResponse :: TestId -> Int -> TestResponse
238+
errResponse i code = TestResponse i (Left (TestRpcError code "" Nothing))
244239

245240
defaultId :: TestId
246241
defaultId = idNumber 3
242+
243+
defaultArgs :: [Int]
244+
defaultArgs = [1, 2]

0 commit comments

Comments
 (0)