Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshihitoAso committed Jan 15, 2025
1 parent 51d01fd commit 46e47c0
Show file tree
Hide file tree
Showing 3 changed files with 346 additions and 69 deletions.
19 changes: 9 additions & 10 deletions app/routers/issuer/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
43 changes: 16 additions & 27 deletions tests/app/test_positions_ListAllPositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@
SPDX-License-Identifier: Apache-2.0
"""

from datetime import UTC, datetime, timedelta
from unittest import mock

from app.model.blockchain import IbetShareContract, IbetStraightBondContract
from app.model.db import (
IDXLockedPosition,
IDXPosition,
Token,
TokenCache,
TokenType,
TokenVersion,
)
from config import TOKEN_CACHE_TTL


class TestListAllPositions:
Expand Down Expand Up @@ -1194,7 +1191,8 @@ def test_normal_6(
# <Normal_7_1>
# 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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -1335,7 +1328,8 @@ def test_normal_7_1(self, client, db):
# <Normal_7_2>
# 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"
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down
Loading

0 comments on commit 46e47c0

Please sign in to comment.