Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
Improve moduleinteraction specs
Browse files Browse the repository at this point in the history
  • Loading branch information
felix2feng committed Mar 1, 2019
1 parent e6809d1 commit 920b5b3
Showing 1 changed file with 43 additions and 50 deletions.
93 changes: 43 additions & 50 deletions test/contracts/core/extensions/coreModuleInteraction.spec.ts
Expand Up @@ -164,12 +164,15 @@ contract('CoreModuleInteraction', accounts => {
describe('#batchDepositModule', async () => {
const tokenOwner: Address = ownerAccount;
let tokenCount: number = 3;
let mockTokenAddresses: Address[];

let subjectTokens: Address[];
let subjectQuantities: BigNumber[];
let subjectCaller: Address;

beforeEach(async () => {
mockTokens = await erc20Wrapper.deployTokensAsync(tokenCount, tokenOwner);
mockTokenAddresses = _.map(mockTokens, token => token.address);
subjectTokens = _.map(mockTokens, token => token.address);

const approvePromises = _.map(mockTokens, token =>
token.approve.sendTransactionAsync(
transferProxy.address,
Expand All @@ -178,28 +181,22 @@ contract('CoreModuleInteraction', accounts => {
),
);
await Promise.all(approvePromises);

subjectQuantities = _.map(mockTokens, () => DEPLOYED_TOKEN_QUANTITY);
subjectCaller = moduleAccount;
});

afterEach(async () => {
tokenAddresses = undefined;
amountsToDeposit = undefined;
subjectTokens = undefined;
subjectQuantities = undefined;
});

let tokenAddresses: Address[];
let amountsToDeposit: BigNumber[];

async function subject(): Promise<string> {
// Initialize addresses to deployed tokens' addresses unless tokenAddresses is overwritten in test cases
const addresses = tokenAddresses || _.map(mockTokens, token => token.address);
// Initialize quantities to deployed tokens' quantities unless amountsToDeposit is overwritten in test cases
const quantities = amountsToDeposit || _.map(mockTokens, () => DEPLOYED_TOKEN_QUANTITY);

return core.batchDepositModule.sendTransactionAsync(
ownerAccount,
ownerAccount,
addresses,
quantities,
subjectTokens,
subjectQuantities,
{ from: subjectCaller },
);
}
Expand Down Expand Up @@ -230,7 +227,7 @@ contract('CoreModuleInteraction', accounts => {

it('increments the vault balances of the tokens of the owner by the correct amount', async () => {
const existingOwnerVaultBalances = await coreWrapper.getVaultBalancesForTokensForOwner(
mockTokenAddresses,
subjectTokens,
vault,
ownerAccount,
);
Expand All @@ -241,7 +238,7 @@ contract('CoreModuleInteraction', accounts => {
await subject();

const newOwnerVaultBalances = await coreWrapper.getVaultBalancesForTokensForOwner(
mockTokenAddresses,
subjectTokens,
vault,
ownerAccount,
);
Expand All @@ -250,15 +247,15 @@ contract('CoreModuleInteraction', accounts => {

describe('when the quantities array contains a zero value', async () => {
beforeEach(async () => {
tokenAddresses = _.map(mockTokens, token => token.address);
amountsToDeposit = _.map(mockTokens, () => DEPLOYED_TOKEN_QUANTITY);
amountsToDeposit[tokenCount - 2] = ZERO;
subjectTokens = _.map(mockTokens, token => token.address);
subjectQuantities = _.map(mockTokens, () => DEPLOYED_TOKEN_QUANTITY);
subjectQuantities[tokenCount - 2] = ZERO;
});

it('transfers the correct amount of each token from the caller', async () => {
const existingTokenBalances = await erc20Wrapper.getTokenBalances(mockTokens, ownerAccount);
const expectedNewBalances = _.map(existingTokenBalances, (balance, index) =>
balance.sub(amountsToDeposit[index]),
balance.sub(subjectQuantities[index]),
);

await subject();
Expand All @@ -270,7 +267,7 @@ contract('CoreModuleInteraction', accounts => {
it('transfers the correct amount of each token to the vault', async () => {
const existingTokenBalances = await erc20Wrapper.getTokenBalances(mockTokens, vault.address);
const expectedNewBalances = _.map(existingTokenBalances, (balance, index) =>
balance.add(amountsToDeposit[index]),
balance.add(subjectQuantities[index]),
);

await subject();
Expand All @@ -281,18 +278,18 @@ contract('CoreModuleInteraction', accounts => {

it('increments the vault balances of the tokens of the owner by the correct amount', async () => {
const existingOwnerVaultBalances = await coreWrapper.getVaultBalancesForTokensForOwner(
mockTokenAddresses,
subjectTokens,
vault,
ownerAccount,
);
const expectedNewOwnerVaultBalances = _.map(existingOwnerVaultBalances, (balance, index) =>
balance.add(amountsToDeposit[index]),
balance.add(subjectQuantities[index]),
);

await subject();

const newOwnerVaultBalances = await coreWrapper.getVaultBalancesForTokensForOwner(
mockTokenAddresses,
subjectTokens,
vault,
ownerAccount,
);
Expand All @@ -306,8 +303,8 @@ contract('CoreModuleInteraction', accounts => {
const expectedLogs = getExpectedTransferLogs(
ownerAccount,
vault.address,
amountsToDeposit,
tokenAddresses
subjectQuantities,
subjectTokens
);

expect(formattedLogs).to.deep.include.members(expectedLogs);
Expand All @@ -316,7 +313,7 @@ contract('CoreModuleInteraction', accounts => {

describe('when the token addresses input is empty', async () => {
beforeEach(async () => {
tokenAddresses = [];
subjectTokens = [];
});

it('should revert', async () => {
Expand All @@ -326,7 +323,7 @@ contract('CoreModuleInteraction', accounts => {

describe('when the deposit quantities input is empty', async () => {
beforeEach(async () => {
amountsToDeposit = [];
subjectQuantities = [];
});

it('should revert', async () => {
Expand All @@ -336,8 +333,8 @@ contract('CoreModuleInteraction', accounts => {

describe('when the token addresses input length does not match the deposit quantities input length', async () => {
beforeEach(async () => {
tokenAddresses = [_.first(mockTokens).address];
amountsToDeposit = [DEPLOYED_TOKEN_QUANTITY, DEPLOYED_TOKEN_QUANTITY];
subjectTokens = [_.first(mockTokens).address];
subjectQuantities = [DEPLOYED_TOKEN_QUANTITY, DEPLOYED_TOKEN_QUANTITY];
});

it('should revert', async () => {
Expand Down Expand Up @@ -468,12 +465,14 @@ contract('CoreModuleInteraction', accounts => {
describe('#batchWithdrawModule', async () => {
const tokenOwner: Address = ownerAccount;
let tokenCount: number = 3;
let mockTokenAddresses: Address[];

let subjectTokens: Address[];
let subjectQuantities: BigNumber[];
let subjectCaller: Address;

beforeEach(async () => {
mockTokens = await erc20Wrapper.deployTokensAsync(tokenCount, tokenOwner);
mockTokenAddresses = _.map(mockTokens, token => token.address);
subjectTokens = _.map(mockTokens, token => token.address);
const approvePromises = _.map(mockTokens, token =>
token.approve.sendTransactionAsync(
transferProxy.address,
Expand All @@ -489,28 +488,22 @@ contract('CoreModuleInteraction', accounts => {
_.map(mockTokens, () => DEPLOYED_TOKEN_QUANTITY),
{ from: ownerAccount },
);

subjectQuantities = _.map(subjectTokens, () => DEPLOYED_TOKEN_QUANTITY);
subjectCaller = moduleAccount;
});

afterEach(async () => {
tokenAddresses = undefined;
amountsToWithdraw = undefined;
subjectTokens = undefined;
subjectQuantities = undefined;
});

let tokenAddresses: Address[];
let amountsToWithdraw: BigNumber[];

async function subject(): Promise<string> {
// Initialize addresses to deployed tokens' addresses unless tokenAddresses is overwritten in test cases
const addresses = tokenAddresses || _.map(mockTokens, token => token.address);
// Initialize quantites to deployed tokens' quantities unless amountsToWithdraw is overwritten in test cases
const quantities = amountsToWithdraw || _.map(mockTokens, () => DEPLOYED_TOKEN_QUANTITY);

return core.batchWithdrawModule.sendTransactionAsync(
ownerAccount,
ownerAccount,
addresses,
quantities,
subjectTokens,
subjectQuantities,
{ from: subjectCaller },
);
}
Expand Down Expand Up @@ -541,7 +534,7 @@ contract('CoreModuleInteraction', accounts => {

it('decrements the vault balances of the tokens of the owner by the correct amount', async () => {
const existingOwnerVaultBalances = await coreWrapper.getVaultBalancesForTokensForOwner(
mockTokenAddresses,
subjectTokens,
vault,
ownerAccount,
);
Expand All @@ -552,7 +545,7 @@ contract('CoreModuleInteraction', accounts => {
await subject();

const newOwnerVaultBalances = await coreWrapper.getVaultBalancesForTokensForOwner(
mockTokenAddresses,
subjectTokens,
vault,
ownerAccount,
);
Expand All @@ -561,7 +554,7 @@ contract('CoreModuleInteraction', accounts => {

describe('when the token addresses input is empty', async () => {
beforeEach(async () => {
tokenAddresses = [];
subjectTokens = [];
});

it('should revert', async () => {
Expand All @@ -571,7 +564,7 @@ contract('CoreModuleInteraction', accounts => {

describe('when the withdraw quantities input is empty', async () => {
beforeEach(async () => {
amountsToWithdraw = [];
subjectQuantities = [];
});

it('should revert', async () => {
Expand All @@ -581,8 +574,8 @@ contract('CoreModuleInteraction', accounts => {

describe('when the token addresses input length does not match the withdraw quantities input length', async () => {
beforeEach(async () => {
tokenAddresses = [_.first(mockTokens).address];
amountsToWithdraw = [DEPLOYED_TOKEN_QUANTITY, DEPLOYED_TOKEN_QUANTITY];
subjectTokens = [_.first(mockTokens).address];
subjectQuantities = [DEPLOYED_TOKEN_QUANTITY, DEPLOYED_TOKEN_QUANTITY];
});

it('should revert', async () => {
Expand Down

0 comments on commit 920b5b3

Please sign in to comment.