You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.4 KiB
119 lines
3.4 KiB
4 years ago
|
import assert from 'assert';
|
||
|
import { TRANSACTION_STATUSES } from '../../../shared/constants/transaction';
|
||
4 years ago
|
import migration23 from './023';
|
||
5 years ago
|
|
||
7 years ago
|
const storage = {
|
||
4 years ago
|
meta: {},
|
||
|
data: {
|
||
|
TransactionController: {
|
||
|
transactions: [],
|
||
7 years ago
|
},
|
||
|
},
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
4 years ago
|
const transactions = [];
|
||
|
const transactions40 = [];
|
||
|
const transactions20 = [];
|
||
7 years ago
|
|
||
4 years ago
|
const txStates = Object.values(TRANSACTION_STATUSES);
|
||
7 years ago
|
|
||
4 years ago
|
const deletableTxStates = [
|
||
|
TRANSACTION_STATUSES.CONFIRMED,
|
||
|
TRANSACTION_STATUSES.REJECTED,
|
||
|
TRANSACTION_STATUSES.FAILED,
|
||
|
TRANSACTION_STATUSES.DROPPED,
|
||
4 years ago
|
];
|
||
7 years ago
|
|
||
4 years ago
|
let nonDeletableCount = 0;
|
||
7 years ago
|
|
||
4 years ago
|
let status;
|
||
7 years ago
|
while (transactions.length <= 100) {
|
||
4 years ago
|
status =
|
||
|
txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))];
|
||
4 years ago
|
// This is an old migration, let's allow it
|
||
|
// eslint-disable-next-line no-loop-func
|
||
5 years ago
|
if (!deletableTxStates.find((s) => s === status)) {
|
||
4 years ago
|
nonDeletableCount += 1;
|
||
5 years ago
|
}
|
||
4 years ago
|
transactions.push({ status });
|
||
7 years ago
|
}
|
||
|
|
||
|
while (transactions40.length < 40) {
|
||
4 years ago
|
status =
|
||
|
txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))];
|
||
|
transactions40.push({ status });
|
||
7 years ago
|
}
|
||
|
|
||
|
while (transactions20.length < 20) {
|
||
4 years ago
|
status =
|
||
|
txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))];
|
||
|
transactions20.push({ status });
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
storage.data.TransactionController.transactions = transactions;
|
||
7 years ago
|
|
||
5 years ago
|
describe('storage is migrated successfully and the proper transactions are remove from state', function () {
|
||
|
it('should remove transactions that are unneeded', function (done) {
|
||
4 years ago
|
migration23
|
||
|
.migrate(storage)
|
||
5 years ago
|
.then((migratedData) => {
|
||
4 years ago
|
let leftoverNonDeletableTxCount = 0;
|
||
4 years ago
|
const migratedTransactions =
|
||
4 years ago
|
migratedData.data.TransactionController.transactions;
|
||
5 years ago
|
migratedTransactions.forEach((tx) => {
|
||
|
if (!deletableTxStates.find((s) => s === tx.status)) {
|
||
4 years ago
|
leftoverNonDeletableTxCount += 1;
|
||
5 years ago
|
}
|
||
4 years ago
|
});
|
||
4 years ago
|
assert.equal(
|
||
|
leftoverNonDeletableTxCount,
|
||
|
nonDeletableCount,
|
||
|
"migration shouldn't delete transactions we want to keep",
|
||
4 years ago
|
);
|
||
4 years ago
|
assert(
|
||
|
migratedTransactions.length >= 40,
|
||
|
`should be equal or greater to 40 if they are non deletable states got ${migratedTransactions.length} transactions`,
|
||
4 years ago
|
);
|
||
|
done();
|
||
4 years ago
|
})
|
||
4 years ago
|
.catch(done);
|
||
|
});
|
||
7 years ago
|
|
||
4 years ago
|
it('should not remove any transactions because 40 is the expected limit', function (done) {
|
||
4 years ago
|
storage.meta.version = 22;
|
||
|
storage.data.TransactionController.transactions = transactions40;
|
||
4 years ago
|
migration23
|
||
|
.migrate(storage)
|
||
5 years ago
|
.then((migratedData) => {
|
||
4 years ago
|
const migratedTransactions =
|
||
4 years ago
|
migratedData.data.TransactionController.transactions;
|
||
7 years ago
|
|
||
4 years ago
|
assert.equal(
|
||
|
migratedTransactions.length,
|
||
|
40,
|
||
|
"migration shouldn't delete when at limit",
|
||
4 years ago
|
);
|
||
|
done();
|
||
4 years ago
|
})
|
||
4 years ago
|
.catch(done);
|
||
|
});
|
||
7 years ago
|
|
||
4 years ago
|
it('should not remove any transactions because 20 txs is under the expected limit', function (done) {
|
||
4 years ago
|
storage.meta.version = 22;
|
||
|
storage.data.TransactionController.transactions = transactions20;
|
||
4 years ago
|
migration23
|
||
|
.migrate(storage)
|
||
5 years ago
|
.then((migratedData) => {
|
||
4 years ago
|
const migratedTransactions =
|
||
4 years ago
|
migratedData.data.TransactionController.transactions;
|
||
4 years ago
|
assert.equal(
|
||
|
migratedTransactions.length,
|
||
|
20,
|
||
|
"migration shouldn't delete when under limit",
|
||
4 years ago
|
);
|
||
|
done();
|
||
4 years ago
|
})
|
||
4 years ago
|
.catch(done);
|
||
|
});
|
||
|
});
|