From c140f65edb37c068716da87357a844defbf52b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Obernd=C3=B6rfer?= Date: Thu, 5 Dec 2024 13:23:11 +0100 Subject: [PATCH] Added tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Florian Oberndörfer --- .../backupData/backupData.controller.spec.ts | 26 +++++- .../app/backupData/backupData.service.spec.ts | 21 ++++- .../src/app/tasks/tasks.controller.spec.ts | 89 +++++++++++++++++++ .../src/app/tasks/tasks.service.spec.ts | 73 +++++++++++++++ 4 files changed, 207 insertions(+), 2 deletions(-) create mode 100644 apps/backend/src/app/tasks/tasks.controller.spec.ts create mode 100644 apps/backend/src/app/tasks/tasks.service.spec.ts diff --git a/apps/backend/src/app/backupData/backupData.controller.spec.ts b/apps/backend/src/app/backupData/backupData.controller.spec.ts index 047ccf6..d5cc3ee 100644 --- a/apps/backend/src/app/backupData/backupData.controller.spec.ts +++ b/apps/backend/src/app/backupData/backupData.controller.spec.ts @@ -2,7 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing'; import { INestApplication } from '@nestjs/common'; import request from 'supertest'; import { getRepositoryToken } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { ILike, Repository } from 'typeorm'; import { BackupDataEntity } from './entity/backupData.entity'; import { CreateBackupDataDto } from './dto/createBackupData.dto'; import { BackupDataModule } from './backupData.module'; @@ -139,4 +139,28 @@ describe('BackupDataController (e2e)', () => { where: { creationDate: expect.any(Object), type: BackupType.FULL }, }); }); + it('/backupData (GET) with taskId should return backup data entries with the specified taskId', async () => { + await request(app.getHttpServer()) + .get('/backupData?taskId=task-123') + .expect(200); + + expect(mockBackupDataRepository.findAndCount).toBeCalledWith({ + order: { creationDate: 'DESC' }, + where: { taskId: { id: 'task-123' }, type: BackupType.FULL }, + }); + }); + + it('/backupData (GET) with taskName should return backup data entries with the specified taskName', async () => { + await request(app.getHttpServer()) + .get('/backupData?taskName=backup-task') + .expect(200); + + expect(mockBackupDataRepository.findAndCount).toBeCalledWith({ + order: { creationDate: 'DESC' }, + where: { + taskId: { displayName: ILike('%backup-task%') }, + type: BackupType.FULL, + }, + }); + }); }); diff --git a/apps/backend/src/app/backupData/backupData.service.spec.ts b/apps/backend/src/app/backupData/backupData.service.spec.ts index abd7128..732c1d0 100644 --- a/apps/backend/src/app/backupData/backupData.service.spec.ts +++ b/apps/backend/src/app/backupData/backupData.service.spec.ts @@ -124,6 +124,24 @@ describe('BackupDataService', () => { BadRequestException ); }); + + it('should create a where clause for taskId search', () => { + const filterDto: BackupDataFilterDto = { taskId: 'task-123' }; + const where = service.createWhereClause(filterDto); + expect(where).toEqual({ + taskId: { id: 'task-123' }, + type: BackupType.FULL, + }); + }); + + it('should create a where clause for taskName search', () => { + const filterDto: BackupDataFilterDto = { taskName: 'backup-task' }; + const where = service.createWhereClause(filterDto); + expect(where).toEqual({ + taskId: { displayName: ILike('%backup-task%') }, + type: BackupType.FULL, + }); + }); }); describe('createOrderClause', () => { @@ -151,7 +169,8 @@ describe('BackupDataService', () => { describe('create', () => { it('should create a new backup data entity', async () => { - let createBackupDataDto: CreateBackupDataDto = new CreateBackupDataDto(); + const createBackupDataDto: CreateBackupDataDto = + new CreateBackupDataDto(); Object.assign(createBackupDataDto, mockBackupDataEntity); const result = await service.create(createBackupDataDto); diff --git a/apps/backend/src/app/tasks/tasks.controller.spec.ts b/apps/backend/src/app/tasks/tasks.controller.spec.ts new file mode 100644 index 0000000..c333481 --- /dev/null +++ b/apps/backend/src/app/tasks/tasks.controller.spec.ts @@ -0,0 +1,89 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { INestApplication } from '@nestjs/common'; +import request from 'supertest'; +import { TasksController } from './tasks.controller'; +import { TasksService } from './tasks.service'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { TaskEntity } from './entity/task.entity'; +import { Repository } from 'typeorm'; +import { CreateTaskDto } from './dto/createTask.dto'; + +describe('TasksController (e2e)', () => { + let app: INestApplication; + let repository: Repository; + + const mockTaskRepository = { + find: jest.fn(), + findOneBy: jest.fn(), + save: jest.fn(), + }; + + beforeAll(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + controllers: [TasksController], + providers: [ + TasksService, + { + provide: getRepositoryToken(TaskEntity), + useValue: mockTaskRepository, + }, + ], + }).compile(); + + app = moduleFixture.createNestApplication(); + await app.init(); + repository = moduleFixture.get>(getRepositoryToken(TaskEntity)); + }); + + afterAll(async () => { + await app.close(); + }); + + it('/tasks (GET) should return an array of tasks', async () => { + const result = [new TaskEntity()]; + jest.spyOn(repository, 'find').mockResolvedValue(result); + + const response = await request(app.getHttpServer()) + .get('/tasks') + .expect(200); + + expect(response.body).toEqual(result); + }); + + it('/tasks/:id (GET) should return a task if found', async () => { + const id = 'ea1a2f52-5cf4-44a6-b266-175ee396a18c'; + const task = new TaskEntity(); + jest.spyOn(repository, 'findOneBy').mockResolvedValue(task); + + const response = await request(app.getHttpServer()) + .get(`/tasks/${id}`) + .expect(200); + + expect(response.body).toEqual(task); + }); + + it('/tasks/:id (GET) should throw a NotFoundException if task not found', async () => { + const id = 'ea1a2f52-5cf4-44a6-b266-175ee396a18e'; + jest.spyOn(repository, 'findOneBy').mockResolvedValue(null); + + await request(app.getHttpServer()) + .get(`/tasks/${id}`) + .expect(404); + }); + + it('/tasks (POST) should create and return a task', async () => { + const createTaskDto: CreateTaskDto = { + id: 'someId', + displayName: 'someName', + }; + const task = new TaskEntity(); + jest.spyOn(repository, 'save').mockResolvedValue(task); + + const response = await request(app.getHttpServer()) + .post('/tasks') + .send(createTaskDto) + .expect(201); + + expect(response.body).toEqual(task); + }); +}); \ No newline at end of file diff --git a/apps/backend/src/app/tasks/tasks.service.spec.ts b/apps/backend/src/app/tasks/tasks.service.spec.ts new file mode 100644 index 0000000..365fb66 --- /dev/null +++ b/apps/backend/src/app/tasks/tasks.service.spec.ts @@ -0,0 +1,73 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { NotFoundException } from '@nestjs/common'; +import { Repository } from 'typeorm'; +import { TasksService } from './tasks.service'; +import { TaskEntity } from './entity/task.entity'; + +describe('TasksService', () => { + let service: TasksService; + let repository: Repository; + + const mockTaskRepository = { + find: jest.fn(), + findOneBy: jest.fn(), + save: jest.fn(), + }; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + TasksService, + { + provide: getRepositoryToken(TaskEntity), + useValue: mockTaskRepository, + }, + ], + }).compile(); + + service = module.get(TasksService); + repository = module.get>( + getRepositoryToken(TaskEntity) + ); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); + + describe('findAll', () => { + it('should return an array of tasks', async () => { + const result = [new TaskEntity()]; + jest.spyOn(repository, 'find').mockResolvedValue(result); + + expect(await service.findAll()).toBe(result); + }); + }); + + describe('findOne', () => { + it('should return a task if found', async () => { + const id = 'ea1a2f52-5cf4-44a6-b266-175ee396a18c'; + const task = new TaskEntity(); + jest.spyOn(repository, 'findOneBy').mockResolvedValue(task); + + expect(await service.findOne(id)).toBe(task); + }); + + it('should throw a NotFoundException if task not found', async () => { + const id = 'ea1a2f52-5cf4-44a6-b266-175ee396a18d'; + jest.spyOn(repository, 'findOneBy').mockResolvedValue(null); + + await expect(service.findOne(id)).rejects.toThrow(NotFoundException); + }); + }); + + describe('create', () => { + it('should create and return a task', async () => { + const task = new TaskEntity(); + jest.spyOn(repository, 'save').mockResolvedValue(task); + + expect(await service.create(task)).toBe(task); + }); + }); +});