2018-06-27 16:11:32 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Control.Monad.Trans.Except
|
2018-07-11 10:13:07 +03:00
|
|
|
import Data.Time.Clock (getCurrentTime)
|
|
|
|
import Network.Wai (Application)
|
|
|
|
import Options.Applicative
|
|
|
|
import System.Environment (withArgs)
|
2018-06-27 16:11:32 +03:00
|
|
|
import System.Exit (exitFailure)
|
|
|
|
import Test.Hspec.Core.Runner
|
2018-07-11 10:13:07 +03:00
|
|
|
import Test.Hspec.Formatters
|
2018-06-27 16:11:32 +03:00
|
|
|
import Test.Hspec.Wai
|
2018-07-11 10:13:07 +03:00
|
|
|
import Web.Spock.Core (spockAsApp, spockT)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-07-11 10:13:07 +03:00
|
|
|
import qualified Data.Aeson as J
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BLC
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
import Hasura.Prelude
|
2018-07-11 10:13:07 +03:00
|
|
|
import Hasura.Server.App (AuthMode (..), RavenLogger, app,
|
|
|
|
ravenLogGen)
|
|
|
|
import Hasura.Server.Init
|
|
|
|
import Hasura.Server.Logging (withStdoutLogger)
|
|
|
|
import Ops (initCatalogSafe)
|
|
|
|
import Spec (mkSpecs)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-07-11 10:13:07 +03:00
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Database.PG.Query as PGQ
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
|
2018-07-11 10:13:07 +03:00
|
|
|
data ConnectionParams = ConnectionParams RawConnInfo Q.ConnParams
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
defTxMode :: Q.TxMode
|
|
|
|
defTxMode = (Q.Serializable, Nothing)
|
|
|
|
|
|
|
|
resetStateTx :: Q.TxE PGQ.PGExecErr ()
|
|
|
|
resetStateTx = do
|
|
|
|
Q.unitQE PGQ.PGExecErrTx "DROP SCHEMA hdb_catalog CASCADE" () False
|
2018-07-11 10:13:07 +03:00
|
|
|
Q.unitQE PGQ.PGExecErrTx "DROP SCHEMA hdb_views CASCADE" () False
|
2018-06-27 16:11:32 +03:00
|
|
|
Q.unitQE PGQ.PGExecErrTx "DROP SCHEMA public CASCADE" () False
|
|
|
|
Q.unitQE PGQ.PGExecErrTx "CREATE SCHEMA public" () False
|
|
|
|
|
2018-07-11 10:13:07 +03:00
|
|
|
ravenApp :: RavenLogger -> PGQ.PGPool -> IO Application
|
|
|
|
ravenApp rlogger pool = do
|
|
|
|
let corsCfg = CorsConfigG "*" True -- cors is disabled
|
|
|
|
spockAsApp $ spockT id $ app Q.Serializable Nothing rlogger pool AMNoAuth corsCfg True -- no access key and no webhook
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = withStdoutLogger ravenLogGen $ \rlogger -> do
|
2018-07-11 10:13:07 +03:00
|
|
|
-- parse CLI flags for connection params
|
|
|
|
ConnectionParams rci cp <- parseArgs
|
|
|
|
-- form the postgres connection info
|
2018-06-27 16:11:32 +03:00
|
|
|
ci <- either ((>> exitFailure) . (putStrLn . connInfoErrModifier))
|
2018-07-06 08:13:46 +03:00
|
|
|
return $ mkConnInfo Nothing rci
|
2018-07-11 10:13:07 +03:00
|
|
|
-- intialize the pool
|
2018-06-27 16:11:32 +03:00
|
|
|
pool <- Q.initPGPool ci cp
|
2018-07-11 10:13:07 +03:00
|
|
|
-- reset state in the database
|
|
|
|
void $ liftIO $ runExceptT $ Q.runTx pool defTxMode resetStateTx
|
|
|
|
-- intialize state for graphql-engine in the database
|
|
|
|
liftIO $ initialise pool
|
|
|
|
specs <- mkSpecs
|
|
|
|
-- run the tests
|
|
|
|
withArgs [] $ hspecWith defaultConfig {configFormatter = Just progress} $
|
|
|
|
with (ravenApp rlogger pool) specs
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-07-11 10:13:07 +03:00
|
|
|
where
|
|
|
|
initialise :: Q.PGPool -> IO ()
|
|
|
|
initialise pool = do
|
|
|
|
currentTime <- getCurrentTime
|
|
|
|
res <- runExceptT $ Q.runTx pool defTxMode $ initCatalogSafe currentTime
|
|
|
|
either ((>> exitFailure) . (BLC.putStrLn . J.encode)) putStrLn res
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
|
2018-07-11 10:13:07 +03:00
|
|
|
parseArgs :: IO ConnectionParams
|
2018-06-27 16:11:32 +03:00
|
|
|
parseArgs = execParser opts
|
|
|
|
where
|
2018-07-11 10:13:07 +03:00
|
|
|
optParser = ConnectionParams <$> parseRawConnInfo <*> parseConnParams
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
opts = info (helper <*> optParser)
|
|
|
|
( fullDesc <>
|
2018-07-11 10:13:07 +03:00
|
|
|
header "graphql-engine-test")
|