git.iko.soy/typeable/bloodhound
on master help sign in
Fix up tests for script data types.
commit23e8a8e1c725b45e55724c93c07ad4a59581b2a6 authorWill Jones <will@sacharissa.co.uk> dateWed 12 Jul 2017, 17:35 (9 years ago) parent2be0cb9
2 files changed+64−20
src/Database/V5/Bloodhound/Types.hs+25−2 tests/V5/tests.hs+39−18
src/Database/V5/Bloodhound/Types.hs+25−2
@@ -122,6 +122,7 @@ module Database.V5.Bloodhound.Types
122122 , Pattern(..)
123123 , ShardResult(..)
124124 , Hit(..)
125+ , HitFields(..)
125126 , Filter(..)
126127 , Seminearring(..)
127128 , BoolMatch(..)
@@ -150,6 +151,9 @@ module Database.V5.Bloodhound.Types
150151 , RegexpFlags(..)
151152 , RegexpFlag(..)
152153 , FieldName(..)
154+ , ScriptFields(..)
155+ , ScriptFieldName
156+ , ScriptFieldValue
153157 , Script(..)
154158 , ScriptLanguage(..)
155159 , ScriptInline(..)
@@ -1013,7 +1017,12 @@ newtype QueryString = QueryString Text deriving (Eq, Generic, Read, Show, ToJSON
10131017-}
10141018newtype FieldName = FieldName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
10151019
1020+newtype ScriptFields =
1021+ ScriptFields (HM.HashMap ScriptFieldName ScriptFieldValue)
1022+ deriving (Eq, Read, Show, Generic, Typeable)
1023+
10161024type ScriptFieldName = Text
1025+type ScriptFieldValue = Value
10171026
10181027data Script =
10191028 Script { scriptLanguage :: Maybe ScriptLanguage
@@ -1157,7 +1166,7 @@ data Search = Search { queryBody :: Maybe Query
11571166 , size :: Size
11581167 , searchType :: SearchType
11591168 , fields :: Maybe [FieldName]
1160− , scriptFields :: Maybe (HM.HashMap ScriptFieldName Script)
1169+ , scriptFields :: Maybe ScriptFields
11611170 , source :: Maybe Source
11621171 , suggestBody :: Maybe Suggest -- ^ Only one Suggestion request / response per Search is supported.
11631172 } deriving (Eq, Read, Show, Generic, Typeable)
@@ -1815,7 +1824,7 @@ data Hit a =
18151824 , hitDocId :: DocId
18161825 , hitScore :: Score
18171826 , hitSource :: Maybe a
1818− , hitFields :: Maybe (HM.HashMap Text [Value])
1827+ , hitFields :: Maybe HitFields
18191828 , hitHighlight :: Maybe HitHighlight } deriving (Eq, Read, Show, Generic, Typeable)
18201829
18211830data ShardResult =
@@ -1823,6 +1832,10 @@ data ShardResult =
18231832 , shardsSuccessful :: Int
18241833 , shardsFailed :: Int } deriving (Eq, Read, Show, Generic, Typeable)
18251834
1835+newtype HitFields =
1836+ HitFields (M.Map Text [Value])
1837+ deriving (Eq, Read, Show, Generic, Typeable)
1838+
18261839type HitHighlight = M.Map Text [Text]
18271840
18281841showText :: Show a => a -> Text
@@ -2957,6 +2970,13 @@ parseFunctionScoreFunction o =
29572970 singleRandom o' = FunctionScoreFunctionRandom <$> o' .: "seed"
29582971 singleFieldValueFactor = pure . FunctionScoreFunctionFieldValueFactor
29592972
2973+instance ToJSON ScriptFields where
2974+ toJSON (ScriptFields x) = Object x
2975+
2976+instance FromJSON ScriptFields where
2977+ parseJSON (Object o) = pure (ScriptFields o)
2978+ parseJSON _ = fail "error parsing ScriptFields"
2979+
29602980instance ToJSON Script where
29612981 toJSON (Script lang inline stored params) =
29622982 object [ "script" .= omitNulls base ]
@@ -3948,6 +3968,9 @@ instance FromJSON ShardResult where
39483968 v .: "failed"
39493969 parseJSON _ = empty
39503970
3971+instance FromJSON HitFields where
3972+ parseJSON x
3973+ = HitFields <$> parseJSON x
39513974
39523975instance FromJSON DocVersion where
39533976 parseJSON v = do
tests/V5/tests.hs+39−18
@@ -15,7 +15,7 @@
1515module Main where
1616
1717import Control.Applicative
18−import Control.Error
18+import Control.Error hiding (Script)
1919import Control.Exception (evaluate)
2020import Control.Monad
2121import Control.Monad.Catch
@@ -509,6 +509,11 @@ instance ApproxEq TemplateName
509509instance ApproxEq TemplatePattern
510510instance ApproxEq QueryString
511511instance ApproxEq FieldName
512+instance ApproxEq Script
513+instance ApproxEq ScriptLanguage
514+instance ApproxEq ScriptInline
515+instance ApproxEq ScriptId
516+instance ApproxEq ScriptParams where (=~) = (==)
512517instance ApproxEq CacheName
513518instance ApproxEq CacheKey
514519instance ApproxEq Existence
@@ -563,11 +568,8 @@ instance ApproxEq FunctionScoreQuery
563568instance ApproxEq BoostMode
564569instance ApproxEq ScoreMode
565570instance ApproxEq FunctionScoreFunctions
566−instance ApproxEq ComponentFunctionScoreFunction
567571instance ApproxEq FunctionScoreFunction
568−instance ApproxEq FunctionScoreScript
569−instance ApproxEq FunctionScoreScriptInline
570−instance ApproxEq FunctionScoreScriptParams where (=~) = (==)
572+instance ApproxEq ComponentFunctionScoreFunction
571573instance ApproxEq Weight where (=~) = (==)
572574instance ApproxEq Seed where (=~) = (==)
573575instance ApproxEq FieldValueFactor
@@ -686,8 +688,12 @@ instance (Arbitrary a, Typeable a) => Arbitrary (Hit a) where
686688 <*> arbitraryScore
687689 <*> arbitrary
688690 <*> arbitrary
691+ <*> arbitrary
689692 shrink = genericShrink
690693
694+instance Arbitrary HitFields where
695+ arbitrary = pure (HitFields M.empty)
696+ shrink = const []
691697
692698instance (Arbitrary a, Typeable a) => Arbitrary (SearchHits a) where
693699 arbitrary = reduceSize $ do
@@ -740,6 +746,27 @@ instance Arbitrary FieldName where
740746 arbitrary = FieldName . T.pack <$> listOf1 arbitraryAlphaNum
741747 shrink = genericShrink
742748
749+instance Arbitrary ScriptFields where
750+ arbitrary =
751+ pure $ ScriptFields $
752+ HM.fromList []
753+
754+ shrink = const []
755+
756+instance Arbitrary Script where arbitrary = sopArbitrary; shrink = genericShrink
757+instance Arbitrary ScriptLanguage where arbitrary = sopArbitrary; shrink = genericShrink
758+instance Arbitrary ScriptInline where arbitrary = sopArbitrary; shrink = genericShrink
759+instance Arbitrary ScriptId where arbitrary = sopArbitrary; shrink = genericShrink
760+
761+instance Arbitrary ScriptParams where
762+ arbitrary =
763+ pure $ ScriptParams $
764+ HM.fromList [ ("a", Number 42)
765+ , ("b", String "forty two")
766+ ]
767+
768+ shrink = const []
769+
743770
744771instance Arbitrary RegexpFlags where
745772 arbitrary = oneof [ pure AllRegexpFlags
@@ -885,18 +912,8 @@ instance Arbitrary FunctionScoreQuery where arbitrary = sopArbitrary; shrink = g
885912instance Arbitrary BoostMode where arbitrary = sopArbitrary; shrink = genericShrink
886913instance Arbitrary ScoreMode where arbitrary = sopArbitrary; shrink = genericShrink
887914instance Arbitrary FunctionScoreFunctions where arbitrary = sopArbitrary; shrink = genericShrink
888−instance Arbitrary ComponentFunctionScoreFunction where arbitrary = sopArbitrary; shrink = genericShrink
889915instance Arbitrary FunctionScoreFunction where arbitrary = sopArbitrary; shrink = genericShrink
890−instance Arbitrary FunctionScoreScript where arbitrary = sopArbitrary; shrink = genericShrink
891−instance Arbitrary FunctionScoreScriptInline where arbitrary = sopArbitrary; shrink = genericShrink
892−instance Arbitrary FunctionScoreScriptParams where
893− arbitrary =
894− pure $ FunctionScoreScriptParams $
895− HM.fromList [ ("a", Number 42)
896− , ("b", String "forty two")
897− ]
898−
899− shrink = const []
916+instance Arbitrary ComponentFunctionScoreFunction where arbitrary = sopArbitrary; shrink = genericShrink
900917instance Arbitrary Weight where arbitrary = sopArbitrary; shrink = genericShrink
901918instance Arbitrary Seed where arbitrary = sopArbitrary; shrink = genericShrink
902919instance Arbitrary FieldValueFactor where arbitrary = sopArbitrary; shrink = genericShrink
@@ -1192,7 +1209,7 @@ main = hspec $ do
11921209 let search = Search Nothing
11931210 Nothing (Just [sortSpec]) Nothing Nothing
11941211 False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing
1195− Nothing
1212+ Nothing Nothing
11961213 result <- searchTweets search
11971214 let myTweet = grabFirst result
11981215 liftIO $
@@ -1659,6 +1676,11 @@ main = hspec $ do
16591676 propJSON (Proxy :: Proxy TemplatePattern)
16601677 propJSON (Proxy :: Proxy QueryString)
16611678 propJSON (Proxy :: Proxy FieldName)
1679+ propJSON (Proxy :: Proxy Script)
1680+ propJSON (Proxy :: Proxy ScriptLanguage)
1681+ propJSON (Proxy :: Proxy ScriptInline)
1682+ propJSON (Proxy :: Proxy ScriptId)
1683+ propJSON (Proxy :: Proxy ScriptParams)
16621684 propJSON (Proxy :: Proxy CacheName)
16631685 propJSON (Proxy :: Proxy CacheKey)
16641686 propJSON (Proxy :: Proxy Existence)
@@ -1713,7 +1735,6 @@ main = hspec $ do
17131735 propJSON (Proxy :: Proxy BoostMode)
17141736 propJSON (Proxy :: Proxy ScoreMode)
17151737 propJSON (Proxy :: Proxy ComponentFunctionScoreFunction)
1716− propJSON (Proxy :: Proxy FunctionScoreScript)
17171738 propJSON (Proxy :: Proxy FieldValueFactor)
17181739 propJSON (Proxy :: Proxy FactorModifier)
17191740 propJSON (Proxy :: Proxy DisMaxQuery)