Skip to content

Conversation

@newhoggy
Copy link
Collaborator

@newhoggy newhoggy commented Nov 9, 2025

Changelog

- description: |
    Added comprehensive test suite for TxOut JSON serialization and deserialization instances across all Cardano eras, ensuring robust handling of datum types and edge cases. Also exported caseBabbageOnlyOrConwayEraOnwards helper function to the public API for cleaner era-specific conditional logic.
  type:
  - test           # fixes/modifies tests
  - refactoring    # QoL changes
  projects:
  - cardano-api

Context

This PR introduces an extensive test suite to validate the JSON instances for TxOut across all Cardano eras and transaction contexts. The tests ensure that JSON serialization and deserialization behave correctly for all datum type variations, including edge cases and error scenarios. Additionally, a helper function for era-specific branching has been exposed in the public API to support cleaner conditional logic when dealing with Babbage vs Conway+ era differences.

Preparation for work on #926

How to trust this PR

The test suite provides comprehensive coverage of TxOut JSON behavior:

Test Coverage Added:

  • Roundtrip tests: Validate JSON encode/decode for all eras (Shelley through Conway) in both CtxTx and CtxUTxO contexts
  • Edge case tests: Handle supplemental datum ambiguity, null field behavior, and ToJSON output validation
  • Error case tests: Verify proper rejection of conflicting datums, mismatched hashes, partial fields, and invalid data
  • Datum-specific tests: Cover all datum types (None, Hash, Supplemental, Inline) with dedicated test scenarios

Test Infrastructure:

  • Test.Cardano.Api.TxOut.Gen: Specialized generators for TxOut with specific datum types and invalid JSON scenarios
  • Test.Cardano.Api.TxOut.Helpers: Utilities for JSON field assertions, parse failure validation, and datum equality checks
  • Test.Cardano.Api.TxOut.Json: Main test module organizing all test suites
  • Test.Cardano.Api.TxOut.JsonRoundtrip: Property tests for all era/context combinations
  • Test.Cardano.Api.TxOut.JsonEdgeCases: Tests for supplemental datum behavior and null field handling
  • Test.Cardano.Api.TxOut.JsonErrorCases: Validation of error handling for malformed JSON

API Enhancement:

  • Exported caseBabbageOnlyOrConwayEraOnwards from Cardano.Api.Era to enable cleaner branching logic for Babbage vs Conway+ era features

To run the new tests:

cabal test cardano-api-test --test-option='-p "TxOut.Json"'

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

Copy link
Contributor

@Jimbo4350 Jimbo4350 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need JSON serialization across all eras? Where is this used? If we do, I want to do away with all of the intermediate eons and parameterize on ShelleyBasedEra era. Soon all of the intermediate eons will be removed.

Test.Cardano.Api.Transaction.Autobalance
Test.Cardano.Api.Transaction.Body.Plutus.Scripts
Test.Cardano.Api.TxBody
Test.Cardano.Api.TxOut.Gen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be in the gen library?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved!

, caseShelleyToBabbageOrConwayEraOnwards

-- ** Case on BabbageEraOnwards
, caseBabbageOnlyOrConwayEraOnwards
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actively looking to remove these functions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed!

@newhoggy newhoggy force-pushed the newhoggy/tests-for-TxOut branch from 4f64fb9 to 7ee9c53 Compare November 12, 2025 12:11
Implements extensive test coverage for the ToJSON and FromJSON instances
of TxOut across all eras and contexts, ensuring robust JSON serialization
and deserialization behavior.

Test modules added:
- Test.Cardano.Api.TxOut.Gen: Specialized generators for TxOut with specific
  datum types (no datum, datum hash, supplemental, inline) and invalid JSON
  scenarios for error testing
- Test.Cardano.Api.TxOut.Helpers: Test utilities including JSON field
  assertions, parse failure validators, and datum equality checks
- Test.Cardano.Api.TxOut.Json: Main test module organizing all test suites
- Test.Cardano.Api.TxOut.JsonRoundtrip: Roundtrip property tests for all
  eras (Shelley through Conway) in both CtxTx and CtxUTxO contexts
- Test.Cardano.Api.TxOut.JsonEdgeCases: Edge case tests for supplemental
  datum behavior, null field handling, and ToJSON output validation
- Test.Cardano.Api.TxOut.JsonErrorCases: Error case tests for conflicting
  datums, mismatched hashes, partial fields, and invalid data

Coverage highlights:
- All eras from Byron through Dijkstra (where supported)
- Both transaction contexts (CtxTx and CtxUTxO)
- All datum types including edge cases like supplemental datums
- Comprehensive error handling validation
- JSON field presence and null handling verification

This test suite ensures the TxOut JSON instances maintain backward
compatibility while properly handling the complex datum type variations
across different Cardano eras.
@newhoggy newhoggy force-pushed the newhoggy/tests-for-TxOut branch from 7ee9c53 to 9782105 Compare November 12, 2025 12:23
@newhoggy newhoggy requested a review from Jimbo4350 November 12, 2025 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants