1 | import test from 'ava'
|
2 | import Chance from '../chance.js'
|
3 | import _ from 'lodash'
|
4 |
|
5 | const chance = new Chance()
|
6 |
|
7 |
|
8 | const CHILD_AGE_MIN = 0
|
9 | const CHILD_AGE_MAX = 12
|
10 | const TEEN_AGE_MIN = 13
|
11 | const TEEN_AGE_MAX = 19
|
12 | const ADULT_AGE_MIN = 18
|
13 | const ADULT_AGE_MAX = 65
|
14 | const SENIOR_AGE_MIN = 65
|
15 | const SENIOR_AGE_MAX = 100
|
16 | const AGE_MIN = 0
|
17 | const AGE_MAX = 100
|
18 |
|
19 | const currentYear = new Date().getFullYear()
|
20 |
|
21 |
|
22 | test('age() returns a random age within expected bounds', t => {
|
23 | _.times(1000, () => {
|
24 | let age = chance.age()
|
25 | t.true(_.isNumber(age))
|
26 | t.true(age >= ADULT_AGE_MIN)
|
27 | t.true(age <= ADULT_AGE_MAX)
|
28 | })
|
29 | })
|
30 |
|
31 | test('age() returns a random age within expected bounds for all', t => {
|
32 | _.times(1000, () => {
|
33 | let age = chance.age({ type: 'all' })
|
34 | t.true(_.isNumber(age))
|
35 | t.true(age >= AGE_MIN)
|
36 | t.true(age <= AGE_MAX)
|
37 | })
|
38 | })
|
39 |
|
40 | test('age() returns a proper age for a child', t => {
|
41 | _.times(1000, () => {
|
42 | let age = chance.age({ type: 'child' })
|
43 | t.true(_.isNumber(age))
|
44 | t.true(age >= CHILD_AGE_MIN)
|
45 | t.true(age <= CHILD_AGE_MAX)
|
46 | })
|
47 | })
|
48 |
|
49 | test('age() returns a proper age for a teen', t => {
|
50 | _.times(1000, () => {
|
51 | let age = chance.age({ type: 'teen' })
|
52 | t.true(_.isNumber(age))
|
53 | t.true(age >= TEEN_AGE_MIN)
|
54 | t.true(age <= TEEN_AGE_MAX)
|
55 | })
|
56 | })
|
57 |
|
58 | test('age() returns a proper age for an adult', t => {
|
59 | _.times(1000, () => {
|
60 | let age = chance.age({ type: 'adult' })
|
61 | t.true(_.isNumber(age))
|
62 | t.true(age >= ADULT_AGE_MIN)
|
63 | t.true(age <= ADULT_AGE_MAX)
|
64 | })
|
65 | })
|
66 |
|
67 | test('age() returns a proper age for a senior', t => {
|
68 | _.times(1000, () => {
|
69 | let age = chance.age({ type: 'senior' })
|
70 | t.true(_.isNumber(age))
|
71 | t.true(age >= SENIOR_AGE_MIN)
|
72 | t.true(age <= SENIOR_AGE_MAX)
|
73 | })
|
74 | })
|
75 |
|
76 |
|
77 | test('birthday() works as expected', t => {
|
78 | _.times(1000, () => {
|
79 | let birthday = chance.birthday()
|
80 | t.true(_.isDate(birthday))
|
81 | let year = birthday.getFullYear()
|
82 | let curYear = new Date().getFullYear()
|
83 | t.true(year > (curYear - AGE_MAX))
|
84 | t.true(year < curYear)
|
85 | })
|
86 | })
|
87 |
|
88 | test('birthday() can have a string returned', t => {
|
89 | _.times(1000, () => {
|
90 | let birthday = chance.birthday({ string: true })
|
91 | t.true(_.isString(birthday))
|
92 | t.false(_.isDate(birthday))
|
93 | t.true(/^[0-9][0-9]?\/[0-9][0-9]?\/[0-9]{4}/m.test(birthday))
|
94 | })
|
95 | })
|
96 |
|
97 | test('birthday() can have a year specified', t => {
|
98 | _.times(1000, () => {
|
99 | t.is(chance.birthday({ year: 1983 }).getFullYear(), 1983)
|
100 | })
|
101 | })
|
102 |
|
103 | test('birthday() can have an age range specified for an adult', t => {
|
104 | _.times(1000, () => {
|
105 | let birthday = chance.birthday({ type: 'adult' })
|
106 | let min = new Date().setFullYear(currentYear - ADULT_AGE_MAX - 1)
|
107 | let max = new Date().setFullYear(currentYear - ADULT_AGE_MIN)
|
108 | t.true(birthday.getTime() >= min)
|
109 | t.true(birthday.getTime() <= max)
|
110 | })
|
111 | })
|
112 |
|
113 | test('birthday() can have an age range specified for a teen', t => {
|
114 | _.times(1000, () => {
|
115 | let birthday = chance.birthday({ type: 'teen' })
|
116 | let min = new Date().setFullYear(currentYear - TEEN_AGE_MAX - 1)
|
117 | let max = new Date().setFullYear(currentYear - TEEN_AGE_MIN)
|
118 | t.true(birthday.getTime() >= min)
|
119 | t.true(birthday.getTime() <= max)
|
120 | })
|
121 | })
|
122 |
|
123 | test('birthday() can have an age range specified for a child', t => {
|
124 | _.times(1000, () => {
|
125 | let birthday = chance.birthday({ type: 'child' })
|
126 | let min = new Date().setFullYear(currentYear - CHILD_AGE_MAX - 1)
|
127 | let max = new Date().setFullYear(currentYear - CHILD_AGE_MIN)
|
128 | t.true(birthday.getTime() >= min)
|
129 | t.true(birthday.getTime() <= max)
|
130 | })
|
131 | })
|
132 |
|
133 | test('birthday() can have an age range specified for a senior', t => {
|
134 | _.times(1000, () => {
|
135 | let birthday = chance.birthday({ type: 'senior' })
|
136 | let min = new Date().setFullYear(currentYear - SENIOR_AGE_MAX - 1)
|
137 | let max = new Date().setFullYear(currentYear - SENIOR_AGE_MIN)
|
138 | t.true(birthday.getTime() >= min)
|
139 | t.true(birthday.getTime() <= max)
|
140 | })
|
141 | })
|
142 |
|
143 |
|
144 | test('cnpj() returns a random cnpj', t => {
|
145 | _.times(1000, () => {
|
146 | let hidn = chance.HIDN()
|
147 | t.true(_.isString(hidn))
|
148 | })
|
149 | })
|
150 |
|
151 |
|
152 | test('company() returns a random company', t => {
|
153 | _.times(1000, () => {
|
154 | let company = chance.company()
|
155 | t.true(_.isString(company))
|
156 | t.true(company.length > 4)
|
157 | })
|
158 | })
|
159 |
|
160 |
|
161 | test('cpf() returns a random valid taxpayer number for Brazil citizens (CPF)', t => {
|
162 | _.times(1000, () => {
|
163 | let cpf = chance.cpf()
|
164 | t.true(_.isString(cpf))
|
165 | t.true(/^\d{3}.\d{3}.\d{3}-\d{2}$/m.test(cpf))
|
166 | t.is(cpf.length, 14)
|
167 | })
|
168 | })
|
169 |
|
170 |
|
171 | test('first() returns a random first name', t => {
|
172 | _.times(1000, () => {
|
173 | let first = chance.first()
|
174 | t.true(_.isString(first))
|
175 | t.true(first.length >= 2)
|
176 | t.true(first.length <= 20)
|
177 | t.is(first.split(' ').length, 1)
|
178 | })
|
179 | })
|
180 |
|
181 |
|
182 | test('gender() returns a random gender', t => {
|
183 | _.times(1000, () => t.true(/(Male|Female)/.test(chance.gender())))
|
184 | })
|
185 |
|
186 | test('gender() can take extra genders', t => {
|
187 | _.times(1000, () => {
|
188 | let gender = chance.gender({ extraGenders: ['Unknown', 'Transgender'] })
|
189 | t.true(/(Male|Female|Unknown|Transgender)/.test(gender))
|
190 | })
|
191 | })
|
192 |
|
193 |
|
194 | test('HIDN() returns a random HIDN', t => {
|
195 | _.times(1000, () => {
|
196 | let hidn = chance.HIDN()
|
197 | t.true(_.isString(hidn))
|
198 | t.true(/^\d{6}[A-Z]{2}$/m.test(hidn))
|
199 | t.is(hidn.length, 8)
|
200 | })
|
201 | })
|
202 |
|
203 |
|
204 | test('israelId() returns a valid Isreal id', t => {
|
205 | let id = chance.israelId()
|
206 | t.true(_.isString(id))
|
207 | t.is(id.length, 9)
|
208 | let acc = 0
|
209 | for (let i = 0; i < 8; i++) {
|
210 | let thisDigit = id[i] * ( i / 2 === parseInt(i/2, 10) ? 1 : 2)
|
211 | thisDigit = chance.pad(thisDigit, 2)
|
212 | thisDigit = parseInt(thisDigit[0], 10) + parseInt(thisDigit[1], 10)
|
213 | acc += thisDigit
|
214 | }
|
215 | let lastDigit = (10 - parseInt(acc.toString().slice(-1), 10).toString().slice(-1)).toString().slice(-1)
|
216 | t.is(id[8], lastDigit)
|
217 | })
|
218 |
|
219 |
|
220 | test('last() returns a random last name', t => {
|
221 | _.times(1000, () => {
|
222 | let last = chance.last()
|
223 | t.true(_.isString(last))
|
224 | t.true(last.length >= 2)
|
225 | t.true(last.length <= 20)
|
226 | t.true(last.split(' ').length <= 3)
|
227 | })
|
228 | })
|
229 |
|
230 |
|
231 | test('name() returns a random name', t => {
|
232 | _.times(1000, () => {
|
233 | let name = chance.name()
|
234 | t.true(_.isString(name))
|
235 | t.true(name.length >= 2)
|
236 | t.true(name.length <= 30)
|
237 | t.is(name.split(' ').length, 2)
|
238 | t.true(/[a-zA-Z]+\ [a-zA-Z]+/.test(name))
|
239 | })
|
240 | })
|
241 |
|
242 | test('name() can have the middle name specified', t => {
|
243 | _.times(1000, () => {
|
244 | let name = chance.name({ middle: true })
|
245 | t.true(_.isString(name))
|
246 | t.is(name.split(' ').length, 3)
|
247 | t.true(/[a-zA-Z]+\ [a-zA-Z]+\ [a-zA-Z]+/.test(name))
|
248 | })
|
249 | })
|
250 |
|
251 | test('name() can have the middle initial specified', t => {
|
252 | _.times(1000, () => {
|
253 | let name = chance.name({ middle_initial: true })
|
254 | t.true(_.isString(name))
|
255 | t.is(name.split(' ').length, 3)
|
256 | t.true(/[a-zA-Z]+\ [a-zA-Z]\.\ [a-zA-Z]+/.test(name))
|
257 | })
|
258 | })
|
259 |
|
260 | test('name() can have the prefix specified', t => {
|
261 | _.times(1000, () => {
|
262 | let name = chance.name({ prefix: true })
|
263 | t.true(_.isString(name))
|
264 | t.is(name.split(' ').length, 3)
|
265 | t.true(/[a-zA-Z]{2,4}\.? [a-zA-Z]+\ [a-zA-Z]+/.test(name))
|
266 | })
|
267 | })
|
268 |
|
269 | test('name() can have the suffix specified', t => {
|
270 | _.times(1000, () => {
|
271 | let name = chance.name({ suffix: true })
|
272 | t.true(_.isString(name))
|
273 | t.is(name.split(' ').length, 3)
|
274 | t.true(/[a-zA-Z]+\ [a-zA-Z]+\ [a-zA-Z\.]+/.test(name))
|
275 | })
|
276 | })
|
277 |
|
278 |
|
279 | test('name_prefix() returns a random prefix', t => {
|
280 | _.times(1000, () => {
|
281 | let prefix = chance.name_prefix()
|
282 | t.true(_.isString(prefix))
|
283 | t.true(prefix.length < 5)
|
284 | })
|
285 | })
|
286 |
|
287 | test('name_prefix() returns a correctly gendered prefix', t => {
|
288 | _.times(1000, () => {
|
289 | let prefix = chance.name_prefix({ gender: 'female' })
|
290 | t.not(prefix, 'Mr.')
|
291 | prefix = chance.name_prefix({ gender: 'male' })
|
292 | t.not(prefix, 'Mrs.')
|
293 | t.not(prefix, 'Miss')
|
294 | })
|
295 | })
|
296 |
|
297 | test('name_prefix() can return a full prefix', t => {
|
298 | _.times(1000, () => {
|
299 | let prefix = chance.name_prefix({ full: true })
|
300 | t.true(_.isString(prefix))
|
301 | t.true(prefix.length > 3)
|
302 | })
|
303 | })
|
304 |
|
305 |
|
306 | test('name_suffix() returns a random suffix', t => {
|
307 | _.times(1000, () => {
|
308 | let suffix = chance.name_suffix()
|
309 | t.true(_.isString(suffix))
|
310 | t.true(suffix.length < 7)
|
311 | })
|
312 | })
|
313 |
|
314 | test('name_suffix() can return a full suffix', t => {
|
315 | _.times(1000, () => {
|
316 | let suffix = chance.name_suffix({ full: true })
|
317 | t.true(_.isString(suffix))
|
318 | t.true(suffix.length > 5)
|
319 | })
|
320 | })
|
321 |
|
322 |
|
323 | test('nationality() returns a nationality that looks right', t => {
|
324 | _.times(1000, () => {
|
325 | let nationality = chance.nationality()
|
326 | t.true(_.isString(nationality))
|
327 | t.true(nationality.length > 3)
|
328 | t.true(nationality.length < 26)
|
329 | })
|
330 | })
|
331 |
|
332 |
|
333 | test('profession() returns a random profession', t => {
|
334 | _.times(1000, () => {
|
335 | let profession = chance.profession()
|
336 | t.true(_.isString(profession))
|
337 | t.true(profession.length > 3)
|
338 | })
|
339 | })
|
340 |
|
341 | test('profession() returns a ranked profession', t => {
|
342 | _.times(1000, () => {
|
343 | let profession = chance.profession({ rank: true })
|
344 | t.true(_.isString(profession))
|
345 | t.true(profession.split(' ').length > 1)
|
346 | t.true(/(Apprentice|Junior|Senior|Lead)/.test(profession.split(' ')[0]))
|
347 | })
|
348 | })
|
349 |
|
350 |
|
351 | test('ssn() returns a random social security number', t => {
|
352 | _.times(1000, () => {
|
353 | let ssn = chance.ssn()
|
354 | t.true(_.isString(ssn))
|
355 | t.true(/^\d{3}-\d{2}-\d{4}$/m.test(ssn))
|
356 | t.is(ssn.length, 11)
|
357 | })
|
358 | })
|
359 |
|
360 | test('ssn() can return just the last 4', t => {
|
361 | _.times(1000, () => {
|
362 | let ssn = chance.ssn({ ssnFour: true })
|
363 | t.true(_.isString(ssn))
|
364 | t.true(/^\d{4}$/m.test(ssn))
|
365 | t.is(ssn.length, 4)
|
366 | })
|
367 | })
|
368 |
|
369 |
|
370 | test('aadhar() returns a random aadhar number with whitespace as separator', t => {
|
371 | _.times(1000, () => {
|
372 | let aadhar = chance.aadhar()
|
373 | t.true(_.isString(aadhar))
|
374 | t.true(/^\d{4}\s\d{4}\s\d{4}$/m.test(aadhar))
|
375 | t.is(aadhar.length, 14)
|
376 | })
|
377 | })
|
378 |
|
379 |
|
380 | test('aadhar() returns a random aadhar number with no separator', t => {
|
381 | _.times(1000, () => {
|
382 | let aadhar = chance.aadhar({separatedByWhiteSpace : false})
|
383 | t.true(_.isString(aadhar))
|
384 | t.true(/^\d{12}$/m.test(aadhar))
|
385 | t.is(aadhar.length, 12)
|
386 | })
|
387 | })
|
388 |
|
389 |
|
390 | test('aadhar() can return just the last 4', t => {
|
391 | _.times(1000, () => {
|
392 | let aadhar = chance.aadhar({ onlyLastFour: true })
|
393 | t.true(_.isString(aadhar))
|
394 | t.true(/^\d{4}$/m.test(aadhar))
|
395 | t.is(aadhar.length, 4)
|
396 | })
|
397 | })
|
398 |
|
399 |
|
400 | test('suffix() returns a random suffix', t => {
|
401 | _.times(1000, () => {
|
402 | let suffix = chance.suffix()
|
403 | t.true(_.isString(suffix))
|
404 | t.true(suffix.length < 7)
|
405 | })
|
406 | })
|
407 |
|
408 | test('suffix() returns a full random suffix', t => {
|
409 | _.times(1000, () => {
|
410 | let suffix = chance.suffix({ full: true })
|
411 | t.true(_.isString(suffix))
|
412 | t.true(suffix.length > 5)
|
413 | })
|
414 | })
|
415 |
|
416 |
|
417 | test('mrz() should return a valid passport number', t => {
|
418 | let sample = "P<GBRFOLKS<<JOANNE<<<<<<<<<<<<<<<<<<<<<<<<<<2321126135GBR6902069F1601013<<<<<<<<<<<<<<02"
|
419 | let mrz = chance.mrz({
|
420 | first: 'Joanne',
|
421 | last: 'Folks',
|
422 | gender: 'F',
|
423 | dob: '690206',
|
424 | expiry: '160101',
|
425 | passportNumber: '232112613',
|
426 | })
|
427 | t.is(sample, mrz)
|
428 |
|
429 | sample = "P<GBRKELLY<<LIDA<<<<<<<<<<<<<<<<<<<<<<<<<<<<3071365913GBR6606068F2003131<<<<<<<<<<<<<<04"
|
430 | mrz = chance.mrz({
|
431 | first: 'Lida',
|
432 | last: 'Kelly',
|
433 | gender: 'F',
|
434 | dob: '660606',
|
435 | expiry: '200313',
|
436 | passportNumber: '307136591',
|
437 | })
|
438 | t.is(sample, mrz)
|
439 | })
|
440 |
|
441 | test('mrz() should return a valid random passport number when not given any inputs', t => {
|
442 | let mrz = chance.mrz()
|
443 | t.true(_.isString(mrz))
|
444 | t.is(mrz.substr(0, 5), 'P<GBR')
|
445 | t.is(mrz.length, 88)
|
446 | t.true(/^[A-Z0-9<]{9}[0-9]{1}[A-Z]{3}[0-9]{7}[A-Z]{1}[0-9]{7}[A-Z0-9<]{14}[0-9]{2}$/.test(mrz.substr(44)))
|
447 | })
|