packages feed

bloodhound-1.0.0.0: tests/Test/IndexSettingsOptionsSpec.hs

{-# LANGUAGE OverloadedStrings #-}

module Test.IndexSettingsOptionsSpec (spec) where

import Data.List (sort)
import Data.Text (Text)
import Database.Bloodhound.Common.Types
import Test.Hspec (Spec, describe, it, shouldBe, shouldSatisfy)

-- | Stable ordering for equality: 'withQueries' preserves the order in
-- which params are emitted, but the tests compare the @Set@ of params to
-- avoid coupling to internal field ordering of the Options records.
normalize :: [(Text, Maybe Text)] -> [(Text, Maybe Text)]
normalize = sort

spec :: Spec
spec = do
  describe "UpdateIndexSettingsOptions URI param rendering" $ do
    it "defaultUpdateIndexSettingsOptions emits no params" $
      updateIndexSettingsOptionsParams defaultUpdateIndexSettingsOptions
        `shouldBe` []

    it "renders every Bool field as true/false strings" $ do
      let opts =
            defaultUpdateIndexSettingsOptions
              { uisoPreserveExisting = Just True,
                uisoFlatSettings = Just False
              }
      normalize (updateIndexSettingsOptionsParams opts)
        `shouldBe` [ ("flat_settings", Just "false"),
                     ("preserve_existing", Just "true")
                   ]

    it "renders master_timeout and timeout as <n><unit>" $ do
      let opts =
            defaultUpdateIndexSettingsOptions
              { uisoMasterTimeout = Just (TimeUnitSeconds, 30),
                uisoTimeout = Just (TimeUnitMinutes, 1)
              }
      normalize (updateIndexSettingsOptionsParams opts)
        `shouldBe` [ ("master_timeout", Just "30s"),
                     ("timeout", Just "1m")
                   ]

    it "supports every TimeUnits suffix for master_timeout" $ do
      let mk u = updateIndexSettingsOptionsParams defaultUpdateIndexSettingsOptions {uisoMasterTimeout = Just (u, 1)}
      mk TimeUnitDays `shouldBe` [("master_timeout", Just "1d")]
      mk TimeUnitHours `shouldBe` [("master_timeout", Just "1h")]
      mk TimeUnitMinutes `shouldBe` [("master_timeout", Just "1m")]
      mk TimeUnitSeconds `shouldBe` [("master_timeout", Just "1s")]
      mk TimeUnitMilliseconds `shouldBe` [("master_timeout", Just "1ms")]
      mk TimeUnitMicroseconds `shouldBe` [("master_timeout", Just "1micros")]
      mk TimeUnitNanoseconds `shouldBe` [("master_timeout", Just "1nanos")]

    it "emits the full param set when every field is populated" $ do
      let opts =
            defaultUpdateIndexSettingsOptions
              { uisoMasterTimeout = Just (TimeUnitSeconds, 5),
                uisoTimeout = Just (TimeUnitSeconds, 10),
                uisoPreserveExisting = Just True,
                uisoFlatSettings = Just True
              }
      let rendered = updateIndexSettingsOptionsParams opts
      length rendered `shouldBe` 4
      rendered `shouldSatisfy` all (\(_, v) -> v == Just "5s" || v == Just "10s" || v == Just "true")
      sort (map fst rendered)
        `shouldBe` [ "flat_settings",
                     "master_timeout",
                     "preserve_existing",
                     "timeout"
                   ]

  describe "GetIndexSettingsOptions URI param rendering" $ do
    it "defaultGetIndexSettingsOptions emits no params" $
      getIndexSettingsOptionsParams defaultGetIndexSettingsOptions
        `shouldBe` []

    it "renders every Bool field as true/false strings" $ do
      let opts =
            defaultGetIndexSettingsOptions
              { gisoFlatSettings = Just True,
                gisoIncludeDefaults = Just False,
                gisoLocal = Just True
              }
      normalize (getIndexSettingsOptionsParams opts)
        `shouldBe` [ ("flat_settings", Just "true"),
                     ("include_defaults", Just "false"),
                     ("local", Just "true")
                   ]

    it "renders master_timeout as <n><unit>" $ do
      let opts =
            defaultGetIndexSettingsOptions
              { gisoMasterTimeout = Just (TimeUnitSeconds, 30)
              }
      getIndexSettingsOptionsParams opts
        `shouldBe` [("master_timeout", Just "30s")]

    it "supports every TimeUnits suffix for master_timeout" $ do
      let mk u = getIndexSettingsOptionsParams defaultGetIndexSettingsOptions {gisoMasterTimeout = Just (u, 1)}
      mk TimeUnitDays `shouldBe` [("master_timeout", Just "1d")]
      mk TimeUnitHours `shouldBe` [("master_timeout", Just "1h")]
      mk TimeUnitMinutes `shouldBe` [("master_timeout", Just "1m")]
      mk TimeUnitSeconds `shouldBe` [("master_timeout", Just "1s")]
      mk TimeUnitMilliseconds `shouldBe` [("master_timeout", Just "1ms")]
      mk TimeUnitMicroseconds `shouldBe` [("master_timeout", Just "1micros")]
      mk TimeUnitNanoseconds `shouldBe` [("master_timeout", Just "1nanos")]

    it "emits the full param set when every field is populated" $ do
      let opts =
            defaultGetIndexSettingsOptions
              { gisoMasterTimeout = Just (TimeUnitSeconds, 5),
                gisoFlatSettings = Just True,
                gisoIncludeDefaults = Just True,
                gisoLocal = Just False
              }
      let rendered = getIndexSettingsOptionsParams opts
      length rendered `shouldBe` 4
      sort (map fst rendered)
        `shouldBe` [ "flat_settings",
                     "include_defaults",
                     "local",
                     "master_timeout"
                   ]