Skip to content

Commit

Permalink
Python: add API responses test (#2009)
Browse files Browse the repository at this point in the history
* Add API responses test

* Cleanup

* Add TODOs

* Update import

* Update more TODOs

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Thoralf-M and thibault-martinez authored Feb 20, 2024
1 parent bbe86bb commit 901fe28
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bindings/python/iota_sdk/types/node_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ class ProtocolParameters:
bech32_hrp: str
storage_score_parameters: StorageScoreParameters
work_score_parameters: WorkScoreParameters
mana_parameters: ManaParameters
token_supply: int = field(metadata=config(
encoder=str
))
Expand All @@ -288,7 +289,6 @@ class ProtocolParameters:
))
slot_duration_in_seconds: int
slots_per_epoch_exponent: int
mana_parameters: ManaParameters
staking_unbonding_period: int
validation_blocks_per_slot: int
punishment_epochs: int
Expand Down
91 changes: 91 additions & 0 deletions bindings/python/tests/test_api_responses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Copyright 2024 IOTA Stiftung
# SPDX-License-Identifier: Apache-2.0

from typing import Generic, TypeVar
from json import load, loads, dumps
from iota_sdk import RoutesResponse, CongestionResponse, ManaRewardsResponse, ValidatorResponse, ValidatorsResponse, CommitteeResponse, IssuanceBlockHeaderResponse, Block, OutputResponse, SlotCommitment


base_path = '../../sdk/tests/types/api/fixtures/'
T = TypeVar("T")


def test_api_responses():
def test_api_response(cls_type: Generic[T], path: str):
fixture_str = ''
with open(base_path + path, "r", encoding="utf-8") as payload:
fixture = load(payload)
cls = cls_type.from_dict(fixture)

# We need to sort the keys because optional fields in classes must be last in Python
fixture_str = dumps(fixture, sort_keys=True)
recreated = dumps(
loads(cls.to_json()), sort_keys=True)
assert fixture_str == recreated

# GET /api/routes
test_api_response(RoutesResponse, "get-routes-response-example.json")
# GET /api/core/v3/info
# TODO: enable when the fixture is updated https://github.com/iotaledger/iota-sdk/issues/2015
# test_api_response(InfoResponse, "get-info-response-example.json")
# GET /api/core/v3/accounts/{bech32Address}/congestion
test_api_response(CongestionResponse,
"get-congestion-estimate-response-example.json")
# GET /api/core/v3/rewards/{outputId}
test_api_response(ManaRewardsResponse, "get-mana-rewards-example.json")
# GET /api/core/v3/validators
test_api_response(ValidatorsResponse, "get-validators-example.json")
# GET /api/core/v3/validators/{bech32Address}
test_api_response(ValidatorResponse, "get-validator-example.json")
# GET /api/core/v3/committee
test_api_response(CommitteeResponse, "get-committee-example.json")
# GET /api/core/v3/blocks/issuance
test_api_response(IssuanceBlockHeaderResponse,
"get-buildingBlock-response-example.json")
# GET /api/core/v3/blocks/{blockId}
test_api_response(Block, "get-block-by-id-empty-response-example.json")
test_api_response(Block, "tagged-data-block-example.json")
test_api_response(Block, "transaction-block-example.json")
test_api_response(
Block, "get-block-by-id-validation-response-example.json")
# GET /api/core/v3/blocks/{blockId}/metadata
# TODO enable when Block and Tx State enums are fixed https://github.com/iotaledger/iota-sdk/issues/2019
# test_api_response(BlockMetadataResponse,
# "get-block-by-id-response-example-new-transaction.json")
# test_api_response(BlockMetadataResponse,
# "get-block-by-id-response-example-new.json")
# test_api_response(BlockMetadataResponse,
# "get-block-by-id-response-example-confirmed-transaction.json")
# test_api_response(BlockMetadataResponse,
# "get-block-by-id-response-example-confirmed.json")
# test_api_response(BlockMetadataResponse,
# "get-block-by-id-response-example-conflicting-transaction.json")
# # GET /api/core/v3/blocks/{blockId}/full
# test_api_response(BlockWithMetadataResponse,
# "get-full-block-by-id-tagged-data-response-example.json")
# GET /api/core/v3/outputs/{outputId}
test_api_response(
OutputResponse, "get-outputs-by-id-response-example.json")
# GET /api/core/v3/outputs/{outputId}/metadata
# TODO: enable when https://github.com/iotaledger/iota-sdk/issues/2020 is fixed
# test_api_response(
# OutputMetadata, "get-output-metadata-by-id-response-unspent-example.json")
# test_api_response(
# OutputMetadata, "get-output-metadata-by-id-response-spent-example.json")
# GET /api/core/v3/outputs/{outputId}/full
# TODO: enable when OutputWithMetadata is updated with OutputIdProof https://github.com/iotaledger/iota-sdk/issues/2021
# test_api_response(OutputWithMetadata,
# "get-full-output-metadata-example.json")
# GET /api/core/v3/transactions/{transactionId}/metadata
# TODO enable when Tx State enum is fixed https://github.com/iotaledger/iota-sdk/issues/2019
# test_api_response(TransactionMetadataResponse,
# "get-transaction-metadata-by-id-response-example.json")
# GET /api/core/v3/commitments/{commitmentId}
test_api_response(SlotCommitment, "get-commitment-response-example.json")
# GET /api/core/v3/commitments/{commitmentId}/utxo-changes
# TODO: enable when https://github.com/iotaledger/iota-sdk/issues/2020 is fixed
# test_api_response(UtxoChangesResponse,
# "get-utxo-changes-response-example.json")
# GET /api/core/v3/commitments/{commitmentId}/utxo-changes/full
# test_api_response(UtxoChangesFullResponse,
# "get-utxo-changes-full-response-example.json")

0 comments on commit 901fe28

Please sign in to comment.