node-tests.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. "use strict";
  2. var Papa = require("../papaparse.js");
  3. var fs = require('fs');
  4. var assert = require('assert');
  5. var longSampleRawCsv = fs.readFileSync(__dirname + '/long-sample.csv', 'utf8');
  6. function assertLongSampleParsedCorrectly(parsedCsv) {
  7. assert.equal(8, parsedCsv.data.length);
  8. assert.deepEqual(parsedCsv.data[0], [
  9. 'Grant',
  10. 'Dyer',
  11. 'Donec.elementum@orciluctuset.example',
  12. '2013-11-23T02:30:31-08:00',
  13. '2014-05-31T01:06:56-07:00',
  14. 'Magna Ut Associates',
  15. 'ljenkins'
  16. ]);
  17. assert.deepEqual(parsedCsv.data[7], [
  18. 'Talon',
  19. 'Salinas',
  20. 'posuere.vulputate.lacus@Donecsollicitudin.example',
  21. '2015-01-31T09:19:02-08:00',
  22. '2014-12-17T04:59:18-08:00',
  23. 'Aliquam Iaculis Incorporate',
  24. 'Phasellus@Quisquetincidunt.example'
  25. ]);
  26. assert.deepEqual(parsedCsv.meta, {
  27. "delimiter": ",",
  28. "linebreak": "\n",
  29. "aborted": false,
  30. "truncated": false,
  31. "cursor": 1209
  32. });
  33. assert.equal(parsedCsv.errors.length, 0);
  34. }
  35. describe('PapaParse', function() {
  36. it('synchronously parsed CSV should be correctly parsed', function() {
  37. assertLongSampleParsedCorrectly(Papa.parse(longSampleRawCsv));
  38. });
  39. it('Pause and resume works (Regression Test for Bug #636)', function(done) {
  40. this.timeout(30000);
  41. var mod200Rows = [
  42. ["Etiam a dolor vitae est vestibulum","84","DEF"],
  43. ["Etiam a dolor vitae est vestibulum","84","DEF"],
  44. ["Lorem ipsum dolor sit","42","ABC"],
  45. ["Etiam a dolor vitae est vestibulum","84","DEF"],
  46. ["Etiam a dolor vitae est vestibulum","84"],
  47. ["Lorem ipsum dolor sit","42","ABC"],
  48. ["Etiam a dolor vitae est vestibulum","84","DEF"],
  49. ["Etiam a dolor vitae est vestibulum","84","DEF"],
  50. ["Lorem ipsum dolor sit","42","ABC"],
  51. ["Lorem ipsum dolor sit","42"]
  52. ];
  53. var stepped = 0;
  54. var dataRows = [];
  55. Papa.parse(fs.createReadStream(__dirname + '/verylong-sample.csv'), {
  56. step: function(results, parser) {
  57. stepped++;
  58. if (results)
  59. {
  60. parser.pause();
  61. parser.resume();
  62. if (results.data && stepped % 200 === 0) {
  63. dataRows.push(results.data);
  64. }
  65. }
  66. },
  67. complete: function() {
  68. assert.strictEqual(2001, stepped);
  69. assert.deepEqual(mod200Rows, dataRows);
  70. done();
  71. }
  72. });
  73. });
  74. it('asynchronously parsed CSV should be correctly parsed', function(done) {
  75. Papa.parse(longSampleRawCsv, {
  76. complete: function(parsedCsv) {
  77. assertLongSampleParsedCorrectly(parsedCsv);
  78. done();
  79. },
  80. });
  81. });
  82. it('asynchronously parsed streaming CSV should be correctly parsed', function(done) {
  83. Papa.parse(fs.createReadStream(__dirname + '/long-sample.csv', 'utf8'), {
  84. complete: function(parsedCsv) {
  85. assertLongSampleParsedCorrectly(parsedCsv);
  86. done();
  87. },
  88. });
  89. });
  90. it('reports the correct row number on FieldMismatch errors', function(done) {
  91. Papa.parse(fs.createReadStream(__dirname + '/verylong-sample.csv'), {
  92. header: true,
  93. fastMode: true,
  94. complete: function(parsedCsv) {
  95. assert.deepEqual(parsedCsv.errors, [
  96. {
  97. "type": "FieldMismatch",
  98. "code": "TooFewFields",
  99. "message": "Too few fields: expected 3 fields but parsed 2",
  100. "row": 498
  101. },
  102. {
  103. "type": "FieldMismatch",
  104. "code": "TooFewFields",
  105. "message": "Too few fields: expected 3 fields but parsed 2",
  106. "row": 998
  107. },
  108. {
  109. "type": "FieldMismatch",
  110. "code": "TooFewFields",
  111. "message": "Too few fields: expected 3 fields but parsed 2",
  112. "row": 1498
  113. },
  114. {
  115. "type": "FieldMismatch",
  116. "code": "TooFewFields",
  117. "message": "Too few fields: expected 3 fields but parsed 2",
  118. "row": 1998
  119. }
  120. ]);
  121. assert.strictEqual(2000, parsedCsv.data.length);
  122. done();
  123. },
  124. });
  125. });
  126. it('piped streaming CSV should be correctly parsed', function(done) {
  127. var data = [];
  128. var readStream = fs.createReadStream(__dirname + '/long-sample.csv', 'utf8');
  129. var csvStream = readStream.pipe(Papa.parse(Papa.NODE_STREAM_INPUT));
  130. csvStream.on('data', function(item) {
  131. data.push(item);
  132. });
  133. csvStream.on('end', function() {
  134. assert.deepEqual(data[0], [
  135. 'Grant',
  136. 'Dyer',
  137. 'Donec.elementum@orciluctuset.example',
  138. '2013-11-23T02:30:31-08:00',
  139. '2014-05-31T01:06:56-07:00',
  140. 'Magna Ut Associates',
  141. 'ljenkins'
  142. ]);
  143. assert.deepEqual(data[7], [
  144. 'Talon',
  145. 'Salinas',
  146. 'posuere.vulputate.lacus@Donecsollicitudin.example',
  147. '2015-01-31T09:19:02-08:00',
  148. '2014-12-17T04:59:18-08:00',
  149. 'Aliquam Iaculis Incorporate',
  150. 'Phasellus@Quisquetincidunt.example'
  151. ]);
  152. done();
  153. });
  154. });
  155. it('piped streaming CSV should be correctly parsed when header is true', function(done) {
  156. var data = [];
  157. var readStream = fs.createReadStream(__dirname + '/sample-header.csv', 'utf8');
  158. var csvStream = readStream.pipe(Papa.parse(Papa.NODE_STREAM_INPUT, {header: true}));
  159. csvStream.on('data', function(item) {
  160. data.push(item);
  161. });
  162. csvStream.on('end', function() {
  163. assert.deepEqual(data[0], { title: 'test title 01', name: 'test name 01' });
  164. assert.deepEqual(data[1], { title: '', name: 'test name 02' });
  165. done();
  166. });
  167. });
  168. it('should support pausing and resuming on same tick when streaming', function(done) {
  169. var rows = [];
  170. Papa.parse(fs.createReadStream(__dirname + '/long-sample.csv', 'utf8'), {
  171. chunk: function(results, parser) {
  172. rows = rows.concat(results.data);
  173. parser.pause();
  174. parser.resume();
  175. },
  176. error: function(err) {
  177. done(new Error(err));
  178. },
  179. complete: function() {
  180. assert.deepEqual(rows[0], [
  181. 'Grant',
  182. 'Dyer',
  183. 'Donec.elementum@orciluctuset.example',
  184. '2013-11-23T02:30:31-08:00',
  185. '2014-05-31T01:06:56-07:00',
  186. 'Magna Ut Associates',
  187. 'ljenkins'
  188. ]);
  189. assert.deepEqual(rows[7], [
  190. 'Talon',
  191. 'Salinas',
  192. 'posuere.vulputate.lacus@Donecsollicitudin.example',
  193. '2015-01-31T09:19:02-08:00',
  194. '2014-12-17T04:59:18-08:00',
  195. 'Aliquam Iaculis Incorporate',
  196. 'Phasellus@Quisquetincidunt.example'
  197. ]);
  198. done();
  199. }
  200. });
  201. });
  202. it('should support pausing and resuming asynchronously when streaming', function(done) {
  203. var rows = [];
  204. Papa.parse(fs.createReadStream(__dirname + '/long-sample.csv', 'utf8'), {
  205. chunk: function(results, parser) {
  206. rows = rows.concat(results.data);
  207. parser.pause();
  208. setTimeout(function() {
  209. parser.resume();
  210. }, 200);
  211. },
  212. error: function(err) {
  213. done(new Error(err));
  214. },
  215. complete: function() {
  216. assert.deepEqual(rows[0], [
  217. 'Grant',
  218. 'Dyer',
  219. 'Donec.elementum@orciluctuset.example',
  220. '2013-11-23T02:30:31-08:00',
  221. '2014-05-31T01:06:56-07:00',
  222. 'Magna Ut Associates',
  223. 'ljenkins'
  224. ]);
  225. assert.deepEqual(rows[7], [
  226. 'Talon',
  227. 'Salinas',
  228. 'posuere.vulputate.lacus@Donecsollicitudin.example',
  229. '2015-01-31T09:19:02-08:00',
  230. '2014-12-17T04:59:18-08:00',
  231. 'Aliquam Iaculis Incorporate',
  232. 'Phasellus@Quisquetincidunt.example'
  233. ]);
  234. done();
  235. }
  236. });
  237. });
  238. it('handles errors in beforeFirstChunk', function(done) {
  239. var expectedError = new Error('test');
  240. Papa.parse(fs.createReadStream(__dirname + '/long-sample.csv', 'utf8'), {
  241. beforeFirstChunk: function() {
  242. throw expectedError;
  243. },
  244. error: function(err) {
  245. assert.deepEqual(err, expectedError);
  246. done();
  247. }
  248. });
  249. });
  250. it('handles errors in chunk', function(done) {
  251. var expectedError = new Error('test');
  252. Papa.parse(fs.createReadStream(__dirname + '/long-sample.csv', 'utf8'), {
  253. chunk: function() {
  254. throw expectedError;
  255. },
  256. error: function(err) {
  257. assert.deepEqual(err, expectedError);
  258. done();
  259. }
  260. });
  261. });
  262. it('handles errors in step', function(done) {
  263. var expectedError = new Error('test');
  264. Papa.parse(fs.createReadStream(__dirname + '/long-sample.csv', 'utf8'), {
  265. step: function() {
  266. throw expectedError;
  267. },
  268. error: function(err) {
  269. assert.deepEqual(err, expectedError);
  270. done();
  271. }
  272. });
  273. });
  274. });