From ec422c1c55f0b253dafe99ca32f9cd9b89038867 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 27 Feb 2024 08:33:12 -0600 Subject: [PATCH] Add accounts and address tables to model --- src/Zenith/DB.hs | 29 +++++++++++++--- test/Spec.hs | 88 ++++++++++++++++++++++++++++-------------------- zenith.cabal | 1 + 3 files changed, 77 insertions(+), 41 deletions(-) diff --git a/src/Zenith/DB.hs b/src/Zenith/DB.hs index c1d9310..6d4fbd7 100644 --- a/src/Zenith/DB.hs +++ b/src/Zenith/DB.hs @@ -31,16 +31,35 @@ share [persistLowerCase| ZcashWallet seedPhrase Phrase - spendingKey BS.ByteString - tPrivateKey BS.ByteString birthdayHeight Int name T.Text network ZcashNet deriving Show + ZcashAccount + walletId ZcashWalletId + index Int + orchSpendKey BS.ByteString + sapSpendKey BS.ByteString + tPrivateKey BS.ByteString + deriving Show + WalletAddress + accId ZcashAccountId + index Int + orchRec BS.ByteString Maybe + sapRec BS.ByteString Maybe + tRec BS.ByteString Maybe + encoded T.Text + deriving Show |] -getWallets :: ZcashNet -> IO [Entity ZcashWallet] -getWallets n = - runSqlite "zenith.db" $ do +getWallets :: T.Text -> ZcashNet -> IO [Entity ZcashWallet] +getWallets dbFp n = + runSqlite dbFp $ do s <- selectList [ZcashWalletNetwork ==. n] [] liftIO $ return s + +getAccounts :: T.Text -> ZcashWalletId -> IO [Entity ZcashAccount] +getAccounts dbFp w = + runSqlite dbFp $ do + s <- selectList [ZcashAccountWalletId ==. w] [] + liftIO $ return s diff --git a/test/Spec.hs b/test/Spec.hs index e3556ce..42f0b3d 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -1,51 +1,67 @@ {-# LANGUAGE OverloadedStrings #-} -import Control.Monad.IO.Class (liftIO) +import Control.Monad (when) import Database.Persist import Database.Persist.Sqlite +import System.Directory import Test.Hspec import ZcashHaskell.Types (ZcashNet(..)) import Zenith.DB -import Zenith.DB - ( EntityField(ZcashWalletId, ZcashWalletName) - , ZcashWallet(zcashWalletName) - ) main :: IO () main = do + checkDbFile <- doesFileExist "test.db" + when checkDbFile $ removeFile "test.db" hspec $ do describe "Database tests" $ do it "Create table" $ do s <- runSqlite "test.db" $ do runMigration migrateAll s `shouldBe` () - it "insert wallet record" $ do - s <- - runSqlite "test.db" $ do - insert $ - ZcashWallet - "one two three four five six seven eight nine ten eleven twelve" - "123456789" - "987654321" - 2000000 - "Main Wallet" - MainNet - fromSqlKey s `shouldBe` 1 - it "read wallet record" $ do - s <- - runSqlite "test.db" $ do - selectList [ZcashWalletBirthdayHeight >. 0] [] - length s `shouldBe` 1 - it "modify wallet record" $ do - s <- - runSqlite "test.db" $ do - let recId = toSqlKey 1 :: ZcashWalletId - update recId [ZcashWalletName =. "New Wallet"] - get recId - "New Wallet" `shouldBe` maybe "None" zcashWalletName s - it "delete wallet record" $ do - s <- - runSqlite "test.db" $ do - let recId = toSqlKey 1 :: ZcashWalletId - delete recId - get recId - "None" `shouldBe` maybe "None" zcashWalletName s + describe "Wallet Table" $ do + it "insert wallet record" $ do + s <- + runSqlite "test.db" $ do + insert $ + ZcashWallet + "one two three four five six seven eight nine ten eleven twelve" + 2000000 + "Main Wallet" + MainNet + fromSqlKey s `shouldBe` 1 + it "read wallet record" $ do + s <- + runSqlite "test.db" $ do + selectList [ZcashWalletBirthdayHeight >. 0] [] + length s `shouldBe` 1 + it "modify wallet record" $ do + s <- + runSqlite "test.db" $ do + let recId = toSqlKey 1 :: ZcashWalletId + update recId [ZcashWalletName =. "New Wallet"] + get recId + "New Wallet" `shouldBe` maybe "None" zcashWalletName s + it "delete wallet record" $ do + s <- + runSqlite "test.db" $ do + let recId = toSqlKey 1 :: ZcashWalletId + delete recId + get recId + "None" `shouldBe` maybe "None" zcashWalletName s + describe "Account table" $ do + it "insert account" $ do + s <- + runSqlite "test.db" $ do + insert $ + ZcashWallet + "one two three four five six seven eight nine ten eleven twelve" + 2000000 + "Main Wallet" + MainNet + t <- + runSqlite "test.db" $ do + insert $ ZcashAccount s 0 "132465798" "987654321" "739182462" + fromSqlKey t `shouldBe` 1 + it "read accounts for wallet" $ do + wList <- getWallets "test.db" MainNet + acc <- getAccounts "test.db" $ entityKey (head wList) + length acc `shouldBe` 1 diff --git a/zenith.cabal b/zenith.cabal index d3e5b78..b14f4ea 100644 --- a/zenith.cabal +++ b/zenith.cabal @@ -104,6 +104,7 @@ test-suite zenith-tests , persistent , persistent-sqlite , hspec + , directory , zcash-haskell , zenith pkgconfig-depends: rustzcash_wrapper