DynamoDB용 통합 테스트 도구
11737 단어 awstypescriptserverlesstesting
이제 sls-test-tools가 새로운 DynamoDB 어설션과 함께 제공된다는 소식을 전하게 되어 기쁩니다!
간단한 예를 들어보겠습니다 🏃♂️ !
1️⃣ 테스트할 기능
클라이언트 트랜잭션 데이터를 DynamoDB에 업로드하는 간단한 Lambda 함수를 고려해 보겠습니다.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb";
type TransactionEvent = { clientName: string; transactionData: string };
const ddbDocClient = DynamoDBDocumentClient.from(new DynamoDBClient({}));
export const handler = async (event: TransactionEvent): Promise<string> => {
const { clientName, transactionData } = event;
await ddbDocClient.send(
new PutCommand({
TableName: "Sells",
Item: {
PK: clientName,
SK: new Date().toISOString(),
transactionData,
},
})
);
return "Transaction saved !";
};
보시다시피 이 함수는 AWS SDK를 사용하여 DynamoDB에 대해 단일 "put"호출을 수행합니다. 통합 테스트에서 데이터가 실제로 Dynamo에 기록되었는지 확인하려고 합니다.
2️⃣ 필기시험!
우리는 sls-test-tools new assertion
toExistInDynamoTable
을 사용할 것입니다.통합 테스트는 3단계로 수행할 수 있습니다.
이 세 단계에 따라 통합 테스트 구현은 다음과 같습니다.
import { DocumentClient } from "aws-sdk/clients/dynamodb";
import MockDate from "mockdate";
import { AWSClient } from "sls-test-tools";
import { handler } from "./uploadTransactionDataToDynamo";
describe("Sells data upload integration testing", () => {
const documentClient: DocumentClient = new AWSClient.DocumentClient();
const mockDate = "2022-01-01T00:00:00.000Z";
MockDate.set(mockDate);
const event = { clientName: "John", transactionData: "someData" };
afterAll(async () => {
// 🧹 clean what you've created
await documentClient.delete({
TableName: "Sells",
Key: { PK: "John", SK: mockDate },
});
});
it("should upload transaction data to Dynamo", async () => {
// 🔥 trigger the initial event
await handler(event);
// ✅ assert the functional behavior you are testing
await expect({
PK: "John",
SK: mockDate,
}).toExistInDynamoTable("Sells");
});
물론 모든 sls-test-tools 어설션은
expect(..).not
를 사용하여 반전될 수 있습니다.이전 예제와 같이 파티션 키
PK
와 정렬 키SK
가 포함된 복합 기본 인덱스 테이블은 물론 기본 인덱스가 파티션 키.it("should upload transaction data to Dynamo", async () => {
await handler(event);
await expect({ PK: "John" }).toExistInDynamoTable("Sells");
await documentClient.delete({
TableName: "Sells",
Key: { PK: "John", SK: mockDate },
});
});
3️⃣ 다음은 !
우리 팀은 sls-test-tools를 입력하고 더 많은 사용자 지정 jest 어설션을 추가하는 작업을 하고 있습니다. 우리는 또한 서버리스 통합 테스트에 대한 보다 철저한 기사를 작성하고 있습니다. 언제 나올지 알림을 받을 수 있도록 언제든지 구독하십시오!
⭐️⭐️⭐️ 해피 2️⃣0️⃣2️⃣2️⃣ ⭐️⭐️⭐️
sls-test-tools에 대한 자세한 배경 정보는 Integration Testing Strategy for EventBridge 및 Bringing Simplicity to Serverless Integration Testing 두 문서를 참조하십시오.
Reference
이 문제에 관하여(DynamoDB용 통합 테스트 도구), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/kumo/integration-testing-tool-for-dynamodb-59kb텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)