Skip to content

Commit a19e8ba

Browse files
authored
Fix emit batch strategy (#9)
1 parent 7a0436c commit a19e8ba

File tree

5 files changed

+133
-4
lines changed

5 files changed

+133
-4
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = {
55
},
66
rules: {
77
'import/extensions': 'off',
8+
"no-use-before-define": 0,
89
},
910
};

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.1.3 (April 22, 2022)
2+
* Fix a bug when emit strategy 'Emit Batch' did not process correctly
3+
14
## 3.1.2 (April 14, 2022)
25
* Update `component-commons-library` to read and upload attachments through `Maester`
36
* Update `elasticio-sailor-nodejs` to v2.6.27

component.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"title": "CSV",
33
"description": "A comma-separated values (CSV) file stores tabular data (numbers and text) in plain-text form",
44
"docsUrl": "https://github.com/elasticio/csv-component",
5-
"version": "3.1.2",
5+
"version": "3.1.3",
66
"actions": {
77
"read_action": {
88
"main": "./lib/actions/read.js",
@@ -85,4 +85,4 @@
8585
"dynamicMetadata": true
8686
}
8787
}
88-
}
88+
}

lib/actions/read.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ async function readCSV(msg, cfg) {
121121
if (emitBehavior === 'fetchAll' || cfg.emitAll === true || cfg.emitAll === 'true') {
122122
await this.emit('data', messages.newMessageWithBody({ result }))
123123
} else if (emitBehavior === 'emitBatch') {
124-
const chunks = sliceIntoChunks(result, body.batchSize);
124+
const { batchSize } = body;
125+
if (!isPositiveInteger(batchSize)) {
126+
throw new Error("'batchSize' must be a positive integer!");
127+
}
128+
const chunks = sliceIntoChunks(result, batchSize);
125129
// eslint-disable-next-line no-plusplus
126130
for (let i = 0; i < chunks.length; i++) {
127131
// eslint-disable-next-line no-await-in-loop
@@ -162,7 +166,7 @@ module.exports.getMetaModel = async function getMetaModel(cfg) {
162166
out: {}
163167
};
164168

165-
if (cfg.emitBehavior === 'emitBatch') {
169+
if (cfg.emitAll === 'emitBatch') {
166170
meta.in.properties.batchSize = {
167171
title: 'Batch Size',
168172
type: 'number',
@@ -171,3 +175,7 @@ module.exports.getMetaModel = async function getMetaModel(cfg) {
171175
}
172176
return meta;
173177
}
178+
179+
function isPositiveInteger(input) {
180+
return Number.isSafeInteger(input) && input > 0;
181+
}

spec/read.spec.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,87 @@ describe('CSV Read component', async () => {
6262
});
6363
});
6464

65+
describe('emitAll: emitBatch', async () => {
66+
it('should contain batchSize field', async () => {
67+
cfg = {
68+
emitAll: 'emitBatch',
69+
};
70+
const expectedMetadata = {
71+
in: {
72+
type: 'object',
73+
properties: {
74+
url: {
75+
type: 'string',
76+
required: true,
77+
title: 'URL',
78+
},
79+
header: {
80+
type: 'boolean',
81+
required: false,
82+
title: 'Contains headers',
83+
},
84+
delimiter: {
85+
type: 'string',
86+
required: false,
87+
title: 'Delimiter',
88+
},
89+
dynamicTyping: {
90+
type: 'boolean',
91+
required: false,
92+
title: 'Convert Data types',
93+
},
94+
batchSize: {
95+
title: 'Batch Size',
96+
type: 'number',
97+
required: true,
98+
},
99+
},
100+
},
101+
out: {},
102+
};
103+
const metadata = await readCSV.getMetaModel(cfg);
104+
expect(metadata).to.deep.equal(expectedMetadata);
105+
});
106+
});
107+
108+
describe('emitAll: fetchAll', async () => {
109+
it('should not contain batchSize field', async () => {
110+
cfg = {
111+
emitAll: 'fetchAll',
112+
};
113+
const expectedMetadata = {
114+
in: {
115+
type: 'object',
116+
properties: {
117+
url: {
118+
type: 'string',
119+
required: true,
120+
title: 'URL',
121+
},
122+
header: {
123+
type: 'boolean',
124+
required: false,
125+
title: 'Contains headers',
126+
},
127+
delimiter: {
128+
type: 'string',
129+
required: false,
130+
title: 'Delimiter',
131+
},
132+
dynamicTyping: {
133+
type: 'boolean',
134+
required: false,
135+
title: 'Convert Data types',
136+
},
137+
},
138+
},
139+
out: {},
140+
};
141+
const metadata = await readCSV.getMetaModel(cfg);
142+
expect(metadata).to.deep.equal(expectedMetadata);
143+
});
144+
});
145+
65146
it('One file', async () => {
66147
msg.body = {
67148
url: 'http://test.env.mock/formats.csv',
@@ -133,6 +214,42 @@ describe('CSV Read component', async () => {
133214
.to.equal(2.71828); // Number
134215
});
135216

217+
it('emitBatch: true, batchSize is negative', async () => {
218+
msg.body = {
219+
url: 'http://test.env.mock/formats.csv',
220+
header: true,
221+
dynamicTyping: true,
222+
batchSize: -5,
223+
};
224+
cfg = {
225+
emitAll: 'emitBatch',
226+
};
227+
context.emit = sinon.spy();
228+
try {
229+
await readCSV.process.call(context, msg, cfg);
230+
} catch (err) {
231+
expect(err.message).to.be.equal('\'batchSize\' must be a positive integer!');
232+
}
233+
});
234+
235+
it('emitBatch: true, batchSize is string', async () => {
236+
msg.body = {
237+
url: 'http://test.env.mock/formats.csv',
238+
header: true,
239+
dynamicTyping: true,
240+
batchSize: 'asd',
241+
};
242+
cfg = {
243+
emitAll: 'emitBatch',
244+
};
245+
context.emit = sinon.spy();
246+
try {
247+
await readCSV.process.call(context, msg, cfg);
248+
} catch (err) {
249+
expect(err.message).to.be.equal('\'batchSize\' must be a positive integer!');
250+
}
251+
});
252+
136253
it('emitAll: emitIndividually, header: false, dynamicTyping: false', async () => {
137254
msg.body = {
138255
url: 'http://test.env.mock/formats.csv',

0 commit comments

Comments
 (0)