Drop homespun query rendering, fix tests

This commit is contained in:
Michael Xavier 2015-07-10 12:09:14 -07:00
parent 2d8be0d3e7
commit 270aa8b5f7
3 changed files with 18 additions and 11 deletions

View File

@ -37,7 +37,8 @@ library
vector >= 0.10.9 && <0.11,
uri-bytestring >= 0.1 && <0.2,
exceptions,
data-default-class
data-default-class,
blaze-builder
default-language: Haskell2010
test-suite tests

View File

@ -54,6 +54,7 @@ module Database.Bloodhound.Client
)
where
import qualified Blaze.ByteString.Builder as BB
import Control.Applicative
import Control.Monad
import Control.Monad.Catch
@ -72,6 +73,7 @@ import qualified Data.Vector as V
import Network.HTTP.Client
import qualified Network.HTTP.Types.Method as NHTM
import qualified Network.HTTP.Types.Status as NHTS
import qualified Network.HTTP.Types.URI as NHTU
import Prelude hiding (filter, head)
import URI.ByteString hiding (Query)
@ -168,11 +170,12 @@ joinPath ps = do
-- | Severely dumbed down query renderer. Assumes your data doesn't
-- need any encoding
addQuery :: [(Text, Text)] -> Text -> Text
addQuery ps u = u <> "?" <> params
addQuery :: [(Text, Maybe Text)] -> Text -> Text
addQuery q u = u <> rendered
where
params = T.intercalate "&" (uncurry mkParam <$> ps)
mkParam k v = k <> "=" <> v
rendered =
T.decodeUtf8 $ BB.toByteString $ NHTU.renderQueryText prependQuestionMark q
prependQuestionMark = True
bindM2 :: (Applicative m, Monad m) => (a -> b -> m c) -> m a -> m b -> m c
@ -346,7 +349,9 @@ indexDocument (IndexName indexName)
ExternalGTE (ExternalDocVersion v) -> versionParams v "external_gte"
ForceVersion (ExternalDocVersion v) -> versionParams v "force"
vt = T.pack . show . docVersionNumber
versionParams v t = [("version", vt v), ("version_type", t)]
versionParams v t = [ ("version", Just $ vt v)
, ("version_type", Just t)
]
body = Just (encode document)
-- | 'deleteDocument' is the primary way to delete a single document.

View File

@ -5,6 +5,7 @@
module Main where
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.Reader
import Data.Aeson
@ -688,10 +689,10 @@ main = hspec $ do
describe "Enum DocVersion" $ do
it "follows the laws of Enum, Bounded" $ do
return (succ maxBound :: DocVersion) `shouldThrow` anyErrorCall
return (pred minBound :: DocVersion) `shouldThrow` anyErrorCall
return (toEnum 0 :: DocVersion) `shouldThrow` anyErrorCall
return (toEnum 9200000000000000001 :: DocVersion) `shouldThrow` anyErrorCall
evaluate (succ maxBound :: DocVersion) `shouldThrow` anyErrorCall
evaluate (pred minBound :: DocVersion) `shouldThrow` anyErrorCall
evaluate (toEnum 0 :: DocVersion) `shouldThrow` anyErrorCall
evaluate (toEnum 9200000000000000001 :: DocVersion) `shouldThrow` anyErrorCall
enumFrom (pred maxBound :: DocVersion) `shouldBe` [pred maxBound, maxBound]
enumFrom (pred maxBound :: DocVersion) `shouldBe` [pred maxBound, maxBound]
enumFromThen minBound (pred maxBound :: DocVersion) `shouldBe` [minBound, pred maxBound, maxBound]
enumFromThen minBound (pred maxBound :: DocVersion) `shouldBe` [minBound, pred maxBound]