This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
134 lines (108 loc) · 4.51 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const expect = require('chai').expect;
const util = require('../lib/util');
const files = {
originalXLS: path.resolve(__dirname, 'data/original.xls'),
originalJSON: path.resolve(__dirname, 'data/original.json'),
currencyJSON: path.resolve(__dirname, 'data/currency.json'),
sortedJSON: path.resolve(__dirname, 'data/sorted.json'),
processedJSON: path.resolve(__dirname, 'data/processed.json'),
basicJSON: path.resolve(__dirname, 'data/basic.json'),
noRebateJSON: path.resolve(__dirname, 'data/no-rebate.json'),
bonusJSON: path.resolve(__dirname, 'data/bonus.json'),
detailJSON: path.resolve(__dirname, 'data/detail.json'),
overLimitJSON: path.resolve(__dirname, 'data/over-limit.json')
}
const readFile = path => {
return fs.readFileSync(path, 'utf8');
}
const writeJSONFile = (file, data) => {
fs.writeFileSync(
path.resolve(__dirname, file),
JSON.stringify(data, null, 2)
);
}
describe('Data Processing', () => {
it('should convert XLS to JSON w/ specfic format', () => {
const originalJSON = readFile(files.originalJSON);
const resultJSON = JSON.stringify(util.parseXLS(files.originalXLS), null, 2);
expect(resultJSON).to.equal(originalJSON);
});
it('should transform currency from string to integer', () => {
const currencyJSON = readFile(files.currencyJSON);
const originalJSON = JSON.parse(readFile(files.originalJSON));
const resultJSON = JSON.stringify(util._.convertCurrency(originalJSON), null, 2);
expect(resultJSON).to.equal(currencyJSON);
});
it('should sort records by posting date', () => {
const currencyResult = JSON.parse(readFile(files.currencyJSON));
const resultJSON = JSON.stringify(util._.sortByPostingDate(currencyResult), null, 2);
const sortedJSON = readFile(files.sortedJSON);
expect(resultJSON).to.equal(sortedJSON);
});
it('should generate JSON which is sorted and have correct type format', () => {
const processedJSON = readFile(files.processedJSON);
const originalJSON = JSON.parse(readFile(files.originalJSON));
const resultJSON = JSON.stringify(util.processBills(originalJSON), null, 2);
expect(resultJSON).to.equal(processedJSON);
});
});
describe('Rebate Calculation', () => {
const rateTable = require('../lib/rate');
const calculateRecord = (record) => {
let resultJSON;
let isCashBack;
_.each(rateTable, rate => {
if (rate.match.test(record.detail)) {
if (rate.rate !== 0) {
resultJSON = util._.calculateRebate(record, 0 - record.amount, rate.rate);
} else {
resultJSON = util._.calculateRebate(record, 0, 0);
}
isCashBack = true;
return false;
}
if (!isCashBack) {
resultJSON = util._.calculateRebate(record, 0 - record.amount, 0);
}
});
return resultJSON;
}
it('should have basic rebate', () => {
const processedJSON = JSON.parse(readFile(files.processedJSON));
const basicBill = processedJSON[0];
const basicJSON = readFile(files.basicJSON);
const resultJSON = JSON.stringify(calculateRecord(basicBill), null, 2);
expect(resultJSON).to.equal(basicJSON);
});
it('should have no rebate', () => {
const processedJSON = JSON.parse(readFile(files.processedJSON));
const basicBill = processedJSON[1];
const noRebateJSON = readFile(files.noRebateJSON);
const resultJSON = JSON.stringify(calculateRecord(basicBill), null, 2);
expect(resultJSON).to.equal(noRebateJSON);
});
it('should have bonus rebate', () => {
const processedJSON = JSON.parse(readFile(files.processedJSON));
const basicBill = processedJSON[3];
const bonusJSON = readFile(files.bonusJSON);
const resultJSON = JSON.stringify(calculateRecord(basicBill), null, 2);
expect(resultJSON).to.equal(bonusJSON);
});
it('should generate detail of rebate', () => {
const processedJSON = JSON.parse(readFile(files.processedJSON));
const detailJSON = readFile(files.detailJSON);
const resultJSON = JSON.stringify(util.calculateTotalRebate(processedJSON, 0, 4), null, 2);
expect(resultJSON).to.equal(detailJSON);
});
it('should not over rebate limit', () => {
const processedJSON = JSON.parse(readFile(files.processedJSON));
const overLimitJSON = readFile(files.overLimitJSON);
let resultJSON;
processedJSON[3].amount = -30000;
resultJSON = JSON.stringify(util.calculateTotalRebate(processedJSON, 0, 4), null, 2);
expect(resultJSON).to.equal(overLimitJSON);
});
});