Rename fakeDB
to db
This commit is contained in:
parent
c6d6f8c71a
commit
3dd594999a
1 changed files with 31 additions and 31 deletions
|
@ -8,8 +8,8 @@ import Control.Exception
|
||||||
testDBName :: Database
|
testDBName :: Database
|
||||||
testDBName = "mongodb-haskell-test"
|
testDBName = "mongodb-haskell-test"
|
||||||
|
|
||||||
fakeDB :: Action IO a -> IO a
|
db :: Action IO a -> IO a
|
||||||
fakeDB action = do
|
db action = do
|
||||||
pipe <- connect (host "127.0.0.1")
|
pipe <- connect (host "127.0.0.1")
|
||||||
result <- access pipe master testDBName action
|
result <- access pipe master testDBName action
|
||||||
close pipe
|
close pipe
|
||||||
|
@ -18,41 +18,41 @@ fakeDB action = do
|
||||||
withCleanDatabase :: IO a -> IO ()
|
withCleanDatabase :: IO a -> IO ()
|
||||||
withCleanDatabase action = dropDB >> action >> dropDB >> return ()
|
withCleanDatabase action = dropDB >> action >> dropDB >> return ()
|
||||||
where
|
where
|
||||||
dropDB = fakeDB $ dropDatabase testDBName
|
dropDB = db $ dropDatabase testDBName
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = around withCleanDatabase $ do
|
spec = around withCleanDatabase $ do
|
||||||
describe "useDb" $ do
|
describe "useDb" $ do
|
||||||
it "changes the db" $ do
|
it "changes the db" $ do
|
||||||
let anotherDBName = "another-mongodb-haskell-test"
|
let anotherDBName = "another-mongodb-haskell-test"
|
||||||
fakeDB thisDatabase `shouldReturn` testDBName
|
db thisDatabase `shouldReturn` testDBName
|
||||||
fakeDB (useDb anotherDBName thisDatabase) `shouldReturn` anotherDBName
|
db (useDb anotherDBName thisDatabase) `shouldReturn` anotherDBName
|
||||||
|
|
||||||
describe "insert" $ do
|
describe "insert" $ do
|
||||||
it "inserts a document to the collection and returns its _id" $ do
|
it "inserts a document to the collection and returns its _id" $ do
|
||||||
_id <- fakeDB $ insert "team" ["name" =: "Yankees", "league" =: "American"]
|
_id <- db $ insert "team" ["name" =: "Yankees", "league" =: "American"]
|
||||||
result <- fakeDB $ rest =<< find (select [] "team")
|
result <- db $ rest =<< find (select [] "team")
|
||||||
result `shouldBe` [["_id" =: _id, "name" =: "Yankees", "league" =: "American"]]
|
result `shouldBe` [["_id" =: _id, "name" =: "Yankees", "league" =: "American"]]
|
||||||
|
|
||||||
describe "insert_" $ do
|
describe "insert_" $ do
|
||||||
it "inserts a document to the collection and doesn't return _id" $ do
|
it "inserts a document to the collection and doesn't return _id" $ do
|
||||||
_id <- fakeDB $ insert_ "team" ["name" =: "Yankees", "league" =: "American"]
|
_id <- db $ insert_ "team" ["name" =: "Yankees", "league" =: "American"]
|
||||||
fakeDB (count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
|
db (count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
|
||||||
_id `shouldBe` ()
|
_id `shouldBe` ()
|
||||||
|
|
||||||
describe "insertMany" $ do
|
describe "insertMany" $ do
|
||||||
it "inserts documents to the collection and returns their _ids" $ do
|
it "inserts documents to the collection and returns their _ids" $ do
|
||||||
(_id1:_id2:_) <- fakeDB $ insertMany "team" [ ["name" =: "Yankees", "league" =: "American"]
|
(_id1:_id2:_) <- db $ insertMany "team" [ ["name" =: "Yankees", "league" =: "American"]
|
||||||
, ["name" =: "Dodgers", "league" =: "American"]
|
, ["name" =: "Dodgers", "league" =: "American"]
|
||||||
]
|
]
|
||||||
result <- fakeDB $ rest =<< find (select [] "team")
|
result <- db $ rest =<< find (select [] "team")
|
||||||
result `shouldBe` [ ["_id" =: _id1, "name" =: "Yankees", "league" =: "American"]
|
result `shouldBe` [ ["_id" =: _id1, "name" =: "Yankees", "league" =: "American"]
|
||||||
, ["_id" =: _id2, "name" =: "Dodgers", "league" =: "American"]
|
, ["_id" =: _id2, "name" =: "Dodgers", "league" =: "American"]
|
||||||
]
|
]
|
||||||
context "Insert a document with duplicating key" $ do
|
context "Insert a document with duplicating key" $ do
|
||||||
let insertDuplicatingDocument = do
|
let insertDuplicatingDocument = do
|
||||||
_id <- fakeDB $ insert "team" ["name" =: "Dodgers", "league" =: "American"]
|
_id <- db $ insert "team" ["name" =: "Dodgers", "league" =: "American"]
|
||||||
_ <- fakeDB $ insertMany "team" [ ["name" =: "Yankees", "league" =: "American"]
|
_ <- db $ insertMany "team" [ ["name" =: "Yankees", "league" =: "American"]
|
||||||
-- Try to insert document with
|
-- Try to insert document with
|
||||||
-- duplicate key
|
-- duplicate key
|
||||||
, ["name" =: "Dodgers", "league" =: "American", "_id" =: _id]
|
, ["name" =: "Dodgers", "league" =: "American", "_id" =: _id]
|
||||||
|
@ -62,10 +62,10 @@ spec = around withCleanDatabase $ do
|
||||||
|
|
||||||
before (insertDuplicatingDocument `catch` \(_ :: Failure) -> return ()) $ do
|
before (insertDuplicatingDocument `catch` \(_ :: Failure) -> return ()) $ do
|
||||||
it "inserts documents before it" $
|
it "inserts documents before it" $
|
||||||
fakeDB ( count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
|
db ( count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
|
||||||
|
|
||||||
it "doesn't insert documents after it" $
|
it "doesn't insert documents after it" $
|
||||||
fakeDB ( count $ select ["name" =: "Indians", "league" =: "American"] "team") `shouldReturn` 0
|
db ( count $ select ["name" =: "Indians", "league" =: "American"] "team") `shouldReturn` 0
|
||||||
|
|
||||||
it "raises exception" $
|
it "raises exception" $
|
||||||
insertDuplicatingDocument `shouldThrow` anyException
|
insertDuplicatingDocument `shouldThrow` anyException
|
||||||
|
@ -73,15 +73,15 @@ spec = around withCleanDatabase $ do
|
||||||
|
|
||||||
describe "insertMany_" $ do
|
describe "insertMany_" $ do
|
||||||
it "inserts documents to the collection and returns nothing" $ do
|
it "inserts documents to the collection and returns nothing" $ do
|
||||||
ids <- fakeDB $ insertMany_ "team" [ ["name" =: "Yankees", "league" =: "American"]
|
ids <- db $ insertMany_ "team" [ ["name" =: "Yankees", "league" =: "American"]
|
||||||
, ["name" =: "Dodgers", "league" =: "American"]
|
, ["name" =: "Dodgers", "league" =: "American"]
|
||||||
]
|
]
|
||||||
ids `shouldBe` ()
|
ids `shouldBe` ()
|
||||||
|
|
||||||
context "Insert a document with duplicating key" $ do
|
context "Insert a document with duplicating key" $ do
|
||||||
let insertDuplicatingDocument = do
|
let insertDuplicatingDocument = do
|
||||||
_id <- fakeDB $ insert "team" ["name" =: "Dodgers", "league" =: "American"]
|
_id <- db $ insert "team" ["name" =: "Dodgers", "league" =: "American"]
|
||||||
_ <- fakeDB $ insertMany_ "team" [ ["name" =: "Yankees", "league" =: "American"]
|
_ <- db $ insertMany_ "team" [ ["name" =: "Yankees", "league" =: "American"]
|
||||||
-- Try to insert document with
|
-- Try to insert document with
|
||||||
-- duplicate key
|
-- duplicate key
|
||||||
, ["name" =: "Dodgers", "league" =: "American", "_id" =: _id]
|
, ["name" =: "Dodgers", "league" =: "American", "_id" =: _id]
|
||||||
|
@ -91,26 +91,26 @@ spec = around withCleanDatabase $ do
|
||||||
|
|
||||||
before (insertDuplicatingDocument `catch` \(_ :: Failure) -> return ()) $ do
|
before (insertDuplicatingDocument `catch` \(_ :: Failure) -> return ()) $ do
|
||||||
it "inserts documents before it" $
|
it "inserts documents before it" $
|
||||||
fakeDB (count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
|
db (count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
|
||||||
it "doesn't insert documents after it" $
|
it "doesn't insert documents after it" $
|
||||||
fakeDB (count $ select ["name" =: "Indians", "league" =: "American"] "team") `shouldReturn` 0
|
db (count $ select ["name" =: "Indians", "league" =: "American"] "team") `shouldReturn` 0
|
||||||
it "raises exception" $
|
it "raises exception" $
|
||||||
insertDuplicatingDocument `shouldThrow` anyException
|
insertDuplicatingDocument `shouldThrow` anyException
|
||||||
|
|
||||||
describe "insertAll" $ do
|
describe "insertAll" $ do
|
||||||
it "inserts documents to the collection and returns their _ids" $ do
|
it "inserts documents to the collection and returns their _ids" $ do
|
||||||
(_id1:_id2:_) <- fakeDB $ insertAll "team" [ ["name" =: "Yankees", "league" =: "American"]
|
(_id1:_id2:_) <- db $ insertAll "team" [ ["name" =: "Yankees", "league" =: "American"]
|
||||||
, ["name" =: "Dodgers", "league" =: "American"]
|
, ["name" =: "Dodgers", "league" =: "American"]
|
||||||
]
|
]
|
||||||
result <- fakeDB $ rest =<< find (select [] "team")
|
result <- db $ rest =<< find (select [] "team")
|
||||||
result `shouldBe` [["_id" =: _id1, "name" =: "Yankees", "league" =: "American"]
|
result `shouldBe` [["_id" =: _id1, "name" =: "Yankees", "league" =: "American"]
|
||||||
,["_id" =: _id2, "name" =: "Dodgers", "league" =: "American"]
|
,["_id" =: _id2, "name" =: "Dodgers", "league" =: "American"]
|
||||||
]
|
]
|
||||||
|
|
||||||
context "Insert a document with duplicating key" $ do
|
context "Insert a document with duplicating key" $ do
|
||||||
let insertDuplicatingDocument = do
|
let insertDuplicatingDocument = do
|
||||||
_id <- fakeDB $ insert "team" ["name" =: "Dodgers", "league" =: "American"]
|
_id <- db $ insert "team" ["name" =: "Dodgers", "league" =: "American"]
|
||||||
_ <- fakeDB $ insertAll "team" [ ["name" =: "Yankees", "league" =: "American"]
|
_ <- db $ insertAll "team" [ ["name" =: "Yankees", "league" =: "American"]
|
||||||
-- Try to insert document with
|
-- Try to insert document with
|
||||||
-- duplicate key
|
-- duplicate key
|
||||||
, ["name" =: "Dodgers", "league" =: "American", "_id" =: _id]
|
, ["name" =: "Dodgers", "league" =: "American", "_id" =: _id]
|
||||||
|
@ -120,23 +120,23 @@ spec = around withCleanDatabase $ do
|
||||||
|
|
||||||
before (insertDuplicatingDocument `catch` \(_ :: Failure) -> return ()) $ do
|
before (insertDuplicatingDocument `catch` \(_ :: Failure) -> return ()) $ do
|
||||||
it "inserts all documents which can be inserted" $ do
|
it "inserts all documents which can be inserted" $ do
|
||||||
fakeDB (count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
|
db (count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
|
||||||
fakeDB (count $ select ["name" =: "Indians", "league" =: "American"] "team") `shouldReturn` 1
|
db (count $ select ["name" =: "Indians", "league" =: "American"] "team") `shouldReturn` 1
|
||||||
|
|
||||||
it "raises exception" $
|
it "raises exception" $
|
||||||
insertDuplicatingDocument `shouldThrow` anyException
|
insertDuplicatingDocument `shouldThrow` anyException
|
||||||
|
|
||||||
describe "insertAll_" $ do
|
describe "insertAll_" $ do
|
||||||
it "inserts documents to the collection and returns their _ids" $ do
|
it "inserts documents to the collection and returns their _ids" $ do
|
||||||
ids <- fakeDB $ insertAll_ "team" [ ["name" =: "Yankees", "league" =: "American"]
|
ids <- db $ insertAll_ "team" [ ["name" =: "Yankees", "league" =: "American"]
|
||||||
, ["name" =: "Dodgers", "league" =: "American"]
|
, ["name" =: "Dodgers", "league" =: "American"]
|
||||||
]
|
]
|
||||||
ids `shouldBe` ()
|
ids `shouldBe` ()
|
||||||
|
|
||||||
context "Insert a document with duplicating key" $ do
|
context "Insert a document with duplicating key" $ do
|
||||||
let insertDuplicatingDocument = do
|
let insertDuplicatingDocument = do
|
||||||
_id <- fakeDB $ insert "team" ["name" =: "Dodgers", "league" =: "American"]
|
_id <- db $ insert "team" ["name" =: "Dodgers", "league" =: "American"]
|
||||||
_ <- fakeDB $ insertAll_ "team" [ ["name" =: "Yankees", "league" =: "American"]
|
_ <- db $ insertAll_ "team" [ ["name" =: "Yankees", "league" =: "American"]
|
||||||
-- Try to insert document with
|
-- Try to insert document with
|
||||||
-- duplicate key
|
-- duplicate key
|
||||||
, ["name" =: "Dodgers", "league" =: "American", "_id" =: _id]
|
, ["name" =: "Dodgers", "league" =: "American", "_id" =: _id]
|
||||||
|
@ -146,8 +146,8 @@ spec = around withCleanDatabase $ do
|
||||||
|
|
||||||
before (insertDuplicatingDocument `catch` \(_ :: Failure) -> return ()) $ do
|
before (insertDuplicatingDocument `catch` \(_ :: Failure) -> return ()) $ do
|
||||||
it "inserts all documents which can be inserted" $ do
|
it "inserts all documents which can be inserted" $ do
|
||||||
fakeDB (count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
|
db (count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
|
||||||
fakeDB (count $ select ["name" =: "Indians", "league" =: "American"] "team") `shouldReturn` 1
|
db (count $ select ["name" =: "Indians", "league" =: "American"] "team") `shouldReturn` 1
|
||||||
|
|
||||||
it "raises exception" $
|
it "raises exception" $
|
||||||
insertDuplicatingDocument `shouldThrow` anyException
|
insertDuplicatingDocument `shouldThrow` anyException
|
||||||
|
|
Loading…
Reference in a new issue