-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from anton-k/write-core-tests
Draft: Write core tests
- Loading branch information
Showing
12 changed files
with
759 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build: | |
stack build | ||
|
||
test: | ||
stack test | ||
stack test | ||
|
||
run: | ||
stack run | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Test.Api qualified as Api | ||
import Test.Server qualified as Server | ||
|
||
import Test.Hspec | ||
|
||
main :: IO () | ||
main = | ||
hspec $ do | ||
Api.spec | ||
Server.spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module Test.Api (spec) where | ||
|
||
import Data.Map.Strict qualified as Map | ||
import Data.Text (Text) | ||
import Mig.Core.Api | ||
import Test.Hspec | ||
|
||
spec :: Spec | ||
spec = describe "api" $ do | ||
checkRoutes | ||
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)" $ | ||
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 = | ||
WithPath "api/v1" $ | ||
mconcat | ||
[ WithPath "hello" (HandleRoute "hello") | ||
, WithPath "bye" (HandleRoute "bye") | ||
] | ||
|
||
-- captures | ||
|
||
checkCaptures :: Spec | ||
checkCaptures = do | ||
it "captures (positive cases)" $ do | ||
getPath ["api", "capture1", "hello"] captureApi | ||
`shouldBe` Just ("capture1", Map.fromList [("name1", "hello")]) | ||
getPath ["api", "capture2", "hello", "bye"] captureApi | ||
`shouldBe` Just ("capture2", Map.fromList [("name1", "hello"), ("name2", "bye")]) | ||
|
||
it "captures (negative cases)" $ | ||
mapM_ | ||
(notFound . flip getPath captureApi) | ||
[ ["api", "capture1"] | ||
, ["api", "capture2", "hello"] | ||
, ["api", "capture2", "hello", "bye", "error"] | ||
] | ||
|
||
captureApi :: Api Text | ||
captureApi = | ||
WithPath "api" $ | ||
mconcat | ||
[ 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") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Test.Server (spec) where | ||
|
||
import Test.Hspec | ||
import Test.Server.Counter qualified as Counter | ||
import Test.Server.Hello qualified as Hello | ||
import Test.Server.RouteArgs qualified as RouteArgs | ||
|
||
spec :: Spec | ||
spec = describe "server" $ do | ||
Hello.spec | ||
RouteArgs.spec | ||
Counter.spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Test.Server.Common ( | ||
emptyReq, | ||
jsonResp, | ||
parseResp, | ||
) where | ||
|
||
import Data.Aeson qualified as Json | ||
import Data.Map.Strict qualified as Map | ||
import Mig.Core | ||
import Network.HTTP.Types.Method (methodGet) | ||
import Network.HTTP.Types.Status (ok200) | ||
|
||
emptyReq :: Request | ||
emptyReq = | ||
Request | ||
{ path = [] | ||
, query = mempty | ||
, capture = mempty | ||
, headers = Map.fromList [("Accept", "application/json")] | ||
, method = methodGet | ||
, readBody = pure (Right "") | ||
, isSecure = False | ||
} | ||
|
||
jsonResp :: (Json.ToJSON a) => a -> Response | ||
jsonResp a = | ||
Response | ||
{ status = ok200 | ||
, headers = [("Content-Type", "application/json")] | ||
, body = RawResp "application/json" (Json.encode a) | ||
} | ||
|
||
parseResp :: (Json.FromJSON a) => Response -> Maybe a | ||
parseResp resp = case resp.body of | ||
RawResp "application/json" bsResp -> Json.decode bsResp | ||
_ -> Nothing |
Oops, something went wrong.