Add aggregate test

This commit is contained in:
Sean Leather 2015-03-18 03:35:51 -07:00
parent 6fd7c099a5
commit 77cde91c61
3 changed files with 43 additions and 0 deletions

View file

@ -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

View file

@ -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"]]

View file

@ -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"))