diff --git a/dictation_function/package-lock.json b/dictation_function/package-lock.json index 4da5447..8ebaf00 100644 --- a/dictation_function/package-lock.json +++ b/dictation_function/package-lock.json @@ -20,8 +20,10 @@ "@types/jest": "^27.5.0", "@types/node": "18.x", "@types/redis": "^2.8.13", + "@types/redis-mock": "^0.17.3", "azure-functions-core-tools": "^4.x", "jest": "^28.0.3", + "redis-mock": "^0.56.3", "rimraf": "^5.0.0", "sqlite3": "^5.1.6", "supertest": "^6.1.3", @@ -2000,6 +2002,15 @@ "@types/node": "*" } }, + "node_modules/@types/redis-mock": { + "version": "0.17.3", + "resolved": "https://registry.npmjs.org/@types/redis-mock/-/redis-mock-0.17.3.tgz", + "integrity": "sha512-1baXyGxRKEDog8p1ReiypODwiST2n3/0pBbgUKEuv9pBXnY6ttRzKATcW5Xz20ZOl9qkKtPIeq20tHgHSdQBAQ==", + "dev": true, + "dependencies": { + "@types/redis": "^2.8.0" + } + }, "node_modules/@types/stack-utils": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.2.tgz", @@ -6409,6 +6420,15 @@ "node": ">=4" } }, + "node_modules/redis-mock": { + "version": "0.56.3", + "resolved": "https://registry.npmjs.org/redis-mock/-/redis-mock-0.56.3.tgz", + "integrity": "sha512-ynaJhqk0Qf3Qajnwvy4aOjS4Mdf9IBkELWtjd+NYhpiqu4QCNq6Vf3Q7c++XRPGiKiwRj9HWr0crcwy7EiPjYQ==", + "dev": true, + "engines": { + "node": ">=6" + } + }, "node_modules/redis-parser": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", diff --git a/dictation_function/package.json b/dictation_function/package.json index 8a6260d..33f2287 100644 --- a/dictation_function/package.json +++ b/dictation_function/package.json @@ -25,8 +25,10 @@ "@types/jest": "^27.5.0", "@types/node": "18.x", "@types/redis": "^2.8.13", + "@types/redis-mock": "^0.17.3", "azure-functions-core-tools": "^4.x", "jest": "^28.0.3", + "redis-mock": "^0.56.3", "rimraf": "^5.0.0", "sqlite3": "^5.1.6", "supertest": "^6.1.3", diff --git a/dictation_function/src/functions/licenseAlert.ts b/dictation_function/src/functions/licenseAlert.ts index bbab8b0..ad4119f 100644 --- a/dictation_function/src/functions/licenseAlert.ts +++ b/dictation_function/src/functions/licenseAlert.ts @@ -77,7 +77,7 @@ export async function licenseAlertProcessing( const keys = await keysAsync(`${SEND_COMPLETE_PREFIX}${formattedDate}*`); console.log(`delete terget:${keys}`); if (keys.length > 0) { - const delResult = await delAsync(...keys); + const delResult = await delAsync(keys); console.log(`delete number:${delResult}`); } } catch (e) { diff --git a/dictation_function/src/test/licenseAlert.spec.ts b/dictation_function/src/test/licenseAlert.spec.ts index 390dc18..c3891a0 100644 --- a/dictation_function/src/test/licenseAlert.spec.ts +++ b/dictation_function/src/test/licenseAlert.spec.ts @@ -13,13 +13,15 @@ import { ADB2C_SIGN_IN_TYPE } from "../constants"; import { SendGridService } from "../sendgrid/sendgrid"; import { AdB2cService } from "../adb2c/adb2c"; import { InvocationContext } from "@azure/functions"; -import { RedisClient } from "redis"; -import { createRedisClient } from "../redis/redis"; +import { RedisClient, createClient } from "redis-mock"; +import { promisify } from "util"; describe("licenseAlert", () => { dotenv.config({ path: ".env" }); dotenv.config({ path: ".env.local", override: true }); let source: DataSource | null = null; + const redisClient = createClient(); + beforeEach(async () => { source = new DataSource({ type: "sqlite", @@ -35,17 +37,15 @@ describe("licenseAlert", () => { if (!source) return; await source.destroy(); source = null; + //licenseAlertProcessingの処理の最後にキャッシュ削除処理があるため、ここでクリーンアップは行わない }); - it("テストを通すための仮", async () => {}); - /* - it("ライセンス在庫不足メールが送信され、ライセンス失効警告メールが送信されないこと", async () => { if (!source) fail(); const context = new InvocationContext(); const sendgridMock = new SendGridServiceMock() as SendGridService; const adb2cMock = new AdB2cServiceMock() as AdB2cService; - const redisClient = createRedisClient(); + // 呼び出し回数でテスト成否を判定 const spySend = jest.spyOn(sendgridMock, "sendMail"); @@ -77,7 +77,10 @@ describe("licenseAlert", () => { adb2cMock ); expect(spySend.mock.calls).toHaveLength(1); - redisClient.quit; + // redisからキャッシュが削除されていることを確認 + const getAsync = promisify(redisClient.keys).bind(redisClient); + const keys = await getAsync(`*`); + expect(keys).toHaveLength(0); }); it("ライセンス在庫不足メール、ライセンス失効警告メールが送信されること", async () => { @@ -85,7 +88,6 @@ describe("licenseAlert", () => { const context = new InvocationContext(); const sendgridMock = new SendGridServiceMock() as SendGridService; const adb2cMock = new AdB2cServiceMock() as AdB2cService; - const redisClient = createRedisClient(); // 呼び出し回数でテスト成否を判定 const spySend = jest.spyOn(sendgridMock, "sendMail"); @@ -118,7 +120,10 @@ describe("licenseAlert", () => { adb2cMock ); expect(spySend.mock.calls).toHaveLength(2); - redisClient.quit; + // redisからキャッシュが削除されていることを確認 + const getAsync = promisify(redisClient.keys).bind(redisClient); + const keys = await getAsync(`*`); + expect(keys).toHaveLength(0); }); it("在庫があるため、ライセンス在庫不足メールが送信されないこと", async () => { @@ -126,7 +131,6 @@ describe("licenseAlert", () => { const context = new InvocationContext(); const sendgridMock = new SendGridServiceMock() as SendGridService; const adb2cMock = new AdB2cServiceMock() as AdB2cService; - const redisClient = createRedisClient(); // 呼び出し回数でテスト成否を判定 const spySend = jest.spyOn(sendgridMock, "sendMail"); @@ -172,7 +176,10 @@ describe("licenseAlert", () => { adb2cMock ); expect(spySend.mock.calls).toHaveLength(0); - redisClient.quit; + // redisからキャッシュが削除されていることを確認 + const getAsync = promisify(redisClient.keys).bind(redisClient); + const keys = await getAsync(`*`); + expect(keys).toHaveLength(0); }); it("AutoRenewがtureのため、ライセンス失効警告メールが送信されないこと", async () => { @@ -180,7 +187,6 @@ describe("licenseAlert", () => { const context = new InvocationContext(); const sendgridMock = new SendGridServiceMock() as SendGridService; const adb2cMock = new AdB2cServiceMock() as AdB2cService; - const redisClient = createRedisClient(); // 呼び出し回数でテスト成否を判定 const spySend = jest.spyOn(sendgridMock, "sendMail"); @@ -213,9 +219,11 @@ describe("licenseAlert", () => { adb2cMock ); expect(spySend.mock.calls).toHaveLength(1); - redisClient.quit; + // redisからキャッシュが削除されていることを確認 + const getAsync = promisify(redisClient.keys).bind(redisClient); + const keys = await getAsync(`*`); + expect(keys).toHaveLength(0); }); - */ }); // テスト用sendgrid