diff --git a/mongoDB.cabal b/mongoDB.cabal index 6c42d91..376985c 100644 --- a/mongoDB.cabal +++ b/mongoDB.cabal @@ -89,6 +89,7 @@ test-suite test main-is: Main.hs other-modules: Spec , QuerySpec + , IndexSpec , TestImport ghc-options: -Wall -with-rtsopts "-K64m" type: exitcode-stdio-1.0 diff --git a/test/IndexSpec.hs b/test/IndexSpec.hs new file mode 100644 index 0000000..7c59c0f --- /dev/null +++ b/test/IndexSpec.hs @@ -0,0 +1,30 @@ +{-# LANGUAGE OverloadedStrings, ExtendedDefaultRules, ScopedTypeVariables #-} +{-# OPTIONS_GHC -fno-warn-type-defaults #-} + +module IndexSpec (spec) where + +import TestImport +import System.Environment (getEnv) +import System.IO.Error (catchIOError) + +testDBName :: Database +testDBName = "mongodb-haskell-test" + +db :: Action IO a -> IO a +db action = do + mongodbHost <- getEnv mongodbHostEnvVariable `catchIOError` (\_ -> return "localhost") + pipe <- connect (host mongodbHost) + result <- access pipe master testDBName action + close pipe + return result + +withCleanDatabase :: ActionWith () -> IO () +withCleanDatabase action = dropDB >> action () >> dropDB >> return () + where + dropDB = db $ dropDatabase testDBName + +spec :: Spec +spec = around withCleanDatabase $ do + describe "make index" $ do + it "single row index" $ do + db (createIndex (index "thiscollection" ["field" =: (1::Int)]) >> getIndexes "thiscollection") `shouldReturn` [["field" =: (1::Int)]]