RawMongoHelpers.hs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module RawMongoHelpers where
  3. import qualified Database.MongoDB as MongoDB
  4. import Database.Persist.MongoDB (toInsertDoc, docToEntityThrow, collectionName, recordToDocument)
  5. import MongoInit
  6. import PersistentTest (cleanDB)
  7. import PersistentTestModels
  8. db :: ReaderT MongoDB.MongoContext IO () -> IO ()
  9. db = db' cleanDB
  10. specs :: Spec
  11. specs = do
  12. describe "raw MongoDB helpers" $ do
  13. it "collectionName" $ do
  14. collectionName (Person "Duder" 0 Nothing) @?= "Person"
  15. it "toInsertFields, entityFields, & docToEntityThrow" $ db $ do
  16. let p1 = Person "Duder" 0 Nothing
  17. let doc = toInsertDoc p1
  18. MongoDB.ObjId _id <- MongoDB.insert "Person" $ doc
  19. let idSelector = "_id" MongoDB.=: _id
  20. Entity _ ent1 <- docToEntityThrow $ idSelector:doc
  21. liftIO $ p1 @?= ent1
  22. let p2 = p1 {personColor = Just "blue"}
  23. let doc2 = idSelector:recordToDocument p2
  24. MongoDB.save "Person" doc2
  25. Entity _ ent2 <- docToEntityThrow doc2
  26. liftIO $ p2 @?= ent2