2024-03-29 19:52:05 +00:00
|
|
|
module Test.Common where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2024-04-01 20:03:17 +00:00
|
|
|
import Data.Either (Either(..))
|
|
|
|
import Data.String.Regex (Regex)
|
|
|
|
import Data.String.Regex as Regex
|
|
|
|
import Data.String.Regex.Flags (RegexFlags(..))
|
2024-04-01 20:20:59 +00:00
|
|
|
import Data.Tuple.Nested (type (/\), (/\))
|
2024-03-29 19:52:05 +00:00
|
|
|
import Effect (Effect)
|
2024-04-01 20:20:59 +00:00
|
|
|
import Effect.Aff (Aff, bracket, makeAff)
|
2024-03-31 01:38:52 +00:00
|
|
|
import Effect.Aff.Postgres.Client (Client)
|
|
|
|
import Effect.Aff.Postgres.Client as Client
|
|
|
|
import Effect.Aff.Postgres.Pool (Pool)
|
|
|
|
import Effect.Aff.Postgres.Pool as Pool
|
2024-03-29 19:52:05 +00:00
|
|
|
import Effect.Class (liftEffect)
|
2024-03-31 01:38:52 +00:00
|
|
|
import Effect.Unsafe (unsafePerformEffect)
|
2024-04-01 20:20:59 +00:00
|
|
|
import Node.EventEmitter (EventHandle(..))
|
|
|
|
import Node.EventEmitter as EE
|
2024-03-29 19:52:05 +00:00
|
|
|
import Node.Path as Path
|
|
|
|
import Node.Process (cwd)
|
2024-04-01 20:03:17 +00:00
|
|
|
import Partial.Unsafe (unsafePartial)
|
2024-04-01 18:46:33 +00:00
|
|
|
import Record (insert)
|
|
|
|
import Type.Prelude (Proxy(..))
|
2024-03-29 19:52:05 +00:00
|
|
|
|
2024-04-01 20:03:17 +00:00
|
|
|
type Config =
|
|
|
|
{ database :: String
|
|
|
|
, host :: String
|
|
|
|
, password :: String
|
|
|
|
, user :: String
|
|
|
|
, max :: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
config :: Effect Config
|
2024-03-29 19:52:05 +00:00
|
|
|
config = do
|
|
|
|
cwd' <- liftEffect cwd
|
|
|
|
host <- liftEffect $ Path.resolve [ cwd' ] "./pg"
|
2024-04-01 18:46:33 +00:00
|
|
|
pure { host, user: "postgres", password: "password", database: "postgres", max: 3 }
|
2024-03-29 19:52:05 +00:00
|
|
|
|
2024-04-01 20:03:17 +00:00
|
|
|
withConfig :: (Config -> Aff Unit) -> Aff Unit
|
|
|
|
withConfig f = f =<< liftEffect config
|
|
|
|
|
2024-03-29 19:52:05 +00:00
|
|
|
withClient :: (Client -> Aff Unit) -> Aff Unit
|
2024-03-31 01:38:52 +00:00
|
|
|
withClient = bracket (Client.connected =<< liftEffect config) Client.end
|
|
|
|
|
|
|
|
pool :: Pool
|
|
|
|
pool = unsafePerformEffect $ Pool.make =<< liftEffect config
|
|
|
|
|
2024-04-01 18:46:33 +00:00
|
|
|
withPool :: (Pool -> Aff Unit) -> Aff Unit
|
|
|
|
withPool = bracket (liftEffect $ Pool.make =<< config) Pool.end
|
|
|
|
|
2024-03-31 01:38:52 +00:00
|
|
|
withPoolClient :: (Client -> Aff Unit) -> Aff Unit
|
2024-04-01 18:46:33 +00:00
|
|
|
withPoolClient = bracket (Pool.connect pool) (liftEffect <<< Pool.release pool)
|
2024-04-01 20:03:17 +00:00
|
|
|
|
|
|
|
unsafeFromRight :: forall a b. Either a b -> b
|
|
|
|
unsafeFromRight e = unsafePartial $ case e of Right b -> b
|
|
|
|
|
|
|
|
re :: String -> RegexFlags -> Regex
|
|
|
|
re s f = unsafeFromRight $ Regex.regex s f
|
2024-04-01 20:20:59 +00:00
|
|
|
|
|
|
|
class Curried a b | a -> b where
|
|
|
|
curried :: (b -> Effect Unit) -> a
|
|
|
|
|
|
|
|
instance Curried (Effect Unit) Unit where
|
|
|
|
curried f = f unit
|
|
|
|
|
|
|
|
instance Curried (a -> Effect Unit) a where
|
|
|
|
curried = identity
|
|
|
|
|
|
|
|
instance Curried (a -> b -> Effect Unit) (a /\ b) where
|
|
|
|
curried f = \a b -> f $ a /\ b
|
|
|
|
|
|
|
|
instance Curried (a -> b -> c -> Effect Unit) (a /\ b /\ c) where
|
|
|
|
curried f = \a b c -> f $ a /\ b /\ c
|
|
|
|
|
|
|
|
onceAff :: forall psCb jsCb emitter r. Curried psCb r => EventHandle emitter psCb jsCb -> emitter -> Aff r
|
|
|
|
onceAff ev em = makeAff \res -> mempty <* EE.once ev (curried (res <<< Right)) em
|