Merged PR 689: licenseAlertのテスト修正

## 概要
[Task3275: licenseAlertのテスト修正](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/3275)

- redisをモックに切り替えてテストを行うように修正
  - redisのモックは`redis-mock`というライブラリを使用

## レビューポイント
- 使用したライブラリに問題はありそうか
  - https://www.npmjs.com/package/redis-mock/v/0.56.3
  - テスト用に使用しているだけなので大きな問題になることはない認識

## UIの変更
- Before/Afterのスクショなど
- スクショ置き場

## 動作確認状況
- ローカルで確認、Pipeline上でテストが通ることを確認

## 補足
- 相談、参考資料などがあれば
This commit is contained in:
saito.k 2024-01-17 06:02:20 +00:00
parent 7075c2a9eb
commit 46fdef854e
4 changed files with 45 additions and 15 deletions

View File

@ -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",

View File

@ -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",

View File

@ -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) {

View File

@ -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