Skip to content

Instantly share code, notes, and snippets.

@codegino
Last active August 2, 2018 03:57
Show Gist options
  • Save codegino/c485cc7b3ab7c3dfe2e7677777add609 to your computer and use it in GitHub Desktop.
Save codegino/c485cc7b3ab7c3dfe2e7677777add609 to your computer and use it in GitHub Desktop.
let assert = require("chai").assert;
const {
getAmountByCategoryInPeriod
} = require('./transaction-test-1.js');
const CATEGORIES = {
GROCERIES: 'groceries',
EATING_OUT: 'eating_out',
OTHER: 'other',
SALARY: 'salary',
};
describe("getAmountByCategoryInPeriod()", () => {
it("returns 0 if there are no transactions", () => {
assert.equal(
getAmountByCategoryInPeriod(
[],
CATEGORIES.GROCERIES,
new Date("2018-03-01"),
new Date("2018-03-31")
),
0
);
});
it("should have sum of 10 for all groceries within Mar 9 - Mar 13", () => {
assert.equal(
getAmountByCategoryInPeriod(
list,
CATEGORIES.GROCERIES,
new Date("2018-03-09"),
new Date("2018-03-13")
),
10
);
});
it("should have sum of -10 for all groceries within Mar 9 - Mar 15", () => {
assert.equal(
getAmountByCategoryInPeriod(
list,
CATEGORIES.GROCERIES,
new Date("2018-03-09"),
new Date("2018-03-15")
), -10
);
});
it("should process exclusive end date", () => {
assert.equal(
getAmountByCategoryInPeriod(
list,
CATEGORIES.GROCERIES,
new Date("2018-03-09"),
new Date('2018-03-14T12:34:00Z')
),
10
);
});
it("should process inclusive start date", () => {
assert.equal(
getAmountByCategoryInPeriod(
list,
CATEGORIES.GROCERIES,
new Date('2018-03-14T12:34:00Z'),
new Date('2018-03-15T12:35:00Z')
), -20
);
});
});
const list = [{
id: 1,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: 10,
category: CATEGORIES.EATING_OUT,
time: '2018-03-12T12:34:00Z'
},
{
id: 2,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -20,
category: CATEGORIES.GROCERIES,
time: '2018-03-14T12:34:00Z'
},
{
id: 3,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: 30,
category: CATEGORIES.OTHER,
time: '2018-03-11T12:34:00Z'
},
{
id: 5,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -40,
category: CATEGORIES.GROCERIES,
time: 'Mar 10 2018'
},
{
id: 6,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: 50,
category: CATEGORIES.GROCERIES,
time: 'March 11 2018'
},
]
const assert = require('chai').assert
const {
findCategoriesOverBudgetInPeriod,
getOverSpendingCategories,
createCategoryAndAmountMapping,
filterBasedOnDateRange
} = require('./transaction-test-2.js');
describe('filterBasedOnDateRange()', () => {
it('will not remove any if all is included', () => {
assert.equal(filterBasedOnDateRange(testData, new Date('Jan-03-2018'), new Date('Dec-02-2019')).length, 6);
})
it('will not remove date before and after 2018', () => {
assert.equal(filterBasedOnDateRange(testData, new Date('Jan-01-2018'), new Date('Dec-31-2018')).length, 5);
})
});
describe('getOverSpendingCategories()', () => {
it('will remove negative values and change them to positive', () => {
const input = {
eating_out: 300,
groceries: -2450,
other: 0
};
const expected = {
groceries: 2450
};
assert.deepEqual(getOverSpendingCategories(input), expected);
})
it('will return empty object when there is no negative', () => {
const input = {
eating_out: 300,
groceries: 2450,
other: 0
};
const expected = {};
assert.deepEqual(getOverSpendingCategories(input), expected);
})
})
describe('createCategoryAndAmountMapping()', () => {
it('will create objects with three keys', () => {
const expected = {
eating_out: 300,
groceries: -2450,
other: 0
};
assert.deepEqual(createCategoryAndAmountMapping(testData, commonBudget), expected);
})
})
describe('findCategoriesOverBudgetInPeriod()', () => {
it('returns empty object if no budget nor transactions is passed in', () => {
assert.deepEqual(findCategoriesOverBudgetInPeriod(), {})
})
it('returns object with groceries and other', () => {
assert.deepEqual(findCategoriesOverBudgetInPeriod(testData, commonBudget, new Date('Jan-03-2018'), new Date('Dec-02-2018')), {
'groceries': 2450,
'other': 3000
})
})
it('returns object with other only', () => {
assert.deepEqual(findCategoriesOverBudgetInPeriod(testData, commonBudget, new Date('2018-03-25T14:00:00Z'), new Date('Mar-26-2018')), {
'other': 3000
})
})
it('returns empty object for when income is included', () => {
assert.deepEqual(findCategoriesOverBudgetInPeriod(testData, commonBudget, new Date('2018-03-25T14:00:00Z'), new Date('Mar-26-2019')), {})
})
})
const commonBudget = {
'other': 1000,
'groceries': 1000,
'eating_out': 1000,
}
const testData = [{
id: 1,
sourceAccount: "A",
targetAccount: "B",
amount: -500,
category: "eating_out",
time: "2018-03-02T10:33:00Z"
},
{
id: 2,
sourceAccount: "A",
targetAccount: "C",
amount: -1450,
category: "groceries",
time: "2018-03-12T13:33:50Z"
},
{
id: 3,
sourceAccount: "A",
targetAccount: "B",
amount: -200,
category: "eating_out",
time: "2018-03-15T15:34:30Z"
},
{
id: 4,
sourceAccount: "A",
targetAccount: "C",
amount: -2000,
category: "groceries",
time: "2018-03-20T09:36:00Z"
},
{
id: 5,
sourceAccount: "A",
targetAccount: "D",
amount: -4000,
category: "other",
time: "2018-03-25T14:00:00Z"
},
{
id: 6,
sourceAccount: "A",
targetAccount: "D",
amount: 3000,
category: "other",
time: "2019-03-25T14:00:00Z"
},
];
const assert = require('chai').assert;
const {
findDuplicateTransactions,
createListOfDuplicateTransactions,
createMapOfDuplicates,
addDuplicateTransactionToList,
checkAndRemoveLastIndex,
duplicateTransactionInExpectedFormat,
} = require('./transaction-test-3.js');
describe('findDuplicateTransactions()', () => {
it('returns empty array if there are no transactions', function () {
assert.deepEqual(findDuplicateTransactions([]), []);
});
it('returns expected', () => {
assert.deepEqual(findDuplicateTransactions(smallData), expectedForSmallTestValue);
})
it('returns expected final data', () => {
assert.deepEqual(findDuplicateTransactions(largeData), expectedForLargeTestValue);
})
});
describe('duplicateTransactionInExpectedFormat()', () => {
const list = [ { id: 5, time: '2018-03-02T10:33:00.000Z' }];
const key = 'A::C::250::other';
it('returns empty array if there are no transactions', function () {
const expectedFormat = {
id: 5,
sourceAccount: 'A',
targetAccount: 'C',
category: 'other',
amount: 250,
time: '2018-03-02T10:33:00.000Z'
}
assert.deepEqual(duplicateTransactionInExpectedFormat(list, key), [expectedFormat]);
});
});
describe('addDuplicateTransactionToList()', () => {
let accumulatedList = [];
let itemToAdd = {}
beforeEach(() => {
accumulatedList = [{
id: 6,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:33:05.000Z',
}];
itemToAdd = {
id: 6,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:33:05.000Z',
};
})
it('add item when list is empty', () => {
accumulatedList = [];
assert.equal(addDuplicateTransactionToList(accumulatedList, itemToAdd, itemToAdd.time).length, 1);
})
it('add item when date difference is less than 1 minute', () => {
itemToAdd.time = '2018-03-02T10:33:25.000Z'
assert.equal(addDuplicateTransactionToList(accumulatedList, itemToAdd, itemToAdd.time).length, 2);
})
it('ignore item when date difference is more than 1 minute', () => {
itemToAdd.time = '2018-03-02T10:35:05.000Z'
assert.equal(addDuplicateTransactionToList(accumulatedList, itemToAdd, itemToAdd.time).length, 1);
})
it('will support multiple addition of multiple duplicates', () => {
itemToAdd.time = '2018-03-02T10:33:35.000Z'
assert.equal(addDuplicateTransactionToList(accumulatedList, itemToAdd, itemToAdd.time).length, 2);
itemToAdd.time = '2018-03-02T10:33:35.000Z'
assert.equal(addDuplicateTransactionToList(accumulatedList, itemToAdd, itemToAdd.time).length, 3);
itemToAdd.time = '2018-03-02T10:35:35.000Z'
assert.equal(addDuplicateTransactionToList(accumulatedList, itemToAdd, itemToAdd.time).length, 4);
itemToAdd.time = '2018-03-02T10:35:45.000Z'
assert.equal(addDuplicateTransactionToList(accumulatedList, itemToAdd, itemToAdd.time).length, 5);
})
});
describe('createListOfDuplicateTransactions()', () => {
it('returns 2 for final data', () => {
assert.equal(createListOfDuplicateTransactions(largeData).length, 2);
})
it('returns 2 for input data', () => {
assert.equal(createListOfDuplicateTransactions(smallData).length, 2);
})
});
describe('createMapOfDuplicates()', () => {
it('returns 20 for final data', () => {
assert.equal(createMapOfDuplicates(largeData).length, 20);
})
it('returns 2 for input data', () => {
assert.equal(createMapOfDuplicates(smallData).length, 2);
})
});
describe('checkAndRemoveLastIndex()', () => {
const erroneousLastIndex = [{
id: 5,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:33:00.000Z'
},
{
id: 6,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:33:05.000Z'
},
{
id: 7,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:35:05.000Z'
},
]
it('returns length of 2 for final data', () => {
checkAndRemoveLastIndex(erroneousLastIndex);
assert.equal(erroneousLastIndex.length, 2);
})
it('returns 2 for input data', () => {
assert.equal(createMapOfDuplicates(smallData).length, 2);
})
})
const smallData = [{
id: 1,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:33:00.000Z'
},
{
id: 2,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:33:50.000Z'
},
{
id: 3,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:34:30.000Z'
},
{
id: 4,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:36:00.000Z'
},
{
id: 5,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:33:00.000Z'
},
{
id: 6,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:33:05.000Z'
}
];
const largeData = [{
id: 3,
sourceAccount: 'my_account',
targetAccount: 'supermarket',
amount: -1000,
category: 'groceries',
time: '2018-03-01T17:28:32.000Z'
},
{
id: 4,
sourceAccount: 'my_account',
targetAccount: 'cinema',
amount: -330,
category: 'other',
time: '2018-03-01T20:10:15.000Z'
},
{
id: 1,
sourceAccount: 'company_x',
targetAccount: 'my_account',
amount: 10000,
category: 'salary',
time: '2018-02-25T08:00:00.000Z'
},
{
id: 11,
sourceAccount: 'my_account',
targetAccount: 'supermarket',
amount: -1540,
category: 'groceries',
time: '2018-03-05T16:24:31.000Z'
},
{
id: 35,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:58:06.000Z'
},
{
id: 38,
sourceAccount: 'my_account',
targetAccount: 'restaurant',
amount: -970,
category: 'eating_out',
time: '2018-05-17T19:52:46.000Z'
},
{
id: 25,
sourceAccount: 'my_account',
targetAccount: 'supermarket',
amount: -850,
category: 'groceries',
time: '2018-04-20T18:51:31.000Z'
},
{
id: 16,
sourceAccount: 'company_x',
targetAccount: 'my_account',
amount: 10000,
category: 'salary',
time: '2018-03-25T08:10:00.000Z'
},
{
id: 26,
sourceAccount: 'my_account',
targetAccount: 'cinema',
amount: -450,
category: 'other',
time: '2018-04-23T19:13:10.000Z'
},
{
id: 24,
sourceAccount: 'my_account',
targetAccount: 'fitness_club',
amount: -610,
category: 'other',
time: '2018-04-22T11:54:10.000Z'
},
{
id: 27,
sourceAccount: 'company_x',
targetAccount: 'my_account',
amount: 10000,
category: 'salary',
time: '2018-04-25T08:00:00.000Z'
},
{
id: 32,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:56:09.000Z'
},
{
id: 7,
sourceAccount: 'my_account',
targetAccount: 'supermarket',
amount: -160,
category: 'groceries',
time: '2018-03-02T13:14:00.000Z'
},
{
id: 15,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-04-01T10:25:10.000Z'
},
{
id: 30,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:54:21.000Z'
},
{
id: 18,
sourceAccount: 'my_account',
targetAccount: 'cinema',
amount: -580,
category: 'other',
time: '2018-04-05T20:01:18.000Z'
},
{
id: 8,
sourceAccount: 'my_account',
targetAccount: 'restaurant',
amount: -670,
category: 'eating_out',
time: '2018-03-02T18:54:45.000Z'
},
{
id: 19,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-04-07T09:54:21.000Z'
},
{
id: 41,
sourceAccount: 'my_account',
targetAccount: 'cinema',
amount: -450,
category: 'other',
time: '2018-05-23T19:13:10.000Z'
},
{
id: 14,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-04-01T10:24:40.000Z'
},
{
id: 29,
sourceAccount: 'my_account',
targetAccount: 'cinema',
amount: -580,
category: 'other',
time: '2018-05-05T20:01:18.000Z'
},
{
id: 9,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-03-04T07:14:20.000Z'
},
{
id: 36,
sourceAccount: 'my_account',
targetAccount: 'internet_shop',
amount: -1650,
category: 'other',
time: '2018-05-08T21:36:41.000Z'
},
{
id: 33,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:57:05.000Z'
},
{
id: 21,
sourceAccount: 'my_account',
targetAccount: 'supermarket',
amount: -1690,
category: 'groceries',
time: '2018-04-10T18:14:10.000Z'
},
{
id: 39,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -70,
category: 'eating_out',
time: '2018-05-15T09:12:20.000Z'
},
{
id: 5,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-03-02T09:25:20.000Z'
},
{
id: 13,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-04-01T10:24:00.000Z'
},
{
id: 23,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -70,
category: 'eating_out',
time: '2018-04-15T09:12:20.000Z'
},
{
id: 2,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-03-01T12:34:00.000Z'
},
{
id: 6,
sourceAccount: 'my_account',
targetAccount: 'internet_shop',
amount: -250,
category: 'other',
time: '2018-03-01T22:16:40.000Z'
},
{
id: 31,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:55:10.000Z'
},
{
id: 28,
sourceAccount: 'my_account',
targetAccount: 'supermarket',
amount: -1870,
category: 'groceries',
time: '2018-05-05T10:24:30.000Z'
},
{
id: 12,
sourceAccount: 'my_account',
targetAccount: 'bowling_place',
amount: -600,
category: 'other',
time: '2018-03-05T21:12:10.000Z'
},
{
id: 17,
sourceAccount: 'my_account',
targetAccount: 'supermarket',
amount: -1870,
category: 'groceries',
time: '2018-04-05T10:24:30.000Z'
},
{
id: 37,
sourceAccount: 'my_account',
targetAccount: 'supermarket',
amount: -1690,
category: 'groceries',
time: '2018-05-10T18:14:10.000Z'
},
{
id: 10,
sourceAccount: 'my_account',
targetAccount: 'fitness_club',
amount: -560,
category: 'other',
time: '2018-03-04T12:54:10.000Z'
},
{
id: 39,
sourceAccount: 'my_account',
targetAccount: 'fitness_club',
amount: -610,
category: 'other',
time: '2018-05-22T11:54:10.000Z'
},
{
id: 20,
sourceAccount: 'my_account',
targetAccount: 'internet_shop',
amount: -1650,
category: 'other',
time: '2018-04-08T21:36:41.000Z'
},
{
id: 22,
sourceAccount: 'my_account',
targetAccount: 'restaurant',
amount: -970,
category: 'eating_out',
time: '2018-04-17T19:52:46.000Z'
},
{
id: 40,
sourceAccount: 'my_account',
targetAccount: 'supermarket',
amount: -850,
category: 'groceries',
time: '2018-05-20T18:51:31.000Z'
}
]
const expectedForSmallTestValue = [
[{
id: 1,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:33:00.000Z'
},
{
id: 2,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:33:50.000Z'
},
{
id: 3,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:34:30.000Z'
}
],
[{
id: 5,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:33:00.000Z'
},
{
id: 6,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:33:05.000Z'
}
]
];
const expectedForLargeTestValue = [
[{
id: 13,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-04-01T10:24:00.000Z'
},
{
id: 14,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-04-01T10:24:40.000Z'
},
{
id: 15,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -50,
category: 'eating_out',
time: '2018-04-01T10:25:10.000Z'
}
],
[{
id: 30,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:54:21.000Z'
},
{
id: 31,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:55:10.000Z'
},
{
id: 32,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:56:09.000Z'
},
{
id: 33,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -90,
category: 'eating_out',
time: '2018-05-07T09:57:05.000Z'
}
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment