Skip to content

Commit

Permalink
QSM: a little more clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed May 28, 2019
1 parent 92bb74b commit 87d7633
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
13 changes: 5 additions & 8 deletions lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import Cardano.Wallet.DB.Sqlite
( newDBLayer )
import Cardano.Wallet.DBSpec
( DummyTarget, dbPropertyTests, withDB )
import Cardano.Wallet.DBSpec2
( prop_sequential )
import Cardano.Wallet.Primitive.AddressDerivation
( Passphrase (..)
, encryptPassphrase
Expand Down Expand Up @@ -68,19 +70,14 @@ import System.IO.Unsafe
import Test.Hspec
( Spec, beforeAll, describe, it, shouldReturn )

import qualified Cardano.Wallet.DBSpec2 as QSM
import qualified Data.Map as Map

spec :: Spec
spec = do
describe "Simple tests" simpleSpec
-- describe "Sqlite Property tests" $ withDB newMemoryDBLayer dbPropertyTests
describe "Sqlite state machine tests" $ beforeAll (mkConnect Nothing) QSM.spec

mkConnect :: Maybe FilePath -> IO (IO (DBLayer IO (SeqState DummyTarget) DummyTarget))
mkConnect f = do
db <- newDBLayer f
pure (pure db) -- doh
describe "Sqlite state machine tests" $ beforeAll (pure $ newDBLayer Nothing) $
it "Sequential tests" prop_sequential
describe "Sqlite Property tests" $ withDB newMemoryDBLayer dbPropertyTests

simpleSpec :: Spec
simpleSpec = withDB newMemoryDBLayer $ do
Expand Down
24 changes: 9 additions & 15 deletions lib/core/test/unit/Cardano/Wallet/DBSpec2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.Wallet.DBSpec2
( spec
( prop_sequential
, showLabelledExamples
) where

Expand Down Expand Up @@ -66,10 +66,8 @@ import GHC.Generics
( Generic )
import System.Random
( getStdRandom, randomR )
import Test.Hspec
( SpecWith, describe, it )
import Test.QuickCheck
( Arbitrary (..), Gen, Property, quickCheck )
( Arbitrary (..), Gen, Property )
import Test.StateMachine
( CommandNames (..)
, Concrete
Expand Down Expand Up @@ -114,9 +112,9 @@ eval :: Expr MWid -> MWid
eval (Val wid) = wid
eval (Var wid) = wid

evalConcrete :: Expr WalletId -> WalletId
evalConcrete (Val mwid) = unMockWid mwid
evalConcrete (Var wid) = wid
evalWalletId :: Expr WalletId -> WalletId
evalWalletId (Val mwid) = unMockWid mwid
evalWalletId (Var wid) = wid

{-------------------------------------------------------------------------------
Errors
Expand All @@ -137,14 +135,13 @@ errWalletAlreadyExists (ErrWalletAlreadyExists wid) = WalletAlreadyExists wid
Mock implementation
-------------------------------------------------------------------------------}

-- | Shortcut for wallet type. We don't use SeqState yet.
-- | Shortcut for wallet type.
type MWallet = Wallet (SeqState DummyTarget) DummyTarget

-- | Mock wallet ID -- simple and easy to read
newtype MWid = MWid String
deriving (Show, Eq, Ord, Generic)


widPK :: MWid -> PrimaryKey WalletId
widPK = PrimaryKey . unMockWid

Expand All @@ -153,15 +150,17 @@ unMockWid :: MWid -> WalletId
unMockWid (MWid wid) = WalletId . hash . B8.pack $ wid

widPK' :: Expr WalletId -> PrimaryKey WalletId
widPK' = PrimaryKey . evalConcrete
widPK' = PrimaryKey . evalWalletId

-- | Represent (XPrv, Hash) as a string.
type MPrivKey = String

-- | Stuff a mock private key into the type used by DBLayer.
fromMockPrivKey :: MPrivKey -> (Key purpose XPrv, Hash "encryption")
fromMockPrivKey s = (k, Hash (B8.pack s))
where Right (k, _) = deserializeXPrv (B8.replicate 256 '0', mempty)

-- | Unstuff the DBLayer private key into the mock type.
toMockPrivKey :: (Key purpose XPrv, Hash "encryption") -> MPrivKey
toMockPrivKey (_, Hash h) = B8.unpack h

Expand Down Expand Up @@ -701,11 +700,6 @@ instance ToExpr MWid where
Top-level tests
-------------------------------------------------------------------------------}

spec :: SpecWith (IO (DBLayer IO (SeqState DummyTarget) DummyTarget))
spec = describe "DBLayer state machine tests" $ do
it "Sequential tests" $
quickCheck . prop_sequential

prop_sequential :: IO (DBLayer IO (SeqState DummyTarget) DummyTarget) -> Property
prop_sequential connect =
forAllCommands (sm dbFileUnused) Nothing $ \cmds ->
Expand Down

0 comments on commit 87d7633

Please sign in to comment.