test.person.js 13 KB

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