-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
74 additions
and
0 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
73 changes: 73 additions & 0 deletions
73
lib/wallet/test/unit/Cardano/Wallet/DB/Sqlite/Migration/NewSpec.hs
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,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')" |