git.iko.soy/typeable/bloodhound
on master help sign in
tests passing
commit891eee61a3ddb133377910b18508edd7bdc1e11e authorChris Allen <cma@bitemyapp.com> dateFri 16 Oct 2015, 15:05 (11 years ago) parent225f13a
3 files changed+23−16
bloodhound.cabal−1 src/Database/Bloodhound/Types.hs+6−7 tests/tests.hs+17−8
bloodhound.cabal−1
@@ -32,7 +32,6 @@ library
3232 bytestring >= 0.10.0 && <0.11,
3333 containers >= 0.5.0.0 && <0.6,
3434 aeson >= 0.7 && <0.11,
35− aeson-extra >= 0.1 && <0.2,
3635 http-client >= 0.3 && <0.5,
3736 network-uri >= 2.6 && <2.7,
3837 semigroups >= 0.15 && <0.18,
src/Database/Bloodhound/Types.hs+6−7
@@ -236,8 +236,7 @@ import Control.Monad.Except
236236import Control.Monad.Reader
237237import Control.Monad.State
238238import Control.Monad.Writer
239−import Data.Aeson hiding ((.:?))
240−import Data.Aeson.Compat ((.:?))
239+import Data.Aeson
241240import Data.Aeson.Types (Pair, emptyObject, parseMaybe)
242241import qualified Data.ByteString.Lazy.Char8 as L
243242import qualified Data.HashMap.Strict as HM (union)
@@ -2409,11 +2408,11 @@ instance (FromJSON a) => FromJSON (SearchHits a) where
24092408
24102409instance (FromJSON a) => FromJSON (Hit a) where
24112410 parseJSON (Object v) = Hit <$>
2412− v .: "_index" <*>
2413− v .: "_type" <*>
2414− v .: "_id" <*>
2415− v .: "_score" <*>
2416− v .:? "_source" <*>
2411+ v .: "_index" <*>
2412+ v .: "_type" <*>
2413+ v .: "_id" <*>
2414+ v .: "_score" <*>
2415+ v .:? "_source" <*>
24172416 v .:? "highlight"
24182417 parseJSON _ = empty
24192418
tests/tests.hs+17−8
@@ -14,6 +14,7 @@ import Data.List (nub)
1414import Data.List.NonEmpty (NonEmpty (..))
1515import qualified Data.List.NonEmpty as NE
1616import qualified Data.Map.Strict as M
17+import qualified Data.Maybe as MY
1718import Data.Monoid
1819import Data.Text (Text)
1920import qualified Data.Text as T
@@ -27,7 +28,7 @@ import Network.HTTP.Client
2728import qualified Network.HTTP.Types.Status as NHTS
2829import Prelude hiding (filter)
2930import Test.Hspec
30−import Test.QuickCheck.Property.Monoid (prop_Monoid, eq, T(..))
31+import Test.QuickCheck.Property.Monoid (T (..), eq, prop_Monoid)
3132
3233import Test.Hspec.QuickCheck (prop)
3334import Test.QuickCheck
@@ -196,8 +197,9 @@ insertWithSpaceInId = do
196197searchTweet :: Search -> BH IO (Either String Tweet)
197198searchTweet search = do
198199 result <- searchTweets search
199− let myTweet = fmap (hitSource . head . hits . searchHits) result
200− return (either (Left "myTweet was Nothing") id myTweet)
200+ let myTweet :: Either String Tweet
201+ myTweet = grabFirst result
202+ return myTweet
201203
202204searchTweets :: Search -> BH IO (Either String (SearchResult Tweet))
203205searchTweets search = eitherDecode . responseBody <$> searchByIndex testIndex search
@@ -247,8 +249,7 @@ searchExpectSource src expected = do
247249 let search = (mkSearch (Just query) Nothing) { source = Just src }
248250 reply <- searchAll search
249251 let result = eitherDecode (responseBody reply) :: Either String (SearchResult Value)
250− let value = fmap (hitSource . head . hits . searchHits) result
251− liftIO (print value)
252+ let value = grabFirst result
252253 liftIO $
253254 value `shouldBe` expected
254255
@@ -314,6 +315,13 @@ instance Arbitrary a => Arbitrary (SearchHits a) where
314315getSource :: EsResult a -> Maybe a
315316getSource = fmap _source . foundResult
316317
318+-- grabFirst :: Either String (SearchResult a) -> Either String a
319+grabFirst r =
320+ case fmap (hitSource . head . hits . searchHits) r of
321+ (Left e) -> Left e
322+ (Right Nothing) -> Left "Source was missing"
323+ (Right (Just x)) -> Right x
324+
317325main :: IO ()
318326main = hspec $ do
319327
@@ -488,7 +496,7 @@ main = hspec $ do
488496 (Just IdentityFilter) (Just [sortSpec]) Nothing Nothing
489497 False (From 0) (Size 10) SearchTypeQueryThenFetch Nothing Nothing
490498 result <- searchTweets search
491− let myTweet = fmap (hitSource . head . hits . searchHits) result
499+ let myTweet = grabFirst result
492500 liftIO $
493501 myTweet `shouldBe` Right otherTweet
494502
@@ -705,6 +713,7 @@ main = hspec $ do
705713 fmap aggregations res `shouldBe` Right (Just (M.fromList [ docCountPair "bitemyapps" 1
706714 , docCountPair "notmyapps" 1
707715 ]))
716+
708717 -- Interaction of date serialization and date histogram aggregation is broken.
709718 -- it "returns date histogram aggregation results" $ withTestEnv $ do
710719 -- _ <- insertData
@@ -754,7 +763,7 @@ main = hspec $ do
754763 it "doesn't include source when sources are disabled" $ withTestEnv $ do
755764 searchExpectSource
756765 NoSource
757− (Left "key \"_source\" not present")
766+ (Left "Source was missing")
758767
759768 it "includes a source" $ withTestEnv $ do
760769 searchExpectSource
@@ -850,4 +859,4 @@ main = hspec $ do
850859 liftIO $
851860 regular_search `shouldBe` Right exampleTweet -- Check that the size restrtiction is being honored
852861 liftIO $
853− scan_search `shouldMatchList` [exampleTweet, otherTweet]
862+ scan_search `shouldMatchList` [Just exampleTweet, Just otherTweet]