Skip to content

Commit

Permalink
chore(transactional): add more tests for transactional adapters (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Papooch authored Jun 27, 2024
1 parent 1ec49ea commit e98711c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -63,6 +63,13 @@ class UserService {
private readonly kysely: Kysely<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');
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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<UserRecord>(
'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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e98711c

Please sign in to comment.