Skip to content

Commit

Permalink
fix(transactional): separate property mantaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobuzeta committed May 15, 2024
1 parent 93ddabe commit 36b4bd9
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 44 deletions.
21 changes: 0 additions & 21 deletions packages/transactional/test/mock-decorators.ts

This file was deleted.

130 changes: 130 additions & 0 deletions packages/transactional/test/transactional.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { Injectable, Module } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { ClsModule } from 'nestjs-cls';
import { ClsPluginTransactional, Transactional, TransactionHost } from '../src';
import {
MockDbConnection,
TransactionAdapterMock,
} from './transaction-adapter-mock';

export function MetadataDefiningDecorator(): MethodDecorator {
return (
_target: any,
_propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<any>,
) => {
Reflect.defineMetadata('testproperty', 'testvalue', descriptor.value);
return descriptor;
};
}

export function PropertyDefiningDecorator(): MethodDecorator {
return (
_target: any,
_propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<any>,
) => {
descriptor.value.testproperty = 'testvalue';
return descriptor;
};
}

@Injectable()
class CalledService {
constructor(
private readonly txHost: TransactionHost<TransactionAdapterMock>,
) {}

async doWork(num: number) {
return this.txHost.tx.query(`SELECT ${num}`);
}

async doOtherWork(num: number) {
return this.txHost.tx.query(`SELECT ${num}`);
}
}

@Injectable()
class CallingService {
constructor(
private readonly calledService: CalledService,
) {}

@Transactional<TransactionAdapterMock>({ serializable: true })
@MetadataDefiningDecorator()
@PropertyDefiningDecorator()
async transactionWithDecoratorWithOptions() {
await this.calledService.doWork(1);
await this.calledService.doOtherWork(2);
}
}

@Module({
providers: [MockDbConnection],
exports: [MockDbConnection],
})
class DbConnectionModule {}

@Module({
imports: [
ClsModule.forRoot({
plugins: [
new ClsPluginTransactional({
imports: [DbConnectionModule],
adapter: new TransactionAdapterMock({
connectionToken: MockDbConnection,
}),
}),
],
}),
],
providers: [CallingService, CalledService],
})
class AppModule {}

describe('Transactional with other decorators', () => {
let module: TestingModule;
let callingService: CallingService;
let mockDbConnection: MockDbConnection;
beforeEach(async () => {
module = await Test.createTestingModule({
imports: [AppModule],
}).compile();
await module.init();
callingService = module.get(CallingService);
mockDbConnection = module.get(MockDbConnection);
});

describe('when using the @Transactional decorator with other decorators', () => {
it('should start a transaction with options', async () => {
await callingService.transactionWithDecoratorWithOptions();
const queries = mockDbConnection.getClientsQueries();
expect(queries).toEqual([
[
'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION;',
'SELECT 1',
'SELECT 2',
'COMMIT TRANSACTION;',
],
]);
});
});
describe('should mantain method properties set by other decorators', () => {
it('should mantain metadata', () => {
expect(
Reflect.getMetadata(
'testproperty',
callingService.transactionWithDecoratorWithOptions,
),
).toEqual('testvalue');
});

it('should mantain property', () => {
expect(
callingService.transactionWithDecoratorWithOptions[
'testproperty'
],
).toEqual('testvalue');
});
});
});
23 changes: 0 additions & 23 deletions packages/transactional/test/transactional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import {
MockDbConnection,
TransactionAdapterMock,
} from './transaction-adapter-mock';
import {
MetadataDefiningDecorator,
PropertyDefiningDecorator,
} from './mock-decorators';

@Injectable()
class CalledService {
Expand All @@ -34,8 +30,6 @@ class CallingService {
) {}

@Transactional()
@MetadataDefiningDecorator()
@PropertyDefiningDecorator()
async transactionWithDecorator() {
const q1 = await this.calledService.doWork(1);
const q2 = await this.calledService.doOtherWork(2);
Expand Down Expand Up @@ -216,21 +210,4 @@ describe('Transactional', () => {
expect(queries).toEqual([['SELECT 5'], ['SELECT 6']]);
});
});

describe('should mantain method properties set by other decorators', () => {
it('should mantain metadata', () => {
expect(
Reflect.getMetadata(
'testproperty',
callingService.transactionWithDecorator,
),
).toEqual('testvalue');
});

it('should mantain property', () => {
expect(
callingService.transactionWithDecorator['testproperty'],
).toEqual('testvalue');
});
});
});

0 comments on commit 36b4bd9

Please sign in to comment.