Skip to content

Commit

Permalink
Add more test server cases
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-k committed Nov 3, 2023
1 parent f469c26 commit 24d1802
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 22 deletions.
3 changes: 2 additions & 1 deletion mig/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ main :: IO ()
main =
hspec $ do
Api.spec
Server.Hello.spec
describe "server" $ do
Server.Hello.spec
49 changes: 29 additions & 20 deletions mig/test/Test/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ spec = describe "api" $ do
checkCaptures
checkFlatApi

notFound :: (Show a, Eq a) => Maybe a -> Expectation
notFound a = a `shouldBe` Nothing

-- static routes

checkRoutes :: Spec
checkRoutes = do
it "enter route (positive cases)" $ do
getPath ["api", "v1", "hello"] helloApi `shouldBe` Just ("hello", mempty)
getPath ["api", "v1", "bye"] helloApi `shouldBe` Just ("bye", mempty)
it "enter route (negative cases)" $ do
getPath ["api", "v1"] helloApi `shouldBe` Nothing
getPath [] helloApi `shouldBe` Nothing
getPath ["api", "v1", "hello", "there"] helloApi `shouldBe` Nothing
getPath ["api", "v1"] (mempty @(Api Text)) `shouldBe` Nothing
getPath [] (mempty @(Api Text)) `shouldBe` Nothing
it "enter route (negative cases)" $
mapM_
notFound
[ getPath ["api", "v1"] helloApi
, getPath [] helloApi
, getPath ["api", "v1", "hello", "there"] helloApi
, getPath ["api", "v1"] (mempty @(Api Text))
, getPath [] (mempty @(Api Text))
]

helloApi :: Api Text
helloApi =
Expand All @@ -33,16 +39,6 @@ helloApi =
, WithPath "bye" (HandleRoute "bye")
]

-- flat api

checkFlatApi :: Spec
checkFlatApi =
it "flat api" $
flatApi helloApi
`shouldBe` [ ("api/v1/hello", "hello")
, ("api/v1/bye", "bye")
]

-- captures

checkCaptures :: Spec
Expand All @@ -53,10 +49,13 @@ checkCaptures = do
getPath ["api", "capture2", "hello", "bye"] captureApi
`shouldBe` Just ("capture2", Map.fromList [("name1", "hello"), ("name2", "bye")])

it "captures (negative cases)" $ do
getPath ["api", "capture1"] captureApi `shouldBe` Nothing
getPath ["api", "capture2", "hello"] captureApi `shouldBe` Nothing
getPath ["api", "capture2", "hello", "bye", "error"] captureApi `shouldBe` Nothing
it "captures (negative cases)" $
mapM_
(notFound . flip getPath captureApi)
[ ["api", "capture1"]
, ["api", "capture2", "hello"]
, ["api", "capture2", "hello", "bye", "error"]
]

captureApi :: Api Text
captureApi =
Expand All @@ -65,3 +64,13 @@ captureApi =
[ WithPath ("capture1" <> Path [CapturePath "name1"]) (HandleRoute "capture1")
, WithPath ("capture2" <> Path [CapturePath "name1", CapturePath "name2"]) (HandleRoute "capture2")
]

-- flat api

checkFlatApi :: Spec
checkFlatApi =
it "flat api" $
flatApi helloApi
`shouldBe` [ ("api/v1/hello", "hello")
, ("api/v1/bye", "bye")
]
12 changes: 12 additions & 0 deletions mig/test/Test/Server.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Test.Server (spec) where

import Test.Server.Hello qualified as Hello
import Test.Server.RouteArgs qualified as RouteArgs
import Test.Server.Counter qualified as Counter
import Test.Hspec

spec :: Spec
spec = describe "server" $ do
Hello.spec
RouteArgs.spec
Counter.spec
6 changes: 6 additions & 0 deletions mig/test/Test/Server/Counter.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Test.Server.Counter (spec) where

import Test.Hspec

spec :: Spec
spec = describe "counter server" $ pure ()
2 changes: 1 addition & 1 deletion mig/test/Test/Server/Hello.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ handleBye = pure $ ok "bye"
-- tests

-- we use low-level representation of server as a function: Request -> m (Maybe Response)
-- to check server properties without wpawning a full server environment
-- to check server properties without launching in a full server environment
spec :: Spec
spec = describe "hello world server" $ do
describe "plain route finder" $ specBy plainApiStrategy
Expand Down
6 changes: 6 additions & 0 deletions mig/test/Test/Server/RouteArgs.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Test.Server.RouteArgs (spec) where

import Test.Hspec

spec :: Spec
spec = describe "route args server" $ pure ()

0 comments on commit 24d1802

Please sign in to comment.