DynamoDB용 통합 테스트 도구

서버리스 아키텍처의 통합 테스트는 어려울 수 있습니다. 관리 서비스 내에서 특정 결과를 테스트하는 것은 번거롭습니다. sls-test-tools은 AWS에서 Serverless Architectures에 대한 효과적인 고품질 통합 테스트를 보다 쉽게 ​​작성할 수 있도록 다양한 유틸리티, 설정, 분해 및 어설션을 제공합니다.

이제 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단계로 수행할 수 있습니다.
  • 초기 이벤트 시작 🔥. 이 시나리오에서는 람다 핸들러를 호출하지만 이벤트 버스에서 이벤트를 보내고 S3에 파일을 업로드하는 것도 상상할 수 있습니다...
  • 예상 동작 어설션 ✅.
  • 생성된 것을 청소합니다 🧹. 테스트를 멱등성으로 유지하려면 이 단계가 매우 중요합니다.

  • 이 세 단계에 따라 통합 테스트 구현은 다음과 같습니다.

    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 두 문서를 참조하십시오.

    좋은 웹페이지 즐겨찾기