From 4858c05221604ac0f4dc279eb486e38eb10e1802 Mon Sep 17 00:00:00 2001 From: paolino Date: Thu, 15 Jun 2023 13:22:11 +0000 Subject: [PATCH] Add backup vs access interaction test for the new migration implementation. --- lib/wallet/cardano-wallet.cabal | 1 + .../Wallet/DB/Sqlite/Migration/NewSpec.hs | 73 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 lib/wallet/test/unit/Cardano/Wallet/DB/Sqlite/Migration/NewSpec.hs diff --git a/lib/wallet/cardano-wallet.cabal b/lib/wallet/cardano-wallet.cabal index 35669e3536d..ee7bd43af3d 100644 --- a/lib/wallet/cardano-wallet.cabal +++ b/lib/wallet/cardano-wallet.cabal @@ -924,6 +924,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 diff --git a/lib/wallet/test/unit/Cardano/Wallet/DB/Sqlite/Migration/NewSpec.hs b/lib/wallet/test/unit/Cardano/Wallet/DB/Sqlite/Migration/NewSpec.hs new file mode 100644 index 00000000000..5f40222dfb4 --- /dev/null +++ b/lib/wallet/test/unit/Cardano/Wallet/DB/Sqlite/Migration/NewSpec.hs @@ -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')"