From 86839c57325c636c7c097c149181ac892e4f9963 Mon Sep 17 00:00:00 2001 From: Erik Aker Date: Thu, 16 Feb 2017 08:02:55 -0800 Subject: [PATCH] Add some more tests for internal functions --- src/Servant/PY/Internal.hs | 14 +++++++++++--- src/Servant/PY/Requests.hs | 2 +- test/Servant/PY/InternalSpec.hs | 20 +++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/Servant/PY/Internal.hs b/src/Servant/PY/Internal.hs index 14843b8..a6afc39 100644 --- a/src/Servant/PY/Internal.hs +++ b/src/Servant/PY/Internal.hs @@ -67,6 +67,7 @@ import Data.Monoid import Data.Proxy import Data.Text (Text) import qualified Data.Text as T +import Data.Text.Encoding (decodeUtf8) import GHC.TypeLits import Servant.Foreign @@ -269,11 +270,18 @@ capturesToFormatArgs segments = map getSegment $ filter isCapture segments getSegment _ = "" getCapture s = s ^. argName . _PathSegment -buildDocString :: T.Text -> PyRequest -> CommonGeneratorOptions -> T.Text -buildDocString method req opts = T.toUpper method <> " \"" <> url <> "\n" - <> if null args then "" else argDocs +buildDocString :: PyRequest -> CommonGeneratorOptions -> T.Text +buildDocString req opts = T.toUpper method <> " \"" <> url <> "\n" + <> includeArgs <> "\n\n" + <> indent' <> "Returns: " <> "\n" + <> indent' <> indent' <> returnVal where args = capturesToFormatArgs $ req ^.. reqUrl.path.traverse + method = decodeUtf8 $ req ^. reqMethod url = makePyUrl' $ req ^.. reqUrl.path.traverse + includeArgs = if null args then "" else argDocs argDocs = indent' <> "Args: " <> "\n" <> indent' <> indent' <> T.intercalate ("\n" <> indent' <> indent') args indent' = indentation opts indent + returnVal = case returnMode opts of + DangerMode -> "JSON response from the endpoint" + RawResponse -> "response (requests.Response) from issuing the request" diff --git a/src/Servant/PY/Requests.hs b/src/Servant/PY/Requests.hs index 30302eb..6648a7e 100644 --- a/src/Servant/PY/Requests.hs +++ b/src/Servant/PY/Requests.hs @@ -44,7 +44,7 @@ generatePyRequestWith :: CommonGeneratorOptions -> PyRequest -> Text generatePyRequestWith opts req = "\n" <> "def " <> fname <> "(" <> argsStr <> "):\n" <> indent' <> docStringMarker - <> indent' <> buildDocString method req opts <> "\n" + <> indent' <> buildDocString req opts <> "\n" <> indent' <> docStringMarker <> indent' <> "url = " <> makePyUrl opts req (indent' <> indent') <> "\n\n" <> headerDef diff --git a/test/Servant/PY/InternalSpec.hs b/test/Servant/PY/InternalSpec.hs index 4c1304a..9c42b52 100644 --- a/test/Servant/PY/InternalSpec.hs +++ b/test/Servant/PY/InternalSpec.hs @@ -66,7 +66,7 @@ captureApi = Proxy customOptions :: CommonGeneratorOptions customOptions = defCommonGeneratorOptions { urlPrefix = "urlForRequesting:9000" - , returnMode = DangerMode + , returnMode = RawResponse } spec :: Spec @@ -89,6 +89,7 @@ instance Arbitrary ASCII where internalSpec :: Spec internalSpec = describe "Internal" $ do + describe "pure text functions" $ do it "should only indent using whitespace" $ property $ \n -> indenter n indent == mconcat (replicate n (T.pack " ")) @@ -99,6 +100,7 @@ internalSpec = describe "Internal" $ do let dict = toPyDict " " ["forty", "one", "people"] dict `shouldBe` "{\"forty\": forty,\n \"one\": one,\n \"people\": people}" + describe "functions that operate on Req objects" $ do let captureList = listFromAPI (Proxy :: Proxy NoTypes) (Proxy :: Proxy NoContent) captureApi it "should correctly find captures" $ do let captured = captures . head $ captureList @@ -123,3 +125,19 @@ internalSpec = describe "Internal" $ do 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 + + it "should build urls properly with / separator" $ do + let pyUrl = makePyUrl customOptions req " " + pyUrl `shouldBe` "\"urlForRequesting:9000/login-with-path-var-and-header/{id}/{Name}/{hungrig}\"" + <> withFormattedCaptures " " pathParts + + it "should do segment-to-str as a plain string for Static" $ + segmentToStr (head pathParts) == "login-with-path-var-and-header" + it "should do segment-to-str in formatting braces for a capture" $ + segmentToStr (last pathParts) == "{hungrig}" + it "should build a doctstring that looks like a regular Python docstring" $ do + let docstring = buildDocString req customOptions + docstring `shouldContain` "POST" + docstring `shouldContain` makePyUrl' pathParts + docstring `shouldContain` "Args:" + docstring `shouldContain` "Returns:"