|
1 | 1 | import csv from "../src"; |
2 | | -var assert = require("assert"); |
| 2 | +import assert from "assert"; |
3 | 3 | var fs = require("fs"); |
4 | 4 | import { sandbox } from "sinon"; |
5 | 5 | import CSVError from "../src/CSVError"; |
@@ -231,13 +231,93 @@ describe("testCSVConverter3", function () { |
231 | 231 | it("should parse header with quotes correctly", function () { |
232 | 232 | var testData = __dirname + "/data/csvWithUnclosedHeader"; |
233 | 233 | return csv({ |
234 | | - headers:["exam_date","sample_no","status","sample_type","patient_id","last_name","first_name","gender_of_patient","patient_birth_date","patient_note","patient_department","accession_number","sample_site","physician","operator","department","note","test_order_code","draw_time","approval_status","approval_time","report_layout","patient_account_number","none_1","errors_detected_during_measurement","age","error_code_01","weight","error_code_02","height","error_code_03","hcg_beta_p","error_code_04","troponin_i_p","error_code_05","ck_mb_p","error_code_06","d_dimer_p","error_code_07","hscrp_p","error_code_08","myoglobin_p","error_code_09","nt_probnp","error_code_10","crp","error_code_11","bnp","error_code_12","tnt","error_code_13","demo_p","error_code_14","pct","error_code_15"] |
| 234 | + headers: ["exam_date", "sample_no", "status", "sample_type", "patient_id", "last_name", "first_name", "gender_of_patient", "patient_birth_date", "patient_note", "patient_department", "accession_number", "sample_site", "physician", "operator", "department", "note", "test_order_code", "draw_time", "approval_status", "approval_time", "report_layout", "patient_account_number", "none_1", "errors_detected_during_measurement", "age", "error_code_01", "weight", "error_code_02", "height", "error_code_03", "hcg_beta_p", "error_code_04", "troponin_i_p", "error_code_05", "ck_mb_p", "error_code_06", "d_dimer_p", "error_code_07", "hscrp_p", "error_code_08", "myoglobin_p", "error_code_09", "nt_probnp", "error_code_10", "crp", "error_code_11", "bnp", "error_code_12", "tnt", "error_code_13", "demo_p", "error_code_14", "pct", "error_code_15"] |
235 | 235 | }) |
236 | | - .fromFile(testData) |
| 236 | + .fromFile(testData) |
| 237 | + .then((d) => { |
| 238 | + assert.equal(d.length, 2); |
| 239 | + assert.equal(d[0].sample_no, "12669"); |
| 240 | + }) |
| 241 | + |
| 242 | + }); |
| 243 | + it ("should stream json string correctly",function(done){ |
| 244 | + const data=`a,b,c |
| 245 | +1,2,3 |
| 246 | +4,5,6` |
| 247 | + let hasLeftBracket=false; |
| 248 | + let hasRightBracket=false; |
| 249 | + csv({ |
| 250 | + downstreamFormat:"array" |
| 251 | + }) |
| 252 | + .fromString(data) |
| 253 | + .on("data",(d)=>{ |
| 254 | + const str=d.toString(); |
| 255 | + if (str[0]==="[" && str.length ===2){ |
| 256 | + hasLeftBracket=true; |
| 257 | + }else if (str[0]==="]" && str.length===2){ |
| 258 | + hasRightBracket=true; |
| 259 | + }else{ |
| 260 | + assert.equal(str[str.length-2],","); |
| 261 | + } |
| 262 | + |
| 263 | + }) |
| 264 | + .on("end",()=>{ |
| 265 | + assert.equal(hasLeftBracket,true); |
| 266 | + assert.equal(hasRightBracket,true); |
| 267 | + done(); |
| 268 | + }) |
| 269 | + }) |
| 270 | + it ("should stream json line correctly",function(done){ |
| 271 | + const data=`a,b,c |
| 272 | +1,2,3 |
| 273 | +4,5,6` |
| 274 | + csv({ |
| 275 | + downstreamFormat:"line" |
| 276 | + }) |
| 277 | + .fromString(data) |
| 278 | + .on("data",(d)=>{ |
| 279 | + const str=d.toString(); |
| 280 | + |
| 281 | + assert.notEqual(str[str.length-2],","); |
| 282 | + }) |
| 283 | + .on("end",()=>{ |
| 284 | + done(); |
| 285 | + }) |
| 286 | + }) |
| 287 | + it ("should not send json if needEmitAll is false",async function(){ |
| 288 | + const data=`a,b,c |
| 289 | +1,2,3 |
| 290 | +4,5,6` |
| 291 | + return csv({ |
| 292 | + needEmitAll:false |
| 293 | + }) |
| 294 | + .fromString(data) |
237 | 295 | .then((d)=>{ |
238 | | - assert.equal(d.length,2); |
239 | | - assert.equal(d[0].sample_no,"12669"); |
| 296 | + assert(d.length===0); |
240 | 297 | }) |
241 | | - |
242 | | - }); |
| 298 | + }) |
| 299 | + it ("should convert null to null object",async function(){ |
| 300 | + const data=`a,b,c |
| 301 | +null,2,3 |
| 302 | +4,5,6` |
| 303 | + return csv({ |
| 304 | + nullObject:true |
| 305 | + }) |
| 306 | + .fromString(data) |
| 307 | + .then((d)=>{ |
| 308 | + assert.equal(d[0].a,null) |
| 309 | + }) |
| 310 | + }) |
| 311 | + it ("should process period properly",async function(){ |
| 312 | + const data=`a..,b,c |
| 313 | +1,2,3 |
| 314 | +4,5,6` |
| 315 | + return csv({ |
| 316 | + }) |
| 317 | + .fromString(data) |
| 318 | + .then((d)=>{ |
| 319 | + assert.equal(d[0]["a.."],1); |
| 320 | + assert.equal(d[1]["a.."],4); |
| 321 | + }) |
| 322 | + }) |
243 | 323 | }); |
0 commit comments