-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathMyInit.hs
More file actions
175 lines (160 loc) · 4.78 KB
/
Copy pathMyInit.hs
File metadata and controls
175 lines (160 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
module MyInit (
(@/=), (@==), (==@)
, asIO
, assertNotEqual
, assertNotEmpty
, assertEmpty
, isTravis
, BackendMonad
, runConn
, MonadIO
, persistSettings
, MkPersistSettings (..)
, db
, BackendKey(..)
, GenerateKey(..)
, RunDb
-- re-exports
, module Database.Persist
, module Database.Persist.Sql.Raw.QQ
, module Test.Hspec
, module Test.HUnit
, MonadUnliftIO
, liftIO
, mkPersist, mkMigrate, share, sqlSettings, persistLowerCase, persistUpperCase
, mkEntityDefList, sqlSettingsUuid
, Int32, Int64
, Text
, module Control.Monad.Trans.Reader
, module Control.Monad
, module Database.Persist.Sql
, BS.ByteString
, migrateModels
, SomeException
, MonadFail
, TestFn(..)
, truncateTimeOfDay
, truncateToMicro
, truncateUTCTime
, arbText
, liftA2
, LoggingT, ResourceT, UUID(..)
) where
import Init
( GenerateKey(..)
, MonadFail
, RunDb
, TestFn(..)
, arbText
, asIO
, assertEmpty
, assertNotEmpty
, assertNotEqual
, isTravis
, truncateTimeOfDay
, truncateToMicro
, truncateUTCTime
, (==@)
, (@/=)
, (@==)
)
-- re-exports
import Control.Applicative (liftA2)
import Control.Exception (SomeException)
import Control.Monad (forM_, liftM, replicateM, void, when)
import Control.Monad.Trans.Reader
import Data.Aeson (FromJSON, ToJSON, Value(..))
import Database.Persist.Sql.Raw.QQ
import Database.Persist.TH
( MkPersistSettings(..)
, migrateModels
, setImplicitIdDef
, mkEntityDefList
, mkMigrate
, mkPersist
, persistLowerCase
, persistUpperCase
, share
, sqlSettings
)
import Test.Hspec
import Test.QuickCheck.Instances ()
import Web.Internal.HttpApiData
import Web.PathPieces
import Database.Persist.ImplicitIdDef
-- testing
import Test.HUnit (Assertion, assertBool, assertFailure, (@=?), (@?=))
import Control.Monad (unless, (>=>))
import Control.Monad.IO.Class
import Control.Monad.IO.Unlift (MonadUnliftIO)
import Control.Monad.Logger
import Control.Monad.Trans.Resource (ResourceT, runResourceT)
import qualified Data.ByteString as BS
import Data.Int (Int32, Int64)
import Data.Text (Text)
import qualified Data.Text.Encoding as TE
import qualified Database.MySQL.Base as MySQL
import System.Log.FastLogger (fromLogStr)
import Database.Persist
import Database.Persist.MySQL
import Database.Persist.Sql
_debugOn :: Bool
_debugOn = False
persistSettings :: MkPersistSettings
persistSettings = sqlSettings
type BackendMonad = SqlBackend
runConn :: MonadUnliftIO m => SqlPersistT (LoggingT m) t -> m ()
runConn f = do
travis <- liftIO isTravis
let debugPrint = not travis && _debugOn
let printDebug = if debugPrint then print . fromLogStr else void . return
flip runLoggingT (\_ _ _ s -> printDebug s) $ do
-- Since version 5.7.5, MySQL adds a mode value `STRICT_TRANS_TABLES`
-- which can cause an exception in MaxLenTest, depending on the server
-- configuration. Persistent tests do not need any of the modes which are
-- set by default, so it is simplest to clear `sql_mode` for the session.
let baseConnectInfo =
defaultConnectInfo {
connectOptions =
connectOptions defaultConnectInfo
++ [MySQL.InitCommand "SET SESSION sql_mode = '';\0"]
}
_ <- if not travis
then withMySQLPool baseConnectInfo
{ connectHost = "localhost"
, connectUser = "test"
, connectPassword = "test"
, connectDatabase = "test"
} 1 $ runSqlPool f
else withMySQLPool baseConnectInfo
{ connectHost = "127.0.0.1"
, connectUser = "test"
, connectPassword = "test"
, connectDatabase = "test"
, connectPort = 33306
} 1 $ runSqlPool f
return ()
db :: SqlPersistT (LoggingT (ResourceT IO)) () -> Assertion
db actions = do
runResourceT $ runConn $ actions >> transactionUndo
newtype UUID = UUID { unUUID :: Text }
deriving stock
(Show, Eq, Ord, Read)
deriving newtype
( ToJSON, FromJSON
, PersistField, PersistFieldSql
, FromHttpApiData, ToHttpApiData, PathPiece
)
sqlSettingsUuid :: Text -> MkPersistSettings
sqlSettingsUuid defExpr =
let
uuidDef =
setImplicitIdDefMaxLen 100 $ mkImplicitIdDef @UUID defExpr
settings =
setImplicitIdDef uuidDef sqlSettings
in
settings