This commit is contained in:
Andrew Kingston 2020-10-20 12:52:55 +01:00
parent 7e74889131
commit f86ee9d573
1 changed files with 30 additions and 29 deletions

View File

@ -1,6 +1,7 @@
const csvParser = require("../csvParser"); const fs = require("fs")
const csvParser = require("../csvParser")
const CSV_PATH = __dirname + "/test.csv"; const CSV_PATH = __dirname + "/test.csv"
const SCHEMAS = { const SCHEMAS = {
VALID: { VALID: {
@ -27,16 +28,16 @@ const SCHEMAS = {
BROKEN: { BROKEN: {
Address: { Address: {
type: "datetime", type: "datetime",
} },
}, },
}; }
describe("CSV Parser", () => { describe("CSV Parser", () => {
const csvString = fs.readFileSync(CSV_PATH, "utf8")
describe("parsing", () => { describe("parsing", () => {
it("returns status and types for a valid CSV transformation", async () => { it("returns status and types for a valid CSV transformation", async () => {
expect( expect(await csvParser.parse(csvString, SCHEMAS.VALID)).toEqual({
await csvParser.parse(CSV_PATH, SCHEMAS.VALID)
).toEqual({
Address: { Address: {
success: true, success: true,
type: "string", type: "string",
@ -49,13 +50,11 @@ describe("CSV Parser", () => {
success: true, success: true,
type: "string", type: "string",
}, },
}); })
}); })
it("returns status and types for an invalid CSV transformation", async () => { it("returns status and types for an invalid CSV transformation", async () => {
expect( expect(await csvParser.parse(csvString, SCHEMAS.INVALID)).toEqual({
await csvParser.parse(CSV_PATH, SCHEMAS.INVALID)
).toEqual({
Address: { Address: {
success: false, success: false,
type: "number", type: "number",
@ -68,41 +67,43 @@ describe("CSV Parser", () => {
success: true, success: true,
type: "string", type: "string",
}, },
}); })
}); })
}); })
describe("transformation", () => { describe("transformation", () => {
it("transforms a CSV file into JSON", async () => { it("transforms a CSV file into JSON", async () => {
expect( expect(
await csvParser.transform({ await csvParser.transform({
schema: SCHEMAS.VALID, schema: SCHEMAS.VALID,
path: CSV_PATH, csvString,
}) })
).toMatchSnapshot(); ).toMatchSnapshot()
}); })
it("transforms a CSV file into JSON ignoring certain fields", async () => { it("transforms a CSV file into JSON ignoring certain fields", async () => {
expect( expect(
await csvParser.transform({ await csvParser.transform({
schema: SCHEMAS.IGNORE, schema: SCHEMAS.IGNORE,
path: CSV_PATH, csvString,
}) })
).toEqual([ ).toEqual([
{ {
Name: "Bert" Name: "Bert",
}, },
{ {
Name: "Ernie" Name: "Ernie",
}, },
{ {
Name: "Big Bird" Name: "Big Bird",
} },
]); ])
}); })
it("throws an error on invalid schema", async () => { it("throws an error on invalid schema", async () => {
await expect(csvParser.transform({ schema: SCHEMAS.BROKEN, path: CSV_PATH })).rejects.toThrow() await expect(
}); csvParser.transform({ schema: SCHEMAS.BROKEN, csvString })
}); ).rejects.toThrow()
}); })
})
})