55
66module Test.Database.LSMTree.UnitTests (tests ) where
77
8+ import Control.Monad (void )
89import Control.Tracer (nullTracer )
910import Data.ByteString (ByteString )
1011import qualified Data.ByteString.Char8 as BS
@@ -15,22 +16,32 @@ import qualified System.FS.API as FS
1516
1617import Database.LSMTree as R
1718
18- import Control.Exception (Exception , try )
19+ import Control.Exception (Exception , bracket , try )
1920import Database.LSMTree.Extras.Generators (KeyForIndexCompact )
2021import qualified Test.QuickCheck as QC
2122import Test.Tasty (TestTree , testGroup )
2223import Test.Tasty.HUnit
24+ import Test.Tasty.QuickCheck (Property , testProperty )
2325import Test.Util.FS (withTempIOHasBlockIO )
2426
2527
2628tests :: TestTree
2729tests =
2830 testGroup " Test.Database.LSMTree.UnitTests"
29- [ testCaseSteps " unit_blobs" unit_blobs
30- , testCase " unit_closed_table" unit_closed_table
31- , testCase " unit_closed_cursor" unit_closed_cursor
32- , testCase " unit_twoTableTypes" unit_twoTableTypes
33- , testCase " unit_snapshots" unit_snapshots
31+ [ testCaseSteps " unit_blobs" unit_blobs
32+ , testCase " unit_closed_table" unit_closed_table
33+ , testCase " unit_closed_cursor" unit_closed_cursor
34+ , testCase " unit_twoTableTypes" unit_twoTableTypes
35+ , testCase " unit_snapshots" unit_snapshots
36+
37+ -- Properties
38+
39+ , testProperty " prop_unions_0" $
40+ -- TODO: enable once unions are implemented
41+ QC. expectFailure prop_unions_0
42+ , testProperty " prop_unions_1" $
43+ -- TODO: enable once unions are implemented
44+ QC. expectFailure prop_unions_1
3445 ]
3546
3647unit_blobs :: (String -> IO () ) -> Assertion
@@ -146,6 +157,44 @@ unit_snapshots =
146157 snap1 = " table1"
147158 snap2 = " table2"
148159
160+ -- | Unions of 0 tables fail with an exception
161+ prop_unions_0 :: Property
162+ prop_unions_0 =
163+ QC. once $ QC. ioProperty $
164+ assertException err $
165+ void $ unions @ _ @ Key1 @ Value1 @ Blob1 V. empty
166+ where
167+ -- TODO: fill in once unions has an implementation
168+ err :: LSMTreeError
169+ err = error " unit_unions_0: unions has no implementation yet"
170+
171+ -- | Unions of 1 table are equivalent to duplicate
172+ prop_unions_1 :: Property
173+ prop_unions_1 =
174+ QC. once $ QC. ioProperty $
175+ withTempIOHasBlockIO " test" $ \ hfs hbio ->
176+ withSession nullTracer hfs hbio (FS. mkFsPath [] ) $ \ sess ->
177+ withTable @ _ @ Key1 @ Value1 @ Blob1 sess defaultTableConfig $ \ table -> do
178+ inserts table [(Key1 17 , Value1 42 , Nothing )]
179+
180+ bracket (unions $ V. singleton table) close $ \ table' ->
181+ bracket (duplicate table) close $ \ table'' -> do
182+ inserts table [(Key1 17 , Value1 43 , Nothing )]
183+ inserts table [(Key1 17 , Value1 44 , Nothing )]
184+
185+ -- The original table is unmodified
186+ r <- lookups table [Key1 17 ]
187+ V. map ignoreBlobRef r @?= [Found (Value1 42 )]
188+
189+ -- The unioned table sees an updated value
190+ r' <- lookups table' [Key1 17 ]
191+ V. map ignoreBlobRef r' @?= [Found (Value1 43 )]
192+
193+ -- The duplicated table sees a different updated value
194+ r'' <- lookups table'' [Key1 17 ]
195+ V. map ignoreBlobRef r'' @?= [Found (Value1 44 )]
196+
197+
149198ignoreBlobRef :: Functor f => f (BlobRef m b ) -> f ()
150199ignoreBlobRef = fmap (const () )
151200
0 commit comments