Skip to content

Commit

Permalink
Merge pull request #57 from anton-k/write-core-tests
Browse files Browse the repository at this point in the history
Improve core tests
  • Loading branch information
anton-k authored Nov 4, 2023
2 parents 03a7c75 + 8e505e1 commit b1cc768
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build:
stack build

test:
stack test
stack test

run:
stack run
Expand Down
30 changes: 21 additions & 9 deletions mig/test/Test/Server/RouteArgs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,16 @@ handleSuccHeaderOpt (OptionalHeader n) = do
Note that function can have any number of arguments.
We encode the input type with proper type-wrapper.
-}
handleAdd :: Query "a" Int -> Query "b" Int -> Get IO (Resp Json Int)
handleAdd :: Query "a" Int -> Query "b" Int -> Get IO (Resp Json Text)
handleAdd (Query a) (Query b) = do
pure $ ok $ a + b
pure $ ok $ toAddResult a b

toAddResult :: Int -> Int -> Text
toAddResult a b =
Text.unwords ["Addition of", intToText a, "and", intToText b, "is", intToText $ a + b]

intToText :: Int -> Text
intToText = Text.pack . show

-- | Using query flag if flag is false returns 0
handleAddIf :: Query "a" Int -> Query "b" Int -> QueryFlag "perform" -> Get IO (Resp Json Int)
Expand All @@ -102,9 +109,14 @@ captured in URL. For example:
> http://localhost:8085/hello/api/mul/3/100
-}
handleMul :: Capture "a" Int -> Capture "b" Int -> Get IO (Resp Json Int)
handleMul :: Capture "a" Int -> Capture "b" Int -> Get IO (Resp Json Text)
handleMul (Capture a) (Capture b) = do
pure $ ok (a * b)
pure $ ok $ toMulResult a b

toMulResult :: Int -> Int -> Text
toMulResult a b =
Text.unwords
["Multiplication of", intToText a, "and", intToText b, "is", intToText $ a * b]

data AddInput = AddInput
{ a :: Int
Expand Down Expand Up @@ -190,7 +202,7 @@ specBy finder = do
describe "query" $ do
it "one query" $ shouldReq @Int queryReq (Just 2)
it "missing query" $ shouldReq @Int (queryReq{query = mempty}) Nothing
it "two queries" $ shouldReq @Int (twoQueryReq 2 2) (Just 4)
it "two queries" $ shouldReq @Text (twoQueryReq 2 3) (Just $ toAddResult 2 3)

queryReq :: Request
queryReq =
Expand Down Expand Up @@ -273,10 +285,10 @@ specBy finder = do
checkCapture :: Spec
checkCapture =
describe "capture" $ do
it "positive case" $ shouldReq @Int (captureReq [2, 3]) (Just 6)
it "not enough captures" $ shouldReq @Int ((captureReq [2]){capture = mempty}) Nothing
it "too many captures" $ shouldReq @Int ((captureReq [2, 3, 4]){capture = mempty}) Nothing
it "missing captures" $ shouldReq @Int ((captureReq []){capture = mempty}) Nothing
it "positive case" $ shouldReq @Text (captureReq [2, 3]) (Just (toMulResult 2 3))
it "not enough captures" $ shouldReq @Text ((captureReq [2]){capture = mempty}) Nothing
it "too many captures" $ shouldReq @Text ((captureReq [2, 3, 4]){capture = mempty}) Nothing
it "missing captures" $ shouldReq @Text ((captureReq []){capture = mempty}) Nothing

captureReq :: [Int] -> Request
captureReq args =
Expand Down

0 comments on commit b1cc768

Please sign in to comment.