/
scrape.test.js
449 lines (361 loc) · 11.8 KB
/
scrape.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//const fs = require('fs')
const scrapeIndex = require('../lib/index')
const scrapeReport = require('../lib/report')
const getDB = require('rwv-sqlite/lib/db')
const reportTable = require('rwv-sqlite/lib/report')
const scrape = require('../scrape')
const { DEFAULT_INDEX_URL, FIRST_PAGE, urlOfPage } = require('../lib/constants')
//const reportJson = require('../samples/report.json')
//Mock HTTP requests
jest.mock('../lib/index')
jest.mock('../lib/report')
//Lets us control timers
jest.useFakeTimers()
describe('Scraper stream', () => {
let db
beforeEach( async (done) => {
setTimeout.mockClear()
scrapeIndex.mockReset()
scrapeReport.mockReset()
getDB().then( ({DB, migrations}) =>{
db = DB
//Start with a clean database
migrations.reset( () => migrations.up( done ) )
})
})
afterEach( () => {
if(db){
db.close()
db = undefined
}
})
test( 'No URLs, one page', async (done) => {
scrapeIndex.mockImplementationOnce(() => ({
reportURLs: [],
pageCount: 1
}))
const reportStream = await scrape( db.db )
expect( scrapeIndex ).toHaveBeenCalledTimes(1)
reportStream.resume()//pipe( process.stdout )
reportStream.on('end', () => {
expect( scrapeReport ).not.toHaveBeenCalled()
done()
})
})
test( 'Wait between requests groups', async (done) => {
scrapeIndex.mockImplementation(() => ({
reportsURLs: [1,2,3,4,5],
pageCount: 1
}))
let c = 0
const longWait = 999999
const reportStream = await scrape( db.db, { groupSize: 2, groupInterval: longWait } )
reportStream.on('data', async(chunk) => {
if( c % 2 == 0 ){
jest.runOnlyPendingTimers()
}
})
reportStream.on('error', (err) => {
throw err
})
reportStream.on('end', () => {
expect( setTimeout ).toHaveBeenCalledTimes(2)
expect( setTimeout ).toHaveBeenLastCalledWith(expect.any(Function), longWait)
expect( scrapeIndex ).toHaveBeenCalledTimes(1)
expect( scrapeReport ).toHaveBeenCalledTimes(5)
expect( scrapeReport ).toHaveBeenCalledWith( 1 )
expect( scrapeReport ).toHaveBeenCalledWith( 2 )
expect( scrapeReport ).toHaveBeenCalledWith( 3 )
expect( scrapeReport ).toHaveBeenCalledWith( 4 )
expect( scrapeReport ).toHaveBeenCalledWith( 5 )
done()
})
})
test( 'Scrape all pages', async (done) => {
scrapeIndex.mockImplementationOnce(() => ({ //page 1
reportsURLs: [1],
pageCount: 3
})).mockImplementationOnce(() => ({ //page 2
reportsURLs: [2,3],
})).mockImplementationOnce(() => ({ //page 3
reportsURLs: [4,5],
}))
const reportStream = await scrape( db.db , { groupSize: 2, groupInterval: 1000 } )
let c = 0
reportStream.on('data', async(chunk) => {
c++
switch( c ){
case 1:
expect( scrapeReport ).toHaveBeenCalledTimes(1)
jest.runOnlyPendingTimers()
break
case 3:
expect( scrapeIndex ).toHaveBeenCalledTimes(2)
jest.runOnlyPendingTimers()
break
case 4: expect( scrapeIndex ).toHaveBeenCalledTimes(3)
break
default:
}
})
reportStream.on('end', () => {
expect( scrapeIndex ).toHaveBeenCalledTimes(3)
expect( scrapeReport ).toHaveBeenCalledTimes(5)
done()
})
})
test( 'Use correct page URLs', async (done) => {
scrapeIndex.mockImplementationOnce(() => ({ //page 1
reportsURLs: [1],
pageCount: 3
})).mockImplementationOnce(() => ({ //page 2
reportsURLs: [2,3],
})).mockImplementationOnce(() => ({ //page 3
reportsURLs: [4,5],
}))
const reportStream = await scrape( db.db, { groupSize: 2, groupInterval: 1 } )
let c = 0
reportStream.on('data', async(chunk) => {
++c
switch( c ){
case 1:
jest.runOnlyPendingTimers()
break
case 3:
jest.runOnlyPendingTimers()
break
default:
}
})
reportStream.on('end', () => {
expect( scrapeIndex ).toHaveBeenCalledTimes(3)
expect( scrapeIndex ).toHaveBeenCalledWith( DEFAULT_INDEX_URL )
expect( scrapeIndex ).toHaveBeenCalledWith( urlOfPage(2) )
expect( scrapeIndex ).toHaveBeenCalledWith( urlOfPage(3) )
done()
})
})
test( 'Start scraping at given report URI (included) and given page URL', async (done) => {
const startAtPageURL = urlOfPage(2)
const startAtReportURI = 2
scrapeIndex.mockImplementationOnce(() => ({ //page 1
reportsURLs: [1,2,3],
pageCount: 3
})).mockImplementationOnce(() => ({ //page 2
reportsURLs: [4],
}))
const reportStream = await scrape( db.db, { startAtPageURL, startAtReportURI, groupSize: 2, groupInterval: 1, stopAtReportURI: 1 } )
let c = 0
reportStream.on('data', async(chunk) => {
switch( c ){
case 1:
throw new Error("Should not have scraped this report.")
break
default:
jest.runOnlyPendingTimers()
}
})
reportStream.on('end', () => {
expect( scrapeIndex ).toHaveBeenCalledTimes(2)
expect( scrapeReport ).toHaveBeenCalledTimes(3)
expect( scrapeReport ).toHaveBeenCalledWith( 2 )
expect( scrapeReport ).toHaveBeenCalledWith( 3 )
expect( scrapeReport ).toHaveBeenCalledWith( 4 )
done()
})
})
test( 'No scraping if start report URI not found', async (done) => {
const startAtPageURL = urlOfPage(2)
const startAtReportURI = 5
scrapeIndex.mockImplementationOnce(() => ({ //page 2
reportsURLs: [1,2,3],
pageCount: 3
})).mockImplementationOnce(() => ({ //page 3
reportsURLs: [4],
}))
const reportStream = await scrape( db.db, { startAtPageURL, startAtReportURI, groupSize: 2, groupInterval: 1, stopAtReportURI: 1 } )
reportStream.on('data', async(chunk) => {
throw new Error("Should not have scraped this report.")
})
reportStream.on('end', () => {
expect( scrapeIndex ).toHaveBeenCalledTimes(2)
expect( scrapeReport ).not.toHaveBeenCalled()
done()
})
})
test( 'Stop scraping at given report URI (excluded)', async (done) => {
scrapeIndex.mockImplementationOnce(() => ({ //page 1
reportsURLs: [1],
pageCount: 3
})).mockImplementationOnce(() => ({ //page 2
reportsURLs: [2,3],
})).mockImplementationOnce(() => ({ //page 3
reportsURLs: [4,5],
}))
const reportStream = await scrape( db.db, { groupSize: 2, groupInterval: 1, stopAtReportURI: 3 } )
let c = 0
reportStream.on('data', async(chunk) => {
expect( scrapeReport ).toHaveBeenCalledTimes(++c)
switch( c ){
case 1:
jest.runOnlyPendingTimers()
break
case 3:
throw new Error("Went beyond the given report.")
break
default:
}
})
reportStream.on('end', () => {
expect( scrapeIndex ).toHaveBeenCalledTimes(2)
expect( scrapeReport ).toHaveBeenCalledTimes(2)
expect( scrapeReport ).toHaveBeenCalledWith( 1 )
expect( scrapeReport ).toHaveBeenCalledWith( 2 )
done()
})
})
test( 'Only scrape reports between start URI and end URI (excluded)', async(done) => {
scrapeIndex.mockImplementationOnce(() => ({ //page 1
reportsURLs: [1,2],
pageCount: 3
})).mockImplementationOnce(() => ({ //page 2
reportsURLs: [3,4],
})).mockImplementationOnce(() => ({ //page 3
reportsURLs: [5,6],
}))
const reportStream = await scrape( db.db, { groupSize: 2, groupInterval: 1, startAtReportURI: 3, stopAtReportURI: 6 } )
let c = 0
reportStream.on('data', async(chunk) => {
switch( c ){
case 1:
case 2:
case 6:
throw new Error("Should not have scraped this report.")
default:
jest.runOnlyPendingTimers()
}
})
reportStream.on('end', () => {
expect( scrapeIndex ).toHaveBeenCalledTimes(3)
expect( scrapeReport ).toHaveBeenCalledTimes(3)
expect( scrapeReport ).toHaveBeenCalledWith( 3 )
expect( scrapeReport ).toHaveBeenCalledWith( 4 )
expect( scrapeReport ).toHaveBeenCalledWith( 5 )
done()
})
})
test( 'Outputs the reports', async(done) => {
scrapeIndex.mockImplementationOnce(() => ({ //page 1
reportsURLs: [1,2],
pageCount: 2
})).mockImplementationOnce(() => ({ //page 2
reportsURLs: [3],
}))
const reportStream = await scrape( db.db, { groupSize: 2, groupInterval: 1 } )
let c = 1
scrapeReport.mockImplementation((d) => d)
const chunks = []
reportStream.on('data', async(chunk) => {
expect( chunk ).toEqual(c)
chunks.push( chunk )
c++
switch( chunk ){
case 2:
jest.runOnlyPendingTimers()
break
default:
}
})
reportStream.on('end', () => {
expect( chunks ).toEqual( [1,2,3] )
done()
})
})
test( 'Nothing to scrape', async (done) => {
scrapeIndex.mockImplementationOnce(() => ({ //page 1
reportsURLs: [1],
pageCount: 3
}))
const reportStream = await scrape( db.db, { groupSize: 2, groupInterval: 1, stopAtReportURI: 1 } )
reportStream.on('data', (chunk) => {
done.fail( new Error('No chunk should have been emitted by the stream') )
})
reportStream.on('end', () => {
expect( scrapeIndex ).toHaveBeenCalledTimes(1)
expect( scrapeReport ).not.toHaveBeenCalled()
done()
})
})
test( 'Throws parsing error and ends stream', async (done) => {
scrapeIndex.mockImplementationOnce(() => ({ //page 1
reportsURLs: [1],
pageCount: 3
})).mockImplementationOnce(() => ({ //page 2
reportsURLs: [2,3],
})).mockImplementationOnce(() => ({ //page 3
reportsURLs: [4,5],
}))
scrapeReport.mockImplementation((url) => {
if(url == 3){
throw new Error('Parsing failed')
}
})
const reportStream = await scrape( db.db, { groupSize: 2, groupInterval: 1 } )
reportStream.on('error', (err) => {
expect( err.message ).toEqual('Parsing failed')
})
reportStream.on('end', () => {
expect( scrapeIndex ).toHaveBeenCalledTimes(2)
expect( scrapeReport ).toHaveBeenCalledTimes(3)
done()
})
reportStream.on('data', (chunk) => {
jest.runOnlyPendingTimers()
})
})
test( 'Stop parsing after already scraped limit reached', async (done) => {
scrapeIndex.mockImplementationOnce(() => ({ //page 1
reportsURLs: ['http://1','http://2'],
pageCount: 3
})).mockImplementationOnce(() => ({ //page 2
reportsURLs: ['http://3','http://4'],
})).mockImplementationOnce(() => ({ //page 3
reportsURLs: ['http://5','http://6'],
}))
//Add reports to the DB
const baseReport = {
description:'description',
startDate: new Date().toISOString(),
iso3166_2: 'DE-BE',
locations: [{
subdivisions:['somewhere','over the rainbow'],
}],
sources: [{
name:'source'
}]
}
await reportTable( {...baseReport, uri:'http://1' }, db )
await reportTable( {...baseReport, uri:'http://2' }, db )
await reportTable( {...baseReport, uri:'http://4' }, db )
const reportStream = await scrape( db.db, { groupSize: 2, groupInterval: 1, alreadyScrapedLimit: 3 } )
let c = 0
reportStream.on('data', async(chunk) => {
switch( c ){
case 'http://1':
case 'http://2':
case 'http://4':
case 'http://5':
case 'http://6':
throw new Error("Should not have scraped this report.")
default:
jest.runOnlyPendingTimers()
}
})
reportStream.on('end', () => {
expect( scrapeIndex ).toHaveBeenCalledTimes(2)
expect( scrapeReport ).toHaveBeenCalledTimes(1)
expect( scrapeReport ).toHaveBeenCalledWith( 'http://3' )
done()
})
})
})