MyInit.hs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module MyInit (
  3. (@/=), (@==), (==@)
  4. , asIO
  5. , assertNotEqual
  6. , assertNotEmpty
  7. , assertEmpty
  8. , isTravis
  9. , BackendMonad
  10. , runConn
  11. , MonadIO
  12. , persistSettings
  13. , MkPersistSettings (..)
  14. , db
  15. , BackendKey(..)
  16. , GenerateKey(..)
  17. , RunDb
  18. -- re-exports
  19. , module Database.Persist
  20. , module Database.Persist.Sql.Raw.QQ
  21. , module Test.Hspec
  22. , module Test.HUnit
  23. , liftIO
  24. , mkPersist, mkMigrate, share, sqlSettings, persistLowerCase, persistUpperCase
  25. , Int32, Int64
  26. , Text
  27. , module Control.Monad.Trans.Reader
  28. , module Control.Monad
  29. , module Database.Persist.Sql
  30. , BS.ByteString
  31. , SomeException
  32. , MonadFail
  33. , TestFn(..)
  34. , truncateTimeOfDay
  35. , truncateToMicro
  36. , truncateUTCTime
  37. , arbText
  38. , liftA2
  39. ) where
  40. import Init
  41. ( TestFn(..), truncateTimeOfDay, truncateUTCTime
  42. , truncateToMicro, arbText, GenerateKey(..)
  43. , (@/=), (@==), (==@)
  44. , assertNotEqual, assertNotEmpty, assertEmpty, asIO
  45. , isTravis, RunDb, MonadFail
  46. )
  47. -- re-exports
  48. import Control.Applicative (liftA2)
  49. import Control.Exception (SomeException)
  50. import Control.Monad (void, replicateM, liftM, when, forM_)
  51. import Control.Monad.Trans.Reader
  52. import Database.Persist.TH (mkPersist, mkMigrate, share, sqlSettings, persistLowerCase, persistUpperCase, MkPersistSettings(..))
  53. import Database.Persist.Sql.Raw.QQ
  54. import Test.Hspec
  55. import Test.QuickCheck.Instances ()
  56. -- testing
  57. import Test.HUnit ((@?=),(@=?), Assertion, assertFailure, assertBool)
  58. import Control.Monad (unless, (>=>))
  59. import Control.Monad.IO.Unlift (MonadUnliftIO)
  60. import Control.Monad.IO.Class
  61. import Control.Monad.Logger
  62. import Control.Monad.Trans.Resource (ResourceT, runResourceT)
  63. import qualified Data.ByteString as BS
  64. import Data.Int (Int32, Int64)
  65. import Data.Text (Text)
  66. import qualified Database.MySQL.Base as MySQL
  67. import System.Log.FastLogger (fromLogStr)
  68. import Database.Persist
  69. import Database.Persist.MySQL
  70. import Database.Persist.Sql
  71. import Database.Persist.TH ()
  72. _debugOn :: Bool
  73. _debugOn = False
  74. persistSettings :: MkPersistSettings
  75. persistSettings = sqlSettings { mpsGeneric = True }
  76. type BackendMonad = SqlBackend
  77. runConn :: MonadUnliftIO m => SqlPersistT (LoggingT m) t -> m ()
  78. runConn f = do
  79. travis <- liftIO isTravis
  80. let debugPrint = not travis && _debugOn
  81. let printDebug = if debugPrint then print . fromLogStr else void . return
  82. flip runLoggingT (\_ _ _ s -> printDebug s) $ do
  83. -- Since version 5.7.5, MySQL adds a mode value `STRICT_TRANS_TABLES`
  84. -- which can cause an exception in MaxLenTest, depending on the server
  85. -- configuration. Persistent tests do not need any of the modes which are
  86. -- set by default, so it is simplest to clear `sql_mode` for the session.
  87. let baseConnectInfo =
  88. defaultConnectInfo {
  89. connectOptions =
  90. connectOptions defaultConnectInfo
  91. ++ [MySQL.InitCommand "SET SESSION sql_mode = '';\0"]
  92. }
  93. _ <- if not travis
  94. then withMySQLPool baseConnectInfo
  95. { connectHost = "localhost"
  96. , connectUser = "test"
  97. , connectPassword = "test"
  98. , connectDatabase = "test"
  99. } 1 $ runSqlPool f
  100. else withMySQLPool baseConnectInfo
  101. { connectHost = "localhost"
  102. , connectUser = "travis"
  103. , connectPassword = ""
  104. , connectDatabase = "persistent"
  105. } 1 $ runSqlPool f
  106. return ()
  107. db :: SqlPersistT (LoggingT (ResourceT IO)) () -> Assertion
  108. db actions = do
  109. runResourceT $ runConn $ actions >> transactionUndo