bloodhound-1.0.0.0: tests/Test/ScriptSpec.hs
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module Test.ScriptSpec (spec) where
import Data.Aeson.KeyMap qualified as X
import Data.Map qualified as M
import TestsUtils.Common
import TestsUtils.Import
spec :: Spec
spec = do
describe "Script" $ do
it "returns a transformed document based on the script field" $
withTestEnv $ do
_ <- insertData
let query = MatchAllQuery Nothing
sfv =
toJSON $
Script
(Just (ScriptLanguage "painless"))
(ScriptInline "doc['age'].value * 2")
Nothing
Nothing
sf =
ScriptFields $
X.fromList [("test1", sfv)]
search' = mkSearch (Just query) Nothing
search = search' {scriptFields = Just sf}
sr <- performBHRequest $ searchByIndex @Value testIndex search
let Just results =
hitFields (head (hits (searchHits sr)))
liftIO $
results `shouldBe` HitFields (M.fromList [("test1", [Number 20000.0])])
-- bloodhound-04f.22: the 'options' field (e.g. mustache
-- @{"content_type": "application/json"}@) must be emitted on the
-- script object and survive a FromJSON/ToJSON round-trip. Pure test —
-- no cluster required.
it "emits and round-trips the options field" $ do
let opts = X.fromList [("content_type", String "application/json")]
script =
Script
(Just (ScriptLanguage "mustache"))
(ScriptInline "{{query}}")
Nothing
(Just opts)
expectedInner =
object
[ "lang" .= String "mustache",
"source" .= String "{{query}}",
"options" .= object ["content_type" .= String "application/json"]
]
toJSON script `shouldBe` object ["script" .= expectedInner]
decode (encode script) `shouldBe` (Just script :: Maybe Script)