Skip to content

Commit f182875

Browse files
committed
Test generators and shrinkers for snapshot metadata
1 parent be4552d commit f182875

File tree

1 file changed

+67
-37
lines changed
  • test/Test/Database/LSMTree/Internal/Snapshot

1 file changed

+67
-37
lines changed

test/Test/Database/LSMTree/Internal/Snapshot/Codec.hs

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import Codec.CBOR.Encoding
77
import Codec.CBOR.FlatTerm
88
import Codec.CBOR.Read
99
import Codec.CBOR.Write
10+
import Control.DeepSeq (NFData)
1011
import qualified Data.ByteString.Lazy as BSL
1112
import Data.Proxy
1213
import Data.Text (Text)
1314
import qualified Data.Text as Text
15+
import Data.Typeable
1416
import qualified Data.Vector as V
1517
import Database.LSMTree.Internal.Config
1618
import Database.LSMTree.Internal.Entry
@@ -21,6 +23,7 @@ import Database.LSMTree.Internal.Snapshot
2123
import Database.LSMTree.Internal.Snapshot.Codec
2224
import Test.Tasty
2325
import Test.Tasty.QuickCheck
26+
import Test.Util.Arbitrary
2427

2528
-- TODO: we should add golden tests for the CBOR encoders. This should prevent
2629
-- accidental breakage in the format.
@@ -34,45 +37,20 @@ tests = testGroup "Test.Database.LSMTree.Internal.Snapshot.Codec" [
3437
testProperty "roundtripCBOR" $ roundtripCBOR (Proxy @(Versioned SnapshotMetaData))
3538
, testProperty "roundtripFlatTerm" $ roundtripFlatTerm (Proxy @(Versioned SnapshotMetaData))
3639
]
37-
, testGroup "roundtripCBOR'" (propAll roundtripCBOR')
38-
, testGroup "roundtripFlatTerm'" (propAll roundtripFlatTerm')
40+
, testGroup "roundtripCBOR'" $
41+
propAll roundtripCBOR'
42+
, testGroup "roundtripFlatTerm'" $
43+
propAll roundtripFlatTerm'
44+
-- Test generators and shrinkers
45+
, testGroup "Generators and shrinkers are finite" $
46+
testAll $ \(p :: Proxy a) ->
47+
testGroup (show $ typeRep p) $
48+
prop_arbitraryAndShrinkPreserveInvariant @a deepseqInvariant
3949
]
4050

41-
-- | Run a property on all types in the snapshot metadata hierarchy.
42-
propAll ::
43-
( forall a. (Encode a, DecodeVersioned a, Eq a, Show a)
44-
=> Proxy a -> a -> Property
45-
)
46-
-> [TestTree]
47-
propAll prop = [
48-
-- SnapshotMetaData
49-
testProperty "SnapshotMetaData" $ prop (Proxy @SnapshotMetaData)
50-
, testProperty "SnapshotLabel" $ prop (Proxy @SnapshotLabel)
51-
, testProperty "SnapshotTableType" $ prop (Proxy @SnapshotTableType)
52-
-- TableConfig
53-
, testProperty "TableConfig" $ prop (Proxy @TableConfig)
54-
, testProperty "MergePolicy" $ prop (Proxy @MergePolicy)
55-
, testProperty "SizeRatio" $ prop (Proxy @SizeRatio)
56-
, testProperty "WriteBufferAlloc" $ prop (Proxy @WriteBufferAlloc)
57-
, testProperty "NumEntries" $ prop (Proxy @NumEntries)
58-
, testProperty "BloomFilterAlloc" $ prop (Proxy @BloomFilterAlloc)
59-
, testProperty "FencePointerIndex" $ prop (Proxy @FencePointerIndex)
60-
, testProperty "DiskCachePolicy" $ prop (Proxy @DiskCachePolicy)
61-
, testProperty "MergeSchedule" $ prop (Proxy @MergeSchedule)
62-
-- SnapLevels
63-
, testProperty "SnapLevels" $ prop (Proxy @(SnapLevels RunNumber))
64-
, testProperty "SnapLevel" $ prop (Proxy @(SnapLevel RunNumber))
65-
, testProperty "Vector RunNumber" $ prop (Proxy @(V.Vector RunNumber))
66-
, testProperty "RunNumber" $ prop (Proxy @RunNumber)
67-
, testProperty "SnapIncomingRun" $ prop (Proxy @(SnapIncomingRun RunNumber))
68-
, testProperty "NumRuns" $ prop (Proxy @NumRuns)
69-
, testProperty "MergePolicyForLevel" $ prop (Proxy @MergePolicyForLevel)
70-
, testProperty "UnspentCredits" $ prop (Proxy @UnspentCredits)
71-
, testProperty "MergeKnownCompleted" $ prop (Proxy @MergeKnownCompleted)
72-
, testProperty "SnapMergingRunState" $ prop (Proxy @(SnapMergingRunState RunNumber))
73-
, testProperty "SpentCredits" $ prop (Proxy @SpentCredits)
74-
, testProperty "Merge.Level" $ prop (Proxy @Merge.Level)
75-
]
51+
{-------------------------------------------------------------------------------
52+
Properties
53+
-------------------------------------------------------------------------------}
7654

7755
-- | @decode . encode = id@
7856
explicitRoundtripCBOR ::
@@ -148,6 +126,58 @@ roundtripFlatTerm' ::
148126
-> Property
149127
roundtripFlatTerm' _ = explicitRoundtripFlatTerm encode (decodeVersioned currentSnapshotVersion)
150128

129+
{-------------------------------------------------------------------------------
130+
Test and property runners
131+
-------------------------------------------------------------------------------}
132+
133+
type Constraints a = (
134+
Eq a, Show a, Typeable a, Arbitrary a
135+
, Encode a, DecodeVersioned a, NFData a
136+
)
137+
138+
-- | Run a property on all types in the snapshot metadata hierarchy.
139+
propAll ::
140+
(forall a. Constraints a => Proxy a -> a -> Property)
141+
-> [TestTree]
142+
propAll prop = testAll mkTest
143+
where
144+
mkTest :: forall a. Constraints a => Proxy a -> TestTree
145+
mkTest pa = testProperty (show $ typeRep pa) (prop pa)
146+
147+
-- | Run a test on all types in the snapshot metadata hierarchy.
148+
testAll ::
149+
(forall a. Constraints a => Proxy a -> TestTree)
150+
-> [TestTree]
151+
testAll test = [
152+
-- SnapshotMetaData
153+
test (Proxy @SnapshotMetaData)
154+
, test (Proxy @SnapshotLabel)
155+
, test (Proxy @SnapshotTableType)
156+
-- TableConfig
157+
, test (Proxy @TableConfig)
158+
, test (Proxy @MergePolicy)
159+
, test (Proxy @SizeRatio)
160+
, test (Proxy @WriteBufferAlloc)
161+
, test (Proxy @NumEntries)
162+
, test (Proxy @BloomFilterAlloc)
163+
, test (Proxy @FencePointerIndex)
164+
, test (Proxy @DiskCachePolicy)
165+
, test (Proxy @MergeSchedule)
166+
-- SnapLevels
167+
, test (Proxy @(SnapLevels RunNumber))
168+
, test (Proxy @(SnapLevel RunNumber))
169+
, test (Proxy @(V.Vector RunNumber))
170+
, test (Proxy @RunNumber)
171+
, test (Proxy @(SnapIncomingRun RunNumber))
172+
, test (Proxy @NumRuns)
173+
, test (Proxy @MergePolicyForLevel)
174+
, test (Proxy @UnspentCredits)
175+
, test (Proxy @MergeKnownCompleted)
176+
, test (Proxy @(SnapMergingRunState RunNumber))
177+
, test (Proxy @SpentCredits)
178+
, test (Proxy @Merge.Level)
179+
]
180+
151181
{-------------------------------------------------------------------------------
152182
Arbitrary: versioning
153183
-------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)