From bbba3f37eafd0415d2b49bb31971f111160726c5 Mon Sep 17 00:00:00 2001 From: Victor Denisov Date: Tue, 29 Sep 2015 22:41:52 -0700 Subject: [PATCH 1/2] Add test for rest function --- test/QuerySpec.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/QuerySpec.hs b/test/QuerySpec.hs index 510d408..671b441 100644 --- a/test/QuerySpec.hs +++ b/test/QuerySpec.hs @@ -135,6 +135,18 @@ spec = around withCleanDatabase $ do liftIO $ (length returnedDocs) `shouldBe` 100000 + describe "rest" $ do + it "returns all documents from the collection" $ do + let docs = (flip map) [0..6000] $ \i -> + ["name" =: (T.pack $ "name " ++ (show i))] + collectionName = "smallCollection" + db $ insertAll_ collectionName docs + db $ do + cur <- find $ (select [] collectionName) + returnedDocs <- rest cur + + liftIO $ (length returnedDocs) `shouldBe` 6001 + describe "aggregate" $ do it "aggregates to normalize and sort documents" $ do db $ insertAll_ "users" [ ["_id" =: "jane", "joined" =: parseDate "2011-03-02", "likes" =: ["golf", "racquetball"]] From 75470af42118b973b52401675223c629ec94a982 Mon Sep 17 00:00:00 2001 From: Victor Denisov Date: Sat, 26 Sep 2015 21:57:06 -0700 Subject: [PATCH 2/2] Retrieve another batch for next in case of unlimited request --- Database/MongoDB/Query.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Database/MongoDB/Query.hs b/Database/MongoDB/Query.hs index 12c156a..c6c2cbf 100644 --- a/Database/MongoDB/Query.hs +++ b/Database/MongoDB/Query.hs @@ -49,7 +49,7 @@ import Prelude hiding (lookup) import Control.Exception (Exception, throwIO) import Control.Monad (unless, replicateM, liftM) import Data.Int (Int32) -import Data.Maybe (listToMaybe, catMaybes) +import Data.Maybe (listToMaybe, catMaybes, isNothing) import Data.Word (Word32) #if !MIN_VERSION_base(4,8,0) import Data.Monoid (mappend) @@ -628,7 +628,7 @@ next (Cursor fcol batchSize var) = modifyMVar var nextState where let newLimit = do limit <- mLimit return $ limit - 1 - dBatch' <- if null docs' && cid /= 0 && (newLimit > (Just 0)) + dBatch' <- if null docs' && cid /= 0 && ((newLimit > (Just 0)) || (isNothing newLimit)) then nextBatch' fcol batchSize newLimit cid else return $ return (Batch newLimit cid docs') when (newLimit == (Just 0)) $ unless (cid == 0) $ send [KillCursors [cid]]