2021-03-23 06:19:45 +13:00
|
|
|
const { readFileSync } = require("../fileSystem")
|
2020-10-21 00:52:55 +13:00
|
|
|
const csvParser = require("../csvParser")
|
2020-10-05 23:48:13 +13:00
|
|
|
|
2020-10-21 00:52:55 +13:00
|
|
|
const CSV_PATH = __dirname + "/test.csv"
|
2020-10-05 23:48:13 +13:00
|
|
|
|
|
|
|
const SCHEMAS = {
|
|
|
|
VALID: {
|
|
|
|
Age: {
|
|
|
|
type: "number",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
INVALID: {
|
|
|
|
Address: {
|
|
|
|
type: "number",
|
|
|
|
},
|
|
|
|
Age: {
|
|
|
|
type: "number",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
IGNORE: {
|
|
|
|
Address: {
|
|
|
|
type: "omit",
|
|
|
|
},
|
|
|
|
Age: {
|
|
|
|
type: "omit",
|
|
|
|
},
|
2021-11-13 08:39:12 +13:00
|
|
|
Name: {
|
|
|
|
type: "string",
|
|
|
|
},
|
2020-10-05 23:48:13 +13:00
|
|
|
},
|
|
|
|
BROKEN: {
|
|
|
|
Address: {
|
|
|
|
type: "datetime",
|
2020-10-21 00:52:55 +13:00
|
|
|
},
|
2020-10-05 23:48:13 +13:00
|
|
|
},
|
2020-10-21 00:52:55 +13:00
|
|
|
}
|
2020-10-05 23:48:13 +13:00
|
|
|
|
|
|
|
describe("CSV Parser", () => {
|
2021-03-23 06:19:45 +13:00
|
|
|
const csvString = readFileSync(CSV_PATH, "utf8")
|
2020-10-21 00:52:55 +13:00
|
|
|
|
2020-10-05 23:48:13 +13:00
|
|
|
describe("parsing", () => {
|
|
|
|
it("returns status and types for a valid CSV transformation", async () => {
|
2020-10-21 00:52:55 +13:00
|
|
|
expect(await csvParser.parse(csvString, SCHEMAS.VALID)).toEqual({
|
2020-10-05 23:48:13 +13:00
|
|
|
Address: {
|
|
|
|
success: true,
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
Age: {
|
|
|
|
success: true,
|
|
|
|
type: "number",
|
|
|
|
},
|
|
|
|
Name: {
|
|
|
|
success: true,
|
|
|
|
type: "string",
|
|
|
|
},
|
2020-10-21 00:52:55 +13:00
|
|
|
})
|
|
|
|
})
|
2020-10-05 23:48:13 +13:00
|
|
|
|
|
|
|
it("returns status and types for an invalid CSV transformation", async () => {
|
2020-10-21 00:52:55 +13:00
|
|
|
expect(await csvParser.parse(csvString, SCHEMAS.INVALID)).toEqual({
|
2020-10-05 23:48:13 +13:00
|
|
|
Address: {
|
|
|
|
success: false,
|
|
|
|
type: "number",
|
|
|
|
},
|
|
|
|
Age: {
|
|
|
|
success: true,
|
|
|
|
type: "number",
|
|
|
|
},
|
|
|
|
Name: {
|
|
|
|
success: true,
|
|
|
|
type: "string",
|
|
|
|
},
|
2020-10-21 00:52:55 +13:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-10-05 23:48:13 +13:00
|
|
|
|
|
|
|
describe("transformation", () => {
|
|
|
|
it("transforms a CSV file into JSON", async () => {
|
|
|
|
expect(
|
|
|
|
await csvParser.transform({
|
|
|
|
schema: SCHEMAS.VALID,
|
2020-10-21 00:52:55 +13:00
|
|
|
csvString,
|
2020-10-05 23:48:13 +13:00
|
|
|
})
|
2020-10-21 00:52:55 +13:00
|
|
|
).toMatchSnapshot()
|
|
|
|
})
|
2020-10-05 23:48:13 +13:00
|
|
|
|
|
|
|
it("transforms a CSV file into JSON ignoring certain fields", async () => {
|
|
|
|
expect(
|
|
|
|
await csvParser.transform({
|
|
|
|
schema: SCHEMAS.IGNORE,
|
2020-10-21 00:52:55 +13:00
|
|
|
csvString,
|
2020-10-05 23:48:13 +13:00
|
|
|
})
|
|
|
|
).toEqual([
|
|
|
|
{
|
2020-12-18 23:20:25 +13:00
|
|
|
Name: "Bertå",
|
2020-10-05 23:48:13 +13:00
|
|
|
},
|
|
|
|
{
|
2020-10-21 00:52:55 +13:00
|
|
|
Name: "Ernie",
|
2020-10-05 23:48:13 +13:00
|
|
|
},
|
|
|
|
{
|
2020-10-21 00:52:55 +13:00
|
|
|
Name: "Big Bird",
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
2020-10-05 23:48:13 +13:00
|
|
|
|
|
|
|
it("throws an error on invalid schema", async () => {
|
2020-10-21 00:52:55 +13:00
|
|
|
await expect(
|
|
|
|
csvParser.transform({ schema: SCHEMAS.BROKEN, csvString })
|
|
|
|
).rejects.toThrow()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|