Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erewok committed Feb 16, 2017
1 parent 9c4d4ef commit 88a5186
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Servant/PY/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ module Servant.PY.Internal
, toPyDict
, toPyParams
, captures
, withFormattedCaptures
, buildDocString
, buildHeaderDict
, functionArguments
, formatBuilder
-- re-exports
, (:<|>)(..)
, (:>)
Expand Down
40 changes: 31 additions & 9 deletions test/Servant/PY/InternalSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

module Servant.PY.InternalSpec where

import Control.Lens
import Data.Aeson
import qualified Data.ByteString.Char8 as B
import Data.Monoid ()
import Data.Monoid
import Data.Proxy
import Data.Text (Text)
import qualified Data.Text as T
Expand Down Expand Up @@ -48,16 +49,20 @@ type TestApi = "counter-req-header" :> Post '[JSON] SomeJson
:<|> "login-params-authors-with-reqBody"
:> QueryParams "authors" T.Text
:> ReqBody '[JSON] SomeJson :> Post '[JSON] SomeJson
:<|> "login-with-path-var-and-header"
:> Capture "id" Int
:> Capture "Name" T.Text
:> Capture "hungrig" Bool
:> ReqBody '[JSON] SomeJson
:> Post '[JSON] (Headers '[Header "test-head" B.ByteString] SomeJson)

testApi :: Proxy TestApi
testApi = Proxy


type CaptureApi = "login-with-path-var-and-header"
:> Capture "id" Int
:> Capture "Name" T.Text
:> Capture "hungrig" Bool
:> ReqBody '[JSON] SomeJson
:> Post '[JSON] (Headers '[Header "test-head" B.ByteString] SomeJson)
captureApi :: Proxy CaptureApi
captureApi = Proxy

customOptions :: CommonGeneratorOptions
customOptions = defCommonGeneratorOptions
{ urlPrefix = "urlForRequesting:9000"
Expand Down Expand Up @@ -94,10 +99,27 @@ internalSpec = describe "Internal" $ do
let dict = toPyDict " " ["forty", "one", "people"]
dict `shouldBe` "{\"forty\": forty,\n \"one\": one,\n \"people\": people}"

let reqList = listFromAPI (Proxy :: Proxy NoTypes) (Proxy :: Proxy NoContent) (Proxy :: Proxy TestApi)
let captureList = listFromAPI (Proxy :: Proxy NoTypes) (Proxy :: Proxy NoContent) captureApi
it "should correctly find captures" $ do
let captured = captures . last $ reqList
let captured = captures . head $ captureList
captured `shouldBe` ["id", "Name", "hungrig"]

let reqList = listFromAPI (Proxy :: Proxy NoTypes) (Proxy :: Proxy NoContent) testApi
it "should not incorrectly find captures" $ do
let captured = captures . head $ reqList
captured `shouldBe` []

let req = head captureList
let pathParts = req ^.. reqUrl.path.traverse
it "should correctly find captures as a list" $
capturesToFormatArgs pathParts `shouldBe` ["id", "Name", "hungrig"]

it "should correctly format captures" $
withFormattedCaptures " " pathParts `shouldBe` ".format(\n id=parse.quote(str(id)),\n"
<> " Name=parse.quote(str(Name)),\n "
<> "hungrig=parse.quote(str(hungrig)))"

it "should build a formatted val with parse.quote and str" $
property $ \s -> T.isInfixOf "=parse.quote(str(" $ formatBuilder $ T.pack s
it "should build a formatted val that ends with parens" $
property $ \s -> T.isSuffixOf (T.pack s <> "))") $ formatBuilder $ T.pack s

0 comments on commit 88a5186

Please sign in to comment.