From e98711ceb07bfb94319232ea8a3bd752d370deec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C5=A0vanda?= <46406259+Papooch@users.noreply.github.com> Date: Thu, 27 Jun 2024 21:45:14 +0200 Subject: [PATCH] chore(transactional): add more tests for transactional adapters (#157) --- .../test/transactional-adapter-knex.spec.ts | 16 +++++++++++++++- .../test/transactional-adapter-kysely.spec.ts | 19 ++++++++++++++++++- .../transactional-adapter-pg-promise.spec.ts | 18 +++++++++++++++++- .../test/transactional-adapter-prisma.spec.ts | 16 +++++++++++++++- .../transactional-adapter-typeorm.spec.ts | 16 +++++++++++++++- 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/packages/transactional-adapters/transactional-adapter-knex/test/transactional-adapter-knex.spec.ts b/packages/transactional-adapters/transactional-adapter-knex/test/transactional-adapter-knex.spec.ts index cad4ec8e..17d6b719 100644 --- a/packages/transactional-adapters/transactional-adapter-knex/test/transactional-adapter-knex.spec.ts +++ b/packages/transactional-adapters/transactional-adapter-knex/test/transactional-adapter-knex.spec.ts @@ -8,7 +8,7 @@ import { import { Inject, Injectable, Module } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import Knex from 'knex'; -import { ClsModule } from 'nestjs-cls'; +import { ClsModule, UseCls } from 'nestjs-cls'; import { TransactionalAdapterKnex } from '../src'; const KNEX = 'KNEX'; @@ -41,6 +41,13 @@ class UserService { private readonly knex: Knex.Knex, ) {} + @UseCls() + async withoutTransaction() { + const r1 = await this.userRepository.createUser('Jim'); + const r2 = await this.userRepository.getUserById(r1.id); + return { r1, r2 }; + } + @Transactional() async transactionWithDecorator() { const r1 = await this.userRepository.createUser('John'); @@ -147,6 +154,13 @@ describe('Transactional', () => { }); describe('TransactionalAdapterKnex', () => { + it('should work without an active transaction', async () => { + const { r1, r2 } = await callingService.withoutTransaction(); + expect(r1).toEqual(r2); + const users = await knex('user'); + expect(users).toEqual(expect.arrayContaining([r1])); + }); + it('should run a transaction with the default options with a decorator', async () => { const { r1, r2 } = await callingService.transactionWithDecorator(); expect(r1).toEqual(r2); diff --git a/packages/transactional-adapters/transactional-adapter-kysely/test/transactional-adapter-kysely.spec.ts b/packages/transactional-adapters/transactional-adapter-kysely/test/transactional-adapter-kysely.spec.ts index c249d1b3..7e034731 100644 --- a/packages/transactional-adapters/transactional-adapter-kysely/test/transactional-adapter-kysely.spec.ts +++ b/packages/transactional-adapters/transactional-adapter-kysely/test/transactional-adapter-kysely.spec.ts @@ -9,7 +9,7 @@ import { Inject, Injectable, Module } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { execSync } from 'child_process'; import { Generated, Kysely, PostgresDialect } from 'kysely'; -import { ClsModule } from 'nestjs-cls'; +import { ClsModule, UseCls } from 'nestjs-cls'; import { Pool } from 'pg'; import { TransactionalAdapterKysely } from '../src'; @@ -63,6 +63,13 @@ class UserService { private readonly kysely: Kysely, ) {} + @UseCls() + async withoutTransaction() { + const r1 = await this.userRepository.createUser('Jim'); + const r2 = await this.userRepository.getUserById(r1.id); + return { r1, r2 }; + } + @Transactional() async transactionWithDecorator() { const r1 = await this.userRepository.createUser('John'); @@ -186,6 +193,16 @@ describe('Transactional', () => { }, 60_000); describe('TransactionalAdapterKysely', () => { + it('should work without an active transaction', async () => { + const { r1, r2 } = await callingService.withoutTransaction(); + expect(r1).toEqual(r2); + const users = await kyselyDb + .selectFrom('user') + .selectAll() + .execute(); + expect(users).toEqual(expect.arrayContaining([r1])); + }); + it('should run a transaction with the default options with a decorator', async () => { const { r1, r2 } = await callingService.transactionWithDecorator(); expect(r1).toEqual(r2); diff --git a/packages/transactional-adapters/transactional-adapter-pg-promise/test/transactional-adapter-pg-promise.spec.ts b/packages/transactional-adapters/transactional-adapter-pg-promise/test/transactional-adapter-pg-promise.spec.ts index ca3c49b7..dca255d4 100644 --- a/packages/transactional-adapters/transactional-adapter-pg-promise/test/transactional-adapter-pg-promise.spec.ts +++ b/packages/transactional-adapters/transactional-adapter-pg-promise/test/transactional-adapter-pg-promise.spec.ts @@ -7,7 +7,7 @@ import { } from '@nestjs-cls/transactional'; import { Inject, Injectable, Module } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { ClsModule } from 'nestjs-cls'; +import { ClsModule, UseCls } from 'nestjs-cls'; import { execSync } from 'node:child_process'; import pgPromise from 'pg-promise'; import { Database, TransactionalAdapterPgPromise } from '../src'; @@ -62,6 +62,13 @@ class UserService { private readonly db: Database, ) {} + @UseCls() + async withoutTransaction() { + const r1 = await this.userRepository.createUser('Jim'); + const r2 = await this.userRepository.getUserById(r1.id); + return { r1, r2 }; + } + @Transactional() async transactionWithDecorator() { const r1 = await this.userRepository.createUser('John'); @@ -169,6 +176,15 @@ describe('Transactional', () => { }, 60_000); describe('TransactionalAdapterPgPromise', () => { + it('should work without an active transaction', async () => { + const { r1, r2 } = await callingService.withoutTransaction(); + expect(r1).toEqual(r2); + const users = await db.many( + 'SELECT * FROM public.user', + ); + expect(users).toEqual(expect.arrayContaining([r1])); + }); + it('should run a transaction with the default options with a decorator', async () => { const { r1, r2 } = await callingService.transactionWithDecorator(); expect(r1).toEqual(r2); diff --git a/packages/transactional-adapters/transactional-adapter-prisma/test/transactional-adapter-prisma.spec.ts b/packages/transactional-adapters/transactional-adapter-prisma/test/transactional-adapter-prisma.spec.ts index 1de480ee..a03d54f5 100644 --- a/packages/transactional-adapters/transactional-adapter-prisma/test/transactional-adapter-prisma.spec.ts +++ b/packages/transactional-adapters/transactional-adapter-prisma/test/transactional-adapter-prisma.spec.ts @@ -9,7 +9,7 @@ import { Injectable, Module } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { Prisma, PrismaClient } from '@prisma/client'; import { execSync } from 'child_process'; -import { ClsModule } from 'nestjs-cls'; +import { ClsModule, UseCls } from 'nestjs-cls'; import { TransactionalAdapterPrisma } from '../src'; process.env.DATA_SOURCE_URL = 'file:../tmp/test.db'; @@ -40,6 +40,13 @@ class UserService { private readonly prisma: PrismaClient, ) {} + @UseCls() + async withoutTransaction() { + const r1 = await this.userRepository.createUser('Jim'); + const r2 = await this.userRepository.getUserById(r1.id); + return { r1, r2 }; + } + @Transactional() async transactionWithDecorator() { const r1 = await this.userRepository.createUser('John'); @@ -126,6 +133,13 @@ describe('Transactional', () => { }); describe('TransactionalAdapterPrisma', () => { + it('should work without an active transaction', async () => { + const { r1, r2 } = await callingService.withoutTransaction(); + expect(r1).toEqual(r2); + const users = await prisma.user.findMany(); + expect(users).toEqual(expect.arrayContaining([r1])); + }); + it('should run a transaction with the default options with a decorator', async () => { const { r1, r2 } = await callingService.transactionWithDecorator(); expect(r1).toEqual(r2); diff --git a/packages/transactional-adapters/transactional-adapter-typeorm/test/transactional-adapter-typeorm.spec.ts b/packages/transactional-adapters/transactional-adapter-typeorm/test/transactional-adapter-typeorm.spec.ts index e92c4c5f..1aa4654a 100644 --- a/packages/transactional-adapters/transactional-adapter-typeorm/test/transactional-adapter-typeorm.spec.ts +++ b/packages/transactional-adapters/transactional-adapter-typeorm/test/transactional-adapter-typeorm.spec.ts @@ -7,7 +7,7 @@ import { } from '@nestjs-cls/transactional'; import { Injectable, Module } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { ClsModule } from 'nestjs-cls'; +import { ClsModule, UseCls } from 'nestjs-cls'; import { execSync } from 'node:child_process'; import { Column, DataSource, Entity, PrimaryGeneratedColumn } from 'typeorm'; import { TransactionalAdapterTypeOrm } from '../src'; @@ -62,6 +62,13 @@ class UserService { private readonly dataSource: DataSource, ) {} + @UseCls() + async withoutTransaction() { + const r1 = await this.userRepository.createUser('Jim'); + const r2 = await this.userRepository.getUserById(r1.id); + return { r1, r2 }; + } + @Transactional() async transactionWithDecorator() { const r1 = await this.userRepository.createUser('John'); @@ -172,6 +179,13 @@ describe('Transactional', () => { }, 60_000); describe('TransactionalAdapterTypeOrmPromise', () => { + it('should work without an active transaction', async () => { + const { r1, r2 } = await callingService.withoutTransaction(); + expect(r1).toEqual(r2); + const users = await dataSource.manager.find(User); + expect(users).toEqual(expect.arrayContaining([r1])); + }); + it('should run a transaction with the default options with a decorator', async () => { const { r1, r2 } = await callingService.transactionWithDecorator(); expect(r1).toEqual(r2);