Skip to content

Commit

Permalink
Merge #3993
Browse files Browse the repository at this point in the history
3993: [ADP-3054] test migration backup r=paolino a=paolino

A small addition to this task to prove that DB backup and DB access work well on all platforms.

ADP-3054

Co-authored-by: paolino <[email protected]>
  • Loading branch information
iohk-bors[bot] and paolino authored Jun 16, 2023
2 parents ae49698 + 4858c05 commit ba1f811
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ test-suite unit
Cardano.Wallet.DB.MigrationSpec
Cardano.Wallet.DB.Properties
Cardano.Wallet.DB.Pure.ImplementationSpec
Cardano.Wallet.DB.Sqlite.Migration.NewSpec
Cardano.Wallet.DB.Sqlite.TypesSpec
Cardano.Wallet.DB.StateMachine
Cardano.Wallet.DB.Store.Checkpoints.StoreSpec
Expand Down
73 changes: 73 additions & 0 deletions lib/wallet/test/unit/Cardano/Wallet/DB/Sqlite/Migration/NewSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Copyright: © 2023 IOHK License: Apache-2.0
--
-- Tests for new database migration sqlite instance. A module that tests a new
-- database migration sqlite instance.
module Cardano.Wallet.DB.Sqlite.Migration.NewSpec
( spec
) where

import Cardano.DB.Sqlite
( DBHandle (dbBackend) )
import Cardano.Wallet.DB.Migration
( MigrationInterface (..), Version (..) )
import Cardano.Wallet.DB.Sqlite.Migration.New
( newMigrationInterface )
import Control.Tracer
( nullTracer )
import Data.List
( sort )
import Data.Text
( Text )
import Prelude hiding
( (.) )
import System.Directory
( listDirectory )
import System.IO.Temp
( withSystemTempDirectory )
import Test.Hspec
( Spec, describe, it, shouldReturn )
import UnliftIO
( MonadUnliftIO )

import qualified Database.Persist.Sqlite as Sqlite

{-----------------------------------------------------------------------------
Tests
------------------------------------------------------------------------------}
spec :: Spec
spec = do
describe "new migrations" $ do
it "handles backupDatabaseFile and withDatabaseFile" $ do
withSystemTempDirectory "test" $ \dir -> do
let interface = newMigrationInterface nullTracer
let dbf = dir <> "/db"
execute interface dbf createTable
backupDatabaseFile interface dbf $ Version 1
execute interface dbf populateTable
backupDatabaseFile interface dbf $ Version 2
sort <$> listDirectory dir `shouldReturn`
sort ["db", "db.v1.bak", "db.v2.bak"]

execute
:: MonadUnliftIO m
=> MigrationInterface m DBHandle
-> FilePath
-> Text
-> m ()
execute interface dbf t =
withDatabaseFile interface dbf $ \handle ->
Sqlite.runSqlConn
(Sqlite.rawExecute t [])
(dbBackend handle)

createTable :: Text
createTable =
"CREATE TABLE IF NOT EXISTS test \
\(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)"

populateTable :: Text
populateTable =
"INSERT INTO test (name) VALUES ('hello')"

0 comments on commit ba1f811

Please sign in to comment.