Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow empty input #419

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions app/model/schema/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

from .types import (
MMDD_constr,
YYYYMMDD_constr
YYYYMMDD_constr,
EMPTY_str
)


Expand Down Expand Up @@ -194,9 +195,9 @@ class IbetShareCreate(BaseModel):
total_supply: int = Field(..., ge=0, le=1_000_000_000_000)
symbol: Optional[str] = Field(max_length=100)
dividends: Optional[float] = Field(None, ge=0.00, le=5_000_000_000.00)
dividend_record_date: Optional[YYYYMMDD_constr]
dividend_payment_date: Optional[YYYYMMDD_constr]
cancellation_date: Optional[YYYYMMDD_constr]
dividend_record_date: Optional[YYYYMMDD_constr | EMPTY_str]
dividend_payment_date: Optional[YYYYMMDD_constr | EMPTY_str]
cancellation_date: Optional[YYYYMMDD_constr | EMPTY_str]
transferable: Optional[bool]
status: Optional[bool]
is_offering: Optional[bool]
Expand Down Expand Up @@ -231,9 +232,9 @@ def personal_info_contract_address_is_valid_address(cls, v):

class IbetShareUpdate(BaseModel):
"""ibet Share schema (Update)"""
cancellation_date: Optional[YYYYMMDD_constr]
dividend_record_date: Optional[YYYYMMDD_constr]
dividend_payment_date: Optional[YYYYMMDD_constr]
cancellation_date: Optional[YYYYMMDD_constr | EMPTY_str]
dividend_record_date: Optional[YYYYMMDD_constr | EMPTY_str]
dividend_payment_date: Optional[YYYYMMDD_constr | EMPTY_str]
dividends: Optional[float] = Field(None, ge=0.00, le=5_000_000_000.00)
tradable_exchange_contract_address: Optional[str]
personal_info_contract_address: Optional[str]
Expand Down
4 changes: 2 additions & 2 deletions app/model/schema/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

SPDX-License-Identifier: Apache-2.0
"""
from enum import Enum
from typing import Optional
from typing import Optional, Literal

from pydantic import (
BaseModel,
Expand All @@ -26,6 +25,7 @@

MMDD_constr = constr(regex="^(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$")
YYYYMMDD_constr = constr(regex="^(19[0-9]{2}|20[0-9]{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$")
EMPTY_str = Literal[""]


class ResultSet(BaseModel):
Expand Down
166 changes: 166 additions & 0 deletions tests/test_app_routers_share_tokens_POST.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,154 @@ def test_normal_3(self, client, db):
update_token = db.query(UpdateToken).first()
assert update_token is None

# <Normal_4_1>
# YYYYMMDD parameter is not empty
def test_normal_4_1(self, client, db):
test_account = config_eth_account("user1")

# prepare data
account = Account()
account.issuer_address = test_account["address"]
account.keyfile = test_account["keyfile_json"]
account.eoa_password = E2EEUtils.encrypt("password")
db.add(account)

# mock
IbetShareContract_create = patch(
target="app.model.blockchain.token.IbetShareContract.create",
return_value=("contract_address_test1", "abi_test1", "tx_hash_test1")
)
TokenListContract_register = patch(
target="app.model.blockchain.token_list.TokenListContract.register",
return_value=None
)
ContractUtils_get_block_by_transaction_hash = patch(
target="app.utils.contract_utils.ContractUtils.get_block_by_transaction_hash",
return_value={
"number": 12345,
"timestamp": datetime(2021, 4, 27, 12, 34, 56, tzinfo=timezone.utc).timestamp()
}
)

with IbetShareContract_create, \
TokenListContract_register, \
ContractUtils_get_block_by_transaction_hash:
# request target api
req_param = {
"name": "name_test1",
"symbol": "symbol_test1",
"issue_price": 1000,
"total_supply": 10000,
"dividends": 123.45,
"dividend_record_date": "20211231",
"dividend_payment_date": "20211231",
"cancellation_date": "20221231",
"principal_value": 1000
}
resp = client.post(
self.apiurl,
json=req_param,
headers={
"issuer-address": test_account["address"],
"eoa-password": E2EEUtils.encrypt("password")
}
)

# assertion
IbetShareContract.create.assert_called_with(
args=[
"name_test1",
"symbol_test1",
1000,
10000,
1234500000000000,
"20211231",
"20211231",
"20221231",
1000
],
tx_from=test_account["address"],
private_key=ANY
)

assert resp.status_code == 200
assert resp.json()["token_address"] == "contract_address_test1"
assert resp.json()["token_status"] == 1

# <Normal_4_2>
# YYYYMMDD parameter is empty
def test_normal_4_2(self, client, db):
test_account = config_eth_account("user1")

# prepare data
account = Account()
account.issuer_address = test_account["address"]
account.keyfile = test_account["keyfile_json"]
account.eoa_password = E2EEUtils.encrypt("password")
db.add(account)

# mock
IbetShareContract_create = patch(
target="app.model.blockchain.token.IbetShareContract.create",
return_value=("contract_address_test1", "abi_test1", "tx_hash_test1")
)
TokenListContract_register = patch(
target="app.model.blockchain.token_list.TokenListContract.register",
return_value=None
)
ContractUtils_get_block_by_transaction_hash = patch(
target="app.utils.contract_utils.ContractUtils.get_block_by_transaction_hash",
return_value={
"number": 12345,
"timestamp": datetime(2021, 4, 27, 12, 34, 56, tzinfo=timezone.utc).timestamp()
}
)

with IbetShareContract_create, \
TokenListContract_register, \
ContractUtils_get_block_by_transaction_hash:
# request target api
req_param = {
"name": "name_test1",
"symbol": "symbol_test1",
"issue_price": 1000,
"total_supply": 10000,
"dividends": 123.45,
"dividend_record_date": "",
"dividend_payment_date": "",
"cancellation_date": "",
"principal_value": 1000
}
resp = client.post(
self.apiurl,
json=req_param,
headers={
"issuer-address": test_account["address"],
"eoa-password": E2EEUtils.encrypt("password")
}
)

# assertion
IbetShareContract.create.assert_called_with(
args=[
"name_test1",
"symbol_test1",
1000,
10000,
1234500000000000,
"",
"",
"",
1000
],
tx_from=test_account["address"],
private_key=ANY
)

assert resp.status_code == 200
assert resp.json()["token_address"] == "contract_address_test1"
assert resp.json()["token_status"] == 1

###########################################################################
# Error Case
###########################################################################
Expand Down Expand Up @@ -910,17 +1058,35 @@ def test_error_2_6(self, client, db):
'type': 'value_error.str.regex',
'ctx': {'pattern': '^(19[0-9]{2}|20[0-9]{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$'}
},
{
'loc': ['body', 'dividend_record_date'],
'msg': "unexpected value; permitted: ''",
'type': 'value_error.const',
'ctx': {'given': '202101010', 'permitted': ['']}
},
{
'loc': ['body', 'dividend_payment_date'],
'msg': 'string does not match regex "^(19[0-9]{2}|20[0-9]{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$"',
'type': 'value_error.str.regex',
'ctx': {'pattern': '^(19[0-9]{2}|20[0-9]{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$'}
},
{
'loc': ['body', 'dividend_payment_date'],
'msg': "unexpected value; permitted: ''",
'type': 'value_error.const',
'ctx': {'given': '202101010', 'permitted': ['']}
},
{
'loc': ['body', 'cancellation_date'],
'msg': 'string does not match regex "^(19[0-9]{2}|20[0-9]{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$"',
'type': 'value_error.str.regex',
'ctx': {'pattern': '^(19[0-9]{2}|20[0-9]{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$'}
},
{
'loc': ['body', 'cancellation_date'],
'msg': "unexpected value; permitted: ''",
'type': 'value_error.const',
'ctx': {'given': '202201010', 'permitted': ['']}
}
]
}
Expand Down
Loading