From 13aca3cde00b8a71e199e1ea674fcaa4abf722fa Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 5 Oct 2020 11:48:13 +0100 Subject: [PATCH] unit tests for csv parser --- packages/server/src/utilities/csvParser.js | 1 + .../__snapshots__/csvParser.spec.js.snap | 21 ++++ .../src/utilities/tests/csvParser.spec.js | 108 ++++++++++++++++++ packages/server/src/utilities/tests/test.csv | 4 + 4 files changed, 134 insertions(+) create mode 100644 packages/server/src/utilities/tests/__snapshots__/csvParser.spec.js.snap create mode 100644 packages/server/src/utilities/tests/test.csv diff --git a/packages/server/src/utilities/csvParser.js b/packages/server/src/utilities/csvParser.js index bc7dac2fad..4a86ede309 100644 --- a/packages/server/src/utilities/csvParser.js +++ b/packages/server/src/utilities/csvParser.js @@ -71,6 +71,7 @@ async function transform({ schema, path }) { return json } catch (err) { console.error(`Error transforming CSV to JSON for data import`, err) + throw err } } diff --git a/packages/server/src/utilities/tests/__snapshots__/csvParser.spec.js.snap b/packages/server/src/utilities/tests/__snapshots__/csvParser.spec.js.snap new file mode 100644 index 0000000000..1966637f56 --- /dev/null +++ b/packages/server/src/utilities/tests/__snapshots__/csvParser.spec.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`CSV Parser transformation transforms a CSV file into JSON 1`] = ` +Array [ + Object { + "Address": "5 Sesame Street", + "Age": 4324, + "Name": "Bert", + }, + Object { + "Address": "1 World Trade Center", + "Age": 34, + "Name": "Ernie", + }, + Object { + "Address": "44 Second Avenue", + "Age": 23423, + "Name": "Big Bird", + }, +] +`; diff --git a/packages/server/src/utilities/tests/csvParser.spec.js b/packages/server/src/utilities/tests/csvParser.spec.js index e69de29bb2..a210f604e6 100644 --- a/packages/server/src/utilities/tests/csvParser.spec.js +++ b/packages/server/src/utilities/tests/csvParser.spec.js @@ -0,0 +1,108 @@ +const csvParser = require("../csvParser"); + +const CSV_PATH = __dirname + "/test.csv"; + +const SCHEMAS = { + VALID: { + Age: { + type: "number", + }, + }, + INVALID: { + Address: { + type: "number", + }, + Age: { + type: "number", + }, + }, + IGNORE: { + Address: { + type: "omit", + }, + Age: { + type: "omit", + }, + }, + BROKEN: { + Address: { + type: "datetime", + } + }, +}; + +describe("CSV Parser", () => { + describe("parsing", () => { + it("returns status and types for a valid CSV transformation", async () => { + expect( + await csvParser.parse(CSV_PATH, SCHEMAS.VALID) + ).toEqual({ + Address: { + success: true, + type: "string", + }, + Age: { + success: true, + type: "number", + }, + Name: { + success: true, + type: "string", + }, + }); + }); + + it("returns status and types for an invalid CSV transformation", async () => { + expect( + await csvParser.parse(CSV_PATH, SCHEMAS.INVALID) + ).toEqual({ + Address: { + success: false, + type: "number", + }, + Age: { + success: true, + type: "number", + }, + Name: { + success: true, + type: "string", + }, + }); + }); + }); + + describe("transformation", () => { + it("transforms a CSV file into JSON", async () => { + expect( + await csvParser.transform({ + schema: SCHEMAS.VALID, + path: CSV_PATH, + }) + ).toMatchSnapshot(); + }); + + it("transforms a CSV file into JSON ignoring certain fields", async () => { + expect( + await csvParser.transform({ + schema: SCHEMAS.IGNORE, + path: CSV_PATH, + }) + ).toEqual([ + { + Name: "Bert" + }, + { + Name: "Ernie" + }, + { + Name: "Big Bird" + } + ]); + }); + + it("throws an error on invalid schema", async () => { + await expect(csvParser.transform({ schema: SCHEMAS.BROKEN, path: CSV_PATH })).rejects.toThrow() + }); + }); +}); diff --git a/packages/server/src/utilities/tests/test.csv b/packages/server/src/utilities/tests/test.csv new file mode 100644 index 0000000000..2266b348cd --- /dev/null +++ b/packages/server/src/utilities/tests/test.csv @@ -0,0 +1,4 @@ +"Name","Age","Address" +"Bert","4324","5 Sesame Street" +"Ernie","34","1 World Trade Center" +"Big Bird","23423","44 Second Avenue" \ No newline at end of file