Skip to content
Permalink
Browse files

Handling Ready <-> Restoring Percent 100 and adding exemplary unit tests

  • Loading branch information...
paweljakubas committed May 15, 2019
1 parent 2d62186 commit 8da7be4ad146768bb44f5817dbd3f74cc4b0f019
Showing with 49 additions and 4 deletions.
  1. +4 −1 lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs
  2. +45 −3 lib/core/test/unit/Cardano/Wallet/DBSpec.hs
@@ -26,6 +26,7 @@ import Cardano.Wallet.DBSpec
, prop_putBeforeInit
, prop_readAfterDelete
, prop_readAfterPut
, prop_readAfterPutBoundary
, prop_removeWalletTwice
, prop_sequentialPut
, readTxHistoryF
@@ -55,12 +56,14 @@ spec = do
describe "put . read yields a result" $ do
xit "Checkpoint"
(property . (prop_readAfterPut putCheckpoint readCheckpoint))
xit "Wallet Metadata"
it "Wallet Metadata"
(property . (prop_readAfterPut putWalletMeta readWalletMeta))
xit "Tx History"
(property . (prop_readAfterPut putTxHistory readTxHistoryF))
it "Private Key"
(property . (prop_readAfterPut putPrivateKey readPrivateKey))
it "check that Restoring . Quantity 100 upon write results in Ready upon read"
(property . (prop_readAfterPutBoundary putWalletMeta readWalletMeta))

before (newDBLayer Nothing :: IO (DBLayer IO DummyStateSqlite DummyTarget)) $
describe "can't put before wallet exists" $ do
@@ -1,5 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
@@ -23,6 +24,7 @@ module Cardano.Wallet.DBSpec
, prop_createWalletTwice
, prop_removeWalletTwice
, prop_readAfterPut
, prop_readAfterPutBoundary
, prop_putBeforeInit
, prop_sequentialPut
, prop_isolation
@@ -84,7 +86,7 @@ import Data.Functor.Identity
import Data.Map.Strict
( Map )
import Data.Quantity
( Quantity (..) )
( Percentage, Quantity (..), mkPercentage )
import Data.Word
( Word32 )
import GHC.Generics
@@ -106,6 +108,7 @@ import Test.QuickCheck
, genericShrink
, oneof
, scale
, suchThat
, vectorOf
)
import Test.QuickCheck.Instances.Time
@@ -228,12 +231,17 @@ instance Arbitrary TxMeta where
<*> (SlotId <$> arbitrary <*> choose (0, 21600))
<*> fmap (Quantity . fromIntegral) (arbitrary @Word32)

customizedGen :: Gen Percentage
customizedGen = do
let (Right upperBound) = mkPercentage @Int 100
arbitraryBoundedEnum `suchThat` (/= upperBound)

instance Arbitrary WalletMetadata where
shrink _ = []
arbitrary = WalletMetadata
arbitrary = WalletMetadata
<$> (WalletName <$> elements ["bulbazaur", "charmander", "squirtle"])
<*> (fmap WalletPassphraseInfo <$> arbitrary)
<*> oneof [pure Ready, Restoring . Quantity <$> arbitraryBoundedEnum]
<*> oneof [pure Ready, Restoring . Quantity <$> customizedGen]
<*> pure NotDelegating

instance Arbitrary Address where
@@ -402,6 +410,40 @@ prop_readAfterPut putOp readOp dbLayer (key, a) =
res <- readOp db key
res `shouldBe` pure a

-- | Checks that a given resource can be read after having been inserted in DB for
-- boundary condition of Restoring . Quantity 100
prop_readAfterPutBoundary
:: ( Show (f WalletMetadata), Eq (f WalletMetadata), Applicative f
, Show s, Eq s, IsOurs s, NFData s, Arbitrary (Wallet s DummyTarget))
=> ( DBLayer IO s DummyTarget
-> PrimaryKey WalletId
-> WalletMetadata
-> ExceptT ErrNoSuchWallet IO ()
) -- ^ Put Operation
-> ( DBLayer IO s DummyTarget
-> PrimaryKey WalletId
-> IO (f WalletMetadata)
) -- ^ Read Operation
-> DBLayer IO s DummyTarget
-> (PrimaryKey WalletId, WalletMetadata)
-- ^ Property arguments
-> Property
prop_readAfterPutBoundary putOp readOp dbLayer (key, (WalletMetadata wmName wmPass _ wmDel)) = do
monadicIO (setup >>= prop)
where
meta p = WalletMetadata wmName wmPass (Restoring $ Quantity p) wmDel
metaReady = WalletMetadata wmName wmPass Ready wmDel
setup = do
cp <- pick arbitrary
let (Right upper) = mkPercentage @Int 100
liftIO $ unsafeRunExceptT $ createWallet dbLayer key cp (meta upper)
return dbLayer
prop db = liftIO $ do
let (Right upper) = mkPercentage @Int 100
unsafeRunExceptT $ putOp db key (meta upper)
res <- readOp db key
res `shouldBe` pure metaReady

-- | Can't put resource before a wallet has been initialized
prop_putBeforeInit
:: (Show (f a), Eq (f a), Applicative f, Show s, Eq s, IsOurs s, NFData s)

0 comments on commit 8da7be4

Please sign in to comment.
You can’t perform that action at this time.