-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[23] Add mockup unit test [23] Add create/get wallet tests [23] fix last test
- Loading branch information
1 parent
d1c2a87
commit fc23a5f
Showing
5 changed files
with
159 additions
and
13 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
module Cardano.NetworkLayer.HttpBridgeSpec | ||
( spec | ||
, mockNetworkLayer | ||
, noLog | ||
) where | ||
|
||
import Prelude | ||
|
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,127 @@ | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# OPTIONS_GHC -fno-warn-orphans #-} | ||
|
||
module Cardano.WalletLayerSpec | ||
( spec | ||
) where | ||
|
||
|
||
import Prelude | ||
|
||
import Cardano.DBLayer | ||
( DBLayer (..), PrimaryKey (..) ) | ||
import Cardano.DBLayer.MVar | ||
( newDBLayer ) | ||
import Cardano.NetworkLayer.HttpBridgeSpec | ||
( mockNetworkLayer, noLog ) | ||
import Cardano.Wallet | ||
( WalletId (..), WalletName (..) ) | ||
import Cardano.Wallet.AddressDerivationSpec | ||
() | ||
import Cardano.Wallet.AddressDiscovery | ||
( SeqState ) | ||
import Cardano.Wallet.AddressDiscoverySpec | ||
() | ||
import Cardano.Wallet.MnemonicSpec | ||
() | ||
import Cardano.Wallet.Primitive | ||
( SlotId (..) ) | ||
import Cardano.WalletLayer | ||
( NewWallet (..), WalletLayer (..), mkWalletLayer ) | ||
import Control.Monad.IO.Class | ||
( liftIO ) | ||
import Control.Monad.Trans.Except | ||
( runExceptT ) | ||
import Data.Either | ||
( isLeft, isRight ) | ||
import Data.Maybe | ||
( isJust ) | ||
import Test.Hspec | ||
( Spec, describe, it, shouldSatisfy ) | ||
import Test.QuickCheck | ||
( Arbitrary (..), Property, checkCoverage ) | ||
import Test.QuickCheck.Monadic | ||
( monadicIO ) | ||
|
||
import qualified Data.Text as T | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "WalletLayer works as expected" $ do | ||
it "Wallet upon creation is written down in db and cannot be created more than once" | ||
(checkCoverage walletCreationProp) | ||
it "Wallet after being created can be got using valid wallet Id" | ||
(checkCoverage walletGetProp) | ||
it "Wallet with wrong wallet Id cannot be got" | ||
(checkCoverage walletGetWrongIdProp) | ||
|
||
|
||
{------------------------------------------------------------------------------- | ||
Test Logic | ||
-------------------------------------------------------------------------------} | ||
|
||
data WalletLayerFixture = WalletLayerFixture { | ||
_fixtureDBLayer :: DBLayer IO SeqState | ||
, _fixtureWalletLayer :: WalletLayer IO SeqState | ||
} | ||
|
||
prepareWalletLayerFixture :: IO WalletLayerFixture | ||
prepareWalletLayerFixture = do | ||
db <- newDBLayer | ||
let network = mockNetworkLayer noLog 105 (SlotId 106 1492) | ||
let walletLayer = mkWalletLayer db network | ||
return $ WalletLayerFixture db walletLayer | ||
|
||
|
||
instance Arbitrary NewWallet where | ||
-- No shrinking | ||
arbitrary = NewWallet | ||
<$> arbitrary | ||
<*> arbitrary | ||
<*> pure (WalletName "My Wallet") | ||
<*> arbitrary | ||
<*> arbitrary | ||
|
||
|
||
walletCreationProp | ||
:: NewWallet | ||
-> Property | ||
walletCreationProp newWallet = monadicIO $ liftIO $ do | ||
(WalletLayerFixture db wl) <- liftIO prepareWalletLayerFixture | ||
res <- runExceptT $ createWallet wl newWallet | ||
case res of | ||
Left e -> | ||
fail $ show e | ||
Right walletId -> do | ||
resFromDb <- readCheckpoints db (PrimaryKey walletId) | ||
resFromDb `shouldSatisfy` isJust | ||
--the same wallet creation second time | ||
secondTrial <- runExceptT $ createWallet wl newWallet | ||
secondTrial `shouldSatisfy` isLeft | ||
|
||
walletGetProp | ||
:: NewWallet | ||
-> Property | ||
walletGetProp newWallet = monadicIO $ liftIO $ do | ||
(WalletLayerFixture _db wl) <- liftIO prepareWalletLayerFixture | ||
res <- runExceptT $ createWallet wl newWallet | ||
case res of | ||
Left e -> | ||
fail $ show e | ||
Right walletId -> do | ||
resFromGet <- runExceptT $ getWallet wl walletId | ||
resFromGet `shouldSatisfy` isRight | ||
|
||
walletGetWrongIdProp | ||
:: NewWallet | ||
-> Property | ||
walletGetWrongIdProp newWallet = monadicIO $ liftIO $ do | ||
(WalletLayerFixture _db wl) <- liftIO prepareWalletLayerFixture | ||
res <- runExceptT $ createWallet wl newWallet | ||
case res of | ||
Left e -> | ||
fail $ show e | ||
Right (WalletId walletId) -> do | ||
let corruptedWalletId = WalletId $ T.append "@ " walletId | ||
attempt <- runExceptT $ getWallet wl corruptedWalletId | ||
attempt `shouldSatisfy` isLeft |