Skip to content

Commit 86948f5

Browse files
committed
chore(transactional): add more tests for transactional adapters
1 parent 1ec49ea commit 86948f5

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

packages/transactional-adapters/transactional-adapter-knex/test/transactional-adapter-knex.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { Inject, Injectable, Module } from '@nestjs/common';
99
import { Test, TestingModule } from '@nestjs/testing';
1010
import Knex from 'knex';
11-
import { ClsModule } from 'nestjs-cls';
11+
import { ClsModule, UseCls } from 'nestjs-cls';
1212
import { TransactionalAdapterKnex } from '../src';
1313

1414
const KNEX = 'KNEX';
@@ -41,6 +41,13 @@ class UserService {
4141
private readonly knex: Knex.Knex,
4242
) {}
4343

44+
@UseCls()
45+
async withoutTransaction() {
46+
const r1 = await this.userRepository.createUser('Jim');
47+
const r2 = await this.userRepository.getUserById(r1.id);
48+
return { r1, r2 };
49+
}
50+
4451
@Transactional()
4552
async transactionWithDecorator() {
4653
const r1 = await this.userRepository.createUser('John');
@@ -147,6 +154,13 @@ describe('Transactional', () => {
147154
});
148155

149156
describe('TransactionalAdapterKnex', () => {
157+
it('should work without an active transaction', async () => {
158+
const { r1, r2 } = await callingService.withoutTransaction();
159+
expect(r1).toEqual(r2);
160+
const users = await knex('user');
161+
expect(users).toEqual(expect.arrayContaining([r1]));
162+
});
163+
150164
it('should run a transaction with the default options with a decorator', async () => {
151165
const { r1, r2 } = await callingService.transactionWithDecorator();
152166
expect(r1).toEqual(r2);

packages/transactional-adapters/transactional-adapter-kysely/test/transactional-adapter-kysely.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Inject, Injectable, Module } from '@nestjs/common';
99
import { Test, TestingModule } from '@nestjs/testing';
1010
import { execSync } from 'child_process';
1111
import { Generated, Kysely, PostgresDialect } from 'kysely';
12-
import { ClsModule } from 'nestjs-cls';
12+
import { ClsModule, UseCls } from 'nestjs-cls';
1313
import { Pool } from 'pg';
1414
import { TransactionalAdapterKysely } from '../src';
1515

@@ -63,6 +63,13 @@ class UserService {
6363
private readonly kysely: Kysely<Database>,
6464
) {}
6565

66+
@UseCls()
67+
async withoutTransaction() {
68+
const r1 = await this.userRepository.createUser('Jim');
69+
const r2 = await this.userRepository.getUserById(r1.id);
70+
return { r1, r2 };
71+
}
72+
6673
@Transactional()
6774
async transactionWithDecorator() {
6875
const r1 = await this.userRepository.createUser('John');
@@ -186,6 +193,16 @@ describe('Transactional', () => {
186193
}, 60_000);
187194

188195
describe('TransactionalAdapterKysely', () => {
196+
it('should work without an active transaction', async () => {
197+
const { r1, r2 } = await callingService.withoutTransaction();
198+
expect(r1).toEqual(r2);
199+
const users = await kyselyDb
200+
.selectFrom('user')
201+
.selectAll()
202+
.execute();
203+
expect(users).toEqual(expect.arrayContaining([r1]));
204+
});
205+
189206
it('should run a transaction with the default options with a decorator', async () => {
190207
const { r1, r2 } = await callingService.transactionWithDecorator();
191208
expect(r1).toEqual(r2);

packages/transactional-adapters/transactional-adapter-pg-promise/test/transactional-adapter-pg-promise.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@nestjs-cls/transactional';
88
import { Inject, Injectable, Module } from '@nestjs/common';
99
import { Test, TestingModule } from '@nestjs/testing';
10-
import { ClsModule } from 'nestjs-cls';
10+
import { ClsModule, UseCls } from 'nestjs-cls';
1111
import { execSync } from 'node:child_process';
1212
import pgPromise from 'pg-promise';
1313
import { Database, TransactionalAdapterPgPromise } from '../src';
@@ -62,6 +62,13 @@ class UserService {
6262
private readonly db: Database,
6363
) {}
6464

65+
@UseCls()
66+
async withoutTransaction() {
67+
const r1 = await this.userRepository.createUser('Jim');
68+
const r2 = await this.userRepository.getUserById(r1.id);
69+
return { r1, r2 };
70+
}
71+
6572
@Transactional()
6673
async transactionWithDecorator() {
6774
const r1 = await this.userRepository.createUser('John');
@@ -169,6 +176,15 @@ describe('Transactional', () => {
169176
}, 60_000);
170177

171178
describe('TransactionalAdapterPgPromise', () => {
179+
it('should work without an active transaction', async () => {
180+
const { r1, r2 } = await callingService.withoutTransaction();
181+
expect(r1).toEqual(r2);
182+
const users = await db.many<UserRecord>(
183+
'SELECT * FROM public.user',
184+
);
185+
expect(users).toEqual(expect.arrayContaining([r1]));
186+
});
187+
172188
it('should run a transaction with the default options with a decorator', async () => {
173189
const { r1, r2 } = await callingService.transactionWithDecorator();
174190
expect(r1).toEqual(r2);

packages/transactional-adapters/transactional-adapter-prisma/test/transactional-adapter-prisma.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Injectable, Module } from '@nestjs/common';
99
import { Test, TestingModule } from '@nestjs/testing';
1010
import { Prisma, PrismaClient } from '@prisma/client';
1111
import { execSync } from 'child_process';
12-
import { ClsModule } from 'nestjs-cls';
12+
import { ClsModule, UseCls } from 'nestjs-cls';
1313
import { TransactionalAdapterPrisma } from '../src';
1414

1515
process.env.DATA_SOURCE_URL = 'file:../tmp/test.db';
@@ -40,6 +40,13 @@ class UserService {
4040
private readonly prisma: PrismaClient,
4141
) {}
4242

43+
@UseCls()
44+
async withoutTransaction() {
45+
const r1 = await this.userRepository.createUser('Jim');
46+
const r2 = await this.userRepository.getUserById(r1.id);
47+
return { r1, r2 };
48+
}
49+
4350
@Transactional()
4451
async transactionWithDecorator() {
4552
const r1 = await this.userRepository.createUser('John');
@@ -126,6 +133,13 @@ describe('Transactional', () => {
126133
});
127134

128135
describe('TransactionalAdapterPrisma', () => {
136+
it('should work without an active transaction', async () => {
137+
const { r1, r2 } = await callingService.withoutTransaction();
138+
expect(r1).toEqual(r2);
139+
const users = await prisma.user.findMany();
140+
expect(users).toEqual(expect.arrayContaining([r1]));
141+
});
142+
129143
it('should run a transaction with the default options with a decorator', async () => {
130144
const { r1, r2 } = await callingService.transactionWithDecorator();
131145
expect(r1).toEqual(r2);

packages/transactional-adapters/transactional-adapter-typeorm/test/transactional-adapter-typeorm.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@nestjs-cls/transactional';
88
import { Injectable, Module } from '@nestjs/common';
99
import { Test, TestingModule } from '@nestjs/testing';
10-
import { ClsModule } from 'nestjs-cls';
10+
import { ClsModule, UseCls } from 'nestjs-cls';
1111
import { execSync } from 'node:child_process';
1212
import { Column, DataSource, Entity, PrimaryGeneratedColumn } from 'typeorm';
1313
import { TransactionalAdapterTypeOrm } from '../src';
@@ -62,6 +62,13 @@ class UserService {
6262
private readonly dataSource: DataSource,
6363
) {}
6464

65+
@UseCls()
66+
async withoutTransaction() {
67+
const r1 = await this.userRepository.createUser('Jim');
68+
const r2 = await this.userRepository.getUserById(r1.id);
69+
return { r1, r2 };
70+
}
71+
6572
@Transactional()
6673
async transactionWithDecorator() {
6774
const r1 = await this.userRepository.createUser('John');
@@ -172,6 +179,13 @@ describe('Transactional', () => {
172179
}, 60_000);
173180

174181
describe('TransactionalAdapterTypeOrmPromise', () => {
182+
it('should work without an active transaction', async () => {
183+
const { r1, r2 } = await callingService.withoutTransaction();
184+
expect(r1).toEqual(r2);
185+
const users = await dataSource.manager.find(User);
186+
expect(users).toEqual(expect.arrayContaining([r1]));
187+
});
188+
175189
it('should run a transaction with the default options with a decorator', async () => {
176190
const { r1, r2 } = await callingService.transactionWithDecorator();
177191
expect(r1).toEqual(r2);

0 commit comments

Comments
 (0)