Skip to content

Commit

Permalink
use constants instead of hard-coded numbers in Transactions Scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed May 21, 2019
1 parent 37daa16 commit 252cd12
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
20 changes: 14 additions & 6 deletions lib/http-bridge/test/integration/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand Down Expand Up @@ -52,6 +53,7 @@ module Test.Integration.Framework.DSL
, json
, tearDown
, fixtureWallet
, oneMillionAda

-- * CLI
, cardanoWalletCLI
Expand Down Expand Up @@ -382,14 +384,14 @@ walletId =
_set :: HasType (ApiT WalletId) s => (s, Text) -> s
_set (s, v) = set typed (ApiT $ WalletId (unsafeCreateDigest v)) s

amount :: HasType (Quantity "lovelace" Natural) s => Lens' s Int
amount :: HasType (Quantity "lovelace" Natural) s => Lens' s Natural
amount =
lens _get _set
where
_get :: HasType (Quantity "lovelace" Natural) s => s -> Int
_get = fromIntegral . fromQuantity @"lovelace" @Natural . view typed
_set :: HasType (Quantity "lovelace" Natural) s => (s, Int) -> s
_set (s, v) = set typed (Quantity @"lovelace" @Natural $ fromIntegral v) s
_get :: HasType (Quantity "lovelace" Natural) s => s -> Natural
_get = fromQuantity @"lovelace" @Natural . view typed
_set :: HasType (Quantity "lovelace" Natural) s => (s, Natural) -> s
_set (s, v) = set typed (Quantity @"lovelace" @Natural v) s

direction :: HasType (ApiT Direction) s => Lens' s Direction
direction =
Expand Down Expand Up @@ -430,7 +432,7 @@ fixtureWallet ctx@(Context _ _ _ faucet) = do
Left _ -> fail "fixtureWallet: waited too long for initial transaction"
Right a -> return a
where
oneSecond = 1*1000*1000
oneSecond = 1_000_000
sixtySeconds = 60*oneSecond
checkBalance :: Text -> IO ApiWallet
checkBalance wid = do
Expand All @@ -439,6 +441,12 @@ fixtureWallet ctx@(Context _ _ _ faucet) = do
then return (getFromResponse id r)
else threadDelay oneSecond *> checkBalance wid

-- | One million ADA, in Lovelace, just like this.
oneMillionAda :: Natural
oneMillionAda = ada (1_000_000)
where
ada = (*) (1_000_000)

fromQuantity :: Quantity (u :: Symbol) a -> a
fromQuantity (Quantity a) = a

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Test.Integration.Framework.DSL
, expectSuccess
, fixtureWallet
, json
, oneMillionAda
, request
, status
, unsafeRequest
Expand All @@ -55,46 +56,46 @@ spec = do
(wa, wb) <- (,) <$> fixtureWallet ctx <*> fixtureWallet ctx
(_, addrs) <-
unsafeRequest @[ApiAddress] ctx ("GET", getAddresses wb) Empty
let amt = 1
let destination = (addrs !! 1) ^. #id
let payload = Json [json|{
"payments": [{
"address": #{destination},
"amount": {
"quantity": 1,
"quantity": #{amt},
"unit": "lovelace"
}
}],
"passphrase": "cardano-wallet"
}|]
let fee = 168653

r <- request @ApiTransaction ctx ("POST", postTx wa) Default payload
verify r
[ expectSuccess
, expectResponseCode HTTP.status202
, expectFieldEqual amount 168654
, expectFieldEqual amount (fee + amt)
, expectFieldEqual direction Outgoing
, expectFieldEqual status Pending
]

ra <- request @ApiWallet ctx ("GET", getWallet wa) Default payload
verify ra
[ expectSuccess
, expectFieldEqual balanceTotal 999999831346
, expectFieldEqual balanceTotal (oneMillionAda - fee - amt)
, expectFieldEqual balanceAvailable 0
]

rb <- request @ApiWallet ctx ("GET", getWallet wb) Default payload
verify rb
[ expectSuccess
, expectEventually ctx balanceAvailable (oneMillionAda + 1)
, expectEventually ctx balanceAvailable (oneMillionAda + amt)
]

verify ra
[ expectEventually ctx balanceAvailable 999999831346
[ expectEventually ctx balanceAvailable (oneMillionAda - fee - amt)
]
where
oneMillionAda =
1 * 1000 * 1000 * 1000 * 1000
getAddresses (w :: ApiWallet) =
"v2/wallets/" <> w ^. walletId <> "/addresses"
postTx (w :: ApiWallet) =
Expand Down

0 comments on commit 252cd12

Please sign in to comment.