@@ -1,17 +1,24 @@
1{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE DeriveGeneric #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
3module Main where
4module Main where
6+import Control.Applicative
5import Database.Bloodhound
7import Database.Bloodhound
6import Data.Aeson
8import Data.Aeson
10+import Data.List.NonEmpty (NonEmpty(..))
7import Data.Time.Calendar (Day(..))
11import Data.Time.Calendar (Day(..))
8import Data.Time.Clock (secondsToDiffTime, UTCTime(..))
12import Data.Time.Clock (secondsToDiffTime, UTCTime(..))
9import Data.Text (Text)
13import Data.Text (Text)
14+import qualified Data.Text as T
10import GHC.Generics (Generic)
15import GHC.Generics (Generic)
11import Network.HTTP.Client
16import Network.HTTP.Client
12import qualified Network.HTTP.Types.Status as NHTS
17import qualified Network.HTTP.Types.Status as NHTS
13import Prelude hiding (filter, putStrLn)
18import Prelude hiding (filter, putStrLn)
14import Test.Hspec
19import Test.Hspec
20+import Test.Hspec.QuickCheck (prop)
21+import Test.QuickCheck
16testServer :: Server
23testServer :: Server
17testServer = Server "http://localhost:9200"
24testServer = Server "http://localhost:9200"
@@ -104,6 +111,27 @@ data BulkTest = BulkTest { name :: Text } deriving (Eq, Generic, Show)
104instance FromJSON BulkTest
111instance FromJSON BulkTest
105instance ToJSON BulkTest
112instance ToJSON BulkTest
114+noDuplicates :: Eq a => [a] -> Bool
115+noDuplicates xs = nub xs == xs
117+instance Arbitrary RegexpFlags where
118+ arbitrary = oneof [ pure AllRegexpFlags
119+ , pure NoRegexpFlags
120+ , SomeRegexpFlags <$> arbitrary
123+instance Arbitrary a => Arbitrary (NonEmpty a) where
124+ arbitrary = liftA2 (:|) arbitrary arbitrary
126+instance Arbitrary RegexpFlag where
127+ arbitrary = oneof [ pure AnyString
107main :: IO ()
135main :: IO ()
108main = hspec $ do
136main = hspec $ do
@@ -314,7 +342,7 @@ main = hspec $ do
314 it "returns document for regexp filter" $ do
342 it "returns document for regexp filter" $ do
315 _ <- insertData
343 _ <- insertData
316 let filter = RegexpFilter (FieldName "user") (Regexp "bite.*app")
344 let filter = RegexpFilter (FieldName "user") (Regexp "bite.*app")
317− (RegexpFlags "ALL") (CacheName "test") False (CacheKey "key")
345+ AllRegexpFlags (CacheName "test") False (CacheKey "key")
318 let search = mkSearch Nothing (Just filter)
346 let search = mkSearch Nothing (Just filter)
319 myTweet <- searchTweet search
347 myTweet <- searchTweet search
320 myTweet `shouldBe` Right exampleTweet
348 myTweet `shouldBe` Right exampleTweet
@@ -322,7 +350,26 @@ main = hspec $ do
322 it "doesn't return document for non-matching regexp filter" $ do
350 it "doesn't return document for non-matching regexp filter" $ do
323 _ <- insertData
351 _ <- insertData
324 let filter = RegexpFilter (FieldName "user")
352 let filter = RegexpFilter (FieldName "user")
325− (Regexp "boy") (RegexpFlags "ALL")
353+ (Regexp "boy") AllRegexpFlags
326 (CacheName "test") False (CacheKey "key")
354 (CacheName "test") False (CacheKey "key")
327 let search = mkSearch Nothing (Just filter)
355 let search = mkSearch Nothing (Just filter)
328 searchExpectNoResults search
356 searchExpectNoResults search
357+ describe "ToJSON RegexpFlags" $ do
358+ it "generates the correct JSON for AllRegexpFlags" $
359+ toJSON AllRegexpFlags `shouldBe` String "ALL"
361+ it "generates the correct JSON for NoRegexpFlags" $
362+ toJSON NoRegexpFlags `shouldBe` String "NONE"
364+ it "generates the correct JSON for SomeRegexpFlags" $
365+ let flags = AnyString :| [ Automaton
370+ in toJSON (SomeRegexpFlags flags) `shouldBe` String "ANYSTRING|AUTOMATON|COMPLEMENT|EMPTY|INTERSECTION|INTERVAL"
372+ prop "removes duplicates from flags" $ \(flags :: RegexpFlags) ->
373+ let String str = toJSON flags
374+ flagStrs = T.splitOn "|" str
375+ in noDuplicates flagStrs