Skip to content

Commit 9450e98

Browse files
authored
Merge pull request #4 from elasticio/add-emit-behaviour-jacob
Add better backwards compatibility.
2 parents d32a361 + 27b066b commit 9450e98

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

component.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"link": "/components/csv/actions#read-csv-attachment"
1414
},
1515
"fields": {
16-
"emitBehavior": {
16+
"emitAll": {
1717
"label": "Emit Behavior",
1818
"required": true,
1919
"viewClass": "SelectView",
@@ -86,4 +86,4 @@
8686
"dynamicMetadata": true
8787
}
8888
}
89-
}
89+
}

lib/actions/read.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function sliceIntoChunks(arr, chunkSize) {
3737

3838
async function readCSV(msg, cfg) {
3939
const that = this
40-
const { emitBehavior } = cfg;
40+
const emitBehavior = cfg.emitAll;
4141
const { body } = msg;
4242

4343
// check if url provided in msg
@@ -132,7 +132,7 @@ async function readCSV(msg, cfg) {
132132
}
133133

134134
module.exports.process = readCSV;
135-
module.exports.getMetaModel = async function (cfg) {
135+
module.exports.getMetaModel = async function getMetaModel(cfg) {
136136
const meta = {
137137
in: {
138138
type: 'object',

spec/read.spec.js

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,49 @@ describe('CSV Read component', async () => {
1919
.times(10)
2020
.replyWithFile(200, `${__dirname}/../test/formats.csv`);
2121

22+
// Previously "Fetch All" was true and "Emit Individually" was false
23+
// The next two tests check that this backwards compatability is preserved.
24+
describe('Backwards compatibility check', async () => {
25+
it('Fetch All', async () => {
26+
msg.body = {
27+
url: 'http://test.env.mock/formats.csv',
28+
header: true,
29+
dynamicTyping: true,
30+
delimiter: '',
31+
};
32+
cfg = {
33+
emitAll: 'true',
34+
};
35+
context.emit = sinon.spy();
36+
await readCSV.process.call(context, msg, cfg);
37+
38+
expect(context.emit.callCount)
39+
.to.equal(1); // one emit call
40+
41+
expect(context.emit.lastCall.firstArg)
42+
.to.equal('data'); // with data
43+
});
44+
45+
it('emitAll: false, header: false, dynamicTyping: false', async () => {
46+
msg.body = {
47+
url: 'http://test.env.mock/formats.csv',
48+
header: false,
49+
dynamicTyping: false,
50+
};
51+
cfg = {
52+
emitAll: 'false',
53+
};
54+
context.emit = sinon.spy();
55+
await readCSV.process.call(context, msg, cfg);
56+
57+
expect(context.emit.callCount)
58+
.to.equal(3); // 3 emit calls
59+
60+
expect(context.emit.getCall(1).args[1].body.column0)
61+
.to.equal('2.71828'); // result is number as string
62+
});
63+
});
64+
2265
it('One file', async () => {
2366
msg.body = {
2467
url: 'http://test.env.mock/formats.csv',
@@ -27,7 +70,7 @@ describe('CSV Read component', async () => {
2770
delimiter: '',
2871
};
2972
cfg = {
30-
emitBehavior: 'fetchAll',
73+
emitAll: 'fetchAll',
3174
};
3275
context.emit = sinon.spy();
3376
await readCSV.process.call(context, msg, cfg);
@@ -39,14 +82,14 @@ describe('CSV Read component', async () => {
3982
.to.equal('data'); // with data
4083
});
4184

42-
it('emitAll: true, header: true, dynamicTyping: true', async () => {
85+
it('emitAll: fetchAll, header: true, dynamicTyping: true', async () => {
4386
msg.body = {
4487
url: 'http://test.env.mock/formats.csv',
4588
header: true,
4689
dynamicTyping: true,
4790
};
4891
cfg = {
49-
emitBehavior: 'fetchAll',
92+
emitAll: 'fetchAll',
5093
};
5194
context.emit = sinon.spy();
5295
await readCSV.process.call(context, msg, cfg);
@@ -72,7 +115,7 @@ describe('CSV Read component', async () => {
72115
batchSize: 1,
73116
};
74117
cfg = {
75-
emitBehavior: 'emitBatch',
118+
emitAll: 'emitBatch',
76119
};
77120
context.emit = sinon.spy();
78121
await readCSV.process.call(context, msg, cfg);
@@ -90,14 +133,14 @@ describe('CSV Read component', async () => {
90133
.to.equal(2.71828); // Number
91134
});
92135

93-
it('emitAll: false, header: false, dynamicTyping: false', async () => {
136+
it('emitAll: emitIndividually, header: false, dynamicTyping: false', async () => {
94137
msg.body = {
95138
url: 'http://test.env.mock/formats.csv',
96139
header: false,
97140
dynamicTyping: false,
98141
};
99142
cfg = {
100-
emitBehavior: 'emitIndividually',
143+
emitAll: 'emitIndividually',
101144
};
102145
context.emit = sinon.spy();
103146
await readCSV.process.call(context, msg, cfg);
@@ -126,7 +169,7 @@ describe('CSV Read component', async () => {
126169
header: true,
127170
};
128171
cfg = {
129-
emitBehavior: 'fetchAll',
172+
emitAll: 'fetchAll',
130173
};
131174
context.emit = sinon.spy();
132175
await readCSV.process.call(context, msg, cfg);
@@ -140,7 +183,7 @@ describe('CSV Read component', async () => {
140183
header: 'asd',
141184
};
142185
cfg = {
143-
emitBehavior: 'fetchAll',
186+
emitAll: 'fetchAll',
144187
};
145188
context.emit = sinon.spy();
146189
await readCSV.process.call(context, msg, cfg);
@@ -154,7 +197,7 @@ describe('CSV Read component', async () => {
154197
dynamicTyping: 'asd',
155198
};
156199
cfg = {
157-
emitBehavior: 'fetchAll',
200+
emitAll: 'fetchAll',
158201
};
159202
context.emit = sinon.spy();
160203
await readCSV.process.call(context, msg, cfg);

0 commit comments

Comments
 (0)