git.iko.soy/typeable/bloodhound
on master help sign in
Merge pull request #34 from bitemyapp/31-search-hits-monoid

Add Monoid instance for Search Hits

commit7da3fe28de7569bf468e41de1dd2d4c21f484c6c authorChris Allen <cma@bitemyapp.com> dateThu 14 May 2015, 01:08 (11 years ago) parent2727e0e2ff6378
3 files changed+64−17
bloodhound.cabal+2−1 src/Database/Bloodhound/Types.hs+7 tests/tests.hs+55−16
bloodhound.cabal+2−1
@@ -62,7 +62,8 @@ test-suite tests
6262 QuickCheck,
6363 vector,
6464 unordered-containers >= 0.2.5.0 && <0.3,
65− mtl
65+ mtl,
66+ quickcheck-properties
6667 default-language: Haskell2010
6768
6869test-suite doctests
src/Database/Bloodhound/Types.hs+7
@@ -1125,6 +1125,13 @@ data SearchHits a =
11251125 , maxScore :: Score
11261126 , hits :: [Hit a] } deriving (Eq, Show)
11271127
1128+
1129+instance Monoid (SearchHits a) where
1130+ mempty = SearchHits 0 Nothing mempty
1131+ mappend (SearchHits ta ma ha) (SearchHits tb mb hb) =
1132+ SearchHits (ta + tb) (max ma mb) (ha <> hb)
1133+
1134+
11281135data Hit a =
11291136 Hit { hitIndex :: IndexName
11301137 , hitType :: MappingName
tests/tests.hs+55−16
@@ -1,31 +1,33 @@
11{-# LANGUAGE DeriveGeneric #-}
22{-# LANGUAGE OverloadedStrings #-}
33{-# LANGUAGE ScopedTypeVariables #-}
4−
4+{-# OPTIONS_GHC -fno-warn-orphans #-}
55module Main where
66
77import Control.Applicative
88import Control.Monad
99import Control.Monad.Reader
1010import Data.Aeson
11−import qualified Data.HashMap.Strict as HM
12−import Data.List (nub)
13−import Data.List.NonEmpty (NonEmpty (..))
14−import qualified Data.List.NonEmpty as NE
15−import qualified Data.Map.Strict as M
16−import Data.Text (Text)
17−import qualified Data.Text as T
18−import Data.Time.Calendar (Day (..))
19−import Data.Time.Clock (UTCTime (..), secondsToDiffTime)
20−import qualified Data.Vector as V
11+import qualified Data.HashMap.Strict as HM
12+import Data.List (nub)
13+import Data.List.NonEmpty (NonEmpty (..))
14+import qualified Data.List.NonEmpty as NE
15+import qualified Data.Map.Strict as M
16+import Data.Text (Text)
17+import qualified Data.Text as T
18+import Data.Time.Calendar (Day (..))
19+import Data.Time.Clock (UTCTime (..),
20+ secondsToDiffTime)
21+import qualified Data.Vector as V
2122import Database.Bloodhound
22−import GHC.Generics (Generic)
23+import GHC.Generics (Generic)
2324import Network.HTTP.Client
24−import qualified Network.HTTP.Types.Status as NHTS
25−import Prelude hiding (filter, putStrLn)
25+import qualified Network.HTTP.Types.Status as NHTS
26+import Prelude hiding (filter, putStrLn)
2627import Test.Hspec
28+import Test.QuickCheck.Property.Monoid
2729
28−import Test.Hspec.QuickCheck (prop)
30+import Test.Hspec.QuickCheck (prop)
2931import Test.QuickCheck
3032
3133testServer :: Server
@@ -224,6 +226,40 @@ instance Arbitrary RegexpFlag where
224226 , pure Interval
225227 ]
226228
229+arbitraryScore :: Gen Score
230+arbitraryScore = fmap getPositive <$> arbitrary
231+
232+instance Arbitrary Text where
233+ arbitrary = T.pack <$> arbitrary
234+
235+instance (Arbitrary k, Arbitrary v, Ord k) => Arbitrary (M.Map k v) where
236+ arbitrary = M.fromList <$> arbitrary
237+
238+instance Arbitrary IndexName where
239+ arbitrary = IndexName <$> arbitrary
240+
241+instance Arbitrary MappingName where
242+ arbitrary = MappingName <$> arbitrary
243+
244+instance Arbitrary DocId where
245+ arbitrary = DocId <$> arbitrary
246+
247+instance Arbitrary a => Arbitrary (Hit a) where
248+ arbitrary = Hit <$> arbitrary
249+ <*> arbitrary
250+ <*> arbitrary
251+ <*> arbitraryScore
252+ <*> arbitrary
253+ <*> arbitrary
254+
255+
256+instance Arbitrary a => Arbitrary (SearchHits a) where
257+ arbitrary = sized $ \n -> resize (n `div` 2) $ do
258+ tot <- getPositive <$> arbitrary
259+ score <- arbitraryScore
260+ hs <- arbitrary
261+ return $ SearchHits tot score hs
262+
227263main :: IO ()
228264main = hspec $ do
229265
@@ -610,4 +646,7 @@ main = hspec $ do
610646 let notDropped = omitNulls $ [ "test1" .= (toJSON (1 :: Int))
611647 , "test2" .= (toJSON ("some value" :: Text))]
612648 in notDropped `shouldBe` Object (HM.fromList [ ("test1", Number 1.0)
613− , ("test2", String "some value")])
649+ , ("test2", String "some value")])
650+ describe "Monoid (SearchHits a)" $ do
651+ prop "abides the monoid laws" $ eq $
652+ prop_Monoid (T :: T (SearchHits ()))