git.iko.soy/typeable/bloodhound
on a9c52fa help sign in
95 lines 3.5 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
3module Test.BulkAPI (spec) where
5import Test.Common
6import Test.Import
8import qualified Data.Vector as V
9import qualified Lens.Micro.Aeson as LMA
11newtype BulkTest =
12 BulkTest Text
13 deriving (Eq, Show)
15instance ToJSON BulkTest where
16 toJSON (BulkTest name') =
17 object ["name" .= name']
19instance FromJSON BulkTest where
20 parseJSON = withObject "BulkTest" parse
21 where
22 parse o = do
23 t <- o .: "name"
24 BulkTest <$> parseJSON t
26spec :: Spec
27spec =
28 describe "Bulk API" $
29 it "inserts all documents we request" $ withTestEnv $ do
30 _ <- insertData
31 let firstTest = BulkTest "blah"
32 let secondTest = BulkTest "bloo"
33 let thirdTest = BulkTest "graffle"
34 let fourthTest = BulkTest "garabadoo"
35 let fifthTest = BulkTest "serenity"
36 let firstDoc = BulkIndex testIndex
37 testMapping (DocId "2") (toJSON firstTest)
38 let secondDoc = BulkCreate testIndex
39 testMapping (DocId "3") (toJSON secondTest)
40 let thirdDoc = BulkCreateEncoding testIndex
41 testMapping (DocId "4") (toEncoding thirdTest)
42 let fourthDoc = BulkIndexAuto testIndex
43 testMapping (toJSON fourthTest)
44 let fifthDoc = BulkIndexEncodingAuto testIndex
45 testMapping (toEncoding fifthTest)
46 let stream = V.fromList [firstDoc, secondDoc, thirdDoc, fourthDoc, fifthDoc]
47 _ <- bulk stream
48 -- liftIO $ pPrint bulkResp
49 _ <- refreshIndex testIndex
50 -- liftIO $ pPrint refreshResp
51 fDoc <- getDocument testIndex testMapping (DocId "2")
52 sDoc <- getDocument testIndex testMapping (DocId "3")
53 tDoc <- getDocument testIndex testMapping (DocId "4")
54 -- note that we cannot query for fourthDoc and fifthDoc since we
55 -- do not know their autogenerated ids.
56 let maybeFirst =
57 eitherDecode
58 $ responseBody fDoc
59 :: Either String (EsResult BulkTest)
60 let maybeSecond =
61 eitherDecode
62 $ responseBody sDoc
63 :: Either String (EsResult BulkTest)
64 let maybeThird =
65 eitherDecode
66 $ responseBody tDoc
67 :: Either String (EsResult BulkTest)
68 -- liftIO $ pPrint [maybeFirst, maybeSecond, maybeThird]
69 liftIO $ do
70 fmap getSource maybeFirst `shouldBe` Right (Just firstTest)
71 fmap getSource maybeSecond `shouldBe` Right (Just secondTest)
72 fmap getSource maybeThird `shouldBe` Right (Just thirdTest)
73 -- Since we can't get the docs by doc id, we check for their existence in
74 -- a match all query.
75 let query = MatchAllQuery Nothing
76 let search = mkSearch (Just query) Nothing
77 resp <- searchByIndex testIndex search
78 parsed <- parseEsResponse resp :: BH IO (Either EsError (SearchResult Value))
79 case parsed of
80 Left e ->
81 liftIO $ expectationFailure ("Expected a script-transformed result but got: " <> show e)
82 (Right sr) -> do
83 liftIO $
84 hitsTotal (searchHits sr) `shouldBe` 6
85 let nameList :: [Text]
86 nameList =
87 hits (searchHits sr)
88 ^.. traverse
89 . to hitSource
90 . _Just
91 . LMA.key "name"
92 . _String
93 liftIO $
94 nameList
95 `shouldBe` ["blah","bloo","graffle","garabadoo","serenity"]