77-}
88
99module TestDB (
10+ WithDB ,
1011 withTestDB , withWritableTestDB , withStackedTestDB ,
11- dbTestCase , dbTestCaseWritable , dbTestCaseSettings , createTestDB
12+ dbTestCase , dbTestCaseWritable , dbTestCaseSettings , createTestDB ,
13+ withDbTests ,
1214) where
1315
1416import Data.Default
1517import Data.Either
18+ import Foreign.Marshal.Utils
1619import Test.HUnit
1720
21+ import Util.IO
22+
1823import Glean.Database.Storage (DBVersion (.. ), currentVersion , writableVersions )
1924import Glean.Database.Test
2025import Glean.Database.Types
@@ -29,25 +34,28 @@ import qualified Glean.Schema.Sys as Sys
2934
3035import TestData
3136
32- afterComplete :: (Env -> Thrift. Repo -> IO a ) -> Env -> Thrift. Repo -> IO a
37+ -- | An action that runs on a test DB
38+ type WithDB a = Env -> Thrift. Repo -> IO a
39+
40+ afterComplete :: WithDB a -> WithDB a
3341afterComplete action env repo = do
3442 completeTestDB env repo
3543 action env repo
3644
37- withTestDB :: [Setting ] -> ( Env -> Thrift. Repo -> IO a ) -> IO a
45+ withTestDB :: [Setting ] -> WithDB a -> IO a
3846withTestDB settings = withWritableTestDB settings . afterComplete
3947
40- createTestDB :: Env -> Thrift. Repo -> IO ()
48+ createTestDB :: WithDB ()
4149createTestDB env repo = do
4250 kickOffTestDB env repo id
4351 writeTestDB env repo testFacts
4452
45- withWritableTestDB :: [Setting ] -> ( Env -> Thrift. Repo -> IO a ) -> IO a
53+ withWritableTestDB :: [Setting ] -> WithDB a -> IO a
4654withWritableTestDB settings action = withEmptyTestDB settings $ \ env repo -> do
4755 writeTestDB env repo testFacts
4856 action env repo
4957
50- withStackedTestDB :: [Setting ] -> ( Env -> Thrift. Repo -> IO a ) -> IO a
58+ withStackedTestDB :: [Setting ] -> WithDB a -> IO a
5159withStackedTestDB settings action = withTestEnv settings $ \ env -> do
5260 kickOffTestDB env repo1 id
5361 writeTestDB env repo1 testFacts1
@@ -62,30 +70,53 @@ withStackedTestDB settings action = withTestEnv settings $ \env -> do
6270 repo1 = Thrift. Repo " dbtest-repo" " 1"
6371 repo2 = Thrift. Repo " dbtest-repo" " 2"
6472
65- testCases :: [Setting ] -> (Env -> Thrift. Repo -> IO () ) -> Test
66- testCases testCaseSettings action = TestList
67- [ TestLabel (label1 ++ label2) $
68- TestCase $ with (settings <> testCaseSettings) action
69- | (label1, with) <-
70- [ (" " , withWritableTestDB)
71- , (" stacked/" , withStackedTestDB) ]
72- , (label2, settings) <-
73+ newtype RunTest = RunTest (forall a . WithDB a -> IO a )
74+
75+ toTestCases :: [(String , RunTest )] -> WithDB () -> Test
76+ toTestCases testCases action = TestList
77+ [ TestLabel label $ TestCase $ with action
78+ | (label, RunTest with) <- testCases
79+ ]
80+
81+ dbFlavours :: [Setting ] -> [(String , RunTest )]
82+ dbFlavours testCaseSettings =
83+ [ (label1 ++ label2, run)
84+ | (label2, settings) <-
7385 [ (" memory" , [setMemoryStorage])
7486 , (" rocksdb" , [] )
7587 ]
7688 ++
7789 [ (" rocksdb-" ++ show (unDBVersion v), [setDBVersion v])
7890 | v <- writableVersions, v /= currentVersion ]
91+ , let allSettings = settings <> testCaseSettings
92+ , (label1, run) <-
93+ [ (" " , RunTest (withWritableTestDB allSettings))
94+ , (" stacked/" , RunTest (withStackedTestDB allSettings)) ]
7995 ]
8096
81- dbTestCase :: (Env -> Thrift. Repo -> IO () ) -> Test
82- dbTestCase = testCases [] . afterComplete
97+ -- | Run a test on several flavour of test DB. For a test suite
98+ -- with multiple tests, use 'withDbTests' instead.
99+ dbTestCase :: WithDB () -> Test
100+ dbTestCase = toTestCases (dbFlavours [] ) . afterComplete
101+
102+ -- | Like dbTestCase, but the test DBs are shared between multiple
103+ -- tests, rather than being created afresh for each test. Useful
104+ -- for speeding up test suites that have many individual tests.
105+ withDbTests :: ((WithDB () -> Test ) -> IO a ) -> IO a
106+ withDbTests fn =
107+ withMany lazify (dbFlavours [] ) $ fn . toTestCases
108+ where
109+ lazify :: (String , RunTest ) -> ((String , RunTest ) -> IO a ) -> IO a
110+ lazify (label, RunTest run) fn =
111+ withLazy (run . afterComplete . curry ) $ \ get ->
112+ fn (label, RunTest (\ with -> do (env, repo) <- get; with env repo))
83113
84- dbTestCaseWritable :: ( Env -> Thrift. Repo -> IO () ) -> Test
85- dbTestCaseWritable = testCases []
114+ dbTestCaseWritable :: WithDB ( ) -> Test
115+ dbTestCaseWritable = toTestCases (dbFlavours [] )
86116
87- dbTestCaseSettings :: [Setting ] -> (Env -> Thrift. Repo -> IO () ) -> Test
88- dbTestCaseSettings settings action = testCases settings (afterComplete action)
117+ dbTestCaseSettings :: [Setting ] -> WithDB () -> Test
118+ dbTestCaseSettings settings action =
119+ toTestCases (dbFlavours settings) (afterComplete action)
89120
90121writeTestDB :: Env -> Thrift. Repo -> (forall m . NewFact m => m () ) -> IO ()
91122writeTestDB env repo facts = do
0 commit comments