Skip to content

Commit

Permalink
add core unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Oct 13, 2020
1 parent e83550b commit ddd9d5f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ newtype ApiWithdrawRewards = ApiWithdrawRewards Bool
deriving (Eq, Generic, Show)

newtype ApiVerificationKeyHash = ApiVerificationKeyHash
{ unApiVerificationKeyHash :: ApiT (Hash "ScriptKey")
{ verificationKeyHash :: ApiT (Hash "ScriptKey")
} deriving (Eq, Generic, Show)

-- | Error codes returned by the API, in the form of snake_cased strings
Expand Down
35 changes: 35 additions & 0 deletions lib/core/test/data/Cardano/Wallet/Api/ApiVerificationKeyHash.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"seed": 3843240765986310815,
"samples": [
{
"verification_key_hash": "f83d391427406d430e30534349563a7f58c9383c3b0a9d21091e6354"
},
{
"verification_key_hash": "2b5a99483598726a1975e3465a1a1bdf08516715254a7b4b784b1a22"
},
{
"verification_key_hash": "082d0f1d626904470724a7d8374f4bf20a0218500b057fb85b3b8445"
},
{
"verification_key_hash": "666b2a40060e37485c5633397e354a430b18126b4634335f5d3701cd"
},
{
"verification_key_hash": "3a612ab449126476577c656a92333f7d401021e2283a375abf49a627"
},
{
"verification_key_hash": "0669fc3c392c5e37474eb4031c8125101230f01260472720646b1b44"
},
{
"verification_key_hash": "7f2869354b6a66d10dd9685c04586e252f2c457856123d66896c680a"
},
{
"verification_key_hash": "625301105b51b91772532b55a202104f03251949720c6c7b0327530b"
},
{
"verification_key_hash": "9a6a1860033e420319030e59015d544b0669298d1203097710d34f2a"
},
{
"verification_key_hash": "308e7f5902082e041774b45c515d9801702276230b792a6b76314277"
}
]
}
18 changes: 17 additions & 1 deletion lib/core/test/unit/Cardano/Wallet/Api/Malformed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import Cardano.Wallet.Api.Types
import Cardano.Wallet.Primitive.AddressDerivation
( NetworkDiscriminant (..) )
import Cardano.Wallet.Primitive.Types
( Address, WalletId, walletNameMaxLength )
( Address, DerivationIndex, WalletId, walletNameMaxLength )
import Control.Arrow
( first )
import Data.Aeson.QQ
Expand Down Expand Up @@ -151,6 +151,22 @@ instance Malformed (PathParam ApiTxId) where
where
msg = "Invalid tx hash: expecting a hex-encoded value that is 32 bytes in length."

instance Wellformed (PathParam (ApiT DerivationIndex)) where
wellformed = PathParam <$>
[ "0"
, "1000"
, "2147483647"
]

instance Malformed (PathParam (ApiT DerivationIndex)) where
malformed = first PathParam <$>
[ ("-1",msg)
, ("4.3",msg)
, ("something",msg)
]
where
msg = "Expecting natural number"

instance Wellformed (PathParam ApiPoolId) where
wellformed = PathParam <$>
[ T.replicate 64 "0"
Expand Down
16 changes: 16 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import Cardano.Wallet.Api.Types
, ApiTxInput (..)
, ApiTxMetadata (..)
, ApiUtxoStatistics (..)
, ApiVerificationKeyHash (..)
, ApiWallet (..)
, ApiWalletDelegation (..)
, ApiWalletDelegationNext (..)
Expand Down Expand Up @@ -341,6 +342,7 @@ spec = do
jsonRoundtripAndGolden $ Proxy @ApiFee
jsonRoundtripAndGolden $ Proxy @ApiStakePoolMetrics
jsonRoundtripAndGolden $ Proxy @ApiTxId
jsonRoundtripAndGolden $ Proxy @ApiVerificationKeyHash
jsonRoundtripAndGolden $ Proxy @(PostTransactionData ('Testnet 0))
jsonRoundtripAndGolden $ Proxy @(PostTransactionFeeData ('Testnet 0))
jsonRoundtripAndGolden $ Proxy @WalletPostData
Expand Down Expand Up @@ -515,6 +517,13 @@ spec = do
"invalid-id"
|] `shouldBe` (Left @String @(ApiT WalletId) msg)

it "ApiT (Hash \"ScriptKey\")" $ do
let msg = "Error in $: Invalid scriptKey hash: \
\expecting a hex-encoded value that is 28 bytes in length."
Aeson.parseEither parseJSON [aesonQQ|
"-----"
|] `shouldBe` (Left @String @(ApiT (Hash "ScriptKey")) msg)

it "AddressAmount (too small)" $ do
let msg = "Error in $.amount.quantity: parsing Natural failed, \
\unexpected negative number -14"
Expand Down Expand Up @@ -1306,6 +1315,13 @@ instance Arbitrary (Quantity "percent" Double) where
shrink _ = [Quantity 0.0]
arbitrary = Quantity <$> choose (0,100)

instance Arbitrary ApiVerificationKeyHash where
arbitrary =
ApiVerificationKeyHash . ApiT . Hash . B8.pack <$> replicateM 28 arbitrary

instance ToSchema ApiVerificationKeyHash where
declareNamedSchema _ = declareSchemaForDefinition "ApiVerificationKeyHash"

instance Arbitrary ApiNetworkParameters where
arbitrary = genericArbitrary
shrink = genericShrink
Expand Down
8 changes: 4 additions & 4 deletions specifications/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1761,10 +1761,10 @@ x-parametersIndex: &parametersIndex
required: true
schema:
type: integer
# 2 ^ 31 minimum soft index
minimum: 2147483648
# 2 ^ 32 - 1, maximum soft index
maximum: 4294967295
# minimum soft index
minimum: 0
# 2 ^ 31 - 1, maximum soft index
maximum: 2147483647

x-parametersForceNtpCheck: &parametersForceNtpCheck
in: query
Expand Down

0 comments on commit ddd9d5f

Please sign in to comment.