graphql-engine/server/test/Main.hs

82 lines
3.0 KiB
Haskell
Raw Normal View History

2018-06-27 16:11:32 +03:00
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Monad.Trans.Except
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
import Test.Hspec.Formatters
2018-06-27 16:11:32 +03:00
import Test.Hspec.Wai
import Web.Spock.Core (spockAsApp, spockT)
2018-06-27 16:11:32 +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
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
import qualified Database.PG.Query as Q
import qualified Database.PG.Query as PGQ
2018-06-27 16:11:32 +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
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
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
-- 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))
return $ mkConnInfo Nothing rci
-- intialize the pool
2018-06-27 16:11:32 +03:00
pool <- Q.initPGPool ci cp
-- 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
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
parseArgs :: IO ConnectionParams
2018-06-27 16:11:32 +03:00
parseArgs = execParser opts
where
optParser = ConnectionParams <$> parseRawConnInfo <*> parseConnParams
2018-06-27 16:11:32 +03:00
opts = info (helper <*> optParser)
( fullDesc <>
header "graphql-engine-test")