/
popularity_mapper.js
268 lines (240 loc) · 8.51 KB
/
popularity_mapper.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
const through = require('through2');
const mapper = require('../../stream/popularity_mapper');
const ixtures = require('../fixtures/docs');
const Document = require('pelias-model').Document;
module.exports.tests = {};
// test exports
module.exports.tests.interface = function (test, common) {
test('interface: factory', t => {
t.equal(typeof mapper, 'function', 'stream factory');
t.end();
});
test('interface: stream', t => {
var stream = mapper();
t.equal(typeof stream, 'object', 'valid stream');
t.equal(typeof stream._read, 'function', 'valid readable');
t.equal(typeof stream._write, 'function', 'valid writeable');
t.end();
});
};
// ===================== popularity mapping ======================
module.exports.tests.importance_international = function (test, common) {
var doc = new Document('osm', 'venue', 1);
doc.setMeta('tags', { 'importance': 'international' });
test('maps - importance_international', t => {
var stream = mapper();
stream.pipe(through.obj((doc, enc, next) => {
t.deepEqual(doc.getPopularity(), 50000, 'correctly mapped');
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
module.exports.tests.importance_national = function (test, common) {
var doc = new Document('osm', 'venue', 1);
doc.setMeta('tags', { 'importance': 'national' });
test('maps - importance_national', t => {
var stream = mapper();
stream.pipe(through.obj((doc, enc, next) => {
t.deepEqual(doc.getPopularity(), 10000, 'correctly mapped');
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
module.exports.tests.wikidata = function (test, common) {
var doc = new Document('osm', 'venue', 1);
doc.setMeta('tags', { 'wikidata': 'Q1371018' });
test('maps - wikidata', t => {
var stream = mapper();
stream.pipe(through.obj((doc, enc, next) => {
t.deepEqual(doc.getPopularity(), 3000, 'correctly mapped');
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
module.exports.tests.wikipedia = function (test, common) {
var doc = new Document('osm', 'venue', 1);
doc.setMeta('tags', { 'wikipedia': 'it:Aeroporto di Perugia' });
test('maps - wikipedia', t => {
var stream = mapper();
stream.pipe(through.obj((doc, enc, next) => {
t.deepEqual(doc.getPopularity(), 3000, 'correctly mapped');
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
module.exports.tests.building_hospital = function (test, common) {
var doc = new Document('osm', 'venue', 1);
doc.setMeta('tags', { 'building': 'hospital' });
test('maps - building:hospital', t => {
var stream = mapper();
stream.pipe(through.obj((doc, enc, next) => {
t.deepEqual(doc.getPopularity(), 2000, 'correctly mapped');
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
module.exports.tests.tourism_aquarium = function (test, common) {
var doc = new Document('osm', 'venue', 1);
doc.setMeta('tags', { 'tourism': 'aquarium' });
test('maps - tourism:aquarium', t => {
var stream = mapper();
stream.pipe(through.obj((doc, enc, next) => {
t.deepEqual(doc.getPopularity(), 3000, 'correctly mapped');
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
module.exports.tests.amenity_post_office = function (test, common) {
var doc = new Document('osm', 'venue', 1);
doc.setMeta('tags', { 'amenity': 'post_office' });
test('maps - amenity:post_office', t => {
var stream = mapper();
stream.pipe(through.obj((doc, enc, next) => {
t.deepEqual(doc.getPopularity(), 1000, 'correctly mapped');
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
module.exports.tests.contact_phone = function (test, common) {
var doc = new Document('osm', 'venue', 1);
doc.setMeta('tags', { 'contact:phone': '555-5555' });
test('maps - contact:phone', t => {
var stream = mapper();
stream.pipe(through.obj((doc, enc, next) => {
t.deepEqual(doc.getPopularity(), 200, 'correctly mapped');
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
// ===================== transportation ======================
module.exports.tests.international_airport = function(test, common) {
var doc = new Document('a','venue',1);
doc.setMeta('tags', { 'aerodrome': 'international', 'iata': 'JFK' });
test('alias - international airport', function(t) {
var stream = mapper();
stream.pipe( through.obj( function( doc, enc, next ){
t.deepEqual(doc.getPopularity(), 15000);
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
module.exports.tests.regional_airport = function (test, common) {
var doc = new Document('a', 'venue', 1);
doc.setMeta('tags', { 'aerodrome': 'regional', 'iata': 'None' });
test('alias - regional airport (invalid IATA code)', function (t) {
var stream = mapper();
stream.pipe(through.obj(function (doc, enc, next) {
t.deepEqual(doc.getPopularity(), 5000);
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
module.exports.tests.railway_station = function (test, common) {
var doc = new Document('a', 'venue', 1);
doc.setMeta('tags', { 'railway': 'station' });
test('alias - railway station', function (t) {
var stream = mapper();
stream.pipe(through.obj(function (doc, enc, next) {
t.deepEqual(doc.getPopularity(), 2000);
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
// ===================== do not map non-venue docs ======================
module.exports.tests.nonvenue = function (test, common) {
var doc = new Document('osm', 'address', 1);
doc.setMeta('tags', { 'importance': 'international' });
test('does not map - non-venue', t => {
var stream = mapper();
stream.pipe(through.obj((doc, enc, next) => {
t.false(doc.getPopularity(), 'no mapping performed');
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
// ===================== discard disused places ======================
module.exports.tests.disused = function (test, common) {
var doc = new Document('osm', 'venue', 1);
doc.setMeta('tags', { 'disused:amenity': 'yes' });
test('does not map - disused', t => {
var stream = mapper();
var counter = 0;
stream.pipe(through.obj((doc, enc, next) => {
counter++;
next();
}, (done) => {
t.equal(counter, 0, 'document discarded');
t.end(); // test will fail if not called (or called twice).
done();
}));
stream.write(doc);
stream.end();
});
};
// ===================== discard abandoned places ======================
module.exports.tests.abandoned = function (test, common) {
var doc = new Document('osm', 'venue', 1);
doc.setMeta('tags', { 'abandoned:amenity': 'yes' });
test('does not map - abandoned', t => {
var stream = mapper();
var counter = 0;
stream.pipe(through.obj((doc, enc, next) => {
counter++;
next();
}, (done) => {
t.equal(counter, 0, 'document discarded');
t.end(); // test will fail if not called (or called twice).
done();
}));
stream.write(doc);
stream.end();
});
};
// ======================= errors ========================
// catch general errors in the stream, emit logs and passthrough the doc
module.exports.tests.catch_thrown_errors = function (test, common) {
test('errors - catch thrown errors', t => {
var doc = new Document('osm', 'venue', 1);
// this method will throw a generic Error for testing
doc.getMeta = function () { throw new Error('test'); };
var stream = mapper();
stream.pipe(through.obj((doc, enc, next) => {
t.deepEqual(doc.getType(), 'venue', 'doc passthrough');
t.end(); // test will fail if not called (or called twice).
next();
}));
stream.write(doc);
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('popularity_mapper: ' + name, testFunction);
}
for (var testCase in module.exports.tests) {
module.exports.tests[testCase](test, common);
}
};