-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-parsing.spec.js
460 lines (425 loc) Β· 13.5 KB
/
text-parsing.spec.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
450
451
452
453
454
455
456
457
458
459
460
/* eslint-disable no-irregular-whitespace */
const test = require('ava');
const commaNumber = require('comma-number');
const { VIOLATION } = require('./constants');
const {
generateViolationSummaries,
generateViolationTweets,
matchLicensePlates,
generateAnnualSummaryTweets,
generateNoViolationsTweet,
convertToMonospace,
monthlySummaryTweet,
monthlyByViolationsTweets,
worstDriverTweets
} = require('./text-parsing');
test('matchLicensePlates will match one license plate', t => {
const data = matchLicensePlates('@BadDrivingBmore MD:ATRAIN');
t.deepEqual([['MD', 'ATRAIN']], data);
});
test('matchLicensePlates will match two license plates', t => {
const data = matchLicensePlates('@BadDrivingBmore MD:ATRAIN MD:12345');
t.deepEqual([['MD', 'ATRAIN'], ['MD','12345']], data);
});
test('matchLicensePlates will match license plates even with no space betweeen plates', t => {
const data = matchLicensePlates('@BadDrivingBmore MD:ATRAIN,MD:12345');
t.deepEqual([['MD', 'ATRAIN'], ['MD','12345']], data);
});
test('generateViolationSummaries should handle no violations', t => {
t.deepEqual(generateViolationSummaries([]), []);
});
test('generateViolationSummaries should correctly parse a single violation record', t => {
const data = [
{
violCode: 30,
count: 2
}
];
t.deepEqual(
generateViolationSummaries(data),
[
`${data[0].count} ${VIOLATION[data[0].violCode]}`
]
);
});
test('generateViolationSummaries should correctly parse more than one violation record', t => {
const data = [
{
violCode: 30,
count: 2
},
{
violCode: 32,
count: 3
}
];
t.deepEqual(
generateViolationSummaries(data),
[
`${data[0].count} ${VIOLATION[data[0].violCode]}`,
`${data[1].count} ${VIOLATION[data[1].violCode]}`
]
);
});
test('generateViolationTweets should properly generate a single tweet', t => {
const violations = [
'2 Red light',
'3 Fixed speed camera'
];
t.deepEqual(
generateViolationTweets('MD', 'ATRAIN', violations),
[ `#MD_ATRAIN Violations:\n\n${violations[0]}\n${violations[1]}` ]
);
});
test('generateViolationTweets should properly generate multiple tweets when there are many violations', t => {
const violations = [
'2 Red light',
'3 Fixed speed camera',
'1 Commercial vehicle/residence under 20,000 lbs.',
'3 No stopping/parking stadium event Camden Yards',
'1 No stopping/parking handicapped zone',
'3 Impeding movement of pedestrians',
'1 Blocking garage or driveway',
'2 No stopping/parking stadium event on 33rd St.'
];
t.deepEqual(
generateViolationTweets('MD', 'ATRAIN', violations),
[
'#MD_ATRAIN Violations:\n\n2 Red light\n3 Fixed speed camera\n1 Commercial vehicle/residence under 20,000 lbs.\n3 No stopping/parking stadium event Camden Yards\n1 No stopping/parking handicapped zone\n3 Impeding movement of pedestrians\n1 Blocking garage or driveway',
'#MD_ATRAIN Violations Cont\'d:\n\n2 No stopping/parking stadium event on 33rd St.'
]
);
});
test('generateAnnualSummaryTweets should properly generate a table in a single tweet', t => {
const data = [
{ eXPR1: 2017, count: 2, annualFines: 80 },
{ eXPR1: 2018, count: 2, annualFines: 115 },
{ eXPR1: 2019, count: 4, annualFines: 195 }
];
t.deepEqual(
generateAnnualSummaryTweets('MD', 'ATRAIN', data),
[
`#MD_ATRAIN Fines by Year:
ππππβπ
πππππππππβπ΅ππππ
πΈπΆπ·π½βββββββββββπΈβββββπΎπΆ
πΈπΆπ·πΎβββββββββββπΈββββπ·π·π»
πΈπΆπ·πΏβββββββββββπΊββββπ·πΏπ»
ββββββββββββββββββββββββ
βββββββββββββββπΎβοΌββπΉπΏπΆ`
]
);
});
test('generateAnnualSummaryTweets should properly break the number of years is more than 280 characters', t=> {
const data = [
{ eXPR1: 2006, count: 2, annualFines: 80 },
{ eXPR1: 2007, count: 2, annualFines: 80 },
{ eXPR1: 2008, count: 1, annualFines: 40 },
{ eXPR1: 2009, count: 1, annualFines: 25 },
{ eXPR1: 2010, count: 3, annualFines: 150 },
{ eXPR1: 2011, count: 1, annualFines: 40 },
{ eXPR1: 2012, count: 1, annualFines: 25 },
{ eXPR1: 2013, count: 3, annualFines: 150 },
{ eXPR1: 2014, count: 1, annualFines: 40 },
{ eXPR1: 2015, count: 1, annualFines: 25 },
{ eXPR1: 2016, count: 3, annualFines: 150 },
{ eXPR1: 2017, count: 2, annualFines: 80 },
{ eXPR1: 2018, count: 2, annualFines: 115 },
{ eXPR1: 2019, count: 4, annualFines: 195 }
];
t.deepEqual(
generateAnnualSummaryTweets('MD', 'ATRAIN', data),
[
`#MD_ATRAIN Fines by Year:
ππππβπ
πππππππππβπ΅ππππ
πΈπΆπΆπΌβββββββββββπΈβββββπΎπΆ
πΈπΆπΆπ½βββββββββββπΈβββββπΎπΆ
πΈπΆπΆπΎβββββββββββπ·βββββπΊπΆ
πΈπΆπΆπΏβββββββββββπ·βββββπΈπ»
πΈπΆπ·πΆβββββββββββπΉββββπ·π»πΆ
πΈπΆπ·π·βββββββββββπ·βββββπΊπΆ`,
`#MD_ATRAIN Fines by Year Cont'd:
ππππβπ
πππππππππβπ΅ππππ
πΈπΆπ·πΈβββββββββββπ·βββββπΈπ»
πΈπΆπ·πΉβββββββββββπΉββββπ·π»πΆ
πΈπΆπ·πΊβββββββββββπ·βββββπΊπΆ
πΈπΆπ·π»βββββββββββπ·βββββπΈπ»
πΈπΆπ·πΌβββββββββββπΉββββπ·π»πΆ
πΈπΆπ·π½βββββββββββπΈβββββπΎπΆ`,
`#MD_ATRAIN Fines by Year Cont'd:
ππππβπ
πππππππππβπ΅ππππ
πΈπΆπ·πΎβββββββββββπΈββββπ·π·π»
πΈπΆπ·πΏβββββββββββπΊββββπ·πΏπ»
ββββββββββββββββββββββββ
ββββββββββββββπΈπ½βοΌβπ·π·πΏπ»`
]
);
});
test('generateAnnualSummaryTweets should properly break if the total lines goes over 280 characters', t=> {
const data = [
{ eXPR1: 2008, count: 1, annualFines: 40 },
{ eXPR1: 2009, count: 1, annualFines: 25 },
{ eXPR1: 2010, count: 3, annualFines: 150 },
{ eXPR1: 2011, count: 1, annualFines: 40 },
{ eXPR1: 2012, count: 1, annualFines: 25 },
{ eXPR1: 2013, count: 3, annualFines: 150 },
{ eXPR1: 2014, count: 1, annualFines: 40 },
{ eXPR1: 2015, count: 1, annualFines: 25 },
{ eXPR1: 2016, count: 3, annualFines: 150 },
{ eXPR1: 2017, count: 2, annualFines: 80 },
{ eXPR1: 2018, count: 2, annualFines: 115 },
{ eXPR1: 2019, count: 4, annualFines: 195 }
];
t.deepEqual(
generateAnnualSummaryTweets('MD', 'ATRAIN', data),
[
`#MD_ATRAIN Fines by Year:
ππππβπ
πππππππππβπ΅ππππ
πΈπΆπΆπΎβββββββββββπ·βββββπΊπΆ
πΈπΆπΆπΏβββββββββββπ·βββββπΈπ»
πΈπΆπ·πΆβββββββββββπΉββββπ·π»πΆ
πΈπΆπ·π·βββββββββββπ·βββββπΊπΆ
πΈπΆπ·πΈβββββββββββπ·βββββπΈπ»
πΈπΆπ·πΉβββββββββββπΉββββπ·π»πΆ`,
`#MD_ATRAIN Fines by Year Cont'd:
ππππβπ
πππππππππβπ΅ππππ
πΈπΆπ·πΊβββββββββββπ·βββββπΊπΆ
πΈπΆπ·π»βββββββββββπ·βββββπΈπ»
πΈπΆπ·πΌβββββββββββπΉββββπ·π»πΆ
πΈπΆπ·π½βββββββββββπΈβββββπΎπΆ
πΈπΆπ·πΎβββββββββββπΈββββπ·π·π»
πΈπΆπ·πΏβββββββββββπΊββββπ·πΏπ»`,
`#MD_ATRAIN Fines by Year Cont'd:
ββββββββββββββββββββββββ
ββββββββββββββπΈπΉβοΌβπ·πΆπΉπ»`
]
);
});
test('generateNoViolationsTweet generates an array with a single no violations tweet', t => {
t.deepEqual(
generateNoViolationsTweet('MD', 'ATRAIN'),
[ '#MD_ATRAIN has no known violations in Baltimore π' ]
);
});
test('convertToMonospace handles space character', t => {
t.is(convertToMonospace(' '), 'β');
});
test('convertToMonospace handles all capital letters', t => {
t.is(convertToMonospace('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 'π°π±π²π³π΄π΅πΆπ·πΈπΉπΊπ»πΌπ½πΎπΏππππππ
ππππ');
});
test('convertToMonospace handles all lowercase letters', t => {
t.is(convertToMonospace('abcdefghijklmnopqrstuvwxyz'), 'πππππππππππππππππππππππ π‘π’π£');
});
test('convertToMonospace handles all numbers', t => {
t.is(convertToMonospace('0123456789'), 'πΆπ·πΈπΉπΊπ»πΌπ½πΎπΏ');
});
test('monthlySummaryTweet should retun a summary of the month', t => {
const data = {
month: 1,
year: 2020,
numViolations: 75000,
totalFines: 3000000
};
t.is(
`Monthly statistics for ${data.month}/${data.year}\n\nTotal Violations: ${commaNumber(data.numViolations)}\nTotal Fines: $${commaNumber(data.totalFines)}`,
monthlySummaryTweet(data)
);
});
test('monthlyViolationsByTweets should return an array of one tweet when data is short', t => {
const data = {
month: 1,
year: 2020,
violationTotals: [
{
count: 10,
violCode: 30
}
]
};
t.deepEqual(
[
`Monthly statistics for 1/2020 cont'd
π·πΆββββ Red light
`
],
monthlyByViolationsTweets(data)
);
});
test('monthlyViolationsByTweets should return an array of several tweets when data would overflow a tweet', t => {
const data = {
month: 1,
year: 2020,
violationTotals: [
{
count: 10,
violCode: 30
},
{
count: 10,
violCode: 31
},
{
count: 10,
violCode: 27
},
{
count: 10,
violCode: 28
},
{
count: 10,
violCode: 26
},
{
count: 10,
violCode: 25
},
{
count: 10,
violCode: 23
},
{
count: 10,
violCode: 22
},
{
count: 10,
violCode: 19
},
{
count: 10,
violCode: 20
}
]
};
t.deepEqual(
[
`Monthly statistics for 1/2020 cont'd
π·πΆββββ Red light
π·πΆββββ Right on red
π·πΆββββ No stopping/parking street cleaning
π·πΆββββ Cruising
π·πΆββββ No stopping/parking handicapped zone
π·πΆββββ Less than 30 feet from intersection
π·πΆββββ Taxi stand
π·πΆββββ Expired tags
`,
`Monthly statistics for 1/2020 cont'd
π·πΆββββ Exceeding 48 hour limit
π·πΆββββ Passenger loading zone
`
],
monthlyByViolationsTweets(data)
);
});
test('worstDriverTweets produces 1 tweet if data is short', t => {
const data = {
month: 1,
year: 2020,
worst: [
{
plate: 'MD 12345',
count: 20,
totalFines: 1000,
violationTotals: [
{
count: 10,
violCode: 30
},
{
count: 10,
violCode: 31
}
]
}
]
};
t.deepEqual(
[
`Monthly statistics for 1/2020
Worst driver MD 12345
πΈπΆβ violations
Total fines: $1000
π·πΆβ Red light
π·πΆβ Right on red
`
],
worstDriverTweets(data)
);
});
test('worstDriverTweets produces multiple tweets if data is long', t => {
const data = {
month: 1,
year: 2020,
worst: [
{
plate: 'MD 12345',
count: 19,
totalFines: 1000,
violationTotals: [
{
count: 10,
violCode: 30
},
{
count: 1,
violCode: 31
},
{
count: 1,
violCode: 27
},
{
count: 1,
violCode: 28
},
{
count: 1,
violCode: 26
},
{
count: 1,
violCode: 25
},
{
count: 1,
violCode: 23
},
{
count: 1,
violCode: 22
},
{
count: 1,
violCode: 19
},
{
count: 1,
violCode: 20
}
]
}
]
};
t.deepEqual(
[
`Monthly statistics for 1/2020
Worst driver MD 12345
π·πΏβ violations
Total fines: $1000
π·πΆβ Red light
π·ββ Right on red
π·ββ No stopping/parking street cleaning
π·ββ Cruising
π·ββ No stopping/parking handicapped zone
π·ββ Less than 30 feet from intersection
π·ββ Taxi stand
`,
`Monthly statistics for 1/2020 Cont'd
Worst driver MD 12345π·ββ Expired tags
π·ββ Exceeding 48 hour limit
π·ββ Passenger loading zone
`
],
worstDriverTweets(data)
);
});