git.iko.soy/typeable/bloodhound
on master help sign in
Fix up tests for script data types.
23e8a8eWill Jones
src/Database/V5/Bloodhound/Types.hs+25−2
@@ -122,6 +122,7 @@ module Database.V5.Bloodhound.Types
122 , Pattern(..)
122 , Pattern(..)
123 , ShardResult(..)
123 , ShardResult(..)
124 , Hit(..)
124 , Hit(..)
125+ , HitFields(..)
125 , Filter(..)
126 , Filter(..)
126 , Seminearring(..)
127 , Seminearring(..)
127 , BoolMatch(..)
128 , BoolMatch(..)
@@ -150,6 +151,9 @@ module Database.V5.Bloodhound.Types
150 , RegexpFlags(..)
151 , RegexpFlags(..)
151 , RegexpFlag(..)
152 , RegexpFlag(..)
152 , FieldName(..)
153 , FieldName(..)
154+ , ScriptFields(..)
155+ , ScriptFieldName
156+ , ScriptFieldValue
153 , Script(..)
157 , Script(..)
154 , ScriptLanguage(..)
158 , ScriptLanguage(..)
155 , ScriptInline(..)
159 , ScriptInline(..)
@@ -1013,7 +1017,12 @@ newtype QueryString = QueryString Text deriving (Eq, Generic, Read, Show, ToJSON
1013-}
1017-}
1014newtype FieldName = FieldName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1018newtype FieldName = FieldName Text deriving (Eq, Read, Show, Generic, ToJSON, FromJSON, Typeable)
1015
1019
1020+newtype ScriptFields =
1021+ ScriptFields (HM.HashMap ScriptFieldName ScriptFieldValue)
1022+ deriving (Eq, Read, Show, Generic, Typeable)
1023+
1016type ScriptFieldName = Text
1024type ScriptFieldName = Text
1025+type ScriptFieldValue = Value
1017
1026
1018data Script =
1027data Script =
1019 Script { scriptLanguage :: Maybe ScriptLanguage
1028 Script { scriptLanguage :: Maybe ScriptLanguage
@@ -1157,7 +1166,7 @@ data Search = Search { queryBody :: Maybe Query
1157 , size :: Size
1166 , size :: Size
1158 , searchType :: SearchType
1167 , searchType :: SearchType
1159 , fields :: Maybe [FieldName]
1168 , fields :: Maybe [FieldName]
1160− , scriptFields :: Maybe (HM.HashMap ScriptFieldName Script)
1169+ , scriptFields :: Maybe ScriptFields
1161 , source :: Maybe Source
1170 , source :: Maybe Source
1162 , suggestBody :: Maybe Suggest -- ^ Only one Suggestion request / response per Search is supported.
1171 , suggestBody :: Maybe Suggest -- ^ Only one Suggestion request / response per Search is supported.
1163 } deriving (Eq, Read, Show, Generic, Typeable)
1172 } deriving (Eq, Read, Show, Generic, Typeable)
@@ -1815,7 +1824,7 @@ data Hit a =
1815 , hitDocId :: DocId
1824 , hitDocId :: DocId
1816 , hitScore :: Score
1825 , hitScore :: Score
1817 , hitSource :: Maybe a
1826 , hitSource :: Maybe a
1818− , hitFields :: Maybe (HM.HashMap Text [Value])
1827+ , hitFields :: Maybe HitFields
1819 , hitHighlight :: Maybe HitHighlight } deriving (Eq, Read, Show, Generic, Typeable)
1828 , hitHighlight :: Maybe HitHighlight } deriving (Eq, Read, Show, Generic, Typeable)
1820
1829
1821data ShardResult =
1830data ShardResult =
@@ -1823,6 +1832,10 @@ data ShardResult =
1823 , shardsSuccessful :: Int
1832 , shardsSuccessful :: Int
1824 , shardsFailed :: Int } deriving (Eq, Read, Show, Generic, Typeable)
1833 , shardsFailed :: Int } deriving (Eq, Read, Show, Generic, Typeable)
1825
1834
1835+newtype HitFields =
1836+ HitFields (M.Map Text [Value])
1837+ deriving (Eq, Read, Show, Generic, Typeable)
1838+
1826type HitHighlight = M.Map Text [Text]
1839type HitHighlight = M.Map Text [Text]
1827
1840
1828showText :: Show a => a -> Text
1841showText :: Show a => a -> Text
@@ -2957,6 +2970,13 @@ parseFunctionScoreFunction o =
2957 singleRandom o' = FunctionScoreFunctionRandom <$> o' .: "seed"
2970 singleRandom o' = FunctionScoreFunctionRandom <$> o' .: "seed"
2958 singleFieldValueFactor = pure . FunctionScoreFunctionFieldValueFactor
2971 singleFieldValueFactor = pure . FunctionScoreFunctionFieldValueFactor
2959
2972
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+
2960instance ToJSON Script where
2980instance ToJSON Script where
2961 toJSON (Script lang inline stored params) =
2981 toJSON (Script lang inline stored params) =
2962 object [ "script" .= omitNulls base ]
2982 object [ "script" .= omitNulls base ]
@@ -3948,6 +3968,9 @@ instance FromJSON ShardResult where
3948 v .: "failed"
3968 v .: "failed"
3949 parseJSON _ = empty
3969 parseJSON _ = empty
3950
3970
3971+instance FromJSON HitFields where
3972+ parseJSON x
3973+ = HitFields <$> parseJSON x
3951
3974
3952instance FromJSON DocVersion where
3975instance FromJSON DocVersion where
3953 parseJSON v = do
3976 parseJSON v = do
tests/V5/tests.hs+39−18
@@ -15,7 +15,7 @@
15module Main where
15module Main where
16
16
17import Control.Applicative
17import Control.Applicative
18−import Control.Error
18+import Control.Error hiding (Script)
19import Control.Exception (evaluate)
19import Control.Exception (evaluate)
20import Control.Monad
20import Control.Monad
21import Control.Monad.Catch
21import Control.Monad.Catch
@@ -509,6 +509,11 @@ instance ApproxEq TemplateName
509instance ApproxEq TemplatePattern
509instance ApproxEq TemplatePattern
510instance ApproxEq QueryString
510instance ApproxEq QueryString
511instance ApproxEq FieldName
511instance ApproxEq FieldName
512+instance ApproxEq Script
513+instance ApproxEq ScriptLanguage
514+instance ApproxEq ScriptInline
515+instance ApproxEq ScriptId
516+instance ApproxEq ScriptParams where (=~) = (==)
512instance ApproxEq CacheName
517instance ApproxEq CacheName
513instance ApproxEq CacheKey
518instance ApproxEq CacheKey
514instance ApproxEq Existence
519instance ApproxEq Existence
@@ -563,11 +568,8 @@ instance ApproxEq FunctionScoreQuery
563instance ApproxEq BoostMode
568instance ApproxEq BoostMode
564instance ApproxEq ScoreMode
569instance ApproxEq ScoreMode
565instance ApproxEq FunctionScoreFunctions
570instance ApproxEq FunctionScoreFunctions
566−instance ApproxEq ComponentFunctionScoreFunction
567instance ApproxEq FunctionScoreFunction
571instance ApproxEq FunctionScoreFunction
568−instance ApproxEq FunctionScoreScript
572+instance ApproxEq ComponentFunctionScoreFunction
569−instance ApproxEq FunctionScoreScriptInline
570−instance ApproxEq FunctionScoreScriptParams where (=~) = (==)
571instance ApproxEq Weight where (=~) = (==)
573instance ApproxEq Weight where (=~) = (==)
572instance ApproxEq Seed where (=~) = (==)
574instance ApproxEq Seed where (=~) = (==)
573instance ApproxEq FieldValueFactor
575instance ApproxEq FieldValueFactor
@@ -686,8 +688,12 @@ instance (Arbitrary a, Typeable a) => Arbitrary (Hit a) where
686 <*> arbitraryScore
688 <*> arbitraryScore
687 <*> arbitrary
689 <*> arbitrary
688 <*> arbitrary
690 <*> arbitrary
691+ <*> arbitrary
689 shrink = genericShrink
692 shrink = genericShrink
690
693
694+instance Arbitrary HitFields where
695+ arbitrary = pure (HitFields M.empty)
696+ shrink = const []
691
697
692instance (Arbitrary a, Typeable a) => Arbitrary (SearchHits a) where
698instance (Arbitrary a, Typeable a) => Arbitrary (SearchHits a) where
693 arbitrary = reduceSize $ do
699 arbitrary = reduceSize $ do
@@ -740,6 +746,27 @@ instance Arbitrary FieldName where
740 arbitrary = FieldName . T.pack <$> listOf1 arbitraryAlphaNum
746 arbitrary = FieldName . T.pack <$> listOf1 arbitraryAlphaNum
741 shrink = genericShrink
747 shrink = genericShrink
742
748
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+
743
770
744instance Arbitrary RegexpFlags where
771instance Arbitrary RegexpFlags where
745 arbitrary = oneof [ pure AllRegexpFlags
772 arbitrary = oneof [ pure AllRegexpFlags
@@ -885,18 +912,8 @@ instance Arbitrary FunctionScoreQuery where arbitrary = sopArbitrary; shrink = g
885instance Arbitrary BoostMode where arbitrary = sopArbitrary; shrink = genericShrink
912instance Arbitrary BoostMode where arbitrary = sopArbitrary; shrink = genericShrink
886instance Arbitrary ScoreMode where arbitrary = sopArbitrary; shrink = genericShrink
913instance Arbitrary ScoreMode where arbitrary = sopArbitrary; shrink = genericShrink
887instance Arbitrary FunctionScoreFunctions where arbitrary = sopArbitrary; shrink = genericShrink
914instance Arbitrary FunctionScoreFunctions where arbitrary = sopArbitrary; shrink = genericShrink
888−instance Arbitrary ComponentFunctionScoreFunction where arbitrary = sopArbitrary; shrink = genericShrink
889instance Arbitrary FunctionScoreFunction where arbitrary = sopArbitrary; shrink = genericShrink
915instance 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
916+instance Arbitrary ComponentFunctionScoreFunction 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 []
900instance Arbitrary Weight where arbitrary = sopArbitrary; shrink = genericShrink
917instance Arbitrary Weight where arbitrary = sopArbitrary; shrink = genericShrink
901instance Arbitrary Seed where arbitrary = sopArbitrary; shrink = genericShrink
918instance Arbitrary Seed where arbitrary = sopArbitrary; shrink = genericShrink
902instance Arbitrary FieldValueFactor where arbitrary = sopArbitrary; shrink = genericShrink
919instance Arbitrary FieldValueFactor where arbitrary = sopArbitrary; shrink = genericShrink
@@ -1192,7 +1209,7 @@ main = hspec $ do
1192 let search = Search Nothing
1209 let search = Search Nothing
1193 Nothing (Just [sortSpec]) Nothing Nothing
1210 Nothing (Just [sortSpec]) Nothing Nothing
1194 False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing
1211 False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing
1195− Nothing
1212+ Nothing Nothing
1196 result <- searchTweets search
1213 result <- searchTweets search
1197 let myTweet = grabFirst result
1214 let myTweet = grabFirst result
1198 liftIO $
1215 liftIO $
@@ -1659,6 +1676,11 @@ main = hspec $ do
1659 propJSON (Proxy :: Proxy TemplatePattern)
1676 propJSON (Proxy :: Proxy TemplatePattern)
1660 propJSON (Proxy :: Proxy QueryString)
1677 propJSON (Proxy :: Proxy QueryString)
1661 propJSON (Proxy :: Proxy FieldName)
1678 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)
1662 propJSON (Proxy :: Proxy CacheName)
1684 propJSON (Proxy :: Proxy CacheName)
1663 propJSON (Proxy :: Proxy CacheKey)
1685 propJSON (Proxy :: Proxy CacheKey)
1664 propJSON (Proxy :: Proxy Existence)
1686 propJSON (Proxy :: Proxy Existence)
@@ -1713,7 +1735,6 @@ main = hspec $ do
1713 propJSON (Proxy :: Proxy BoostMode)
1735 propJSON (Proxy :: Proxy BoostMode)
1714 propJSON (Proxy :: Proxy ScoreMode)
1736 propJSON (Proxy :: Proxy ScoreMode)
1715 propJSON (Proxy :: Proxy ComponentFunctionScoreFunction)
1737 propJSON (Proxy :: Proxy ComponentFunctionScoreFunction)
1716− propJSON (Proxy :: Proxy FunctionScoreScript)
1717 propJSON (Proxy :: Proxy FieldValueFactor)
1738 propJSON (Proxy :: Proxy FieldValueFactor)
1718 propJSON (Proxy :: Proxy FactorModifier)
1739 propJSON (Proxy :: Proxy FactorModifier)
1719 propJSON (Proxy :: Proxy DisMaxQuery)
1740 propJSON (Proxy :: Proxy DisMaxQuery)