-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DB-Layer Unit Tests #88
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
56f8903
Increase coverage of PackfileSpec
rvl 8e871ec
Add more information to README.md
rvl d2dfa67
Add API to gh-pages build
rvl acf44ac
Merge pull request #98 from input-output-hk/rvl/94/coverage
KtorZ dc2ed8e
Add db layer unit tests
paweljakubas 1cee09a
Merge pull request #99 from input-output-hk/rvl/94/readme
KtorZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,8 +98,9 @@ jobs: | |
name: "Haddock" | ||
script: | ||
- tar xzf $STACK_WORK_CACHE | ||
- cp -Rv specifications/api api | ||
- mkdir -p haddock && mv $(stack path --local-doc-root)/* haddock | ||
- git add haddock && git commit -m $TRAVIS_COMMIT | ||
- git add api haddock && git commit -m $TRAVIS_COMMIT | ||
- git checkout gh-pages && git cherry-pick -X theirs -n - && git commit --allow-empty --no-edit | ||
- git push -f -q https://WilliamKingNoel-Bot:[email protected]/input-output-hk/cardano-wallet gh-pages &>/dev/null | ||
|
||
|
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 |
---|---|---|
@@ -1,14 +1,139 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
|
||
{-# OPTIONS_GHC -fno-warn-orphans #-} | ||
|
||
module Cardano.DBLayer.MVarSpec | ||
( spec | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.DBLayer | ||
( DBLayer (..), PrimaryKey (..) ) | ||
import Cardano.DBLayer.MVar | ||
() | ||
( newDBLayer ) | ||
import Cardano.Wallet | ||
( Wallet, WalletId (..), initWallet ) | ||
import Cardano.Wallet.Primitive | ||
( IsOurs (..) ) | ||
import Control.Concurrent.Async | ||
( mapConcurrently_ ) | ||
import Control.DeepSeq | ||
( NFData ) | ||
import Control.Monad.IO.Class | ||
( liftIO ) | ||
import Data.List.NonEmpty | ||
( NonEmpty ((:|)) ) | ||
import Test.Hspec | ||
( Spec ) | ||
( Spec, before, describe, it, shouldBe ) | ||
import Test.QuickCheck | ||
( Arbitrary (..), Property, choose, property, vectorOf ) | ||
import Test.QuickCheck.Monadic | ||
( monadicIO ) | ||
|
||
import qualified Data.Set as Set | ||
import qualified Data.Text as T | ||
|
||
spec :: Spec | ||
spec = return () | ||
spec = do | ||
describe "DB works as expected" $ before newDBLayer $ do | ||
it "readCheckpoints . putCheckpoints yields inserted checkpoints" $ | ||
\db -> (property $ dbReadCheckpointsProp db) | ||
it "replacement of values returns last value that was put" $ | ||
\db -> (property $ dbReplaceValsProp db) | ||
it "multiple sequential putCheckpoints work properly" $ | ||
\db -> (property $ dbMultiplePutsSeqProp db) | ||
it "multiple parallel putCheckpoints work properly" $ | ||
\db -> (property $ dbMultiplePutsParProp db) | ||
|
||
{------------------------------------------------------------------------------- | ||
Properties | ||
-------------------------------------------------------------------------------} | ||
|
||
|
||
dbReadCheckpointsProp | ||
:: DBLayer IO DummyState | ||
-> (PrimaryKey WalletId, DummyState) | ||
-> Property | ||
dbReadCheckpointsProp db (key, val) = monadicIO $ liftIO $ do | ||
putCheckpoints db key (toWalletState val) | ||
resFromDb <- readCheckpoints db key | ||
|
||
resFromDb `shouldBe` (Just $ toWalletState val) | ||
|
||
dbReplaceValsProp | ||
:: DBLayer IO DummyState | ||
-> (PrimaryKey WalletId, DummyState, DummyState) | ||
-> Property | ||
dbReplaceValsProp db (key, val1, val2) = monadicIO $ liftIO $ do | ||
putCheckpoints db key (toWalletState val1) | ||
putCheckpoints db key (toWalletState val2) | ||
resFromDb <- readCheckpoints db key | ||
|
||
resFromDb `shouldBe` (Just $ toWalletState val2) | ||
|
||
dbMultiplePutsSeqProp | ||
:: DBLayer IO DummyState | ||
-> KeyValPairs | ||
-> Property | ||
dbMultiplePutsSeqProp db (KeyValPairs keyValPairs) = monadicIO $ liftIO $ do | ||
mapM_ (\(key, val) -> putCheckpoints db key (toWalletState val)) keyValPairs | ||
resFromDb <- Set.fromList <$> readWallets db | ||
|
||
resFromDb `shouldBe` (Set.fromList (map fst keyValPairs)) | ||
|
||
dbMultiplePutsParProp | ||
:: DBLayer IO DummyState | ||
-> KeyValPairs | ||
-> Property | ||
dbMultiplePutsParProp db (KeyValPairs keyValPairs) = monadicIO $ liftIO $ do | ||
mapConcurrently_ (\(key, val) -> putCheckpoints db key (toWalletState val)) keyValPairs | ||
resFromDb <- Set.fromList <$> readWallets db | ||
|
||
resFromDb `shouldBe` (Set.fromList (map fst keyValPairs)) | ||
|
||
{------------------------------------------------------------------------------- | ||
Tests machinery, Arbitrary instances | ||
-------------------------------------------------------------------------------} | ||
|
||
|
||
newtype KeyValPairs = KeyValPairs [(PrimaryKey WalletId, DummyState)] | ||
deriving (Show, Eq) | ||
|
||
instance Arbitrary KeyValPairs where | ||
-- No shrinking | ||
arbitrary = do | ||
pairs <- choose (10, 50) >>= flip vectorOf arbitrary | ||
KeyValPairs <$> pure pairs | ||
|
||
newtype DummyState = DummyState Int | ||
deriving (Show, Eq) | ||
|
||
instance Arbitrary DummyState where | ||
-- No shrinking | ||
arbitrary = DummyState <$> arbitrary | ||
|
||
deriving instance NFData DummyState | ||
|
||
instance IsOurs DummyState where | ||
isOurs _ num = (True, num) | ||
|
||
instance Semigroup DummyState where | ||
(DummyState num1) <> (DummyState num2) | ||
= DummyState (num1 + num2) | ||
|
||
deriving instance Show (PrimaryKey WalletId) | ||
|
||
instance Arbitrary (PrimaryKey WalletId) where | ||
-- No shrinking | ||
arbitrary = do | ||
nums <- vectorOf 10 $ choose (0 :: Int, 9) | ||
let key = (T.pack . show) nums | ||
fmap PrimaryKey $ WalletId <$> pure key | ||
|
||
toWalletState | ||
:: (IsOurs s, Semigroup s, NFData s, Show s) => s | ||
-> NonEmpty (Wallet s) | ||
toWalletState val = initWallet val :| [] |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me wonder if the DBLayer shouldn't just return a
Set
here in a first place 🤔 ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for me using list suggests that the events/data are stored with "time order". For distributed systems with high throughput this is very high requirement, and usually it is better to start with
was stored
requirement (which impliesSet
). Probably, here at this point it does not matter. But to be honest, I do not know requirement hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm.. That's a bit of a false assumption here. Especially because there's no particular ordering defined on the checkpoints and therefore, there's no guarantee that the DB layers will preserve the insertion order.
Yet, one thing a
Set
will convey is the absence of duplicate in the data, which is right.