fix: preserve mobile presign decimal precision.
This commit is contained in:
1 parent
f761c7021e
commit
e0dc7f4cee
2 files changed
+206
-9
No files matched your search
@@ -1437,7 +1437,7 @@ def test_dfj_presign_transfer_can_edit_submit_withdraw_and_soft_delete():
|
||||
ctx=_ctx('specialist'),
|
||||
user_info=_user(),
|
||||
)
|
||||
assert updated['transferAmount'] == 200.25
|
||||
assert updated['transferAmount'] == '200.25'
|
||||
assert updated['remark'] == '更新'
|
||||
assert (
|
||||
db.query(BizPresignTransfer).filter_by(id=transfer_id).one().create_dept_id
|
||||
@@ -2172,3 +2172,135 @@ def test_market_presign_routes_are_fixed_and_expose_scoped_transfer_actions():
|
||||
}
|
||||
)
|
||||
assert transfer_actual == transfer_expected
|
||||
|
||||
|
||||
def test_mobile_presign_money_dto_preserves_decimal_strings_for_presenter(
|
||||
monkeypatch,
|
||||
):
|
||||
import json
|
||||
import subprocess
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
from app.crud import presign_order as presign_crud
|
||||
from app.services import market_presign
|
||||
|
||||
db = _session()
|
||||
normal = _order('money-normal', sales_user_id='u1')
|
||||
normal.contract_amount = Decimal('1234.50')
|
||||
empty = _order('money-empty', sales_user_id='u1')
|
||||
empty.contract_amount = None
|
||||
paid = _transfer(
|
||||
'money-paid', order_id=normal.id, pay_status='yjf'
|
||||
)
|
||||
paid.transfer_amount = Decimal('200.25')
|
||||
draft = _transfer(
|
||||
'money-draft', order_id=normal.id, pay_status='dfj'
|
||||
)
|
||||
draft.transfer_amount = Decimal('12.30')
|
||||
db.add_all([normal, empty, paid, draft])
|
||||
db.commit()
|
||||
|
||||
items, total = market_presign.list_presign_orders(
|
||||
db,
|
||||
ctx=_ctx('specialist'),
|
||||
user_info=_user(),
|
||||
page=1,
|
||||
page_size=10,
|
||||
)
|
||||
by_id = {item['id']: item for item in items}
|
||||
assert total == 2
|
||||
assert by_id['money-normal']['contractAmount'] == '1234.50'
|
||||
assert by_id['money-normal']['confirmedDepositAmount'] == '200.25'
|
||||
assert by_id['money-empty']['contractAmount'] is None
|
||||
assert by_id['money-empty']['confirmedDepositAmount'] == '0.00'
|
||||
|
||||
normal_detail = market_presign.get_presign_order(
|
||||
db,
|
||||
order_id=normal.id,
|
||||
ctx=_ctx('specialist'),
|
||||
user_info=_user(),
|
||||
)
|
||||
transfer_amounts = {
|
||||
transfer['id']: transfer['transferAmount']
|
||||
for transfer in normal_detail['transfers']
|
||||
}
|
||||
assert transfer_amounts == {
|
||||
'money-paid': '200.25',
|
||||
'money-draft': '12.30',
|
||||
}
|
||||
|
||||
maximum = Decimal('9999999999999999.99')
|
||||
maximum_order = _order('money-maximum', sales_user_id='u1')
|
||||
maximum_order.contract_amount = maximum
|
||||
monkeypatch.setattr(
|
||||
market_presign,
|
||||
'assert_presign_access',
|
||||
lambda *_args, **_kwargs: maximum_order,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
presign_crud,
|
||||
'confirmed_deposit_amount',
|
||||
lambda *_args, **_kwargs: maximum,
|
||||
)
|
||||
maximum_detail = market_presign.get_presign_order(
|
||||
db,
|
||||
order_id=maximum_order.id,
|
||||
ctx=_ctx('specialist'),
|
||||
user_info=_user(),
|
||||
)
|
||||
assert maximum_detail['contractAmount'] == str(maximum)
|
||||
assert maximum_detail['confirmedDepositAmount'] == str(maximum)
|
||||
maximum_transfer = _transfer(
|
||||
'money-maximum-transfer', order_id=maximum_order.id
|
||||
)
|
||||
maximum_transfer.transfer_amount = maximum
|
||||
maximum_transfer_dto = market_presign._mobile_transfer_to_dict(
|
||||
db, maximum_transfer, 'tenant-a'
|
||||
)
|
||||
assert maximum_transfer_dto['transferAmount'] == str(maximum)
|
||||
|
||||
serialized = json.dumps(
|
||||
{
|
||||
'maximum': maximum_detail,
|
||||
'maximumTransfer': maximum_transfer_dto,
|
||||
'normal': normal_detail,
|
||||
'empty': by_id['money-empty'],
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
presenter_url = (
|
||||
Path(__file__).resolve().parents[2]
|
||||
/ 'mobile-oa-h5/src/market/presign/presign-presenter.js'
|
||||
).as_uri()
|
||||
script = """
|
||||
import fs from 'node:fs'
|
||||
const { formatPresignMoney } = await import(process.argv[1])
|
||||
const dto = JSON.parse(fs.readFileSync(0, 'utf8'))
|
||||
const values = [
|
||||
dto.maximum.contractAmount,
|
||||
dto.maximum.confirmedDepositAmount,
|
||||
dto.maximumTransfer.transferAmount,
|
||||
dto.normal.contractAmount,
|
||||
dto.normal.transfers.find((item) => item.id === 'money-paid').transferAmount,
|
||||
dto.empty.contractAmount,
|
||||
dto.empty.confirmedDepositAmount
|
||||
]
|
||||
process.stdout.write(JSON.stringify(values.map(formatPresignMoney)))
|
||||
"""
|
||||
formatted = subprocess.run(
|
||||
['node', '--input-type=module', '-e', script, presenter_url],
|
||||
input=serialized,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
assert json.loads(formatted.stdout) == [
|
||||
'¥9,999,999,999,999,999.99',
|
||||
'¥9,999,999,999,999,999.99',
|
||||
'¥9,999,999,999,999,999.99',
|
||||
'¥1,234.50',
|
||||
'¥200.25',
|
||||
'-',
|
||||
'¥0.00',
|
||||
]
|
||||
Reference in new issue
Block a user