Make findCommand tests run just for MongoDB 3.2 or superior

This commit is contained in:
Diego Balseiro 2020-07-31 13:50:33 -05:00
parent a3e1999287
commit c6a9ffcc63
2 changed files with 36 additions and 26 deletions

View file

@ -1045,7 +1045,7 @@ findCommand Query{..} = do
, "hint" =: hint , "hint" =: hint
, "skip" =: toInt32 skip , "skip" =: toInt32 skip
] ]
++ mconcat -- optional fields ++ mconcat -- optional fields. They should not be present if set to 0 and mongo will use defaults
[ "batchSize" =? toMaybe (/= 0) toInt32 batchSize [ "batchSize" =? toMaybe (/= 0) toInt32 batchSize
, "limit" =? toMaybe (/= 0) toInt32 limit , "limit" =? toMaybe (/= 0) toInt32 limit
] ]

View file

@ -43,13 +43,20 @@ insertDuplicateWith testInsert = do
] ]
return () return ()
insertUsers :: ActionWith () -> IO () insertUsers :: IO ()
insertUsers doTest = do insertUsers = db $
db $ insertAll_ "users" [ ["_id" =: "jane", "joined" =: parseDate "2011-03-02", "likes" =: ["golf", "racquetball"]] insertAll_ "users" [ ["_id" =: "jane", "joined" =: parseDate "2011-03-02", "likes" =: ["golf", "racquetball"]]
, ["_id" =: "joe", "joined" =: parseDate "2012-07-02", "likes" =: ["tennis", "golf", "swimming"]] , ["_id" =: "joe", "joined" =: parseDate "2012-07-02", "likes" =: ["tennis", "golf", "swimming"]]
, ["_id" =: "jill", "joined" =: parseDate "2013-11-17", "likes" =: ["cricket", "golf"]] , ["_id" =: "jill", "joined" =: parseDate "2013-11-17", "likes" =: ["cricket", "golf"]]
] ]
doTest ()
pendingIfMongoVersion :: ((Integer, Integer) -> Bool) -> SpecWith () -> Spec
pendingIfMongoVersion invalidVersion = before $ do
version <- db $ extractVersion . T.splitOn "." . at "version" <$> runCommand1 "buildinfo"
when (invalidVersion version) $ pendingWith "This test does not run in the current database version"
where
extractVersion (major:minor:_) = (read $ T.unpack major, read $ T.unpack minor)
extractVersion _ = error "Invalid version specification"
bigDocument :: Document bigDocument :: Document
bigDocument = (flip map) [1..10000] $ \i -> (fromString $ "team" ++ (show i)) =: ("team " ++ (show i) ++ " name") bigDocument = (flip map) [1..10000] $ \i -> (fromString $ "team" ++ (show i)) =: ("team " ++ (show i) ++ " name")
@ -436,31 +443,34 @@ spec = around withCleanDatabase $ do
collections <- db $ allCollections collections <- db $ allCollections
liftIO $ (L.sort collections) `shouldContain` ["team1", "team2", "team3"] liftIO $ (L.sort collections) `shouldContain` ["team1", "team2", "team3"]
describe "aggregate" $ around insertUsers $ describe "aggregate" $ before_ insertUsers $
it "aggregates to normalize and sort documents" $ do it "aggregates to normalize and sort documents" $ do
result <- db $ aggregate "users" [ ["$project" =: ["name" =: ["$toUpper" =: "$_id"], "_id" =: 0]] result <- db $ aggregate "users" [ ["$project" =: ["name" =: ["$toUpper" =: "$_id"], "_id" =: 0]]
, ["$sort" =: ["name" =: 1]] , ["$sort" =: ["name" =: 1]]
] ]
result `shouldBe` [["name" =: "JANE"], ["name" =: "JILL"], ["name" =: "JOE"]] result `shouldBe` [["name" =: "JANE"], ["name" =: "JILL"], ["name" =: "JOE"]]
describe "findCommand" $ around insertUsers $ do -- This feature was introduced in MongoDB version 3.2
it "fetches all the records" $ do -- https://docs.mongodb.com/manual/reference/command/find/
result <- db $ rest =<< findCommand (select [] "users") describe "findCommand" $ pendingIfMongoVersion (< (3,2)) $
length result `shouldBe` 3 context "when mongo version is 3.2 or superior" $ before insertUsers $ do
it "fetches all the records" $ do
result <- db $ rest =<< findCommand (select [] "users")
length result `shouldBe` 3
it "filters the records" $ do it "filters the records" $ do
result <- db $ rest =<< findCommand (select ["_id" =: "joe"] "users") result <- db $ rest =<< findCommand (select ["_id" =: "joe"] "users")
length result `shouldBe` 1 length result `shouldBe` 1
it "projects the records" $ do it "projects the records" $ do
result <- db $ rest =<< findCommand result <- db $ rest =<< findCommand
(select [] "users") { project = [ "_id" =: 1 ] } (select [] "users") { project = [ "_id" =: 1 ] }
result `shouldBe` [["_id" =: "jane"], ["_id" =: "joe"], ["_id" =: "jill"]] result `shouldBe` [["_id" =: "jane"], ["_id" =: "joe"], ["_id" =: "jill"]]
it "sorts the records" $ do it "sorts the records" $ do
result <- db $ rest =<< findCommand result <- db $ rest =<< findCommand
(select [] "users") { project = [ "_id" =: 1 ] (select [] "users") { project = [ "_id" =: 1 ]
, sort = [ "_id" =: 1 ] , sort = [ "_id" =: 1 ]
} }
result `shouldBe` [["_id" =: "jane"], ["_id" =: "jill"], ["_id" =: "joe"]] result `shouldBe` [["_id" =: "jane"], ["_id" =: "jill"], ["_id" =: "joe"]]