-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(csv2json): throw on invalid input. add tests
- Loading branch information
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import csv2json from './csv2json'; | ||
|
||
/* | ||
const toBeType = require('jest-tobetype').toBeType; | ||
expect.extend({ | ||
toBeType, | ||
}); | ||
*/ | ||
|
||
/* | ||
const toBeType = require("jest-tobetype"); | ||
expect.extend(toBeType); | ||
*/ | ||
|
||
describe('csv2json', () => { | ||
|
||
it('handles historical input', () => { | ||
const csv = | ||
"Date,Open,High,Low,Close,Adj Close,Volume\n" + | ||
"2020-10-29,112.370003,116.930000,112.199997,115.320000,115.121384,146129200\n" + | ||
"2020-10-30,111.059998,111.989998,107.720001,108.860001,108.672516,190272600"; | ||
|
||
const obj = csv2json(csv); | ||
expect(obj.length).toBe(2); | ||
|
||
// camelcase | ||
const headers = Object.keys(obj[0]); | ||
expect(headers[0]).toBe('date'); | ||
expect(headers[5]).toBe('adjClose'); | ||
|
||
expect(obj[0].date).toBeInstanceOf(Date); | ||
expect(obj[0].open).toBeType('number'); | ||
}); | ||
|
||
it("returns anything else - not actually used in historical", () => { | ||
const csv = "string\nstring"; | ||
const obj = csv2json(csv); | ||
expect(obj[0].string).toBeType('string'); | ||
}); | ||
|
||
it('throws on weird file (no newlines)', () => { | ||
const csv ="one line file"; | ||
expect(() => csv2json(csv)).toThrow(/No newlines/); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters