From 46e47c0c6c20678f55389d7f69009b1d0f572d4b Mon Sep 17 00:00:00 2001 From: YoshihitoAso Date: Wed, 15 Jan 2025 23:32:15 +0900 Subject: [PATCH] fix --- app/routers/issuer/position.py | 19 +- tests/app/test_positions_ListAllPositions.py | 43 +-- ...sitions_RetrieveTokenPositionForAccount.py | 353 ++++++++++++++++-- 3 files changed, 346 insertions(+), 69 deletions(-) diff --git a/app/routers/issuer/position.py b/app/routers/issuer/position.py index 400af9b0..0293deeb 100644 --- a/app/routers/issuer/position.py +++ b/app/routers/issuer/position.py @@ -154,7 +154,7 @@ async def list_all_positions( positions = [] for _position, _locked, _token in _position_list: - # Get Token Name + # Get Token Attributes token_attr = None if _token.type == TokenType.IBET_STRAIGHT_BOND: token_attr = await IbetStraightBondContract(_token.token_address).get() @@ -636,20 +636,19 @@ async def retrieve_position( balance=0, exchange_balance=0, exchange_commitment=0, pending_transfer=0 ) - # Get Token Name - token_name = None - if _token.type == TokenType.IBET_STRAIGHT_BOND.value: - _bond = await IbetStraightBondContract(_token.token_address).get() - token_name = _bond.name - elif _token.type == TokenType.IBET_SHARE.value: - _share = await IbetShareContract(_token.token_address).get() - token_name = _share.name + # Get Token Attributes + token_attr = None + if _token.type == TokenType.IBET_STRAIGHT_BOND: + token_attr = await IbetStraightBondContract(_token.token_address).get() + elif _token.type == TokenType.IBET_SHARE: + token_attr = await IbetShareContract(_token.token_address).get() resp = { "issuer_address": _token.issuer_address, "token_address": _token.token_address, "token_type": _token.type, - "token_name": token_name, + "token_name": token_attr.name if token_attr is not None else None, + "token_attributes": token_attr.__dict__ if token_attr is not None else None, "balance": _position.balance, "exchange_balance": _position.exchange_balance, "exchange_commitment": _position.exchange_commitment, diff --git a/tests/app/test_positions_ListAllPositions.py b/tests/app/test_positions_ListAllPositions.py index 9400ee3b..463f76e8 100644 --- a/tests/app/test_positions_ListAllPositions.py +++ b/tests/app/test_positions_ListAllPositions.py @@ -17,7 +17,6 @@ SPDX-License-Identifier: Apache-2.0 """ -from datetime import UTC, datetime, timedelta from unittest import mock from app.model.blockchain import IbetShareContract, IbetStraightBondContract @@ -25,11 +24,9 @@ IDXLockedPosition, IDXPosition, Token, - TokenCache, TokenType, TokenVersion, ) -from config import TOKEN_CACHE_TTL class TestListAllPositions: @@ -1194,7 +1191,8 @@ def test_normal_6( # # include_token_attributes: True # - IbetStraightBond - def test_normal_7_1(self, client, db): + @mock.patch("app.model.blockchain.token.IbetStraightBondContract.get") + def test_normal_7_1(self, mock_IbetStraightBondContract_get, client, db): issuer_address = "0x1234567890123456789012345678900000000100" account_address = "0x1234567890123456789012345678900000000000" other_account_address = "0x1234567890123456789012345678911111111111" @@ -1246,7 +1244,10 @@ def test_normal_7_1(self, client, db): _locked_position.value = 5 db.add(_locked_position) - # prepare data: TokenCache + db.commit() + + # mock + bond_1 = IbetStraightBondContract() token_attr = { "issuer_address": issuer_address, "token_address": token_address_1, @@ -1290,16 +1291,8 @@ def test_normal_7_1(self, client, db): "memo": "memo-test", "is_redeemed": True, } - token_cache = TokenCache() - token_cache.token_address = token_address_1 - token_cache.attributes = token_attr - token_cache.cached_datetime = datetime.now(UTC).replace(tzinfo=None) - token_cache.expiration_datetime = datetime.now(UTC).replace( - tzinfo=None - ) + timedelta(seconds=TOKEN_CACHE_TTL) - db.add(token_cache) - - db.commit() + bond_1.__dict__ = token_attr + mock_IbetStraightBondContract_get.side_effect = [bond_1] # request target api resp = client.get( @@ -1335,7 +1328,8 @@ def test_normal_7_1(self, client, db): # # include_token_attributes: True # - IbetShare - def test_normal_7_2(self, client, db): + @mock.patch("app.model.blockchain.token.IbetShareContract.get") + def test_normal_7_2(self, mock_IbetShareContract_get, client, db): issuer_address = "0x1234567890123456789012345678900000000100" account_address = "0x1234567890123456789012345678900000000000" other_account_address = "0x1234567890123456789012345678911111111111" @@ -1387,7 +1381,10 @@ def test_normal_7_2(self, client, db): _locked_position.value = 5 db.add(_locked_position) - # prepare data: TokenCache + db.commit() + + # mock + share_1 = IbetShareContract() token_attr = { "issuer_address": issuer_address, "token_address": token_address_1, @@ -1412,16 +1409,8 @@ def test_normal_7_2(self, client, db): "dividend_record_date": "99991230", "dividend_payment_date": "99991229", } - token_cache = TokenCache() - token_cache.token_address = token_address_1 - token_cache.attributes = token_attr - token_cache.cached_datetime = datetime.now(UTC).replace(tzinfo=None) - token_cache.expiration_datetime = datetime.now(UTC).replace( - tzinfo=None - ) + timedelta(seconds=TOKEN_CACHE_TTL) - db.add(token_cache) - - db.commit() + share_1.__dict__ = token_attr + mock_IbetShareContract_get.side_effect = [share_1] # request target api resp = client.get( diff --git a/tests/app/test_positions_RetrieveTokenPositionForAccount.py b/tests/app/test_positions_RetrieveTokenPositionForAccount.py index acf1e54a..915d572a 100644 --- a/tests/app/test_positions_RetrieveTokenPositionForAccount.py +++ b/tests/app/test_positions_RetrieveTokenPositionForAccount.py @@ -45,9 +45,9 @@ def test_normal_1_1(self, mock_IbetStraightBondContract_get, client, db): _token = Token() _token.token_address = token_address _token.issuer_address = issuer_address - _token.type = TokenType.IBET_STRAIGHT_BOND.value + _token.type = TokenType.IBET_STRAIGHT_BOND _token.tx_hash = "" - _token.abi = "" + _token.abi = {} _token.version = TokenVersion.V_24_09 db.add(_token) @@ -91,7 +91,50 @@ def test_normal_1_1(self, mock_IbetStraightBondContract_get, client, db): # mock bond_1 = IbetStraightBondContract() - bond_1.name = "test_bond_1" + token_attr = { + "issuer_address": issuer_address, + "token_address": token_address, + "name": "test_bond_1", + "symbol": "TEST-test", + "total_supply": 9999999, + "contact_information": "test1", + "privacy_policy": "test2", + "tradable_exchange_contract_address": "0x1234567890123456789012345678901234567890", + "status": False, + "personal_info_contract_address": "0x1234567890123456789012345678901234567891", + "require_personal_info_registered": True, + "transferable": True, + "is_offering": True, + "transfer_approval_required": True, + "face_value": 9999998, + "face_value_currency": "JPY", + "interest_rate": 99.999, + "interest_payment_date": [ + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + ], + "interest_payment_currency": "JPY", + "redemption_date": "99991231", + "redemption_value": 9999997, + "redemption_value_currency": "JPY", + "return_date": "99991230", + "return_amount": "return_amount-test", + "base_fx_rate": 123.456789, + "purpose": "purpose-test", + "memo": "memo-test", + "is_redeemed": True, + } + bond_1.__dict__ = token_attr mock_IbetStraightBondContract_get.side_effect = [bond_1] # request target api @@ -107,8 +150,9 @@ def test_normal_1_1(self, mock_IbetStraightBondContract_get, client, db): assert resp.json() == { "issuer_address": issuer_address, "token_address": token_address, - "token_type": TokenType.IBET_STRAIGHT_BOND.value, + "token_type": TokenType.IBET_STRAIGHT_BOND, "token_name": "test_bond_1", + "token_attributes": token_attr, "balance": 10, "exchange_balance": 11, "exchange_commitment": 12, @@ -130,9 +174,9 @@ def test_normal_1_2(self, mock_IbetShareContract_get, client, db): _token = Token() _token.token_address = token_address _token.issuer_address = issuer_address - _token.type = TokenType.IBET_SHARE.value + _token.type = TokenType.IBET_SHARE _token.tx_hash = "" - _token.abi = "" + _token.abi = {} _token.version = TokenVersion.V_24_09 db.add(_token) @@ -176,7 +220,31 @@ def test_normal_1_2(self, mock_IbetShareContract_get, client, db): # mock share_1 = IbetShareContract() - share_1.name = "test_share_1" + token_attr = { + "issuer_address": issuer_address, + "token_address": token_address, + "name": "test_share_1", + "symbol": "TEST-test", + "total_supply": 999999, + "contact_information": "test1", + "privacy_policy": "test2", + "tradable_exchange_contract_address": "0x1234567890123456789012345678901234567890", + "status": False, + "personal_info_contract_address": "0x1234567890123456789012345678901234567891", + "require_personal_info_registered": False, + "transferable": True, + "is_offering": True, + "transfer_approval_required": True, + "issue_price": 999997, + "cancellation_date": "99991231", + "memo": "memo_test", + "principal_value": 999998, + "is_canceled": True, + "dividends": 9.99, + "dividend_record_date": "99991230", + "dividend_payment_date": "99991229", + } + share_1.__dict__ = token_attr mock_IbetShareContract_get.side_effect = [share_1] # request target api @@ -192,8 +260,9 @@ def test_normal_1_2(self, mock_IbetShareContract_get, client, db): assert resp.json() == { "issuer_address": issuer_address, "token_address": token_address, - "token_type": TokenType.IBET_SHARE.value, + "token_type": TokenType.IBET_SHARE, "token_name": "test_share_1", + "token_attributes": token_attr, "balance": 10, "exchange_balance": 11, "exchange_commitment": 12, @@ -213,9 +282,9 @@ def test_normal_2(self, mock_IbetStraightBondContract_get, client, db): _token = Token() _token.token_address = token_address _token.issuer_address = issuer_address - _token.type = TokenType.IBET_STRAIGHT_BOND.value + _token.type = TokenType.IBET_STRAIGHT_BOND _token.tx_hash = "" - _token.abi = "" + _token.abi = {} _token.version = TokenVersion.V_24_09 db.add(_token) @@ -233,7 +302,50 @@ def test_normal_2(self, mock_IbetStraightBondContract_get, client, db): # mock bond_1 = IbetStraightBondContract() - bond_1.name = "test_bond_1" + token_attr = { + "issuer_address": issuer_address, + "token_address": token_address, + "name": "test_bond_1", + "symbol": "TEST-test", + "total_supply": 9999999, + "contact_information": "test1", + "privacy_policy": "test2", + "tradable_exchange_contract_address": "0x1234567890123456789012345678901234567890", + "status": False, + "personal_info_contract_address": "0x1234567890123456789012345678901234567891", + "require_personal_info_registered": True, + "transferable": True, + "is_offering": True, + "transfer_approval_required": True, + "face_value": 9999998, + "face_value_currency": "JPY", + "interest_rate": 99.999, + "interest_payment_date": [ + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + ], + "interest_payment_currency": "JPY", + "redemption_date": "99991231", + "redemption_value": 9999997, + "redemption_value_currency": "JPY", + "return_date": "99991230", + "return_amount": "return_amount-test", + "base_fx_rate": 123.456789, + "purpose": "purpose-test", + "memo": "memo-test", + "is_redeemed": True, + } + bond_1.__dict__ = token_attr mock_IbetStraightBondContract_get.side_effect = [bond_1] # request target api @@ -248,8 +360,9 @@ def test_normal_2(self, mock_IbetStraightBondContract_get, client, db): assert resp.json() == { "issuer_address": issuer_address, "token_address": token_address, - "token_type": TokenType.IBET_STRAIGHT_BOND.value, + "token_type": TokenType.IBET_STRAIGHT_BOND, "token_name": "test_bond_1", + "token_attributes": token_attr, "balance": 0, "exchange_balance": 0, "exchange_commitment": 12, @@ -270,9 +383,9 @@ def test_normal_3_1(self, mock_IbetStraightBondContract_get, client, db): _token = Token() _token.token_address = token_address _token.issuer_address = issuer_address - _token.type = TokenType.IBET_STRAIGHT_BOND.value + _token.type = TokenType.IBET_STRAIGHT_BOND _token.tx_hash = "" - _token.abi = "" + _token.abi = {} _token.version = TokenVersion.V_24_09 db.add(_token) @@ -290,7 +403,50 @@ def test_normal_3_1(self, mock_IbetStraightBondContract_get, client, db): # mock bond_1 = IbetStraightBondContract() - bond_1.name = "test_bond_1" + token_attr = { + "issuer_address": issuer_address, + "token_address": token_address, + "name": "test_bond_1", + "symbol": "TEST-test", + "total_supply": 9999999, + "contact_information": "test1", + "privacy_policy": "test2", + "tradable_exchange_contract_address": "0x1234567890123456789012345678901234567890", + "status": False, + "personal_info_contract_address": "0x1234567890123456789012345678901234567891", + "require_personal_info_registered": True, + "transferable": True, + "is_offering": True, + "transfer_approval_required": True, + "face_value": 9999998, + "face_value_currency": "JPY", + "interest_rate": 99.999, + "interest_payment_date": [ + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + ], + "interest_payment_currency": "JPY", + "redemption_date": "99991231", + "redemption_value": 9999997, + "redemption_value_currency": "JPY", + "return_date": "99991230", + "return_amount": "return_amount-test", + "base_fx_rate": 123.456789, + "purpose": "purpose-test", + "memo": "memo-test", + "is_redeemed": True, + } + bond_1.__dict__ = token_attr mock_IbetStraightBondContract_get.side_effect = [bond_1] # request target api @@ -306,8 +462,9 @@ def test_normal_3_1(self, mock_IbetStraightBondContract_get, client, db): assert resp.json() == { "issuer_address": issuer_address, "token_address": token_address, - "token_type": TokenType.IBET_STRAIGHT_BOND.value, + "token_type": TokenType.IBET_STRAIGHT_BOND, "token_name": "test_bond_1", + "token_attributes": token_attr, "balance": 0, "exchange_balance": 0, "exchange_commitment": 0, @@ -327,9 +484,9 @@ def test_normal_3_2(self, mock_IbetStraightBondContract_get, client, db): _token = Token() _token.token_address = token_address _token.issuer_address = issuer_address - _token.type = TokenType.IBET_STRAIGHT_BOND.value + _token.type = TokenType.IBET_STRAIGHT_BOND _token.tx_hash = "" - _token.abi = "" + _token.abi = {} _token.version = TokenVersion.V_24_09 db.add(_token) @@ -357,7 +514,50 @@ def test_normal_3_2(self, mock_IbetStraightBondContract_get, client, db): # mock bond_1 = IbetStraightBondContract() - bond_1.name = "test_bond_1" + token_attr = { + "issuer_address": issuer_address, + "token_address": token_address, + "name": "test_bond_1", + "symbol": "TEST-test", + "total_supply": 9999999, + "contact_information": "test1", + "privacy_policy": "test2", + "tradable_exchange_contract_address": "0x1234567890123456789012345678901234567890", + "status": False, + "personal_info_contract_address": "0x1234567890123456789012345678901234567891", + "require_personal_info_registered": True, + "transferable": True, + "is_offering": True, + "transfer_approval_required": True, + "face_value": 9999998, + "face_value_currency": "JPY", + "interest_rate": 99.999, + "interest_payment_date": [ + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + ], + "interest_payment_currency": "JPY", + "redemption_date": "99991231", + "redemption_value": 9999997, + "redemption_value_currency": "JPY", + "return_date": "99991230", + "return_amount": "return_amount-test", + "base_fx_rate": 123.456789, + "purpose": "purpose-test", + "memo": "memo-test", + "is_redeemed": True, + } + bond_1.__dict__ = token_attr mock_IbetStraightBondContract_get.side_effect = [bond_1] # request target api @@ -373,8 +573,9 @@ def test_normal_3_2(self, mock_IbetStraightBondContract_get, client, db): assert resp.json() == { "issuer_address": issuer_address, "token_address": token_address, - "token_type": TokenType.IBET_STRAIGHT_BOND.value, + "token_type": TokenType.IBET_STRAIGHT_BOND, "token_name": "test_bond_1", + "token_attributes": token_attr, "balance": 0, "exchange_balance": 0, "exchange_commitment": 0, @@ -395,9 +596,9 @@ def test_normal_3_3(self, mock_IbetStraightBondContract_get, client, db): _token = Token() _token.token_address = token_address _token.issuer_address = issuer_address - _token.type = TokenType.IBET_STRAIGHT_BOND.value + _token.type = TokenType.IBET_STRAIGHT_BOND _token.tx_hash = "" - _token.abi = "" + _token.abi = {} _token.version = TokenVersion.V_24_09 db.add(_token) @@ -415,7 +616,50 @@ def test_normal_3_3(self, mock_IbetStraightBondContract_get, client, db): # mock bond_1 = IbetStraightBondContract() - bond_1.name = "test_bond_1" + token_attr = { + "issuer_address": issuer_address, + "token_address": token_address, + "name": "test_bond_1", + "symbol": "TEST-test", + "total_supply": 9999999, + "contact_information": "test1", + "privacy_policy": "test2", + "tradable_exchange_contract_address": "0x1234567890123456789012345678901234567890", + "status": False, + "personal_info_contract_address": "0x1234567890123456789012345678901234567891", + "require_personal_info_registered": True, + "transferable": True, + "is_offering": True, + "transfer_approval_required": True, + "face_value": 9999998, + "face_value_currency": "JPY", + "interest_rate": 99.999, + "interest_payment_date": [ + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + ], + "interest_payment_currency": "JPY", + "redemption_date": "99991231", + "redemption_value": 9999997, + "redemption_value_currency": "JPY", + "return_date": "99991230", + "return_amount": "return_amount-test", + "base_fx_rate": 123.456789, + "purpose": "purpose-test", + "memo": "memo-test", + "is_redeemed": True, + } + bond_1.__dict__ = token_attr mock_IbetStraightBondContract_get.side_effect = [bond_1] # request target api @@ -431,8 +675,9 @@ def test_normal_3_3(self, mock_IbetStraightBondContract_get, client, db): assert resp.json() == { "issuer_address": issuer_address, "token_address": token_address, - "token_type": TokenType.IBET_STRAIGHT_BOND.value, + "token_type": TokenType.IBET_STRAIGHT_BOND, "token_name": "test_bond_1", + "token_attributes": token_attr, "balance": 5, "exchange_balance": 10, "exchange_commitment": 15, @@ -452,9 +697,9 @@ def test_normal_3_4(self, mock_IbetStraightBondContract_get, client, db): _token = Token() _token.token_address = token_address _token.issuer_address = issuer_address - _token.type = TokenType.IBET_STRAIGHT_BOND.value + _token.type = TokenType.IBET_STRAIGHT_BOND _token.tx_hash = "" - _token.abi = "" + _token.abi = {} _token.version = TokenVersion.V_24_09 db.add(_token) @@ -482,7 +727,50 @@ def test_normal_3_4(self, mock_IbetStraightBondContract_get, client, db): # mock bond_1 = IbetStraightBondContract() - bond_1.name = "test_bond_1" + token_attr = { + "issuer_address": issuer_address, + "token_address": token_address, + "name": "test_bond_1", + "symbol": "TEST-test", + "total_supply": 9999999, + "contact_information": "test1", + "privacy_policy": "test2", + "tradable_exchange_contract_address": "0x1234567890123456789012345678901234567890", + "status": False, + "personal_info_contract_address": "0x1234567890123456789012345678901234567891", + "require_personal_info_registered": True, + "transferable": True, + "is_offering": True, + "transfer_approval_required": True, + "face_value": 9999998, + "face_value_currency": "JPY", + "interest_rate": 99.999, + "interest_payment_date": [ + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + "99991231", + ], + "interest_payment_currency": "JPY", + "redemption_date": "99991231", + "redemption_value": 9999997, + "redemption_value_currency": "JPY", + "return_date": "99991230", + "return_amount": "return_amount-test", + "base_fx_rate": 123.456789, + "purpose": "purpose-test", + "memo": "memo-test", + "is_redeemed": True, + } + bond_1.__dict__ = token_attr mock_IbetStraightBondContract_get.side_effect = [bond_1] # request target api @@ -498,8 +786,9 @@ def test_normal_3_4(self, mock_IbetStraightBondContract_get, client, db): assert resp.json() == { "issuer_address": issuer_address, "token_address": token_address, - "token_type": TokenType.IBET_STRAIGHT_BOND.value, + "token_type": TokenType.IBET_STRAIGHT_BOND, "token_name": "test_bond_1", + "token_attributes": token_attr, "balance": 5, "exchange_balance": 10, "exchange_commitment": 15, @@ -575,9 +864,9 @@ def test_error_2_2(self, client, db): _token.issuer_address = ( "0x1234567890123456789012345678900000000101" # not target ) - _token.type = TokenType.IBET_STRAIGHT_BOND.value + _token.type = TokenType.IBET_STRAIGHT_BOND _token.tx_hash = "" - _token.abi = "" + _token.abi = {} _token.version = TokenVersion.V_24_09 db.add(_token) @@ -619,9 +908,9 @@ def test_error_3(self, client, db): _token = Token() _token.token_address = token_address _token.issuer_address = issuer_address - _token.type = TokenType.IBET_STRAIGHT_BOND.value + _token.type = TokenType.IBET_STRAIGHT_BOND _token.tx_hash = "" - _token.abi = "" + _token.abi = {} _token.token_status = 0 _token.version = TokenVersion.V_24_09 db.add(_token)