diff --git a/mongoDB.cabal b/mongoDB.cabal index 02660d7..9ef04b1 100644 --- a/mongoDB.cabal +++ b/mongoDB.cabal @@ -63,6 +63,11 @@ test-suite test , base , mtl , hspec >= 2 + -- Keep supporting the old-locale and time < 1.5 packages for + -- now. It's too difficult to support old versions of GHC and + -- the new version of time. + , old-locale + , time default-language: Haskell2010 default-extensions: OverloadedStrings diff --git a/test/QuerySpec.hs b/test/QuerySpec.hs index f688eae..8c5f93c 100644 --- a/test/QuerySpec.hs +++ b/test/QuerySpec.hs @@ -121,3 +121,14 @@ spec = around withCleanDatabase $ do it "raises exception" $ insertDuplicateWith insertAll_ `shouldThrow` anyException + + describe "aggregate" $ do + it "aggregates to normalize and sort documents" $ do + db $ insertAll_ "users" [ ["_id" =: "jane", "joined" =: parseDate "2011-03-02", "likes" =: ["golf", "racquetball"]] + , ["_id" =: "joe", "joined" =: parseDate "2012-07-02", "likes" =: ["tennis", "golf", "swimming"]] + , ["_id" =: "jill", "joined" =: parseDate "2013-11-17", "likes" =: ["cricket", "golf"]] + ] + result <- db $ aggregate "users" [ ["$project" =: ["name" =: ["$toUpper" =: "$_id"], "_id" =: 0]] + , ["$sort" =: ["name" =: 1]] + ] + result `shouldBe` [["name" =: "JANE"], ["name" =: "JILL"], ["name" =: "JOE"]] diff --git a/test/TestImport.hs b/test/TestImport.hs index 9b99618..2294beb 100644 --- a/test/TestImport.hs +++ b/test/TestImport.hs @@ -1,7 +1,34 @@ +{-# LANGUAGE CPP #-} + module TestImport ( + module TestImport, module Export ) where import Test.Hspec as Export hiding (Selector) import Database.MongoDB as Export import Control.Monad.Trans as Export (MonadIO, liftIO) +import Data.Maybe (fromJust) +import Data.Time (ParseTime, UTCTime) +import qualified Data.Time as Time + +-- We support the old version of time because it's easier than trying to use +-- only the new version and test older GHC versions. +#if MIN_VERSION_time(1,5,0) +import Data.Time.Format (defaultTimeLocale, iso8601DateFormat) +#else +import System.Locale (defaultTimeLocale, iso8601DateFormat) +#endif + +parseTime :: ParseTime t => String -> String -> t +#if MIN_VERSION_time(1,5,0) +parseTime = Time.parseTimeOrError True defaultTimeLocale +#else +parseTime fmt = fromJust . Time.parseTime defaultTimeLocale fmt +#endif + +parseDate :: String -> UTCTime +parseDate = parseTime (iso8601DateFormat Nothing) + +parseDateTime :: String -> UTCTime +parseDateTime = parseTime (iso8601DateFormat (Just "%H:%M:%S"))