Skip to content

feat(be): implement transaction rollback in unit test#1562

Merged
gyunseo merged 13 commits into
mainfrom
1561-transaction-rollback-in-unit-test
Mar 18, 2024
Merged

feat(be): implement transaction rollback in unit test#1562
gyunseo merged 13 commits into
mainfrom
1561-transaction-rollback-in-unit-test

Conversation

@gyunseo

@gyunseo gyunseo commented Mar 16, 2024

Copy link
Copy Markdown
Contributor

Description

Close #1561
Backend Unit Test에서 Transaction Rollback을 이용하는 기능을 구현했습니다.

Additional context

prisma.service.ts에 있는 PrismaService Class를 GroupService에 주입하는 대신, 새로 만든 transaction.extension.ts의 transactionextension을 PrismaClient에 extend하여서, extended된 PrismaClient를 PrismaService에 주입했습니다. 이를 이용해 transaction client를 unit test 상에서 구현했습니다.
트랜잭션_클라이언트.$commit(), 트랜잭션_클라이언트.$rollback()을 통해, Transaction 커밋과 롤백을 이용할 수 있습니다.

beforeEach(async function () {
    this.timeout(0)
    //transaction client
    tx = await prisma.$begin()
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        GroupService,
        { provide: PrismaService, useValue: tx },
        ConfigService,
        {
          provide: CACHE_MANAGER,
          useFactory: () => ({
            set: () => [],
            get: () => []
          })
        }
      ]
    }).compile()
    service = module.get<GroupService>(GroupService)
    cache = module.get<Cache>(CACHE_MANAGER)
  })

상기 mocha hook이 local에서는 default timeout setting인 2000ms를 무리 없이 통과하는데요, github action에서는 2000ms 넘겨서 CI 테스트를 통과하지 못했었습니다.
그래서 this.timeout(0)를 넣어서 timeout에 걸리지 않게 했습니다. (0으로 설정하면 disable됨)
tx = await prisma.$begin() 이 부분때문에 약간의 병목이 생긴 것인지는 확실치 않으나, 나름의 최적화 여지가 있을 것 같습니다.

#1257 PR과 관련이 있습니다.

transactionExtensionprisma/prisma-client-extensions#47을 참고하여 작성했습니다.


Before submitting the PR, please make sure you do the following

@vercel

vercel Bot commented Mar 16, 2024

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
codedang ✅ Ready (Inspect) Visit Preview Mar 18, 2024 0:54am

@gyunseo gyunseo requested a review from Jaehyeon1020 March 16, 2024 11:57
@gyunseo gyunseo self-assigned this Mar 16, 2024
@gyunseo gyunseo merged commit f96a3cf into main Mar 18, 2024
@gyunseo gyunseo deleted the 1561-transaction-rollback-in-unit-test branch March 18, 2024 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backend Unit Test에서 Transaction의 Rollback 이용하기

2 participants