Commit 815ba375 authored by Nicolas Frandeboeuf's avatar Nicolas Frandeboeuf Committed by Benjamin Bouvier
Browse files

[tests] Make sure budgets duplicates are cleaned on import

parent 736f63b3
Pipeline #407491 passed with stage
in 4 minutes and 36 seconds
......@@ -61,6 +61,11 @@ export default class Budget {
return await Budget.repo().find({ userId });
}
// Doesn't insert anything in db, only creates a new instance and normalizes its fields.
static cast(args: Partial<Budget>): Budget {
return Budget.repo().create(args);
}
static async create(userId: number, attributes: Partial<Budget>): Promise<Budget> {
const entity = Budget.repo().create({ ...attributes, userId });
return await Budget.repo().save(entity);
......
......@@ -6,6 +6,7 @@ import moment from 'moment';
import {
Access,
Account,
Budget,
Category,
Setting,
Transaction,
......@@ -21,6 +22,7 @@ let { parseOfxDate } = ofxTesting;
async function cleanAll(userId) {
await Access.destroyAll(userId);
await Account.destroyAll(userId);
await Budget.destroyAll(userId);
await Category.destroyAll(userId);
await Setting.destroyAll(userId);
await Transaction.destroyAll(userId);
......@@ -200,6 +202,13 @@ describe('import', () => {
importDate: new Date('2019-01-01:00:00.000Z'),
},
],
budgets: [
// Duplicates should be cleaned and no error should be thrown
{ categoryId: 0, year: 2020, month: 12, threshold: 100 },
{ categoryId: 0, year: 2020, month: 12, threshold: 100 },
],
};
function newWorld() {
......@@ -208,6 +217,7 @@ describe('import', () => {
result.accounts = result.accounts.map(account => Account.cast(account));
result.categories = result.categories.map(category => Category.cast(category));
result.operations = result.operations.map(operation => Transaction.cast(operation));
result.budgets = result.budgets.map(budget => Budget.cast(budget));
return result;
}
......@@ -227,6 +237,11 @@ describe('import', () => {
actualCategories.length.should.equal(data.categories.length);
actualCategories.should.containDeep(data.categories);
// Budgets duplicates should be removed.
const actualBudgets = await Budget.all(USER_ID);
actualBudgets.length.should.equal(1);
actualBudgets.should.containDeep([data.budgets[0]]);
// Test for transactions is done below.
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment