-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
executable file
·171 lines (128 loc) · 4.82 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
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
import test from 'ava';
import fs from 'fs'
import path from 'path';
import nock from 'nock';
import * as lib from './src/node_main.mjs';
const sparqlTransformer = lib.default;
const OUTPUT = './examples/json_transformed/';
const JSONLD_QUERIES = './examples/json_queries/';
const SPARQL_QUERIES = './examples/sparql_queries/';
const SPARQL_OUTPUTS = './examples/sparql_output/';
function mock(file) {
nock('http://dbpedia.org')
.post('/sparql')
.query(true)
.reply(200, file);
}
async function getSparqlQuery(q) {
let sparqlQuery = null;
try {
await sparqlTransformer(q, {
debug: false,
sparqlFunction: async(query) => {
sparqlQuery = ` ${query.trim()}`;
return Promise.reject();
},
});
} catch (e) {
// eslint-disable-next-line no-empty
}
return sparqlQuery;
}
function loadFiles(file) {
const orig = fs.readFileSync(`${SPARQL_OUTPUTS}${file}`, 'utf8');
const q = JSON.parse(fs.readFileSync(`${JSONLD_QUERIES}${file}`, 'utf8'));
const sparql = fs.readFileSync(`${SPARQL_QUERIES}${path.basename(file, path.extname(file))}.rq`, 'utf8');
const expected = JSON.parse(fs.readFileSync(`${OUTPUT}${file}`, 'utf8'));
return [orig, q, sparql, expected];
}
test('DBpedia list of cities (proto)', async(t) => {
const file = 'city.list.json';
const [orig, q, sparql, expected] = loadFiles(file);
mock(orig);
const outSparql = await getSparqlQuery(q);
t.deepEqual(outSparql, sparql);
const out = await sparqlTransformer(q);
// fs.writeFileSync('a.json', JSON.stringify(out, null, 2), 'utf-8');
t.deepEqual(out, expected);
});
test('DBpedia list of cities and regions (jsonld)', async(t) => {
const file = 'city.region.list.ld.json';
const [orig, q, sparql, expected] = loadFiles(file);
mock(orig);
const outSparql = await getSparqlQuery(q);
t.deepEqual(outSparql, sparql);
const out = await sparqlTransformer(q);
// fs.writeFileSync('a.json', JSON.stringify(out, null, 2), 'utf-8');
t.deepEqual(out, expected);
});
test('DBpedia grunge bands', async(t) => {
const file = 'band.json';
const [orig, q, sparql, expected] = loadFiles(file);
mock(orig);
const outSparql = await getSparqlQuery(q);
t.deepEqual(outSparql, sparql);
const out = await sparqlTransformer(q);
// fs.writeFileSync('a.json', JSON.stringify(out, null, 2), 'utf-8');
t.deepEqual(out, expected);
});
test('DBpedia genres with bands', async(t) => {
const file = 'band_reversed.json';
const [orig, q, sparql, expected] = loadFiles(file);
mock(orig);
const outSparql = await getSparqlQuery(q);
t.deepEqual(outSparql.trim(), sparql.trim());
const out = await sparqlTransformer(q);
// fs.writeFileSync('a.json', JSON.stringify(out, null, 2), 'utf-8');
t.deepEqual(out, expected);
});
test('Aggregates', async(t) => {
const file = 'aggregates.json';
const [orig, q, sparql, expected] = loadFiles(file);
mock(orig);
const outSparql = await getSparqlQuery(q);
t.deepEqual(outSparql.trim(), sparql.trim());
const out = await sparqlTransformer(q);
// fs.writeFileSync('a.json', JSON.stringify(out, null, 2), 'utf-8');
t.deepEqual(out, expected);
});
test('No lang tag', async(t) => {
const file = 'city.list.ld.json';
const [orig, q, sparql, expected] = loadFiles(file);
mock(orig);
const outSparql = await getSparqlQuery(q);
t.deepEqual(outSparql, sparql);
const out = await sparqlTransformer(q);
// fs.writeFileSync('a.json', JSON.stringify(out, null, 2), 'utf-8');
t.deepEqual(out, expected);
});
test('Duplicate variable name', async(t) => {
const file = 'issue_10_duplicate_vars.json';
const [orig, q, sparql, expected] = loadFiles(file);
mock(orig);
const outSparql = await getSparqlQuery(q);
t.deepEqual(outSparql, sparql);
const out = await sparqlTransformer(q);
// fs.writeFileSync('a.json', JSON.stringify(out, null, 2), 'utf-8');
t.deepEqual(out, expected);
});
test('List-required fieds', async(t) => {
const file = 'band_forcelist.json';
const [orig, q, sparql, expected] = loadFiles(file);
mock(orig);
const outSparql = await getSparqlQuery(q);
t.deepEqual(outSparql, sparql);
const out = await sparqlTransformer(q);
// fs.writeFileSync('a.json', JSON.stringify(out, null, 2), 'utf-8');
t.deepEqual(out, expected);
});
test('Library limit', async(t) => {
const file = 'band.liblimit.json';
const [orig, q, sparql, expected] = loadFiles(file);
mock(orig);
const outSparql = await getSparqlQuery(q);
t.deepEqual(outSparql.trim(), sparql.trim());
const out = await sparqlTransformer(q);
// fs.writeFileSync('a.json', JSON.stringify(out, null, 2), 'utf-8');
t.deepEqual(out, expected);
});