Commit 471dceb6 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Setup DB during tests, first test passes

parent a2aec781
......@@ -265,14 +265,14 @@ CREATE INDEX ON public.nodes USING btree (user_id, typename, parent_id);
CREATE INDEX ON public.nodes USING btree (id, typename, date ASC);
CREATE INDEX ON public.nodes USING btree (id, typename, date DESC);
CREATE INDEX ON public.nodes USING btree (typename, id);
CREATE UNIQUE INDEX ON public.nodes USING btree (hash_id);
CREATE UNIQUE INDEX IF NOT EXISTS ON public.nodes USING btree (hash_id);
CREATE INDEX ON public.contexts USING gin (hyperdata);
CREATE INDEX ON public.contexts USING btree (user_id, typename, parent_id);
CREATE INDEX ON public.contexts USING btree (id, typename, date ASC);
CREATE INDEX ON public.contexts USING btree (id, typename, date DESC);
CREATE INDEX ON public.contexts USING btree (typename, id);
CREATE UNIQUE INDEX ON public.contexts USING btree (hash_id);
CREATE UNIQUE INDEX IF NOT EXISTS ON public.contexts USING btree (hash_id);
CREATE INDEX ON public.nodescontexts_nodescontexts USING btree (nodescontexts1, nodescontexts2);
-- CREATE UNIQUE INDEX ON public.nodes USING btree (((hyperdata ->> 'uniqId'::text)));
......
......@@ -13,6 +13,8 @@ license: AGPL-3
license-file: LICENSE
build-type: Simple
data-files:
devops/postgres/extensions.sql
devops/postgres/schema.sql
ekg-assets/index.html
ekg-assets/monitor.js
ekg-assets/monitor.css
......@@ -924,12 +926,14 @@ test-suite garg-test
, parsec
, patches-class
, patches-map
, postgres-options
, postgresql-simple
, quickcheck-instances
, raw-strings-qq
, recover-rtti
, resource-pool
, servant-job
, shelly
, stm
, tasty
, tasty-hspec
......
[gargantext]
URL = http://localhost
BACKEND_NAME = localhost
URL_BACKEND_API = http://localhost:8008/api/v1.0
......@@ -17,39 +16,3 @@ MAX_DOCS_SCRAPERS = 10000
JS_JOB_TIMEOUT = 1800
JS_ID_TIMEOUT = 1800
PUBMED_API_KEY = "no_key"
[server]
ALLOWED_ORIGIN = http://localhost
ALLOWED_ORIGIN_PORT = 8008
ALLOWED_HOST = localhost
ALLOWED_HOST_PORT = 3000
JWT_SETTINGS = TODO
[network]
MAIL = username@gargantext.org
HOST = localhost
[database]
DB_HOST = 127.0.0.1
DB_PORT = 5432
DB_NAME = gargandbV5
DB_USER = gargantua
DB_PASS = gargantua_test
[logs]
LOG_FILE = /var/log/gargantext/backend.log
LOG_LEVEL = LevelDebug
LOG_FORMATTER = verbose
[mail]
MAIL_PORT = 25
MAIL_HOST = localhost
MAIL_USER = gargantext
MAIL_PASSWORD =
MAIL_FROM =
MAIL_LOGIN_TYPE = Normal
[nlp]
EN = corenlp://localhost:9000
FR = spacy://localhost:8001
All = corenlp://localhost:9000
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Database.Operations where
import Control.Exception
import Control.Lens
import Control.Monad.Except
import Control.Monad.Reader
import Control.Monad.Trans.Control
import Data.Pool hiding (withResource)
import Data.String
import Gargantext.Core.Types.Individu
import Gargantext.Database.Action.User.New
import Gargantext.Database.Prelude
import Gargantext.Database.Query.Table.Node.Error
import Gargantext.Prelude
import Gargantext.Prelude.Config
import Prelude
import Shelly hiding (FilePath)
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.Hspec
import qualified Data.Pool as Pool
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified Database.PostgreSQL.Simple as PG
import qualified Database.PostgreSQL.Simple.Options as Client
import qualified Database.Postgres.Temp as Tmp
import Paths_gargantext
import Control.Lens
import Gargantext.Database.Query.Table.Node.Error
import Gargantext.Core.Types.Individu
-- | Test DB settings.
dbUser, dbPassword, dbName :: String
dbUser = "gargantua"
dbPassword = "gargantua_test"
dbName = "gargandbV5"
data TestEnv = TestEnv {
test_db :: !DBHandle
......@@ -53,23 +69,50 @@ instance HasConfig TestEnv where
fakeIniPath :: IO FilePath
fakeIniPath = getDataFileName "test-data/test_config.ini"
gargDBSchema :: IO FilePath
gargDBSchema = getDataFileName "devops/postgres/schema.sql"
gargDBExtensionsSchema :: IO FilePath
gargDBExtensionsSchema = getDataFileName "devops/postgres/extensions.sql"
teardown :: TestEnv -> IO ()
teardown TestEnv{..} = do
destroyAllResources $ _DBHandle test_db
Tmp.stop $ _DBTmp test_db
-- | Bootstraps the DB, by creating the DB and the schema.
bootstrapDB :: Tmp.DB -> Pool PG.Connection -> GargConfig -> IO ()
bootstrapDB tmpDB pool _cfg = Pool.withResource pool $ \conn -> do
void $ PG.execute_ conn (fromString $ "ALTER USER \"" <> dbUser <> "\" with PASSWORD '" <> dbPassword <> "'")
schemaPath <- gargDBSchema
let connString = Tmp.toConnectionString tmpDB
(res,ec) <- shelly $ silently $ escaping False $ do
result <- run "psql" ["-d", "\"" <> TE.decodeUtf8 connString <> "\"", "<", fromString schemaPath]
(result,) <$> lastExitCode
unless (ec == 0) $ throwIO (userError $ show ec <> ": " <> T.unpack res)
tmpPgConfig :: Tmp.Config
tmpPgConfig = Tmp.defaultConfig <>
Tmp.optionsToDefaultConfig mempty
{ Client.dbname = pure dbName
, Client.user = pure dbUser
, Client.password = pure dbPassword
}
setup :: IO TestEnv
setup = do
res <- Tmp.startConfig Tmp.defaultConfig
res <- Tmp.startConfig tmpPgConfig
case res of
Left err -> fail $ show err
Right db -> do
gargConfig <- fakeIniPath >>= readConfig
pool <- createPool (PG.connectPostgreSQL (Tmp.toConnectionString db))
(PG.close)
2
60
2
TestEnv <$> (pure $ DBHandle pool db) <*> (fakeIniPath >>= readConfig)
bootstrapDB db pool gargConfig
pure $ TestEnv (DBHandle pool db) gargConfig
tests :: TestTree
tests = withResource setup teardown $
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment