Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
[CO-319] Add roundtrip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akegalj committed Jun 13, 2018
1 parent 8ee4ddc commit 3acda87
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
9 changes: 4 additions & 5 deletions wallet-new/src/Cardano/Wallet/API/V1/Types.hs
Expand Up @@ -862,11 +862,10 @@ instance Bounded AccountIndex where

mkAccountIndex :: Word32 -> Either Text AccountIndex
mkAccountIndex index | index >= getAccIndex minBound = Right $ AccountIndex index
| otherwise = Left $ "mkAccountIndex: Account index should be in range ["
<> show (getAccIndex minBound)
<> ".."
<> show (getAccIndex maxBound)
<> "]"
| otherwise = Left $ sformat
("mkAccountIndex: Account index should be in range ["%int%".."%int%"]")
(getAccIndex minBound)
(getAccIndex maxBound)

instance ToJSON AccountIndex where
toJSON = toJSON . getAccIndex
Expand Down
29 changes: 26 additions & 3 deletions wallet-new/test/MarshallingSpec.hs
Expand Up @@ -11,6 +11,7 @@ import Pos.Client.Txp.Util (InputSelectionPolicy)
import qualified Pos.Crypto as Crypto
import qualified Pos.Txp.Toil.Types as V0
import qualified Pos.Wallet.Web.ClientTypes.Types as V0
import Servant.API (FromHttpApiData (..), ToHttpApiData (..))
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck
Expand All @@ -22,6 +23,7 @@ import Pos.Util.BackupPhrase (BackupPhrase)
import qualified Pos.Core as Core

import Cardano.Wallet.API.Indices
import Cardano.Wallet.API.Request.Pagination (Page, PerPage)
import Cardano.Wallet.API.V1.Errors (WalletError)
import Cardano.Wallet.API.V1.Migration.Types (Migrate (..))
import Cardano.Wallet.API.V1.Types
Expand All @@ -32,7 +34,7 @@ import qualified Cardano.Wallet.Util as Util
spec :: Spec
spec = parallel $ describe "Marshalling & Unmarshalling" $ do
parallel $ describe "Roundtrips" $ do
-- Aeson roundrips
-- Aeson roundtrips
aesonRoundtripProp @(V1 BackupPhrase) Proxy
aesonRoundtripProp @Account Proxy
aesonRoundtripProp @AssuranceLevel Proxy
Expand Down Expand Up @@ -63,8 +65,19 @@ spec = parallel $ describe "Marshalling & Unmarshalling" $ do
aesonRoundtripProp @EstimatedCompletionTime Proxy
aesonRoundtripProp @SyncProgress Proxy
aesonRoundtripProp @SyncThroughput Proxy

-- Migrate roundrips
aesonRoundtripProp @AccountIndex Proxy

-- HttpApiData roundtrips
httpApiDataRoundtripProp @AccountIndex Proxy
httpApiDataRoundtripProp @(V1 Core.TxId) Proxy
httpApiDataRoundtripProp @WalletId Proxy
httpApiDataRoundtripProp @(V1 Core.Timestamp) Proxy
httpApiDataRoundtripProp @(V1 Core.Address) Proxy
httpApiDataRoundtripProp @PerPage Proxy
httpApiDataRoundtripProp @Page Proxy
httpApiDataRoundtripProp @Core.Coin Proxy

-- Migrate roundtrips
migrateRoundtripProp @(V1 Core.Address) @(V0.CId V0.Addr) Proxy Proxy
migrateRoundtripProp @(V1 Core.Coin) @V0.CCoin Proxy Proxy
migrateRoundtripProp @AssuranceLevel @V0.CWalletAssurance Proxy Proxy
Expand Down Expand Up @@ -151,6 +164,16 @@ aesonRoundtripProp
aesonRoundtripProp proxy =
prop ("Aeson " <> show (typeRep proxy) <> " roundtrips") (aesonRoundtrip proxy)

httpApiDataRoundtrip :: (Arbitrary a, FromHttpApiData a, ToHttpApiData a, Eq a, Show a) => proxy a -> Property
httpApiDataRoundtrip (_ :: proxy a) = forAll arbitrary $ \(s :: a) -> do
parseQueryParam (toQueryParam s) === Right s

httpApiDataRoundtripProp
:: (Arbitrary a, ToHttpApiData a, FromHttpApiData a, Eq a, Show a, Typeable a)
=> proxy a -> Spec
httpApiDataRoundtripProp proxy =
prop ("HttpApiData " <> show (typeRep proxy) <> " roundtrips") (httpApiDataRoundtrip proxy)

generalRoundtrip
:: (Arbitrary from, Eq from, Show from, Show e)
=> (from -> to) -> (to -> Either e from) -> Property
Expand Down

0 comments on commit 3acda87

Please sign in to comment.