git.iko.soy/typeable/bloodhound
on index-hack help sign in
77 lines 2.6 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 firstDoc = BulkIndex testIndex
34 testMapping (DocId "2") (toJSON firstTest)
35 let secondDoc = BulkCreate testIndex
36 testMapping (DocId "3") (toJSON secondTest)
37 let stream = V.fromList [firstDoc, secondDoc]
38 _ <- bulk stream
39 _ <- refreshIndex testIndex
40 fDoc <- getDocument testIndex testMapping (DocId "2")
41 sDoc <- getDocument testIndex testMapping (DocId "3")
42 -- note that we cannot query for fourthDoc and fifthDoc since we
43 -- do not know their autogenerated ids.
44 let maybeFirst =
45 eitherDecode
46 $ responseBody fDoc
47 :: Either String (EsResult BulkTest)
48 let maybeSecond =
49 eitherDecode
50 $ responseBody sDoc
51 :: Either String (EsResult BulkTest)
52 liftIO $ do
53 fmap getSource maybeFirst `shouldBe` Right (Just firstTest)
54 fmap getSource maybeSecond `shouldBe` Right (Just secondTest)
55 -- Since we can't get the docs by doc id, we check for their existence in
56 -- a match all query.
57 let query = MatchAllQuery Nothing
58 let search = mkSearch (Just query) Nothing
59 resp <- searchByIndex testIndex search
60 parsed <- parseEsResponse resp :: BH IO (Either EsError (SearchResult Value))
61 case parsed of
62 Left e ->
63 liftIO $ expectationFailure ("Expected a script-transformed result but got: " <> show e)
64 (Right sr) -> do
65 liftIO $
66 hitsTotal (searchHits sr) `shouldBe` 3
67 let nameList :: [Text]
68 nameList =
69 hits (searchHits sr)
70 ^.. traverse
71 . to hitSource
72 . _Just
73 . LMA.key "name"
74 . _String
75 liftIO $
76 nameList
77 `shouldBe` ["blah","bloo"]