Skip to content

Commit

Permalink
test: add test case to verify rollbacks after failed delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliheym committed Dec 21, 2023
1 parent 0f461ee commit 657c2f9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
10 changes: 10 additions & 0 deletions tests/entities/Task.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Column, Entity, PrimaryColumn } from 'typeorm';

@Entity('tasks')
export class Task {
@PrimaryColumn('uuid')
id: string;

@Column({ type: 'text' })
name: string;
}
28 changes: 24 additions & 4 deletions tests/nest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import { UserReaderService } from './services/user-reader.service';
import { UserWriterService } from './services/user-writer.service';

import { initializeTransactionalContext, addTransactionalDataSource, StorageDriver } from '../src';
import { TaskService } from './services/task.service';
import { Task } from './entities/Task.entity';

describe('Integration with Nest.js', () => {
let app: TestingModule;

let readerService: UserReaderService;
let writerService: UserWriterService;
let taskService: TaskService;

let dataSource: DataSource;

Expand All @@ -35,7 +38,7 @@ describe('Integration with Nest.js', () => {
username: 'postgres',
password: 'postgres',
database: 'test',
entities: [User],
entities: [User, Task],
synchronize: true,
logging: false,
};
Expand All @@ -49,22 +52,25 @@ describe('Integration with Nest.js', () => {
},
}),

TypeOrmModule.forFeature([User]),
TypeOrmModule.forFeature([User, Task]),
],
providers: [UserReaderService, UserWriterService],
providers: [UserReaderService, UserWriterService, TaskService],
exports: [],
}).compile();

readerService = app.get<UserReaderService>(UserReaderService);
writerService = app.get<UserWriterService>(UserWriterService);
taskService = app.get<TaskService>(TaskService);

dataSource = app.get(DataSource);

await dataSource.createEntityManager().clear(User);
await dataSource.createEntityManager().clear(Task);
});

afterEach(async () => {
await dataSource.createEntityManager().clear(User);
await dataSource.createEntityManager().clear(Task);
});

afterAll(async () => {
Expand All @@ -89,7 +95,7 @@ describe('Integration with Nest.js', () => {
const name = 'John Doe';
const onTransactionCompleteSpy = jest.fn();

expect(() =>
await expect(() =>
writerService.createUserAndThrow(name, onTransactionCompleteSpy),
).rejects.toThrowError();

Expand All @@ -99,4 +105,18 @@ describe('Integration with Nest.js', () => {
expect(onTransactionCompleteSpy).toBeCalledTimes(1);
expect(onTransactionCompleteSpy).toBeCalledWith(false);
});

it('should rollback transaction if error was thrown', async () => {
const id = '99407c4f-fa64-432b-874e-1f842609983f';

await expect(() =>
taskService.upsertTask({
id,
name: 'Some task',
}),
).rejects.toThrowError();

const task = await taskService.findTaskById(id);
expect(task).toBeNull();
});
});
28 changes: 28 additions & 0 deletions tests/services/task.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Transactional } from '../../src';

import { Task } from '../entities/Task.entity';

@Injectable()
export class TaskService {
constructor(
@InjectRepository(Task)
private readonly taskRepository: Repository<Task>,
) {}

async findTaskById(id: string): Promise<Task | null> {
return this.taskRepository.findOneBy({ id });
}

@Transactional()
async upsertTask({ id, name }: Task): Promise<Task> {
const taskSaved = await this.taskRepository.save({ id, name });

// delete a task that does not exist to test transaction rollback
await this.taskRepository.delete({ id: 'non-existing-id' });

return taskSaved;
}
}

0 comments on commit 657c2f9

Please sign in to comment.