From 909c2a6d55a3dfc1d8addfba0863d1ca075d6c21 Mon Sep 17 00:00:00 2001 From: "saito.k" Date: Mon, 3 Jun 2024 06:36:05 +0000 Subject: [PATCH] =?UTF-8?q?Merged=20PR=20909:=20=E7=94=BB=E9=9D=A2?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=EF=BC=88CSV=E3=83=95=E3=82=A1=E3=82=A4?= =?UTF-8?q?=E3=83=AB=E3=81=AE=E5=A4=89=E6=8F=9B=E5=87=A6=E7=90=86=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 概要 [Task4202: 画面修正(CSVファイルの変換処理)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/4202) - CSVデータの変換関数を修正 - author_id,encryption_passwordが数値のみの場合でも、文字列として扱うようにする ## レビューポイント - ほかに確認したほうが良いテストケースはあるか ## 動作確認状況 - ローカルで確認 - 行った修正がデグレを発生させていないことを確認できるか - 具体的にどのような確認をしたか - 既存のテストはすべて通ることを確認 ## 補足 - 相談、参考資料などがあれば --- dictation_client/src/common/parser.test.ts | 19 +++++++++++++++++++ dictation_client/src/common/parser.ts | 19 ++++++++++++++++++- dictation_client/src/common/test/test_008.csv | 2 ++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 dictation_client/src/common/test/test_008.csv diff --git a/dictation_client/src/common/parser.test.ts b/dictation_client/src/common/parser.test.ts index d4aff39..a318a76 100644 --- a/dictation_client/src/common/parser.test.ts +++ b/dictation_client/src/common/parser.test.ts @@ -131,4 +131,23 @@ describe("parse", () => { 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); + }); }); diff --git a/dictation_client/src/common/parser.ts b/dictation_client/src/common/parser.ts index 15fe85b..30e0b43 100644 --- a/dictation_client/src/common/parser.ts +++ b/dictation_client/src/common/parser.ts @@ -42,7 +42,24 @@ export const parseCSV = async (csvString: string): Promise => download: false, worker: false, // XXX: workerを使うとエラーが発生するためfalseに設定 header: true, - dynamicTyping: true, + dynamicTyping: { + // author_id, encryption_passwordは数値のみの場合、numberに変換されたくないためdynamicTypingをtrueにしない + role: true, + auto_renew: true, + notification: true, + encryption: true, + prompt: true, + }, + // dynamicTypingがfalseの場合、空文字をnullに変換できないためtransformを使用する + transform: (value, field) => { + if (field === "author_id" || field === "encryption_password") { + // 空文字の場合はnullに変換する + if (value === "") { + return null; + } + } + return value; + }, complete: (results: ParseResult) => { // ヘッダーがCSVTypeFieldsと一致しない場合はエラーを返す if (!equals(results.meta.fields ?? [], CSVTypeFields)) { diff --git a/dictation_client/src/common/test/test_008.csv b/dictation_client/src/common/test/test_008.csv new file mode 100644 index 0000000..ccc9f52 --- /dev/null +++ b/dictation_client/src/common/test/test_008.csv @@ -0,0 +1,2 @@ +name,email,role,author_id,auto_renew,notification,encryption,encryption_password,prompt +hoge,sample@example.com,1,1111,1,1,1,222222,0 \ No newline at end of file