Skip to content

Commit

Permalink
Abstract DB creation
Browse files Browse the repository at this point in the history
Summary:
The Driver can now override the DB creation. This is needed so that we
can run snapshot tests on stacked or incremental DBs.

Reviewed By: donsbot

Differential Revision: D51122577

fbshipit-source-id: c6e4febd6cfd05d085131565cf1bbf35d24d51a4
  • Loading branch information
Simon Marlow authored and facebook-github-bot committed Nov 9, 2023
1 parent 5f806e5 commit 6b2f6c4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
32 changes: 10 additions & 22 deletions glean/test/regression/Glean/Regression/Indexer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@

module Glean.Regression.Indexer
( withTestBackend
, withTestDatabase
, runIndexerForTest
) where

import Control.Exception
import qualified Data.IntMap as IntMap
import System.Exit
import System.FilePath

import Glean
import Glean.Database.Config
import Glean.Database.Schema.ComputeIds
import Glean.Database.Types
Expand Down Expand Up @@ -45,23 +43,13 @@ withTestBackend test action =
Nothing -> throwIO $ ErrorCall $ "unknown schema version: " <> show ver
Just id -> return env { envSchemaId = Just id }

-- | Run the supplied Indexer to populate a temporary DB
withTestDatabase
:: Some LocalOrRemote
-> RunIndexer
-> Maybe Dependencies
-> TestConfig
-> (Repo -> IO a)
-> IO a
withTestDatabase backend indexer maybeDeps test action = do
let
repo = testRepo test
params = IndexerParams {
indexerRoot = testRoot test,
indexerProjectRoot = testProjectRoot test,
indexerOutput = testOutput test,
indexerGroup = testGroup test
runIndexerForTest :: Some LocalOrRemote -> RunIndexer -> TestConfig -> IO ()
runIndexerForTest backend indexer test =
indexer backend (testRepo test) params
where
params = IndexerParams {
indexerRoot = testRoot test,
indexerProjectRoot = testProjectRoot test,
indexerOutput = testOutput test,
indexerGroup = testGroup test
}
fillDatabase backend repo "" maybeDeps (die "repo already exists") $
indexer backend repo params
action repo
4 changes: 2 additions & 2 deletions glean/test/regression/Glean/Regression/Snapshot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ runTest
runTest driver@Driver{..} driverOpts root testIn =
withTestBackend testIn $ \backend -> do
let index = indexerRun driverIndexer driverOpts
withTestDatabase backend index Nothing testIn $ \_ ->
runQueries backend driver root testIn
driverCreateDatabase driverOpts backend index testIn
runQueries backend driver root testIn

-- | Run the queries
runQueries
Expand Down
23 changes: 23 additions & 0 deletions glean/test/regression/Glean/Regression/Snapshot/Driver.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ module Glean.Regression.Snapshot.Driver
, externalDriver
) where

import System.Exit

import Glean
import Glean.LocalOrRemote
import Glean.Indexer
import Glean.Indexer.External
import Glean.Regression.Config
import Glean.Regression.Indexer
import Glean.Regression.Snapshot.Transform
import Glean.Util.Some

-- | A test driver describes how to run a set of tests. It is passed to
-- 'Glean.Regression.Snapshot.testMain' to make a complete test executable.
Expand All @@ -27,16 +34,32 @@ data Driver opts = Driver
-- executed once with 'testGroup' set to "".
, driverTransforms :: Transforms
-- ^ Additional query result transformers.
, driverCreateDatabase :: CreateDatabase opts
-- ^ How to create a DB for this driver
}

type CreateDatabase opts
= opts
-> Some LocalOrRemote
-> RunIndexer
-> TestConfig
-> IO ()

type Group = String

driverFromIndexer :: Indexer opts -> Driver opts
driverFromIndexer indexer = Driver
{ driverIndexer = indexer
, driverGroups = const []
, driverTransforms = mempty
, driverCreateDatabase = defaultCreateDatabase
}
where
defaultCreateDatabase :: CreateDatabase opts
defaultCreateDatabase _opts backend indexer test = do
let repo = testRepo test
fillDatabase backend repo "" Nothing (die "repo already exists") $
runIndexerForTest backend indexer test

-- | A 'Driver' using an external 'Indexer'. See
-- "Glean.Indexer.External".
Expand Down
7 changes: 4 additions & 3 deletions glean/test/regression/Glean/Regression/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ mainTestIndexGeneric driver extraOptParser dir testIndex = do

withSetup :: ((Some LocalOrRemote, Repo) -> IO a) -> IO a
withSetup f =
withTestBackend testConfig $ \backend ->
withTestDatabase backend index Nothing testConfig
$ \repo -> f (backend, repo)
withTestBackend testConfig $ \backend -> do
driverCreateDatabase driver driverOpts backend
index testConfig
f (backend, testRepo testConfig)

withLazy withSetup $ \get ->
fn $ TestLabel (mkLabel platform) $
Expand Down

0 comments on commit 6b2f6c4

Please sign in to comment.