-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
module Hydra.SqlLitePersistenceSpec where | ||
|
||
import Hydra.Prelude hiding (drop, label) | ||
import Test.Hydra.Prelude | ||
|
||
import Data.Aeson (Value (..)) | ||
import Data.Aeson qualified as Aeson | ||
import Data.Text qualified as Text | ||
import Hydra.SqlLitePersistence (Persistence (..), PersistenceIncremental (..), createPersistence, createPersistenceIncremental) | ||
import Test.QuickCheck (checkCoverage, cover, elements, oneof, (===)) | ||
import Test.QuickCheck.Gen (listOf) | ||
import Test.QuickCheck.Monadic (monadicIO, monitor, pick, run) | ||
|
||
dbName :: String | ||
dbName = "testdb" | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "SqlLitePersistence" $ do | ||
let persistence = createPersistence dbName | ||
it "can handle empty reads" $ do | ||
Persistence{load} <- persistence | ||
load `shouldReturn` (Nothing :: Maybe Aeson.Value) | ||
|
||
it "is consistent after save/load roundtrip" $ | ||
checkCoverage $ | ||
monadicIO $ do | ||
Persistence{save, load} <- run persistence | ||
item <- pick genPersistenceItem | ||
actualResult <- run $ do | ||
save item | ||
load | ||
pure $ actualResult === Just item | ||
|
||
describe "PersistenceIncremental" $ do | ||
let persistenceIncremental = createPersistenceIncremental dbName | ||
fit "can handle empty reads" $ do | ||
PersistenceIncremental{loadAll} <- persistenceIncremental | ||
loadAll `shouldReturn` ([] :: [Aeson.Value]) | ||
|
||
fit "is consistent after multiple append calls" $ | ||
checkCoverage $ | ||
monadicIO $ do | ||
PersistenceIncremental{loadAll, append} <- run persistenceIncremental | ||
items <- pick $ listOf genPersistenceItem | ||
monitor (cover 1 (null items) "no items stored") | ||
actualResult <- run $ do | ||
forM_ items append | ||
loadAll | ||
pure $ all (`elem` actualResult) items | ||
|
||
genPersistenceItem :: Gen Aeson.Value | ||
genPersistenceItem = | ||
oneof | ||
[ pure Null | ||
, String <$> genSomeText | ||
] | ||
|
||
genSomeText :: Gen Text | ||
genSomeText = do | ||
let t = ['A' .. 'z'] <> ['\n', '\t', '\r'] | ||
Text.pack <$> listOf (elements t) | ||
|
||
containsNewLine :: [Aeson.Value] -> Bool | ||
containsNewLine = \case | ||
[] -> False | ||
(i : is) -> case i of | ||
String t | "\n" `Text.isInfixOf` t -> True | ||
_ -> containsNewLine is |