packages feed

bloodhound-1.0.0.0: tests/Test/SearchIntegrationSpec.hs

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

module Test.SearchIntegrationSpec (spec) where

import Data.List.NonEmpty qualified as NE
import TestsUtils.Common
import TestsUtils.Import

-- | Live integration coverage for the @searchByIndexWith@ \/ @searchAllWith@
-- \/ @searchByIndicesWith@ family. These tests verify that 'SearchOptions'
-- are accepted by a real backend (the params themselves are unit-tested in
-- "Test.SearchOptionsSpec"). Each case asserts that the same document that
-- 'searchByIndex' finds is also found when extra URI params are attached —
-- i.e. the params don't break the request envelope.
spec :: Spec
spec =
  describe "Search *With URI params (integration)" $ do
    it "searchByIndexWith defaultSearchOptions matches searchByIndex" $
      withTestEnv $ do
        _ <- insertData
        let query = TermQuery (Term "user" "bitemyapp") Nothing
        let search = mkSearch (Just query) Nothing
        withDefault <- performBHRequest $ searchByIndexWith @Tweet testIndex defaultSearchOptions search
        plain <- performBHRequest $ searchByIndex testIndex search
        liftIO $
          (hitSource <$> headMay (hits (searchHits withDefault)))
            `shouldBe` (hitSource <$> headMay (hits (searchHits plain)))

    it "searchByIndexWith accepts preference and request_cache" $
      withTestEnv $ do
        _ <- insertData
        let query = TermQuery (Term "user" "bitemyapp") Nothing
        let search = mkSearch (Just query) Nothing
        let opts =
              defaultSearchOptions
                { soPreference = Just "_local",
                  soRequestCache = Just True
                }
        myTweet <- (>>= grabFirst) <$> tryPerformBHRequest (searchByIndexWith @Tweet testIndex opts search)
        liftIO $
          myTweet `shouldBe` Right exampleTweet

    it "searchByIndexWith accepts typed_keys and seq_no_primary_term" $
      withTestEnv $ do
        _ <- insertData
        let search = mkSearch Nothing Nothing
        let opts =
              defaultSearchOptions
                { soTypedKeys = Just True,
                  soSeqNoPrimaryTerm = Just True
                }
        result <- tryPerformBHRequest (searchByIndexWith @Tweet testIndex opts search)
        liftIO $
          result `shouldSatisfy` isRight

    it "searchByIndicesWith searches across multiple indices" $
      withTestEnv $ do
        _ <- insertData
        let search = mkSearch Nothing Nothing
        result <- tryPerformBHRequest (searchByIndicesWith @Tweet (testIndex NE.:| []) defaultSearchOptions search)
        liftIO $
          result `shouldSatisfy` isRight

    it "searchAllWith searches every index" $
      withTestEnv $ do
        _ <- insertData
        let search = mkSearch Nothing Nothing
        -- Decode as Value: /_search returns hits from system indices
        -- (e.g. .tasks) whose _source is not Tweet-shaped. The point of
        -- this case is to verify the request envelope is accepted.
        result <- tryPerformBHRequest (searchAllWith @Value defaultSearchOptions search)
        liftIO $
          result `shouldSatisfy` isRight