From 24d18020328b8d8f40830443f5bb40e6df9836dc Mon Sep 17 00:00:00 2001 From: Anton Kholomiov Date: Fri, 3 Nov 2023 20:40:21 +0300 Subject: [PATCH] Add more test server cases --- mig/test/Spec.hs | 3 +- mig/test/Test/Api.hs | 49 ++++++++++++++++++------------- mig/test/Test/Server.hs | 12 ++++++++ mig/test/Test/Server/Counter.hs | 6 ++++ mig/test/Test/Server/Hello.hs | 2 +- mig/test/Test/Server/RouteArgs.hs | 6 ++++ 6 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 mig/test/Test/Server.hs create mode 100644 mig/test/Test/Server/Counter.hs create mode 100644 mig/test/Test/Server/RouteArgs.hs diff --git a/mig/test/Spec.hs b/mig/test/Spec.hs index da6d373..3218caf 100644 --- a/mig/test/Spec.hs +++ b/mig/test/Spec.hs @@ -7,4 +7,5 @@ main :: IO () main = hspec $ do Api.spec - Server.Hello.spec + describe "server" $ do + Server.Hello.spec diff --git a/mig/test/Test/Api.hs b/mig/test/Test/Api.hs index e0b174c..9f249ea 100644 --- a/mig/test/Test/Api.hs +++ b/mig/test/Test/Api.hs @@ -11,6 +11,9 @@ spec = describe "api" $ do checkCaptures checkFlatApi +notFound :: (Show a, Eq a) => Maybe a -> Expectation +notFound a = a `shouldBe` Nothing + -- static routes checkRoutes :: Spec @@ -18,12 +21,15 @@ 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 = @@ -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 @@ -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 = @@ -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") + ] diff --git a/mig/test/Test/Server.hs b/mig/test/Test/Server.hs new file mode 100644 index 0000000..17962fb --- /dev/null +++ b/mig/test/Test/Server.hs @@ -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 diff --git a/mig/test/Test/Server/Counter.hs b/mig/test/Test/Server/Counter.hs new file mode 100644 index 0000000..9f87e69 --- /dev/null +++ b/mig/test/Test/Server/Counter.hs @@ -0,0 +1,6 @@ +module Test.Server.Counter (spec) where + +import Test.Hspec + +spec :: Spec +spec = describe "counter server" $ pure () diff --git a/mig/test/Test/Server/Hello.hs b/mig/test/Test/Server/Hello.hs index 51d4f30..caf99f5 100644 --- a/mig/test/Test/Server/Hello.hs +++ b/mig/test/Test/Server/Hello.hs @@ -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 diff --git a/mig/test/Test/Server/RouteArgs.hs b/mig/test/Test/Server/RouteArgs.hs new file mode 100644 index 0000000..da4cf9a --- /dev/null +++ b/mig/test/Test/Server/RouteArgs.hs @@ -0,0 +1,6 @@ +module Test.Server.RouteArgs (spec) where + +import Test.Hspec + +spec :: Spec +spec = describe "route args server" $ pure ()