Skip to content

Commit 9b346b8

Browse files
committed
test: Add tx_out.ma_tx_out roundtrip tests
1 parent dd7b031 commit 9b346b8

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

cardano-db-sync/cardano-db-sync.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ test-suite test
349349
Cardano.DbSync.Config.TypesTest
350350
Cardano.DbSync.Era.Shelley.Generic.ScriptDataTest
351351
Cardano.DbSync.Era.Shelley.Generic.ScriptTest
352+
Cardano.DbSync.Era.Shelley.Generic.Tx.TypesTest
352353
Cardano.DbSync.Gen
353354
Cardano.DbSync.Util.AddressTest
354355
Cardano.DbSync.Util.Bech32Test
@@ -372,6 +373,7 @@ test-suite test
372373
, cardano-ledger-allegra:{cardano-ledger-allegra,testlib}
373374
, cardano-ledger-alonzo:{testlib}
374375
, cardano-ledger-byron
376+
, cardano-ledger-mary:{cardano-ledger-mary,testlib}
375377
, cardano-ledger-shelley:{cardano-ledger-shelley,testlib} >= 1.12.3.0
376378
, cardano-db
377379
, cardano-db-sync

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Types.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ import Cardano.Prelude
5454
import Cardano.Slotting.Slot (SlotNo (..))
5555
import Data.Aeson (FromJSON (..), ToJSON (..), (.:), (.=))
5656
import Data.Aeson.Types (object, withObject)
57-
import Data.ByteString.Short (toShort)
57+
import qualified Data.ByteString.Base16 as Base16
58+
import Data.ByteString.Short (fromShort, toShort)
5859
import qualified Data.Map as Map
5960
import Ouroboros.Consensus.Cardano.Block (StandardAlonzo, StandardBabbage, StandardConway, StandardCrypto, StandardShelley)
6061

@@ -182,7 +183,8 @@ instance FromJSON TxOutMultiAsset where
182183
<*> o .: "amount"
183184
where
184185
parseAssetName :: Text -> AssetName
185-
parseAssetName t = AssetName $ toShort (encodeUtf8 t)
186+
parseAssetName =
187+
AssetName . toShort . Base16.decodeLenient . encodeUtf8
186188

187189
toTxCert :: Word16 -> Cert -> TxCertificate
188190
toTxCert idx dcert =
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE NoImplicitPrelude #-}
3+
4+
module Cardano.DbSync.Era.Shelley.Generic.Tx.TypesTest (tests) where
5+
6+
import qualified Cardano.DbSync.Gen as Gen
7+
import Cardano.Prelude
8+
import qualified Data.Aeson as Aeson
9+
import Hedgehog
10+
import qualified Hedgehog.Gen as Gen
11+
12+
tests :: IO Bool
13+
tests =
14+
checkParallel $
15+
Group
16+
"Cardano.DbSync.Era.Shelley.Generic.Tx.Types"
17+
[("TxOutMultiAssets roundtrip", prop_txOutMultiAssetsRoundTrip)]
18+
19+
prop_txOutMultiAssetsRoundTrip :: Property
20+
prop_txOutMultiAssetsRoundTrip = property $ do
21+
multiAsset <- forAll Gen.txOutMultiAsset
22+
23+
tripping multiAsset Aeson.encode Aeson.decode

cardano-db-sync/test/Cardano/DbSync/Gen.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE NumericUnderscores #-}
12
{-# LANGUAGE OverloadedStrings #-}
23
{-# LANGUAGE TypeApplications #-}
34
{-# LANGUAGE NoImplicitPrelude #-}
@@ -19,6 +20,9 @@ module Cardano.DbSync.Gen (
1920
addr,
2021
networkName,
2122

23+
-- * Tx Type generators
24+
txOutMultiAsset,
25+
2226
-- * Utility generators
2327
hashBlake2b_256,
2428
filePath,
@@ -36,14 +40,17 @@ import Cardano.Crypto.Hash.Class (HashAlgorithm (..), hashFromBytes)
3640
import Cardano.Db (PGPassSource (..))
3741
import Cardano.DbSync
3842
import Cardano.DbSync.Config.Types
43+
import Cardano.DbSync.Era.Shelley.Generic (TxOutMultiAsset (..))
3944
import Cardano.Ledger.Slot (EpochNo (..))
4045
import Cardano.Prelude
4146
import Data.ByteString.Short (ShortByteString (), toShort)
4247
import Data.Maybe (fromJust)
4348
import Hedgehog
4449
import qualified Hedgehog.Gen as Gen
50+
import Hedgehog.Gen.QuickCheck (arbitrary)
4551
import qualified Hedgehog.Range as Range
4652
import Ouroboros.Consensus.Cardano.CanHardFork (TriggerHardFork (..))
53+
import Test.Cardano.Ledger.Mary.Arbitrary ()
4754

4855
syncPreConfig :: Gen SyncPreConfig
4956
syncPreConfig =
@@ -189,6 +196,13 @@ scriptHash = toShort <$> Gen.utf8 (Range.linear 1 5) Gen.unicode
189196
networkName :: Gen NetworkName
190197
networkName = NetworkName <$> Gen.text (Range.linear 0 100) Gen.alpha
191198

199+
txOutMultiAsset :: Gen TxOutMultiAsset
200+
txOutMultiAsset =
201+
TxOutMultiAsset
202+
<$> arbitrary
203+
<*> arbitrary
204+
<*> Gen.integral (Range.linear 0 999_999_999_999)
205+
192206
hashBlake2b_256 :: MonadGen m => m (Maybe (Hash Blake2b_256 ByteString))
193207
hashBlake2b_256 = serialiseHash <$> Gen.bytes (Range.linear 0 100)
194208
where

cardano-db-sync/test/Main.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import qualified Cardano.DbSync.ApiTest as Api
44
import qualified Cardano.DbSync.Config.TypesTest as Types
55
import qualified Cardano.DbSync.Era.Shelley.Generic.ScriptDataTest as ScriptData
66
import qualified Cardano.DbSync.Era.Shelley.Generic.ScriptTest as Script
7+
import qualified Cardano.DbSync.Era.Shelley.Generic.Tx.TypesTest as TxTypes
78
import qualified Cardano.DbSync.Util.AddressTest as Address
89
import qualified Cardano.DbSync.Util.Bech32Test as Bech32
910
import qualified Cardano.DbSync.Util.CborTest as Cbor
@@ -20,6 +21,7 @@ main =
2021
, Cbor.tests
2122
, Script.tests
2223
, ScriptData.tests
24+
, TxTypes.tests
2325
, DbSync.tests
2426
, Types.tests
2527
, Api.tests

0 commit comments

Comments
 (0)