2014-10-10 01:38:33 +04:00
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2014-12-11 02:09:54 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2014-06-15 21:56:47 +04:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2015-05-14 03:08:52 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2014-04-05 07:55:53 +04:00
|
|
|
module Main where
|
|
|
|
|
2014-10-10 01:38:33 +04:00
|
|
|
import Control.Applicative
|
2014-10-11 23:36:25 +04:00
|
|
|
import Control.Monad
|
2015-03-03 20:21:01 +03:00
|
|
|
import Control.Monad.Reader
|
2014-10-10 01:38:33 +04:00
|
|
|
import Data.Aeson
|
2015-05-14 03:08:52 +03:00
|
|
|
import qualified Data.HashMap.Strict as HM
|
|
|
|
import Data.List (nub)
|
|
|
|
import Data.List.NonEmpty (NonEmpty (..))
|
|
|
|
import qualified Data.List.NonEmpty as NE
|
|
|
|
import qualified Data.Map.Strict as M
|
|
|
|
import Data.Text (Text)
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Data.Time.Calendar (Day (..))
|
|
|
|
import Data.Time.Clock (UTCTime (..),
|
|
|
|
secondsToDiffTime)
|
|
|
|
import qualified Data.Vector as V
|
2014-10-10 01:38:33 +04:00
|
|
|
import Database.Bloodhound
|
2015-05-14 03:08:52 +03:00
|
|
|
import GHC.Generics (Generic)
|
2014-10-10 01:38:33 +04:00
|
|
|
import Network.HTTP.Client
|
2015-05-14 03:08:52 +03:00
|
|
|
import qualified Network.HTTP.Types.Status as NHTS
|
|
|
|
import Prelude hiding (filter, putStrLn)
|
2014-10-10 01:38:33 +04:00
|
|
|
import Test.Hspec
|
2015-05-14 03:08:52 +03:00
|
|
|
import Test.QuickCheck.Property.Monoid
|
2014-11-19 23:23:33 +03:00
|
|
|
|
2015-05-14 03:08:52 +03:00
|
|
|
import Test.Hspec.QuickCheck (prop)
|
2014-10-10 01:38:33 +04:00
|
|
|
import Test.QuickCheck
|
2014-10-07 17:41:54 +04:00
|
|
|
|
2014-04-27 09:49:44 +04:00
|
|
|
testServer :: Server
|
2014-04-11 01:39:10 +04:00
|
|
|
testServer = Server "http://localhost:9200"
|
2014-04-27 09:49:44 +04:00
|
|
|
testIndex :: IndexName
|
2014-05-15 19:43:46 +04:00
|
|
|
testIndex = IndexName "bloodhound-tests-twitter-1"
|
2014-04-27 09:49:44 +04:00
|
|
|
testMapping :: MappingName
|
2014-04-12 08:02:13 +04:00
|
|
|
testMapping = MappingName "tweet"
|
2014-04-11 04:54:40 +04:00
|
|
|
|
2015-03-17 01:26:50 +03:00
|
|
|
withTestEnv :: BH IO a -> IO a
|
2015-03-04 07:49:34 +03:00
|
|
|
withTestEnv = withBH defaultManagerSettings testServer
|
2015-03-03 20:21:01 +03:00
|
|
|
|
2014-04-27 09:49:44 +04:00
|
|
|
validateStatus :: Response body -> Int -> Expectation
|
2014-04-11 04:54:40 +04:00
|
|
|
validateStatus resp expected =
|
|
|
|
(NHTS.statusCode $ responseStatus resp)
|
|
|
|
`shouldBe` (expected :: Int)
|
|
|
|
|
2015-03-17 01:26:50 +03:00
|
|
|
createExampleIndex :: BH IO Reply
|
2015-03-03 20:21:01 +03:00
|
|
|
createExampleIndex = createIndex defaultIndexSettings testIndex
|
2015-03-17 01:26:50 +03:00
|
|
|
deleteExampleIndex :: BH IO Reply
|
2015-03-03 20:21:01 +03:00
|
|
|
deleteExampleIndex = deleteIndex testIndex
|
2014-04-07 03:29:46 +04:00
|
|
|
|
2014-10-11 21:48:18 +04:00
|
|
|
data ServerVersion = ServerVersion Int Int Int deriving (Show, Eq, Ord)
|
|
|
|
|
|
|
|
es14 :: ServerVersion
|
|
|
|
es14 = ServerVersion 1 4 0
|
|
|
|
|
|
|
|
es13 :: ServerVersion
|
|
|
|
es13 = ServerVersion 1 3 0
|
|
|
|
|
|
|
|
es12 :: ServerVersion
|
|
|
|
es12 = ServerVersion 1 2 0
|
|
|
|
|
|
|
|
es11 :: ServerVersion
|
|
|
|
es11 = ServerVersion 1 1 0
|
|
|
|
|
|
|
|
es10 :: ServerVersion
|
|
|
|
es10 = ServerVersion 1 0 0
|
|
|
|
|
|
|
|
serverBranch :: ServerVersion -> ServerVersion
|
2014-10-20 09:39:27 +04:00
|
|
|
serverBranch (ServerVersion majorVer minorVer patchVer) =
|
|
|
|
ServerVersion majorVer minorVer patchVer
|
2014-10-11 21:48:18 +04:00
|
|
|
|
|
|
|
mkServerVersion :: [Int] -> Maybe ServerVersion
|
2014-10-20 09:39:27 +04:00
|
|
|
mkServerVersion [majorVer, minorVer, patchVer] =
|
|
|
|
Just (ServerVersion majorVer minorVer patchVer)
|
2014-10-11 21:48:18 +04:00
|
|
|
mkServerVersion _ = Nothing
|
|
|
|
|
2015-03-04 07:49:34 +03:00
|
|
|
getServerVersion :: IO (Maybe ServerVersion)
|
2015-03-03 20:21:01 +03:00
|
|
|
getServerVersion = liftM extractVersion (withTestEnv getStatus)
|
2014-10-17 17:45:50 +04:00
|
|
|
where
|
2014-10-11 21:48:18 +04:00
|
|
|
version' = T.splitOn "." . number . version
|
|
|
|
toInt = read . T.unpack
|
|
|
|
parseVersion v = map toInt (version' v)
|
|
|
|
extractVersion = join . liftM (mkServerVersion . parseVersion)
|
2014-10-17 17:45:50 +04:00
|
|
|
|
2014-10-11 21:48:18 +04:00
|
|
|
testServerBranch :: IO (Maybe ServerVersion)
|
2015-03-03 20:21:01 +03:00
|
|
|
testServerBranch = getServerVersion >>= \v -> return $ liftM serverBranch v
|
2014-10-11 21:48:18 +04:00
|
|
|
|
|
|
|
atleast :: ServerVersion -> IO Bool
|
|
|
|
atleast v = testServerBranch >>= \x -> return $ x >= Just (serverBranch v)
|
|
|
|
|
|
|
|
atmost :: ServerVersion -> IO Bool
|
|
|
|
atmost v = testServerBranch >>= \x -> return $ x <= Just (serverBranch v)
|
|
|
|
|
|
|
|
is :: ServerVersion -> IO Bool
|
|
|
|
is v = testServerBranch >>= \x -> return $ x == Just (serverBranch v)
|
|
|
|
|
|
|
|
when' :: Monad m => m Bool -> m () -> m ()
|
|
|
|
when' b f = b >>= \x -> when x f
|
|
|
|
|
2014-04-11 01:39:10 +04:00
|
|
|
data Location = Location { lat :: Double
|
|
|
|
, lon :: Double } deriving (Eq, Generic, Show)
|
|
|
|
|
2014-04-10 09:25:17 +04:00
|
|
|
data Tweet = Tweet { user :: Text
|
2014-04-07 03:29:46 +04:00
|
|
|
, postDate :: UTCTime
|
2014-04-11 01:39:10 +04:00
|
|
|
, message :: Text
|
2014-04-11 09:48:29 +04:00
|
|
|
, age :: Int
|
2014-04-11 01:39:10 +04:00
|
|
|
, location :: Location }
|
2014-04-07 03:29:46 +04:00
|
|
|
deriving (Eq, Generic, Show)
|
|
|
|
|
2014-04-11 01:39:10 +04:00
|
|
|
instance ToJSON Tweet
|
2014-04-07 03:29:46 +04:00
|
|
|
instance FromJSON Tweet
|
2014-04-11 01:39:10 +04:00
|
|
|
instance ToJSON Location
|
|
|
|
instance FromJSON Location
|
|
|
|
|
|
|
|
data TweetMapping = TweetMapping deriving (Eq, Show)
|
|
|
|
|
|
|
|
instance ToJSON TweetMapping where
|
|
|
|
toJSON TweetMapping =
|
|
|
|
object ["tweet" .=
|
2014-06-15 06:27:27 +04:00
|
|
|
object ["properties" .=
|
|
|
|
object ["location" .= object ["type" .= ("geo_point" :: Text)]]]]
|
2014-04-07 03:29:46 +04:00
|
|
|
|
2014-04-27 09:49:44 +04:00
|
|
|
exampleTweet :: Tweet
|
2014-04-07 04:53:53 +04:00
|
|
|
exampleTweet = Tweet { user = "bitemyapp"
|
2014-04-07 07:51:30 +04:00
|
|
|
, postDate = UTCTime
|
|
|
|
(ModifiedJulianDay 55000)
|
2014-04-07 04:53:53 +04:00
|
|
|
(secondsToDiffTime 10)
|
2014-04-11 01:39:10 +04:00
|
|
|
, message = "Use haskell!"
|
2014-04-11 09:48:29 +04:00
|
|
|
, age = 10000
|
2014-04-11 01:39:10 +04:00
|
|
|
, location = Location 40.12 (-71.34) }
|
2014-04-05 07:55:53 +04:00
|
|
|
|
2014-04-27 09:49:44 +04:00
|
|
|
otherTweet :: Tweet
|
2014-04-15 12:10:47 +04:00
|
|
|
otherTweet = Tweet { user = "notmyapp"
|
|
|
|
, postDate = UTCTime
|
|
|
|
(ModifiedJulianDay 55000)
|
|
|
|
(secondsToDiffTime 11)
|
|
|
|
, message = "Use haskell!"
|
|
|
|
, age = 1000
|
|
|
|
, location = Location 40.12 (-71.34) }
|
|
|
|
|
2015-03-17 01:26:50 +03:00
|
|
|
insertData :: BH IO ()
|
2014-04-10 09:25:17 +04:00
|
|
|
insertData = do
|
|
|
|
_ <- deleteExampleIndex
|
2014-04-27 09:49:44 +04:00
|
|
|
_ <- createExampleIndex
|
2015-03-03 20:21:01 +03:00
|
|
|
_ <- putMapping testIndex testMapping TweetMapping
|
|
|
|
_ <- indexDocument testIndex testMapping exampleTweet (DocId "1")
|
|
|
|
_ <- refreshIndex testIndex
|
2014-04-10 09:25:17 +04:00
|
|
|
return ()
|
|
|
|
|
2015-03-17 01:26:50 +03:00
|
|
|
insertOther :: BH IO ()
|
2014-04-15 12:10:47 +04:00
|
|
|
insertOther = do
|
2015-03-03 20:21:01 +03:00
|
|
|
_ <- indexDocument testIndex testMapping otherTweet (DocId "2")
|
|
|
|
_ <- refreshIndex testIndex
|
2014-04-15 12:10:47 +04:00
|
|
|
return ()
|
|
|
|
|
2015-03-17 01:26:50 +03:00
|
|
|
searchTweet :: Search -> BH IO (Either String Tweet)
|
2014-04-11 01:39:10 +04:00
|
|
|
searchTweet search = do
|
2015-03-03 20:21:01 +03:00
|
|
|
reply <- searchByIndex testIndex search
|
2014-04-10 09:25:17 +04:00
|
|
|
let result = eitherDecode (responseBody reply) :: Either String (SearchResult Tweet)
|
|
|
|
let myTweet = fmap (hitSource . head . hits . searchHits) result
|
|
|
|
return myTweet
|
|
|
|
|
2015-03-17 01:26:50 +03:00
|
|
|
searchExpectNoResults :: Search -> BH IO ()
|
2014-04-11 04:15:10 +04:00
|
|
|
searchExpectNoResults search = do
|
2015-03-03 20:21:01 +03:00
|
|
|
reply <- searchByIndex testIndex search
|
2014-04-11 04:15:10 +04:00
|
|
|
let result = eitherDecode (responseBody reply) :: Either String (SearchResult Tweet)
|
|
|
|
let emptyHits = fmap (hits . searchHits) result
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
emptyHits `shouldBe` Right []
|
2014-04-11 04:15:10 +04:00
|
|
|
|
2015-03-17 01:26:50 +03:00
|
|
|
searchExpectAggs :: Search -> BH IO ()
|
2014-10-06 01:58:55 +04:00
|
|
|
searchExpectAggs search = do
|
2015-03-03 20:21:01 +03:00
|
|
|
reply <- searchAll search
|
2014-10-17 17:45:50 +04:00
|
|
|
let isEmpty x = return (M.null x)
|
2014-10-06 01:58:55 +04:00
|
|
|
let result = decode (responseBody reply) :: Maybe (SearchResult Tweet)
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
(result >>= aggregations >>= isEmpty) `shouldBe` Just False
|
2014-10-17 17:45:50 +04:00
|
|
|
|
2015-03-17 01:26:50 +03:00
|
|
|
searchValidBucketAgg :: (BucketAggregation a, FromJSON a, Show a) => Search -> Text -> (Text -> AggregationResults -> Maybe (Bucket a)) -> BH IO ()
|
2014-10-06 01:58:55 +04:00
|
|
|
searchValidBucketAgg search aggKey extractor = do
|
2015-03-03 20:21:01 +03:00
|
|
|
reply <- searchAll search
|
2014-10-06 01:58:55 +04:00
|
|
|
let bucketDocs = docCount . head . buckets
|
|
|
|
let result = decode (responseBody reply) :: Maybe (SearchResult Tweet)
|
|
|
|
let count = result >>= aggregations >>= extractor aggKey >>= \x -> return (bucketDocs x)
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
count `shouldBe` Just 1
|
2014-10-06 01:58:55 +04:00
|
|
|
|
2015-03-17 01:26:50 +03:00
|
|
|
searchTermsAggHint :: [ExecutionHint] -> BH IO ()
|
2014-10-11 21:48:18 +04:00
|
|
|
searchTermsAggHint hints = do
|
|
|
|
let terms hint = TermsAgg $ (mkTermsAggregation "user") { termExecutionHint = Just hint }
|
|
|
|
let search hint = mkAggregateSearch Nothing $ mkAggregations "users" $ terms hint
|
|
|
|
forM_ hints $ searchExpectAggs . search
|
|
|
|
forM_ hints (\x -> searchValidBucketAgg (search x) "users" toTerms)
|
|
|
|
|
2015-03-17 01:26:50 +03:00
|
|
|
searchTweetHighlight :: Search -> BH IO (Either String (Maybe HitHighlight))
|
2014-10-17 17:45:50 +04:00
|
|
|
searchTweetHighlight search = do
|
2015-03-03 20:21:01 +03:00
|
|
|
reply <- searchByIndex testIndex search
|
2014-10-17 17:45:50 +04:00
|
|
|
let result = eitherDecode (responseBody reply) :: Either String (SearchResult Tweet)
|
|
|
|
let myHighlight = fmap (hitHighlight . head . hits . searchHits) result
|
|
|
|
return myHighlight
|
|
|
|
|
2014-04-12 11:56:33 +04:00
|
|
|
data BulkTest = BulkTest { name :: Text } deriving (Eq, Generic, Show)
|
|
|
|
instance FromJSON BulkTest
|
|
|
|
instance ToJSON BulkTest
|
|
|
|
|
2014-06-15 21:56:47 +04:00
|
|
|
noDuplicates :: Eq a => [a] -> Bool
|
|
|
|
noDuplicates xs = nub xs == xs
|
|
|
|
|
|
|
|
instance Arbitrary RegexpFlags where
|
|
|
|
arbitrary = oneof [ pure AllRegexpFlags
|
|
|
|
, pure NoRegexpFlags
|
|
|
|
, SomeRegexpFlags <$> arbitrary
|
|
|
|
]
|
|
|
|
|
|
|
|
instance Arbitrary a => Arbitrary (NonEmpty a) where
|
|
|
|
arbitrary = liftA2 (:|) arbitrary arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary RegexpFlag where
|
|
|
|
arbitrary = oneof [ pure AnyString
|
|
|
|
, pure Automaton
|
|
|
|
, pure Complement
|
|
|
|
, pure Empty
|
|
|
|
, pure Intersection
|
|
|
|
, pure Interval
|
|
|
|
]
|
|
|
|
|
2015-05-14 03:08:52 +03:00
|
|
|
arbitraryScore :: Gen Score
|
|
|
|
arbitraryScore = fmap getPositive <$> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary Text where
|
|
|
|
arbitrary = T.pack <$> arbitrary
|
|
|
|
|
|
|
|
instance (Arbitrary k, Arbitrary v, Ord k) => Arbitrary (M.Map k v) where
|
|
|
|
arbitrary = M.fromList <$> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary IndexName where
|
|
|
|
arbitrary = IndexName <$> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary MappingName where
|
|
|
|
arbitrary = MappingName <$> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary DocId where
|
|
|
|
arbitrary = DocId <$> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary a => Arbitrary (Hit a) where
|
|
|
|
arbitrary = Hit <$> arbitrary
|
|
|
|
<*> arbitrary
|
|
|
|
<*> arbitrary
|
|
|
|
<*> arbitraryScore
|
|
|
|
<*> arbitrary
|
|
|
|
<*> arbitrary
|
|
|
|
|
|
|
|
|
|
|
|
instance Arbitrary a => Arbitrary (SearchHits a) where
|
|
|
|
arbitrary = do
|
|
|
|
tot <- getPositive <$> arbitrary
|
|
|
|
score <- arbitraryScore
|
|
|
|
hs <- arbitrary
|
|
|
|
return $ SearchHits tot score hs
|
|
|
|
|
2014-04-05 07:55:53 +04:00
|
|
|
main :: IO ()
|
|
|
|
main = hspec $ do
|
2014-04-11 04:54:40 +04:00
|
|
|
|
2014-04-10 10:41:21 +04:00
|
|
|
describe "index create/delete API" $ do
|
2015-03-03 20:21:01 +03:00
|
|
|
it "creates and then deletes the requested index" $ withTestEnv $ do
|
2014-04-10 10:41:21 +04:00
|
|
|
-- priming state.
|
|
|
|
_ <- deleteExampleIndex
|
|
|
|
resp <- createExampleIndex
|
|
|
|
deleteResp <- deleteExampleIndex
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $ do
|
|
|
|
validateStatus resp 200
|
|
|
|
validateStatus deleteResp 200
|
2014-04-10 09:25:17 +04:00
|
|
|
|
2014-04-27 09:49:44 +04:00
|
|
|
|
2014-04-07 03:29:46 +04:00
|
|
|
describe "document API" $ do
|
2015-03-03 20:21:01 +03:00
|
|
|
it "indexes, gets, and then deletes the generated document" $ withTestEnv $ do
|
2014-04-10 10:41:21 +04:00
|
|
|
_ <- insertData
|
2015-03-03 20:21:01 +03:00
|
|
|
docInserted <- getDocument testIndex testMapping (DocId "1")
|
2014-04-11 04:54:40 +04:00
|
|
|
let newTweet = eitherDecode
|
|
|
|
(responseBody docInserted) :: Either String (EsResult Tweet)
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $ (fmap _source newTweet `shouldBe` Right exampleTweet)
|
2014-04-10 09:25:17 +04:00
|
|
|
|
2014-04-27 09:49:44 +04:00
|
|
|
|
2014-04-12 11:56:33 +04:00
|
|
|
describe "bulk API" $ do
|
2015-03-03 20:21:01 +03:00
|
|
|
it "inserts all documents we request" $ withTestEnv $ do
|
2014-04-12 11:56:33 +04:00
|
|
|
_ <- insertData
|
|
|
|
let firstTest = BulkTest "blah"
|
|
|
|
let secondTest = BulkTest "bloo"
|
2014-05-15 19:43:46 +04:00
|
|
|
let firstDoc = BulkIndex testIndex
|
2014-09-19 04:20:03 +04:00
|
|
|
testMapping (DocId "2") (toJSON firstTest)
|
2014-05-15 19:43:46 +04:00
|
|
|
let secondDoc = BulkCreate testIndex
|
2014-09-19 04:20:03 +04:00
|
|
|
testMapping (DocId "3") (toJSON secondTest)
|
2014-11-08 10:53:48 +03:00
|
|
|
let stream = V.fromList [firstDoc, secondDoc]
|
2015-03-03 20:21:01 +03:00
|
|
|
_ <- bulk stream
|
|
|
|
_ <- refreshIndex testIndex
|
|
|
|
fDoc <- getDocument testIndex testMapping (DocId "2")
|
|
|
|
sDoc <- getDocument testIndex testMapping (DocId "3")
|
2014-04-12 11:56:33 +04:00
|
|
|
let maybeFirst = eitherDecode $ responseBody fDoc :: Either String (EsResult BulkTest)
|
|
|
|
let maybeSecond = eitherDecode $ responseBody sDoc :: Either String (EsResult BulkTest)
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $ do
|
|
|
|
fmap _source maybeFirst `shouldBe` Right firstTest
|
|
|
|
fmap _source maybeSecond `shouldBe` Right secondTest
|
2014-04-12 11:56:33 +04:00
|
|
|
|
2014-04-27 09:49:44 +04:00
|
|
|
|
2014-04-12 06:58:38 +04:00
|
|
|
describe "query API" $ do
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for term query and identity filter" $ withTestEnv $ do
|
2014-04-12 06:58:38 +04:00
|
|
|
_ <- insertData
|
|
|
|
let query = TermQuery (Term "user" "bitemyapp") Nothing
|
|
|
|
let filter = IdentityFilter <&&> IdentityFilter
|
|
|
|
let search = mkSearch (Just query) (Just filter)
|
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-12 06:58:38 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for terms query and identity filter" $ withTestEnv $ do
|
2014-12-12 09:28:32 +03:00
|
|
|
_ <- insertData
|
|
|
|
let query = TermsQuery (NE.fromList [(Term "user" "bitemyapp")])
|
|
|
|
let filter = IdentityFilter <&&> IdentityFilter
|
|
|
|
let search = mkSearch (Just query) (Just filter)
|
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-12-12 09:28:32 +03:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for match query" $ withTestEnv $ do
|
2014-04-27 09:49:44 +04:00
|
|
|
_ <- insertData
|
|
|
|
let query = QueryMatchQuery $ mkMatchQuery (FieldName "user") (QueryString "bitemyapp")
|
|
|
|
let search = mkSearch (Just query) Nothing
|
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-27 09:49:44 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for multi-match query" $ withTestEnv $ do
|
2014-04-27 13:57:34 +04:00
|
|
|
_ <- insertData
|
|
|
|
let fields = [FieldName "user", FieldName "message"]
|
|
|
|
let query = QueryMultiMatchQuery $ mkMultiMatchQuery fields (QueryString "bitemyapp")
|
|
|
|
let search = mkSearch (Just query) Nothing
|
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-27 13:57:34 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for bool query" $ withTestEnv $ do
|
2014-04-27 15:00:08 +04:00
|
|
|
_ <- insertData
|
|
|
|
let innerQuery = QueryMatchQuery $
|
|
|
|
mkMatchQuery (FieldName "user") (QueryString "bitemyapp")
|
|
|
|
let query = QueryBoolQuery $
|
2014-10-10 01:38:33 +04:00
|
|
|
mkBoolQuery [innerQuery] [] []
|
2014-04-27 15:00:08 +04:00
|
|
|
let search = mkSearch (Just query) Nothing
|
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-27 15:00:08 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for boosting query" $ withTestEnv $ do
|
2014-04-27 16:22:12 +04:00
|
|
|
_ <- insertData
|
|
|
|
let posQuery = QueryMatchQuery $ mkMatchQuery (FieldName "user") (QueryString "bitemyapp")
|
|
|
|
let negQuery = QueryMatchQuery $ mkMatchQuery (FieldName "user") (QueryString "notmyapp")
|
|
|
|
let query = QueryBoostingQuery $ BoostingQuery posQuery negQuery (Boost 0.2)
|
|
|
|
let search = mkSearch (Just query) Nothing
|
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-27 16:22:12 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for common terms query" $ withTestEnv $ do
|
2014-04-27 22:25:50 +04:00
|
|
|
_ <- insertData
|
|
|
|
let query = QueryCommonTermsQuery $
|
|
|
|
CommonTermsQuery (FieldName "user")
|
|
|
|
(QueryString "bitemyapp")
|
|
|
|
(CutoffFrequency 0.0001)
|
|
|
|
Or Or Nothing Nothing Nothing Nothing
|
|
|
|
let search = mkSearch (Just query) Nothing
|
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-27 22:25:50 +04:00
|
|
|
|
|
|
|
|
2014-04-15 12:10:47 +04:00
|
|
|
describe "sorting" $ do
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns documents in the right order" $ withTestEnv $ do
|
2014-04-15 12:10:47 +04:00
|
|
|
_ <- insertData
|
|
|
|
_ <- insertOther
|
|
|
|
let sortSpec = DefaultSortSpec $ mkSort (FieldName "age") Ascending
|
|
|
|
let search = Search Nothing
|
2014-10-17 17:45:50 +04:00
|
|
|
(Just IdentityFilter) (Just [sortSpec]) Nothing Nothing
|
2015-05-12 07:55:33 +03:00
|
|
|
False (From 0) (Size 10)
|
2015-03-03 20:21:01 +03:00
|
|
|
reply <- searchByIndex testIndex search
|
2014-04-15 12:10:47 +04:00
|
|
|
let result = eitherDecode (responseBody reply) :: Either String (SearchResult Tweet)
|
|
|
|
let myTweet = fmap (hitSource . head . hits . searchHits) result
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right otherTweet
|
2014-04-15 12:10:47 +04:00
|
|
|
|
2014-04-27 09:49:44 +04:00
|
|
|
|
2014-04-11 01:39:10 +04:00
|
|
|
describe "filtering API" $ do
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for composed boolmatch and identity" $ withTestEnv $ do
|
2014-04-11 01:39:10 +04:00
|
|
|
_ <- insertData
|
|
|
|
let queryFilter = BoolFilter (MustMatch (Term "user" "bitemyapp") False)
|
|
|
|
<&&> IdentityFilter
|
2014-04-12 03:29:10 +04:00
|
|
|
let search = mkSearch Nothing (Just queryFilter)
|
2014-04-11 01:39:10 +04:00
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-11 01:39:10 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for term filter" $ withTestEnv $ do
|
2014-07-01 19:01:32 +04:00
|
|
|
_ <- insertData
|
|
|
|
let termFilter = TermFilter (Term "user" "bitemyapp") False
|
|
|
|
let search = mkSearch Nothing (Just termFilter)
|
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-07-01 19:01:32 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for existential filter" $ withTestEnv $ do
|
2014-04-11 01:39:10 +04:00
|
|
|
_ <- insertData
|
2014-04-12 03:29:10 +04:00
|
|
|
let search = mkSearch Nothing (Just (ExistsFilter (FieldName "user")))
|
2014-04-11 01:39:10 +04:00
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-11 01:39:10 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for geo boundingbox filter" $ withTestEnv $ do
|
2014-04-10 09:25:17 +04:00
|
|
|
_ <- insertData
|
2014-04-11 01:39:10 +04:00
|
|
|
let box = GeoBoundingBox (LatLon 40.73 (-74.1)) (LatLon 40.10 (-71.12))
|
2014-08-15 22:39:09 +04:00
|
|
|
let bbConstraint = GeoBoundingBoxConstraint (FieldName "tweet.location") box False GeoFilterMemory
|
|
|
|
let geoFilter = GeoBoundingBoxFilter bbConstraint
|
2014-04-12 03:29:10 +04:00
|
|
|
let search = mkSearch Nothing (Just geoFilter)
|
2014-04-11 01:39:10 +04:00
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-11 01:49:15 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "doesn't return document for nonsensical boundingbox filter" $ withTestEnv $ do
|
2014-04-11 01:49:15 +04:00
|
|
|
_ <- insertData
|
|
|
|
let box = GeoBoundingBox (LatLon 0.73 (-4.1)) (LatLon 0.10 (-1.12))
|
2014-08-15 22:39:09 +04:00
|
|
|
let bbConstraint = GeoBoundingBoxConstraint (FieldName "tweet.location") box False GeoFilterMemory
|
|
|
|
let geoFilter = GeoBoundingBoxFilter bbConstraint
|
2014-04-12 03:29:10 +04:00
|
|
|
let search = mkSearch Nothing (Just geoFilter)
|
2014-04-11 04:15:10 +04:00
|
|
|
searchExpectNoResults search
|
2014-04-11 04:01:42 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for geo distance filter" $ withTestEnv $ do
|
2014-04-11 04:01:42 +04:00
|
|
|
_ <- insertData
|
2014-04-12 03:29:10 +04:00
|
|
|
let geoPoint = GeoPoint (FieldName "tweet.location") (LatLon 40.12 (-71.34))
|
2014-04-11 04:01:42 +04:00
|
|
|
let distance = Distance 10.0 Miles
|
|
|
|
let optimizeBbox = OptimizeGeoFilterType GeoFilterMemory
|
2014-04-11 04:15:10 +04:00
|
|
|
let geoFilter = GeoDistanceFilter geoPoint distance SloppyArc optimizeBbox False
|
2014-04-12 03:29:10 +04:00
|
|
|
let search = mkSearch Nothing (Just geoFilter)
|
2014-04-11 04:01:42 +04:00
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-11 04:15:10 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for geo distance range filter" $ withTestEnv $ do
|
2014-04-11 04:15:10 +04:00
|
|
|
_ <- insertData
|
2014-04-12 03:29:10 +04:00
|
|
|
let geoPoint = GeoPoint (FieldName "tweet.location") (LatLon 40.12 (-71.34))
|
2014-04-11 04:15:10 +04:00
|
|
|
let distanceRange = DistanceRange (Distance 0.0 Miles) (Distance 10.0 Miles)
|
|
|
|
let geoFilter = GeoDistanceRangeFilter geoPoint distanceRange
|
2014-04-12 03:29:10 +04:00
|
|
|
let search = mkSearch Nothing (Just geoFilter)
|
2014-04-11 04:15:10 +04:00
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-11 04:15:10 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "doesn't return document for wild geo distance range filter" $ withTestEnv $ do
|
2014-04-11 04:15:10 +04:00
|
|
|
_ <- insertData
|
2014-04-12 03:29:10 +04:00
|
|
|
let geoPoint = GeoPoint (FieldName "tweet.location") (LatLon 40.12 (-71.34))
|
2014-04-11 04:15:10 +04:00
|
|
|
let distanceRange = DistanceRange (Distance 100.0 Miles) (Distance 1000.0 Miles)
|
|
|
|
let geoFilter = GeoDistanceRangeFilter geoPoint distanceRange
|
2014-04-12 03:29:10 +04:00
|
|
|
let search = mkSearch Nothing (Just geoFilter)
|
2014-04-11 04:15:10 +04:00
|
|
|
searchExpectNoResults search
|
2014-04-11 04:54:40 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for geo polygon filter" $ withTestEnv $ do
|
2014-04-11 04:54:40 +04:00
|
|
|
_ <- insertData
|
|
|
|
let points = [LatLon 40.0 (-70.00),
|
|
|
|
LatLon 40.0 (-72.00),
|
|
|
|
LatLon 41.0 (-70.00),
|
|
|
|
LatLon 41.0 (-72.00)]
|
2014-04-12 03:29:10 +04:00
|
|
|
let geoFilter = GeoPolygonFilter (FieldName "tweet.location") points
|
|
|
|
let search = mkSearch Nothing (Just geoFilter)
|
2014-04-11 04:54:40 +04:00
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-11 04:54:40 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "doesn't return document for bad geo polygon filter" $ withTestEnv $ do
|
2014-04-11 04:54:40 +04:00
|
|
|
_ <- insertData
|
|
|
|
let points = [LatLon 40.0 (-70.00),
|
|
|
|
LatLon 40.0 (-71.00),
|
|
|
|
LatLon 41.0 (-70.00),
|
|
|
|
LatLon 41.0 (-71.00)]
|
2014-04-12 03:29:10 +04:00
|
|
|
let geoFilter = GeoPolygonFilter (FieldName "tweet.location") points
|
|
|
|
let search = mkSearch Nothing (Just geoFilter)
|
2014-04-11 04:54:40 +04:00
|
|
|
searchExpectNoResults search
|
2014-04-11 05:04:24 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for ids filter" $ withTestEnv $ do
|
2014-04-11 05:04:24 +04:00
|
|
|
_ <- insertData
|
2014-04-12 08:02:13 +04:00
|
|
|
let filter = IdsFilter (MappingName "tweet") [DocId "1"]
|
2014-04-12 03:29:10 +04:00
|
|
|
let search = mkSearch Nothing (Just filter)
|
2014-04-11 05:04:24 +04:00
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-11 09:48:29 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for Double range filter" $ withTestEnv $ do
|
2014-04-11 09:48:29 +04:00
|
|
|
_ <- insertData
|
2014-04-12 03:29:10 +04:00
|
|
|
let filter = RangeFilter (FieldName "age")
|
2015-02-08 06:28:32 +03:00
|
|
|
(RangeDoubleGtLt (GreaterThan 1000.0) (LessThan 100000.0))
|
2014-04-11 09:48:29 +04:00
|
|
|
RangeExecutionIndex False
|
2014-04-12 03:29:10 +04:00
|
|
|
let search = mkSearch Nothing (Just filter)
|
2014-04-11 09:48:29 +04:00
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-11 10:08:36 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for UTCTime date filter" $ withTestEnv $ do
|
2015-02-05 11:29:05 +03:00
|
|
|
_ <- insertData
|
2015-02-08 00:56:41 +03:00
|
|
|
let filter = RangeFilter (FieldName "postDate")
|
2015-02-08 05:14:26 +03:00
|
|
|
(RangeDateGtLt
|
2015-02-08 06:28:32 +03:00
|
|
|
(GreaterThanD (UTCTime
|
|
|
|
(ModifiedJulianDay 55000)
|
|
|
|
(secondsToDiffTime 9)))
|
|
|
|
(LessThanD (UTCTime
|
|
|
|
(ModifiedJulianDay 55000)
|
|
|
|
(secondsToDiffTime 11))))
|
2015-02-05 11:29:05 +03:00
|
|
|
RangeExecutionIndex False
|
|
|
|
let search = mkSearch Nothing (Just filter)
|
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2015-02-05 11:29:05 +03:00
|
|
|
|
2015-02-08 05:14:26 +03:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns document for regexp filter" $ withTestEnv $ do
|
2014-04-11 10:08:36 +04:00
|
|
|
_ <- insertData
|
2014-04-12 03:29:10 +04:00
|
|
|
let filter = RegexpFilter (FieldName "user") (Regexp "bite.*app")
|
2014-06-15 21:56:47 +04:00
|
|
|
AllRegexpFlags (CacheName "test") False (CacheKey "key")
|
2014-04-12 03:29:10 +04:00
|
|
|
let search = mkSearch Nothing (Just filter)
|
2014-04-11 10:08:36 +04:00
|
|
|
myTweet <- searchTweet search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myTweet `shouldBe` Right exampleTweet
|
2014-04-11 10:10:06 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "doesn't return document for non-matching regexp filter" $ withTestEnv $ do
|
2014-04-11 10:10:06 +04:00
|
|
|
_ <- insertData
|
2014-04-12 03:29:10 +04:00
|
|
|
let filter = RegexpFilter (FieldName "user")
|
2014-06-15 21:56:47 +04:00
|
|
|
(Regexp "boy") AllRegexpFlags
|
2014-04-12 03:29:10 +04:00
|
|
|
(CacheName "test") False (CacheKey "key")
|
|
|
|
let search = mkSearch Nothing (Just filter)
|
2014-04-11 10:10:06 +04:00
|
|
|
searchExpectNoResults search
|
2014-08-15 23:41:03 +04:00
|
|
|
|
2014-10-06 01:58:55 +04:00
|
|
|
describe "Aggregation API" $ do
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns term aggregation results" $ withTestEnv $ do
|
2014-10-06 01:58:55 +04:00
|
|
|
_ <- insertData
|
2014-10-11 21:48:18 +04:00
|
|
|
let terms = TermsAgg $ mkTermsAggregation "user"
|
|
|
|
let search = mkAggregateSearch Nothing $ mkAggregations "users" terms
|
2014-10-06 01:58:55 +04:00
|
|
|
searchExpectAggs search
|
|
|
|
searchValidBucketAgg search "users" toTerms
|
2014-10-11 21:48:18 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "can give collection hint parameters to term aggregations" $ when' (atleast es13) $ withTestEnv $ do
|
2014-10-11 21:48:18 +04:00
|
|
|
_ <- insertData
|
|
|
|
let terms = TermsAgg $ (mkTermsAggregation "user") { termCollectMode = Just BreadthFirst }
|
|
|
|
let search = mkAggregateSearch Nothing $ mkAggregations "users" terms
|
|
|
|
searchExpectAggs search
|
|
|
|
searchValidBucketAgg search "users" toTerms
|
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "can give execution hint paramters to term aggregations" $ when' (atmost es11) $ withTestEnv $ do
|
2014-10-11 21:48:18 +04:00
|
|
|
_ <- insertData
|
|
|
|
searchTermsAggHint [Map, Ordinals]
|
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "can give execution hint paramters to term aggregations" $ when' (is es12) $ withTestEnv $ do
|
2014-10-11 21:48:18 +04:00
|
|
|
_ <- insertData
|
|
|
|
searchTermsAggHint [GlobalOrdinals, GlobalOrdinalsHash, GlobalOrdinalsLowCardinality, Map, Ordinals]
|
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "can give execution hint paramters to term aggregations" $ when' (atleast es12) $ withTestEnv $ do
|
2014-10-11 21:48:18 +04:00
|
|
|
_ <- insertData
|
|
|
|
searchTermsAggHint [GlobalOrdinals, GlobalOrdinalsHash, GlobalOrdinalsLowCardinality, Map]
|
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns date histogram aggregation results" $ withTestEnv $ do
|
2014-10-06 01:58:55 +04:00
|
|
|
_ <- insertData
|
2014-10-11 21:48:18 +04:00
|
|
|
let histogram = DateHistogramAgg $ mkDateHistogram (FieldName "postDate") Minute
|
2014-10-07 17:41:54 +04:00
|
|
|
let search = mkAggregateSearch Nothing (mkAggregations "byDate" histogram)
|
2014-10-06 01:58:55 +04:00
|
|
|
searchExpectAggs search
|
|
|
|
searchValidBucketAgg search "byDate" toDateHistogram
|
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns date histogram using fractional date" $ withTestEnv $ do
|
2014-10-07 17:41:54 +04:00
|
|
|
_ <- insertData
|
2014-10-11 21:48:18 +04:00
|
|
|
let periods = [Year, Quarter, Month, Week, Day, Hour, Minute, Second]
|
|
|
|
let fractionals = map (FractionalInterval 1.5) [Weeks, Days, Hours, Minutes, Seconds]
|
|
|
|
let intervals = periods ++ fractionals
|
|
|
|
let histogram = mkDateHistogram (FieldName "postDate")
|
|
|
|
let search interval = mkAggregateSearch Nothing $ mkAggregations "byDate" $ DateHistogramAgg (histogram interval)
|
2014-10-07 17:41:54 +04:00
|
|
|
let expect interval = searchExpectAggs (search interval)
|
|
|
|
let valid interval = searchValidBucketAgg (search interval) "byDate" toDateHistogram
|
|
|
|
forM_ intervals expect
|
|
|
|
forM_ intervals valid
|
2014-08-15 23:41:03 +04:00
|
|
|
|
2014-10-17 17:45:50 +04:00
|
|
|
describe "Highlights API" $ do
|
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "returns highlight from query when there should be one" $ withTestEnv $ do
|
2014-10-17 17:45:50 +04:00
|
|
|
_ <- insertData
|
|
|
|
_ <- insertOther
|
|
|
|
let query = QueryMatchQuery $ mkMatchQuery (FieldName "_all") (QueryString "haskell")
|
2014-12-10 23:17:14 +03:00
|
|
|
let testHighlight = Highlights Nothing [FieldHighlight (FieldName "message") Nothing]
|
2014-10-17 17:45:50 +04:00
|
|
|
|
2014-12-10 23:17:14 +03:00
|
|
|
let search = mkHighlightSearch (Just query) testHighlight
|
2014-10-17 17:45:50 +04:00
|
|
|
myHighlight <- searchTweetHighlight search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myHighlight `shouldBe` Right (Just (M.fromList [("message",["Use <em>haskell</em>!"])]))
|
2014-10-18 22:51:11 +04:00
|
|
|
|
2015-03-03 20:21:01 +03:00
|
|
|
it "doesn't return highlight from a query when it shouldn't" $ withTestEnv $ do
|
2014-10-18 22:51:11 +04:00
|
|
|
_ <- insertData
|
|
|
|
_ <- insertOther
|
|
|
|
let query = QueryMatchQuery $ mkMatchQuery (FieldName "_all") (QueryString "haskell")
|
2014-12-10 23:17:14 +03:00
|
|
|
let testHighlight = Highlights Nothing [FieldHighlight (FieldName "user") Nothing]
|
2014-10-18 22:51:11 +04:00
|
|
|
|
2014-12-10 23:17:14 +03:00
|
|
|
let search = mkHighlightSearch (Just query) testHighlight
|
2014-10-18 22:51:11 +04:00
|
|
|
myHighlight <- searchTweetHighlight search
|
2015-03-03 20:21:01 +03:00
|
|
|
liftIO $
|
|
|
|
myHighlight `shouldBe` Right Nothing
|
2014-10-18 22:51:11 +04:00
|
|
|
|
|
|
|
|
2014-10-17 17:45:50 +04:00
|
|
|
|
2014-06-15 21:56:47 +04:00
|
|
|
describe "ToJSON RegexpFlags" $ do
|
|
|
|
it "generates the correct JSON for AllRegexpFlags" $
|
|
|
|
toJSON AllRegexpFlags `shouldBe` String "ALL"
|
|
|
|
|
|
|
|
it "generates the correct JSON for NoRegexpFlags" $
|
|
|
|
toJSON NoRegexpFlags `shouldBe` String "NONE"
|
|
|
|
|
|
|
|
it "generates the correct JSON for SomeRegexpFlags" $
|
|
|
|
let flags = AnyString :| [ Automaton
|
|
|
|
, Complement
|
|
|
|
, Empty
|
|
|
|
, Intersection
|
|
|
|
, Interval ]
|
|
|
|
in toJSON (SomeRegexpFlags flags) `shouldBe` String "ANYSTRING|AUTOMATON|COMPLEMENT|EMPTY|INTERSECTION|INTERVAL"
|
|
|
|
|
|
|
|
prop "removes duplicates from flags" $ \(flags :: RegexpFlags) ->
|
|
|
|
let String str = toJSON flags
|
|
|
|
flagStrs = T.splitOn "|" str
|
|
|
|
in noDuplicates flagStrs
|
2014-10-06 01:58:55 +04:00
|
|
|
|
2014-10-10 03:01:04 +04:00
|
|
|
describe "omitNulls" $ do
|
|
|
|
it "checks that omitNulls drops list elements when it should" $
|
|
|
|
let dropped = omitNulls $ [ "test1" .= (toJSON ([] :: [Int]))
|
|
|
|
, "test2" .= (toJSON ("some value" :: Text))]
|
2014-12-12 09:28:32 +03:00
|
|
|
in dropped `shouldBe` Object (HM.fromList [("test2", String "some value")])
|
2014-10-10 03:01:04 +04:00
|
|
|
|
|
|
|
it "checks that omitNulls doesn't drop list elements when it shouldn't" $
|
|
|
|
let notDropped = omitNulls $ [ "test1" .= (toJSON ([1] :: [Int]))
|
|
|
|
, "test2" .= (toJSON ("some value" :: Text))]
|
2014-12-12 09:28:32 +03:00
|
|
|
in notDropped `shouldBe` Object (HM.fromList [ ("test1", Array (V.fromList [Number 1.0]))
|
2014-10-10 03:01:04 +04:00
|
|
|
, ("test2", String "some value")])
|
|
|
|
it "checks that omitNulls drops non list elements when it should" $
|
|
|
|
let dropped = omitNulls $ [ "test1" .= (toJSON Null)
|
|
|
|
, "test2" .= (toJSON ("some value" :: Text))]
|
2014-12-12 09:28:32 +03:00
|
|
|
in dropped `shouldBe` Object (HM.fromList [("test2", String "some value")])
|
2014-10-10 03:01:04 +04:00
|
|
|
it "checks that omitNulls doesn't drop non list elements when it shouldn't" $
|
|
|
|
let notDropped = omitNulls $ [ "test1" .= (toJSON (1 :: Int))
|
|
|
|
, "test2" .= (toJSON ("some value" :: Text))]
|
2014-12-12 09:28:32 +03:00
|
|
|
in notDropped `shouldBe` Object (HM.fromList [ ("test1", Number 1.0)
|
2015-05-14 03:08:52 +03:00
|
|
|
, ("test2", String "some value")])
|
|
|
|
describe "Monoid (SearchHits a)" $ do
|
|
|
|
prop "abides the monoid laws" $ eq $
|
|
|
|
prop_Monoid (T :: T (SearchHits ()))
|