Skip to content

Commit

Permalink
test: add assertAmountsEqual() helper (#2453)
Browse files Browse the repository at this point in the history
add assertAmountsEqual() helper

switched from deepEqual() to assertAmountsEqual() on a non-amount.

* refactor: drop MathKind parameter to assertAmountsEqual()
  • Loading branch information
Chris-Hibbert authored Mar 29, 2021
1 parent 41ded30 commit 2610f7c
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
makeNone,
makeAll,
} from '../../../src/contractSupport/percentMath';
import { assertAmountsEqual } from '../../zoeTestHelpers';

test('percentMath - basic', t => {
const { brand } = makeIssuerKit('moe');
Expand Down Expand Up @@ -63,15 +64,19 @@ test('percentMath - ALL', t => {
/** @param {bigint} value */
const moe = value => amountMath.make(value, brand);

t.deepEqual(moe(100000n), makeAll(brand).scale(moe(100000n)));
assertAmountsEqual(t, moe(100000n), makeAll(brand).scale(moe(100000n)));
});

test('percentMath - NONE', t => {
const { brand } = makeIssuerKit('moe');
/** @param {bigint} value */
const moe = value => amountMath.make(value, brand);

t.deepEqual(amountMath.makeEmpty(brand), makeNone(brand).scale(moe(100000n)));
assertAmountsEqual(
t,
amountMath.makeEmpty(brand),
makeNone(brand).scale(moe(100000n)),
);
});

test('percentMath - complement', t => {
Expand Down
3 changes: 2 additions & 1 deletion packages/zoe/test/unitTests/contracts/loan/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { amountMath } from '@agoric/ertp';
import { setup } from '../../setupBasicMints';
import { setupZCFTest } from '../../zcf/setupZcfTest';
import { makeRatio } from '../../../../src/contractSupport';
import { assertAmountsEqual } from '../../../zoeTestHelpers';

/**
* @param {import("ava").ExecutionContext<unknown>} t
Expand Down Expand Up @@ -78,7 +79,7 @@ export const checkPayouts = async (
const kit = kitKeywordRecord[keyword];
const amount = await kit.issuer.getAmountOf(paymentP);
const expected = expectedKeywordRecord[keyword];
t.deepEqual(amount, expected);
assertAmountsEqual(t, amount, expected);
t.truthy(
amountMath.isEqual(amount, expected),
`amount value: ${amount.value}, expected value: ${expected.value}, message: ${message}`,
Expand Down
82 changes: 62 additions & 20 deletions packages/zoe/test/unitTests/contracts/loan/test-borrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { makeBorrowInvitation } from '../../../../src/contracts/loan/borrow';
import { makeAddCollateralInvitation } from '../../../../src/contracts/loan/addCollateral';
import { makeCloseLoanInvitation } from '../../../../src/contracts/loan/close';
import { makeRatio } from '../../../../src/contractSupport';
import { assertAmountsEqual } from '../../../zoeTestHelpers';

const BASIS_POINTS = 10000n;

Expand Down Expand Up @@ -179,13 +180,13 @@ test('borrow getDebtNotifier', async t => {
const { borrowFacet, maxLoan } = await setupBorrowFacet();
const debtNotifier = await E(borrowFacet).getDebtNotifier();
const state = await debtNotifier.getUpdateSince();
t.deepEqual(state.value, maxLoan);
assertAmountsEqual(t, state.value, maxLoan);
});

test('borrow getRecentCollateralAmount', async t => {
const { borrowFacet, collateral } = await setupBorrowFacet();
const collateralAmount = await E(borrowFacet).getRecentCollateralAmount();
t.deepEqual(collateralAmount, collateral);
assertAmountsEqual(t, collateralAmount, collateral);
});

test('borrow getLiquidationPromise', async t => {
Expand Down Expand Up @@ -213,8 +214,9 @@ test('borrow getLiquidationPromise', async t => {
const { quoteAmount, quotePayment } = await liquidationPromise;
const quoteAmount2 = await E(quoteIssuer).getAmountOf(quotePayment);

t.deepEqual(quoteAmount, quoteAmount2);
t.deepEqual(
assertAmountsEqual(t, quoteAmount, quoteAmount2);
assertAmountsEqual(
t,
quoteAmount,
amountMath.make(
[
Expand Down Expand Up @@ -276,9 +278,9 @@ test('borrow, then addCollateral, then getLiquidationPromise', async t => {

const quoteBrand = await E(quoteIssuer).getBrand();

t.deepEqual(quoteAmount, quoteAmount2);

t.deepEqual(
assertAmountsEqual(t, quoteAmount, quoteAmount2);
assertAmountsEqual(
t,
quoteAmount,
amountMath.make(
[
Expand Down Expand Up @@ -324,21 +326,29 @@ test('getDebtNotifier with interest', async t => {
const { value: originalDebt, updateCount } = await E(
debtNotifier,
).getUpdateSince();
t.deepEqual(originalDebt, maxLoan);
assertAmountsEqual(t, originalDebt, maxLoan);

periodUpdater.updateState(6);

const { value: debtCompounded1, updateCount: updateCount1 } = await E(
debtNotifier,
).getUpdateSince(updateCount);
t.deepEqual(debtCompounded1, amountMath.make(40020n, loanKit.brand));
assertAmountsEqual(
t,
debtCompounded1,
amountMath.make(40020n, loanKit.brand),
);

periodUpdater.updateState(11);

const { value: debtCompounded2 } = await E(debtNotifier).getUpdateSince(
updateCount1,
);
t.deepEqual(debtCompounded2, amountMath.make(40040n, loanKit.brand));
assertAmountsEqual(
t,
debtCompounded2,
amountMath.make(40040n, loanKit.brand),
);

const closeLoanInvitation = E(borrowFacet).makeCloseLoanInvitation();
await checkDescription(t, zoe, closeLoanInvitation, 'repayAndClose');
Expand Down Expand Up @@ -387,14 +397,18 @@ test('aperiodic interest', async t => {
const { value: originalDebt, updateCount } = await E(
debtNotifier,
).getUpdateSince();
t.deepEqual(originalDebt, maxLoan);
assertAmountsEqual(t, originalDebt, maxLoan);

periodUpdater.updateState(6);

const { value: debtCompounded1, updateCount: updateCount1 } = await E(
debtNotifier,
).getUpdateSince(updateCount);
t.deepEqual(debtCompounded1, amountMath.make(40020n, loanKit.brand));
assertAmountsEqual(
t,
debtCompounded1,
amountMath.make(40020n, loanKit.brand),
);

// skip ahead a notification
periodUpdater.updateState(16);
Expand All @@ -404,13 +418,21 @@ test('aperiodic interest', async t => {
debtNotifier,
).getUpdateSince(updateCount1);
t.is(await E(borrowFacet).getLastCalculationTimestamp(), 16n);
t.deepEqual(debtCompounded2, amountMath.make(40060n, loanKit.brand));
assertAmountsEqual(
t,
debtCompounded2,
amountMath.make(40060n, loanKit.brand),
);

periodUpdater.updateState(21);
const { value: debtCompounded3 } = await E(debtNotifier).getUpdateSince(
updateCount2,
);
t.deepEqual(debtCompounded3, amountMath.make(40080n, loanKit.brand));
assertAmountsEqual(
t,
debtCompounded3,
amountMath.make(40080n, loanKit.brand),
);
});

// Show that interest is charged from the time the loan was created.
Expand Down Expand Up @@ -479,7 +501,7 @@ test('short periods', async t => {
const { value: originalDebt, updateCount } = await E(
debtNotifier,
).getUpdateSince();
t.deepEqual(originalDebt, maxLoan);
assertAmountsEqual(t, originalDebt, maxLoan);

periodUpdater.updateState(5);
t.is(await E(borrowFacet).getLastCalculationTimestamp(), 1n);
Expand All @@ -488,28 +510,44 @@ test('short periods', async t => {
const { value: debtCompounded1, updateCount: updateCount1 } = await E(
debtNotifier,
).getUpdateSince(updateCount);
t.deepEqual(debtCompounded1, amountMath.make(40020n, loanKit.brand));
assertAmountsEqual(
t,
debtCompounded1,
amountMath.make(40020n, loanKit.brand),
);
t.is(await E(borrowFacet).getLastCalculationTimestamp(), 6n);

periodUpdater.updateState(14);
const { value: debtCompounded2, updateCount: updateCount2 } = await E(
debtNotifier,
).getUpdateSince(updateCount1);
t.deepEqual(debtCompounded2, amountMath.make(40040n, loanKit.brand));
assertAmountsEqual(
t,
debtCompounded2,
amountMath.make(40040n, loanKit.brand),
);
t.is(await E(borrowFacet).getLastCalculationTimestamp(), 11n);

periodUpdater.updateState(17);
const { value: debtCompounded3, updateCount: updateCount3 } = await E(
debtNotifier,
).getUpdateSince(updateCount2);
t.deepEqual(debtCompounded3, amountMath.make(40060n, loanKit.brand));
assertAmountsEqual(
t,
debtCompounded3,
amountMath.make(40060n, loanKit.brand),
);
t.is(await E(borrowFacet).getLastCalculationTimestamp(), 16n);

periodUpdater.updateState(21);
const { value: debtCompounded4, updateCount: updateCount4 } = await E(
debtNotifier,
).getUpdateSince(updateCount3);
t.deepEqual(debtCompounded4, amountMath.make(40080n, loanKit.brand));
assertAmountsEqual(
t,
debtCompounded4,
amountMath.make(40080n, loanKit.brand),
);
t.is(await E(borrowFacet).getLastCalculationTimestamp(), 21n);

periodUpdater.updateState(25);
Expand All @@ -519,7 +557,11 @@ test('short periods', async t => {
const { value: debtCompounded5 } = await E(debtNotifier).getUpdateSince(
updateCount4,
);
t.deepEqual(debtCompounded5, amountMath.make(40100n, loanKit.brand));
assertAmountsEqual(
t,
debtCompounded5,
amountMath.make(40100n, loanKit.brand),
);
t.is(await E(borrowFacet).getLastCalculationTimestamp(), 26n);
});

Expand Down
7 changes: 6 additions & 1 deletion packages/zoe/test/unitTests/contracts/test-atomicSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Far } from '@agoric/marshal';

import { setup } from '../setupBasicMints';
import { setupNonFungible } from '../setupNonFungibleMints';
import { assertAmountsEqual } from '../../zoeTestHelpers';

const atomicSwapRoot = `${__dirname}/../../../src/contracts/atomicSwap`;

Expand Down Expand Up @@ -424,7 +425,11 @@ test('zoe - atomicSwap like-for-like', async t => {
);

// Alice didn't get any of what Alice put in
t.deepEqual(await moolaIssuer.getAmountOf(aliceAssetPayout), moola(0n));
assertAmountsEqual(
t,
await moolaIssuer.getAmountOf(aliceAssetPayout),
moola(0n),
);

// Alice deposits her payout to ensure she can
const aliceAssetAmount = await aliceMoolaPurse.deposit(aliceAssetPayout);
Expand Down
36 changes: 16 additions & 20 deletions packages/zoe/test/unitTests/contracts/test-coveredCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { sameStructure } from '@agoric/same-structure';
import buildManualTimer from '../../../tools/manualTimer';
import { setup } from '../setupBasicMints';
import { setupNonFungible } from '../setupNonFungibleMints';
import { assertAmountsEqual } from '../../zoeTestHelpers';

const coveredCallRoot = `${__dirname}/../../../src/contracts/coveredCall`;
const atomicSwapRoot = `${__dirname}/../../../src/contracts/atomicSwap`;
Expand Down Expand Up @@ -721,9 +722,7 @@ test('zoe - coveredCall with coveredCall for invitation', async t => {
const daveOptionValue = await E(zoe).getInvitationDetails(daveExclOption);
t.is(daveOptionValue.installation, coveredCallInstallation);
t.is(daveOptionValue.description, 'exerciseOption');
t.truthy(
amountMath.isEqual(daveOptionValue.strikePrice.StrikePrice, bucks(1)),
);
assertAmountsEqual(t, daveOptionValue.strikePrice.StrikePrice, bucks(1));
t.is(daveOptionValue.expirationDate, 100n);
t.deepEqual(daveOptionValue.timeAuthority, timer);

Expand All @@ -736,12 +735,11 @@ test('zoe - coveredCall with coveredCall for invitation', async t => {
daveOptionValue.underlyingAssets.UnderlyingAsset.value[0].expirationDate,
100n,
);
t.truthy(
amountMath.isEqual(
daveOptionValue.underlyingAssets.UnderlyingAsset.value[0].strikePrice
.StrikePrice,
simoleans(7),
),
assertAmountsEqual(
t,
daveOptionValue.underlyingAssets.UnderlyingAsset.value[0].strikePrice
.StrikePrice,
simoleans(7),
);
t.deepEqual(
daveOptionValue.underlyingAssets.UnderlyingAsset.value[0].timeAuthority,
Expand Down Expand Up @@ -932,17 +930,15 @@ test('zoe - coveredCall non-fungible', async t => {
const optionValue = await E(zoe).getInvitationDetails(bobExclOption);
t.is(optionValue.installation, coveredCallInstallation);
t.is(optionValue.description, 'exerciseOption');
t.truthy(
amountMath.isEqual(
optionValue.underlyingAssets.UnderlyingAsset,
growlTigerAmount,
),
);
t.truthy(
amountMath.isEqual(
optionValue.strikePrice.StrikePrice,
aGloriousShieldAmount,
),
assertAmountsEqual(
t,
optionValue.underlyingAssets.UnderlyingAsset,
growlTigerAmount,
);
assertAmountsEqual(
t,
optionValue.strikePrice.StrikePrice,
aGloriousShieldAmount,
);
t.is(optionValue.expirationDate, 1n);
t.deepEqual(optionValue.timeAuthority, timer);
Expand Down
Loading

0 comments on commit 2610f7c

Please sign in to comment.