diff --git a/test/QuerySpec.hs b/test/QuerySpec.hs index 08a33fa..53d3723 100644 --- a/test/QuerySpec.hs +++ b/test/QuerySpec.hs @@ -43,6 +43,14 @@ insertDuplicateWith testInsert = do ] return () +insertUsers :: ActionWith () -> IO () +insertUsers doTest = 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"]] + ] + doTest () + bigDocument :: Document bigDocument = (flip map) [1..10000] $ \i -> (fromString $ "team" ++ (show i)) =: ("team " ++ (show i) ++ " name") @@ -428,13 +436,31 @@ spec = around withCleanDatabase $ do collections <- db $ allCollections liftIO $ (L.sort collections) `shouldContain` ["team1", "team2", "team3"] - describe "aggregate" $ do + describe "aggregate" $ around insertUsers $ 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"]] + + describe "findCommand" $ around insertUsers $ do + it "fetches all the records" $ do + result <- db $ rest =<< findCommand (select [] "users") + length result `shouldBe` 3 + + it "filters the records" $ do + result <- db $ rest =<< findCommand (select ["_id" =: "joe"] "users") + length result `shouldBe` 1 + + it "projects the records" $ do + result <- db $ rest =<< findCommand + (select [] "users") { project = [ "_id" =: 1 ] } + result `shouldBe` [["_id" =: "jane"], ["_id" =: "joe"], ["_id" =: "jill"]] + + it "sorts the records" $ do + result <- db $ rest =<< findCommand + (select [] "users") { project = [ "_id" =: 1 ] + , sort = [ "_id" =: 1 ] + } + result `shouldBe` [["_id" =: "jane"], ["_id" =: "jill"], ["_id" =: "joe"]] +