From 901fe28727fd53228f6d3a6357ff8d5e96c4e543 Mon Sep 17 00:00:00 2001 From: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> Date: Tue, 20 Feb 2024 16:44:08 +0100 Subject: [PATCH] Python: add API responses test (#2009) * Add API responses test * Cleanup * Add TODOs * Update import * Update more TODOs --------- Co-authored-by: Thibault Martinez --- bindings/python/iota_sdk/types/node_info.py | 2 +- bindings/python/tests/test_api_responses.py | 91 +++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 bindings/python/tests/test_api_responses.py diff --git a/bindings/python/iota_sdk/types/node_info.py b/bindings/python/iota_sdk/types/node_info.py index 4875cf80a3..a54aaf386e 100644 --- a/bindings/python/iota_sdk/types/node_info.py +++ b/bindings/python/iota_sdk/types/node_info.py @@ -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 )) @@ -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 diff --git a/bindings/python/tests/test_api_responses.py b/bindings/python/tests/test_api_responses.py new file mode 100644 index 0000000000..e7250c0c06 --- /dev/null +++ b/bindings/python/tests/test_api_responses.py @@ -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")