MongoInit.hs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE TemplateHaskell #-}
  3. {-# OPTIONS_GHC -fno-warn-orphans #-}
  4. -- We create an orphan instance for GenerateKey here to avoid a circular
  5. -- dependency between:
  6. --
  7. -- a) persistent-mongoDB:test depends on
  8. -- b) persistent-test:lib depends on
  9. -- c) persistent-mongODB:lib
  10. --
  11. -- This kind of cycle is all kinds of bad news.
  12. module MongoInit (
  13. BackendMonad
  14. , runConn
  15. , MonadIO
  16. , persistSettings
  17. , MkPersistSettings (..)
  18. , dbName
  19. , db'
  20. , setup
  21. , mkPersistSettings
  22. , Action
  23. , Context
  24. , BackendKey(MongoKey)
  25. -- re-exports
  26. , module Database.Persist
  27. , module Database.Persist.Sql.Raw.QQ
  28. , module Test.Hspec
  29. , module Test.HUnit
  30. , liftIO
  31. , mkPersist, mkMigrate, share, sqlSettings, persistLowerCase, persistUpperCase
  32. , Int32, Int64
  33. , Text
  34. , module Control.Monad.Trans.Reader
  35. , module Control.Monad
  36. , PersistFieldSql(..)
  37. , BS.ByteString
  38. , SomeException
  39. , module Init
  40. ) where
  41. -- we have to be careful with this import becuase CPP is still a problem
  42. import Init
  43. ( TestFn(..), truncateTimeOfDay, truncateUTCTime
  44. , truncateToMicro, arbText, liftA2, GenerateKey(..)
  45. , (@/=), (@==), (==@)
  46. , assertNotEqual, assertNotEmpty, assertEmpty, asIO
  47. , isTravis
  48. )
  49. -- re-exports
  50. import Control.Exception (SomeException)
  51. import Control.Monad (void, replicateM, liftM, when, forM_)
  52. import Control.Monad.Trans.Reader
  53. import Database.Persist.TH (mkPersist, mkMigrate, share, sqlSettings, persistLowerCase, persistUpperCase, MkPersistSettings(..))
  54. import Database.Persist.Sql.Raw.QQ
  55. import Test.Hspec
  56. -- testing
  57. import Test.HUnit ((@?=),(@=?), Assertion, assertFailure, assertBool)
  58. import Control.Monad (unless, (>=>))
  59. import Control.Monad.IO.Class
  60. import Control.Monad.IO.Unlift (MonadUnliftIO)
  61. import qualified Data.ByteString as BS
  62. import Data.Int (Int32, Int64)
  63. import Data.Text (Text)
  64. import qualified Database.MongoDB as MongoDB
  65. import Database.Persist.MongoDB (Action, withMongoPool, runMongoDBPool, defaultMongoConf, applyDockerEnv, BackendKey(..))
  66. import Language.Haskell.TH.Syntax (Type(..))
  67. import Database.Persist
  68. import Database.Persist.Sql (PersistFieldSql(..))
  69. import Database.Persist.TH (mkPersistSettings)
  70. setup :: Action IO ()
  71. setup = setupMongo
  72. type Context = MongoDB.MongoContext
  73. _debugOn :: Bool
  74. _debugOn = True
  75. persistSettings :: MkPersistSettings
  76. persistSettings = (mkPersistSettings $ ConT ''Context) { mpsGeneric = True }
  77. dbName :: Text
  78. dbName = "persistent"
  79. type BackendMonad = Context
  80. runConn :: MonadUnliftIO m => Action m backend -> m ()
  81. runConn f = do
  82. conf <- liftIO $ applyDockerEnv $ defaultMongoConf dbName -- { mgRsPrimary = Just "replicaset" }
  83. void $ withMongoPool conf $ runMongoDBPool MongoDB.master f
  84. setupMongo :: Action IO ()
  85. setupMongo = void $ MongoDB.dropDatabase dbName
  86. db' :: Action IO () -> Action IO () -> Assertion
  87. db' actions cleanDB = do
  88. r <- runConn (actions >> cleanDB)
  89. return r
  90. instance GenerateKey MongoDB.MongoContext where
  91. generateKey = MongoKey `liftM` MongoDB.genObjectId