/* eslint-disable @typescript-eslint/naming-convention */ // Jestによるparser.tsのテスト import fs from "fs"; import { CSVType, parseCSV } from "./parser"; describe("parse", () => { it("指定形式のCSV文字列をパースできる", async () => { const text = fs.readFileSync("src/common/test/test_001.csv", "utf-8"); const actualData = await parseCSV(text); const expectData: CSVType[] = [ { name: "hoge", email: "sample@example.com", role: 1, author_id: "HOGE", auto_renew: 1, notification: 1, encryption: 1, encryption_password: "abcd", prompt: 0, }, ]; expect(actualData).toEqual(expectData); }); it("指定形式のヘッダでない場合、例外が送出される | author_id(値がoptionial)がない", async () => { const text = fs.readFileSync("src/common/test/test_002.csv", "utf-8"); try { await parseCSV(text); fail("例外が発生しませんでした"); } catch (e) { expect(e).toEqual(new Error("Invalid CSV format")); } }); it("指定形式のヘッダでない場合、例外が送出される | email(値が必須)がない", async () => { const text = fs.readFileSync("src/common/test/test_003.csv", "utf-8"); try { await parseCSV(text); fail("例外が発生しませんでした"); } catch (e) { expect(e).toEqual(new Error("Invalid CSV format")); } }); it("指定形式のヘッダでない場合、例外が送出される | emailがスペルミス", async () => { const text = fs.readFileSync("src/common/test/test_004.csv", "utf-8"); try { await parseCSV(text); fail("例外が発生しませんでした"); } catch (e) { expect(e).toEqual(new Error("Invalid CSV format")); } }); it("指定形式のCSV文字列をパースできる | 抜けているパラメータ(文字列)はnullとなる", async () => { const text = fs.readFileSync("src/common/test/test_005.csv", "utf-8"); const actualData = await parseCSV(text); const expectData: CSVType[] = [ { name: "hoge", email: "sample@example.com", role: 1, author_id: null, auto_renew: 1, notification: 1, encryption: 1, encryption_password: "abcd", prompt: 0, }, ]; expect(actualData).toEqual(expectData); }); it("指定形式のCSV文字列をパースできる | 抜けているパラメータ(数値)はnullとなる", async () => { const text = fs.readFileSync("src/common/test/test_006.csv", "utf-8"); const actualData = await parseCSV(text); const expectData: CSVType[] = [ { name: "hoge", email: "sample@example.com", role: null, author_id: "HOGE", auto_renew: 1, notification: 1, encryption: 1, encryption_password: "abcd", prompt: 0, }, ]; expect(actualData).toEqual(expectData); }); it("指定形式のCSV文字列をパースできる | 余計なパラメータがあっても問題はない", async () => { const text = fs.readFileSync("src/common/test/test_007.csv", "utf-8"); const actualData = await parseCSV(text); const expectData: CSVType[] = [ { name: "hoge", email: "sample@example.com", role: 1, author_id: "HOGE", auto_renew: 1, notification: 1, encryption: 1, encryption_password: "abcd", prompt: 0, }, { name: "hoge2", email: "sample2@example.com", role: 1, author_id: "HOGE2", auto_renew: 1, notification: 1, encryption: 1, encryption_password: "abcd2", prompt: 0, }, ]; expect(actualData.length).toBe(expectData.length); // 余計なパラメータ格納用に __parsed_extra: string[] というプロパティが作られてしまうので、既知のプロパティ毎に比較 for (let i = 0; i < actualData.length; i += 1) { const actualValue = actualData[i]; const expectValue = expectData[i]; expect(actualValue.author_id).toEqual(expectValue.author_id); expect(actualValue.auto_renew).toEqual(expectValue.auto_renew); expect(actualValue.email).toEqual(expectValue.email); expect(actualValue.encryption).toEqual(expectValue.encryption); expect(actualValue.encryption_password).toEqual( expectValue.encryption_password ); expect(actualValue.name).toEqual(expectValue.name); expect(actualValue.notification).toEqual(expectValue.notification); expect(actualValue.prompt).toEqual(expectValue.prompt); expect(actualValue.role).toEqual(expectValue.role); } }); it("author_id,encryption_passwordが数値のみの場合でも、文字列として変換できる", async () => { const text = fs.readFileSync("src/common/test/test_008.csv", "utf-8"); const actualData = await parseCSV(text); const expectData: CSVType[] = [ { name: "hoge", email: "sample@example.com", role: 1, author_id: "1111", auto_renew: 1, notification: 1, encryption: 1, encryption_password: "222222", prompt: 0, }, ]; expect(actualData).toEqual(expectData); }); });