-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.test.js
271 lines (220 loc) · 10.4 KB
/
index.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* author: Pieter Heyvaert (pheyvaer.heyvaert@ugent.be)
* Ghent University - imec - IDLab
*/
const assert = require('assert');
const fs = require('fs');
const RMLMapperWrapper = require('./lib/wrapper');
const { strToQuads } = require('./lib/utils');
const { isomorphic } = require("rdf-isomorphic");
const N3 = require('n3');
const rmlmapperPath = './rmlmapper.jar';
const tempFolderPath = './tmp';
describe('Success', function() {
this.timeout(5000);
it('Simple CSV mapping', async () => {
// GIVEN a wrapper and a simple CSV mapping generating one quad
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true);
const rml = fs.readFileSync('./test/tc01/mapping.ttl', 'utf-8');
const sources = {
'student.csv': fs.readFileSync('./test/tc01/student.csv', 'utf-8')
};
// WHEN generating the quads without the metadata and expected the results to by an array of quads
const result = await wrapper.execute(rml, {sources, generateMetadata: false, asQuads: true});
// THEN the mapping should succeed and the output should match one of the file
const expected = await strToQuads(fs.readFileSync('./test/tc01/output.nq', 'utf-8'));
assert.ok(isomorphic(result.output, expected));
});
it('Simple CSV mapping with metadata', async () => {
// GIVEN a wrapper and a simple CSV mapping generating one quad
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true);
const rml = fs.readFileSync('./test/tc01/mapping.ttl', 'utf-8');
const sources = {
'student.csv': fs.readFileSync('./test/tc01/student.csv', 'utf-8')
};
// WHEN generating the quads without the metadata and expected the results to by an array of quads
const result = await wrapper.execute(rml, {sources, generateMetadata: true, asQuads: true});
// THEN the mapping should succeed and the output should match one of the file
const expected = await strToQuads(fs.readFileSync('./test/tc01/output.nq', 'utf-8'));
assert.ok(isomorphic(result.output, expected));
assert.ok(result.metadata.length > 0);
});
it('Invalid mapping', async () => {
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true);
const rml = fs.readFileSync('./test/tc02/mapping.ttl', 'utf-8');
const sources = {
'student.csv': fs.readFileSync('./test/tc02/student.csv', 'utf-8')
};
let error;
try {
await wrapper.execute(rml, {sources, generateMetadata: false, asQuads: true});
} catch (err) {
error = err;
}
assert.strictEqual(error === null, false);
assert.strictEqual(error.message, `Error while executing the rules.`);
assert.strictEqual(error.log.indexOf('Mapping requires at least one TriplesMap') !== -1, true);
});
it('Serialization: JSON-LD', async () => {
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true);
const rml = fs.readFileSync('./test/tc03/mapping.ttl', 'utf-8');
const sources = {
'student.csv': fs.readFileSync('./test/tc03/student.csv', 'utf-8')
};
const result = await wrapper.execute(rml, {sources, serialization: 'jsonld'});
let error = null;
try {
JSON.parse(result.output);
} catch (err) {
error = err;
}
assert.ok(error === null);
});
it('Serialization: undefined', async () => {
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true);
const rml = fs.readFileSync('./test/tc03/mapping.ttl', 'utf-8');
const sources = {
'student.csv': fs.readFileSync('./test/tc03/student.csv', 'utf-8')
};
let error = null;
try {
await wrapper.execute(rml, {sources});
} catch (err) {
error = err;
}
assert.ok(error === null);
});
it('Input: array of quads', done => {
// GIVEN a wrapper and a simple CSV mapping generating one quad
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true);
const rml = fs.readFileSync('./test/tc01/mapping.ttl', 'utf-8');
const parser = new N3.Parser();
const rmlQuads = [];
parser.parse(rml, async (error, quad) => {
if (quad) {
rmlQuads.push(quad);
} else {
const sources = {
'student.csv': fs.readFileSync('./test/tc01/student.csv', 'utf-8')
};
// WHEN generating the quads without the metadata and expected the results to by an array of quads
const result = await wrapper.execute(rmlQuads, {sources, generateMetadata: false, asQuads: true});
// THEN the mapping should succeed and the output should match one of the file
const expected = await strToQuads(fs.readFileSync('./test/tc01/output.nq', 'utf-8'));
assert.ok(isomorphic(result.output, expected));
done();
}
});
});
it('Add Java VM options', done => {
// GIVEN a wrapper and a simple CSV mapping generating one quad
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true,undefined, undefined, {'Dfile.encoding': 'UTF-8'});
const rml = fs.readFileSync('./test/tc04/mapping.ttl', 'utf-8');
const parser = new N3.Parser();
const rmlQuads = [];
parser.parse(rml, async (error, quad) => {
if (quad) {
rmlQuads.push(quad);
} else {
const sources = {
'student.csv': fs.readFileSync('./test/tc04/student.csv', 'utf-8')
};
// WHEN generating the quads without the metadata and expected the results to by an array of quads
const result = await wrapper.execute(rmlQuads, {sources, generateMetadata: false, asQuads: true});
// THEN the mapping should succeed and the output should match one of the file
const expected = await strToQuads(fs.readFileSync('./test/tc04/output.nq', 'utf-8'));
assert.ok(isomorphic(result.output, expected));
done();
}
});
});
it.skip('Parameter: functions', done => {
// GIVEN a wrapper, a JSON input, and an additional function
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, false);
const rml = fs.readFileSync('./test/tc05/mapping.ttl', 'utf-8');
const parser = new N3.Parser();
const rmlQuads = [];
parser.parse(rml, async (error, quad) => {
if (quad) {
rmlQuads.push(quad);
} else {
const sources = {
'message.json': fs.readFileSync('./test/tc05/message.json', 'utf-8')
};
const fnoStr = fs.readFileSync('./test/tc05/functions_all.nt', 'utf-8');
// WHEN generating the quads with additional functions as parameter
const result = await wrapper.execute(rmlQuads, {sources, generateMetadata: false, asQuads: true, fno: fnoStr});
// THEN the mapping should succeed and the output should match one of the file
const expected = await strToQuads(fs.readFileSync('./test/tc05/output.nq', 'utf-8'));
assert.ok(isomorphic(result.output, expected));
done();
}
});
});
it('Single target', async () => {
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true);
const rml = fs.readFileSync('./test/tc06/mapping.ttl', 'utf-8');
const sources = {
'student.csv': fs.readFileSync('./test/tc06/student.csv', 'utf-8')
};
let result = await wrapper.execute(rml, {sources, generateMetadata: false, asQuads: true});
assert.deepStrictEqual(result.output.stdout, []);
result = await strToQuads(result.output['dump1.nt']);
const expected = await strToQuads(fs.readFileSync('./test/tc06/output.nq', 'utf-8'));
assert.ok(isomorphic(result, expected));
});
it('Two targets', async () => {
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true);
const rml = fs.readFileSync('./test/tc07/mapping.ttl', 'utf-8');
const sources = {
'student.csv': fs.readFileSync('./test/tc07/student.csv', 'utf-8')
};
let result = await wrapper.execute(rml, {sources, generateMetadata: false, asQuads: true});
assert.deepStrictEqual(result.output.stdout, []);
const result1 = await strToQuads(result.output['dump1.nt']);
const expected1 = await strToQuads(fs.readFileSync('./test/tc07/output-1.nq', 'utf-8'));
assert.ok(isomorphic(result1, expected1));
const result2 = await strToQuads(result.output['dump2.nt']);
const expected2 = await strToQuads(fs.readFileSync('./test/tc07/output-2.nq', 'utf-8'));
assert.ok(isomorphic(result2, expected2));
});
it('Data source in subdirectory', async () => {
// GIVEN a wrapper and a simple CSV mapping generating one quad
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true);
const rml = fs.readFileSync('./test/tc08/mapping.ttl', 'utf-8');
const sources = {
'./data/student.csv': fs.readFileSync('./test/tc08/data/student.csv', 'utf-8')
};
// WHEN generating the quads without the metadata and expected the results to by an array of quads
const result = await wrapper.execute(rml, {sources, generateMetadata: false, asQuads: true});
// THEN the mapping should succeed and the output should match one of the file
const expected = await strToQuads(fs.readFileSync('./test/tc01/output.nq', 'utf-8'));
assert.ok(isomorphic(result.output, expected));
});
it('Configure function state ID and test state', async () => {
const stateFolder = tempFolderPath + '/fstate';
const wrapper = new RMLMapperWrapper(
rmlmapperPath,
tempFolderPath,
false,
stateFolder,
60);
const rml = fs.readFileSync('./test/functionstate/mapping.ttl', 'utf-8');
const sources1 = {
'student.csv': fs.readFileSync('./test/functionstate/student.csv', 'utf-8')
};
const sources2 = {
'student.csv': fs.readFileSync('./test/functionstate/student.csv', 'utf-8')
};
// Do a first run.
const result1 = await wrapper.execute(rml, {sources: sources1, generateMetadata: false, asQuads: true, functionStateId: 'test state id'});
const expected1 = await strToQuads(fs.readFileSync('./test/functionstate/output1.nq', 'utf-8'));
assert.ok(isomorphic(result1.output, expected1));
// Do a second run. The result should be empty, because the function used in the mappings
// only produces an IRI when it is not produced before.
const result2 = await wrapper.execute(rml, {sources: sources2, generateMetadata: false, asQuads: false, functionStateId: 'test state id'});
assert.equal(result2.output, '');
// remove temp folder
fs.rm(tempFolderPath, {recursive: true}, () => {});
});
});