feat(M01-B): 严格转换旧库订单金额
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
legacyDecimalToCents,
|
||||
MYSQL_UNSIGNED_INT_MAX
|
||||
} from '../dist/db/legacy-money.js';
|
||||
|
||||
assert.equal(legacyDecimalToCents('0'), 0);
|
||||
assert.equal(legacyDecimalToCents('0.01'), 1);
|
||||
assert.equal(legacyDecimalToCents('12.3'), 1230);
|
||||
assert.equal(legacyDecimalToCents('12.34'), 1234);
|
||||
assert.equal(legacyDecimalToCents(12.34), 1234);
|
||||
assert.equal(legacyDecimalToCents('42949672.95'), MYSQL_UNSIGNED_INT_MAX);
|
||||
assert.equal(legacyDecimalToCents(null, { nullable: true }), null);
|
||||
|
||||
for (const invalidValue of [
|
||||
'',
|
||||
' 1.00',
|
||||
'1.00 ',
|
||||
'-0.01',
|
||||
'+1.00',
|
||||
'1.001',
|
||||
'1e2',
|
||||
'NaN',
|
||||
Number.NaN,
|
||||
Number.POSITIVE_INFINITY,
|
||||
{},
|
||||
null
|
||||
]) {
|
||||
assert.throws(
|
||||
() => legacyDecimalToCents(invalidValue),
|
||||
/Legacy money/
|
||||
);
|
||||
}
|
||||
|
||||
assert.throws(
|
||||
() => legacyDecimalToCents('42949672.96', { field: 'member_order_info.price' }),
|
||||
/member_order_info\.price.*exceeds/
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => legacyDecimalToCents('1.00', { maxCents: Number.MAX_SAFE_INTEGER + 1 }),
|
||||
/maxCents/
|
||||
);
|
||||
|
||||
console.log('PASS: legacy DECIMAL money conversion is exact and bounded.');
|
||||
Reference in New Issue
Block a user