-
Notifications
You must be signed in to change notification settings - Fork 25
test(api): add comprehensive JSON tests for TxOut instances #1005
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
Open
newhoggy
wants to merge
4
commits into
master
Choose a base branch
from
newhoggy/tests-for-TxOut
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9eef6f7
test(gen): add generators for Cardano era types
newhoggy 62e156c
test(api): add comprehensive JSON tests for TxOut instances
newhoggy 79ddfb9
docs(eras): add deprecation notice for era case functions
newhoggy 38dcfaa
test(api): refactor TxOut JSON roundtrip tests to use era-agnostic pr…
newhoggy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE GADTs #-} | ||
| {-# LANGUAGE LambdaCase #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
|
|
||
| -- | Additional generators for TxOut JSON testing | ||
| module Test.Gen.Cardano.Api.TxOut | ||
| ( -- * Specific Datum Type Generators | ||
| genTxOutWithNoDatum | ||
| , genTxOutWithDatumHash | ||
| , genTxOutWithSupplementalDatum | ||
| , genTxOutWithInlineDatum | ||
|
|
||
| -- * Invalid JSON Generators | ||
| , genConflictingDatumJSON | ||
| , genMismatchedInlineDatumHashJSON | ||
| , genPartialInlineDatumJSON | ||
|
|
||
| -- * Era-specific TxOut generators | ||
| , genTxOutForEra | ||
| ) | ||
| where | ||
|
|
||
| import Cardano.Api hiding (Value) | ||
|
|
||
| import Data.Aeson (Value (..), object, (.=)) | ||
|
|
||
| import Test.Gen.Cardano.Api.Typed | ||
|
|
||
| import Hedgehog (Gen) | ||
| import Hedgehog.Gen qualified as Gen | ||
|
|
||
| -- | Generate a TxOut with no datum and no reference script | ||
| genTxOutWithNoDatum | ||
| :: ShelleyBasedEra era | ||
| -> Gen (TxOut CtxTx era) | ||
| genTxOutWithNoDatum era = | ||
| TxOut | ||
| <$> genAddressInEra era | ||
| <*> genTxOutValue era | ||
| <*> pure TxOutDatumNone | ||
| <*> pure ReferenceScriptNone | ||
|
|
||
| -- | Generate a TxOut with a datum hash (Alonzo+) | ||
| genTxOutWithDatumHash | ||
| :: forall era | ||
| . AlonzoEraOnwards era | ||
| -> Gen (TxOut CtxTx era) | ||
| genTxOutWithDatumHash w = | ||
| alonzoEraOnwardsConstraints w $ | ||
| TxOut | ||
| <$> genAddressInEra sbe | ||
| <*> genTxOutValue sbe | ||
| <*> (TxOutDatumHash w <$> genHashScriptData) | ||
| <*> genReferenceScript sbe | ||
| where | ||
| sbe :: ShelleyBasedEra era | ||
| sbe = convert w | ||
|
|
||
| -- | Generate a TxOut with a supplemental datum (Alonzo+, CtxTx only) | ||
| genTxOutWithSupplementalDatum | ||
| :: forall era | ||
| . AlonzoEraOnwards era | ||
| -> Gen (TxOut CtxTx era) | ||
| genTxOutWithSupplementalDatum w = | ||
| alonzoEraOnwardsConstraints w $ | ||
| TxOut | ||
| <$> genAddressInEra sbe | ||
| <*> genTxOutValue sbe | ||
| <*> (TxOutSupplementalDatum w <$> genHashableScriptData) | ||
| <*> genReferenceScript sbe | ||
| where | ||
| sbe :: ShelleyBasedEra era | ||
| sbe = convert w | ||
|
|
||
| -- | Generate a TxOut with an inline datum (Babbage+) | ||
| genTxOutWithInlineDatum | ||
| :: forall era | ||
| . BabbageEraOnwards era | ||
| -> Gen (TxOut CtxTx era) | ||
| genTxOutWithInlineDatum w = | ||
| babbageEraOnwardsConstraints w $ | ||
| TxOut | ||
| <$> genAddressInEra sbe | ||
| <*> genTxOutValue sbe | ||
| <*> (TxOutDatumInline w <$> genHashableScriptData) | ||
| <*> genReferenceScript sbe | ||
| where | ||
| sbe :: ShelleyBasedEra era | ||
| sbe = convert w | ||
|
|
||
| -- | Generate JSON with conflicting Alonzo and Babbage datum fields | ||
| genConflictingDatumJSON :: Gen Value | ||
| genConflictingDatumJSON = do | ||
| addr <- genAddressInEra ShelleyBasedEraBabbage | ||
| val <- genTxOutValue ShelleyBasedEraBabbage | ||
| datum1 <- genHashableScriptData | ||
| datum2 <- genHashableScriptData | ||
| let hash1 = hashScriptDataBytes datum1 | ||
| let hash2 = hashScriptDataBytes datum2 | ||
| pure $ | ||
| object | ||
| [ "address" .= addr | ||
| , "value" .= val | ||
| , "datumhash" .= hash1 | ||
| , "datum" .= scriptDataToJson ScriptDataJsonDetailedSchema datum1 | ||
| , "inlineDatumhash" .= hash2 | ||
| , "inlineDatum" .= scriptDataToJson ScriptDataJsonDetailedSchema datum2 | ||
| ] | ||
|
|
||
| -- | Generate JSON with inline datum that doesn't match its hash | ||
| genMismatchedInlineDatumHashJSON :: Gen Value | ||
| genMismatchedInlineDatumHashJSON = do | ||
| addr <- genAddressInEra ShelleyBasedEraBabbage | ||
| val <- genTxOutValue ShelleyBasedEraBabbage | ||
| datum <- genHashableScriptData | ||
| wrongDatum <- Gen.filter (/= datum) genHashableScriptData | ||
| let wrongHash = hashScriptDataBytes wrongDatum | ||
| pure $ | ||
| object | ||
| [ "address" .= addr | ||
| , "value" .= val | ||
| , "inlineDatumhash" .= wrongHash | ||
| , "inlineDatum" .= scriptDataToJson ScriptDataJsonDetailedSchema datum | ||
| ] | ||
|
|
||
| -- | Generate JSON with only partial inline datum fields | ||
| genPartialInlineDatumJSON :: Gen Value | ||
| genPartialInlineDatumJSON = do | ||
| addr <- genAddressInEra ShelleyBasedEraBabbage | ||
| val <- genTxOutValue ShelleyBasedEraBabbage | ||
| datum <- genHashableScriptData | ||
| let hash = hashScriptDataBytes datum | ||
| Gen.choice | ||
| [ -- Only hash, no datum | ||
| pure $ | ||
| object | ||
| [ "address" .= addr | ||
| , "value" .= val | ||
| , "inlineDatumhash" .= hash | ||
| ] | ||
| , -- Only datum, no hash | ||
| pure $ | ||
| object | ||
| [ "address" .= addr | ||
| , "value" .= val | ||
| , "inlineDatum" .= scriptDataToJson ScriptDataJsonDetailedSchema datum | ||
| ] | ||
| ] | ||
|
|
||
| -- | Generate a TxOut for a specific era (using appropriate datum types) | ||
| genTxOutForEra | ||
| :: ShelleyBasedEra era | ||
| -> Gen (TxOut CtxTx era) | ||
| genTxOutForEra = \case | ||
| ShelleyBasedEraShelley -> genTxOutWithNoDatum ShelleyBasedEraShelley | ||
| ShelleyBasedEraAllegra -> genTxOutWithNoDatum ShelleyBasedEraAllegra | ||
| ShelleyBasedEraMary -> genTxOutWithNoDatum ShelleyBasedEraMary | ||
| ShelleyBasedEraAlonzo -> | ||
| Gen.choice | ||
| [ genTxOutWithNoDatum ShelleyBasedEraAlonzo | ||
| , genTxOutWithDatumHash AlonzoEraOnwardsAlonzo | ||
| , genTxOutWithSupplementalDatum AlonzoEraOnwardsAlonzo | ||
| ] | ||
| ShelleyBasedEraBabbage -> | ||
| Gen.choice | ||
| [ genTxOutWithNoDatum ShelleyBasedEraBabbage | ||
| , genTxOutWithDatumHash AlonzoEraOnwardsBabbage | ||
| , genTxOutWithSupplementalDatum AlonzoEraOnwardsBabbage | ||
| , genTxOutWithInlineDatum BabbageEraOnwardsBabbage | ||
| ] | ||
| ShelleyBasedEraConway -> | ||
| Gen.choice | ||
| [ genTxOutWithNoDatum ShelleyBasedEraConway | ||
| , genTxOutWithDatumHash AlonzoEraOnwardsConway | ||
| , genTxOutWithSupplementalDatum AlonzoEraOnwardsConway | ||
| , genTxOutWithInlineDatum BabbageEraOnwardsConway | ||
| ] | ||
| ShelleyBasedEraDijkstra -> | ||
| Gen.choice | ||
| [ genTxOutWithNoDatum ShelleyBasedEraDijkstra | ||
| , genTxOutWithDatumHash AlonzoEraOnwardsDijkstra | ||
| , genTxOutWithSupplementalDatum AlonzoEraOnwardsDijkstra | ||
| , genTxOutWithInlineDatum BabbageEraOnwardsDijkstra | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See: #1040 (comment)