Skip to content

Commit

Permalink
DB.Sqlite: add very basic test of create wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed May 9, 2019
1 parent f60e5ce commit e3aa33d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
6 changes: 1 addition & 5 deletions lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,9 @@ test-suite unit
, swagger2
, text
, text-class
, time
, transformers
, yaml
, persistent
, persistent-sqlite
, conduit
, monad-logger
, mtl
type:
exitcode-stdio-1.0
hs-source-dirs:
Expand Down
61 changes: 36 additions & 25 deletions lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.Wallet.DB.SqliteSpec
( spec
Expand All @@ -9,34 +12,42 @@ module Cardano.Wallet.DB.SqliteSpec
import Prelude

import Cardano.Wallet.DB.Sqlite
( migrateAll )
import Conduit
( MonadUnliftIO, ResourceT, runResourceT )
import Control.Monad.Logger
( LoggingT, runStderrLoggingT )
import Control.Monad.Reader
( ReaderT )
import Data.Text
( Text )
import Database.Persist.Sqlite
( SqlBackend, runMigration, runSqlConn, withSqliteConn )
( newDBLayer )
import Crypto.Hash
( hash )
import Data.ByteString
( ByteString )
import Data.Time.Clock
( getCurrentTime )
import Test.Hspec
( Spec, describe, it, shouldReturn )

runSqlite'
:: (MonadUnliftIO m)
=> Text
-> ReaderT SqlBackend (LoggingT (ResourceT m)) a
-> m a
runSqlite' connstr =
runResourceT . runStderrLoggingT . withSqliteConn connstr . runSqlConn

testMigrate :: IO ()
testMigrate = runSqlite' ":memory:" $ do
runMigration migrateAll
import Cardano.Wallet
( unsafeRunExceptT )
import Cardano.Wallet.DB
import Cardano.Wallet.Primitive.Types
( WalletDelegation (..)
, WalletId (..)
, WalletMetadata (..)
, WalletName (..)
, WalletPassphraseInfo (..)
, WalletState (..)
)

spec :: Spec
spec = do
describe "Generated SQL schema" $ do
it "looks like SQL" $ do
testMigrate `shouldReturn` ()
describe "Wallet table" $ do
it "create and list works" $ do
db <- newDBLayer Nothing
now <- getCurrentTime
let wid = PrimaryKey (WalletId (hash ("test" :: ByteString)))
md = WalletMetadata
{ name = WalletName "test wallet"
, passphraseInfo = WalletPassphraseInfo now
, status = Ready
, delegation = NotDelegating
}
unsafeRunExceptT (createWallet db wid undefined md) `shouldReturn` ()
listWallets db `shouldReturn` [wid]

deriving instance Show (PrimaryKey WalletId)

0 comments on commit e3aa33d

Please sign in to comment.