Skip to content

Commit ad5d621

Browse files
committed
Add unit tests for 0-way and 1-way unions
1 parent 4b53dea commit ad5d621

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

test/Test/Database/LSMTree/UnitTests.hs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
module Test.Database.LSMTree.UnitTests (tests) where
77

8+
import Control.Monad (void)
89
import Control.Tracer (nullTracer)
910
import Data.ByteString (ByteString)
1011
import qualified Data.ByteString.Char8 as BS
@@ -15,22 +16,32 @@ import qualified System.FS.API as FS
1516

1617
import Database.LSMTree as R
1718

18-
import Control.Exception (Exception, try)
19+
import Control.Exception (Exception, bracket, try)
1920
import Database.LSMTree.Extras.Generators (KeyForIndexCompact)
2021
import qualified Test.QuickCheck as QC
2122
import Test.Tasty (TestTree, testGroup)
2223
import Test.Tasty.HUnit
24+
import Test.Tasty.QuickCheck (Property, testProperty)
2325
import Test.Util.FS (withTempIOHasBlockIO)
2426

2527

2628
tests :: TestTree
2729
tests =
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

3647
unit_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+
149198
ignoreBlobRef :: Functor f => f (BlobRef m b) -> f ()
150199
ignoreBlobRef = fmap (const ())
151200

0 commit comments

Comments
 (0)