Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommandResult golden coverage #754

Merged
merged 2 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions golden/accounts-module-failureCR/golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"gas":0,"result":{"status":"failure","error":{"callStack":["golden/golden.accounts.repl:45:4: (with-read (deftable accounts:(defschema account \"Row type fo... \"a\" ({g:<c> \"rowguard\"} [(native `enforce-guard` Exec...)","golden/golden.accounts.repl:151:21: (USER_GUARD \"a\")","golden/golden.accounts.repl:151:4: (with-capability ((defcap accounts.USER_GUARD:<a> (id:<b>)) \"a\") [(native `with-read` Special form to read row fro...)","golden/golden.accounts.repl:85:6: (debit \"a\" 1.0 true {\"transfer-to\": \"b\"})","golden/golden.accounts.repl:84:4: (with-capability ((defcap accounts.TRANSFER:<d> ())) [((defun accounts.debit:<as> (acct:<at> amount:<au...)","<interactive>:0:0: (transfer \"a\" \"b\" 1.0 true)"],"type":"TxFailure","message":"with-read: row not found: a","info":"golden/golden.accounts.repl:45:4"}},"reqKey":"DldRwCblQ7Loqy6wYJnaodHl30d3j3eH-qtFzfEv46g","logs":null,"metaData":null,"continuation":null,"txId":null}
1 change: 1 addition & 0 deletions golden/accounts-module-successCR/golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"gas":0,"result":{"status":"success","data":1},"reqKey":"DldRwCblQ7Loqy6wYJnaodHl30d3j3eH-qtFzfEv46g","logs":"wsATyGqckuIvlm89hhd2j4t6RMkCrcwJe_oeCYr7Th8","metaData":null,"continuation":null,"txId":null}
65 changes: 58 additions & 7 deletions tests/GoldenSpec.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

Expand All @@ -8,30 +9,45 @@ module GoldenSpec
where

import Control.Exception
import Control.Lens
import Data.Aeson
import qualified Data.ByteString.Lazy as BL
import Data.Default
import Data.Either
import Data.Text (Text)
import System.Directory

import Test.Hspec
import Test.Hspec.Golden


import Pact.Gas
import Pact.Interpreter
import Pact.Parse
import Pact.Repl
import Pact.Repl.Types
import Pact.Server.PactService
import Pact.Types.Command
import Pact.Types.Logger
import Pact.Types.Names
import Pact.Types.Persistence
import Pact.Types.RPC
import Pact.Types.Runtime
import Pact.Types.SPV

spec :: Spec
spec = do
describe "goldenAccounts" $
goldenModule "accounts-module" "golden/golden.accounts.repl" "accounts"
[("successCR",acctsSuccessCR)
,("failureCR",acctsFailureCR)
]
describe "goldenAutoCap" $
goldenModule "autocap-module" "golden/golden.autocap.repl" "auto-caps-mod"
goldenModule "autocap-module" "golden/golden.autocap.repl" "auto-caps-mod" []

goldenModule
:: String -> FilePath -> ModuleName -> Spec
goldenModule tn fp mn = after_ (cleanupActual tn) $ do
:: String -> FilePath -> ModuleName -> [(String, String -> ReplState -> Spec)] -> Spec
goldenModule tn fp mn tests = after_ (cleanupActual tn (map fst tests)) $ do
(r,s) <- runIO $ execScript' Quiet fp
it ("loads " ++ fp) $ r `shouldSatisfy` isRight
mr <- runIO $ replLookupModule s mn
Expand All @@ -41,13 +57,48 @@ goldenModule tn fp mn = after_ (cleanupActual tn) $ do
Left e -> it "failed to convert to PersistDirect" $ expectationFailure (show e)
Right m' -> do
it "matches golden" $ golden tn m'
(`mapM_` tests) $ \(n,f) -> do
describe n $ f (subTestName tn n) s

subTestName :: String -> String -> String
subTestName tn n = tn ++ "-" ++ n

cleanupActual :: String -> IO ()
cleanupActual testname =
catch (removeFile $ "golden/" ++ testname ++ "/actual")
(\(_ :: SomeException) -> return ())
acctsSuccessCR :: String -> ReplState -> Spec
acctsSuccessCR tn s = doCRTest tn s "1"

acctsFailureCR :: String -> ReplState -> Spec
acctsFailureCR tn s = doCRTest tn s "(accounts.transfer \"a\" \"b\" 1.0 true)"

doCRTest :: String -> ReplState -> Text -> Spec
doCRTest tn s code = do
let dbEnv = PactDbEnv (view (rEnv . eePactDb) s) (view (rEnv . eePactDbVar) s)
cmd = Command payload [] initialHash
payload = Payload exec "" pubMeta [] Nothing
pubMeta = def
parsedCode = either error id $ parsePact code
exec = Exec $ ExecMsg parsedCode Null
r <- runIO $ applyCmd (newLogger neverLog "") Nothing dbEnv (constGasModel 0) 0 0 ""
noSPVSupport Local cmd (ProcSucc cmd)
-- due to weird StackTrace encoding, we are only interested in ToJSON, so we'll
-- golden on the encoded LBS only
let encoded = encode r
it "matches golden encoded" $ Golden
{ output = encoded
, encodePretty = show
, writeToFile = BL.writeFile
, readFromFile = BL.readFile
, testName = tn
, directory = "golden"
}


cleanupActual :: String -> [String] -> IO ()
cleanupActual testname subs = do
go testname
mapM_ (\n -> go (subTestName testname n)) subs
where
go tn = catch (removeFile $ "golden/" ++ tn ++ "/actual")
(\(_ :: SomeException) -> return ())

golden :: (Show a,FromJSON a,ToJSON a) => String -> a -> Golden a
golden name obj = Golden
Expand Down